package com.mcwl.web.controller.communityCenter;


import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.communityCenter.domain.dto.QuestionDetailRes;
import com.mcwl.communityCenter.domain.dto.QuestionPageRes;
import com.mcwl.communityCenter.domain.dto.QuestionReplyRes;
import com.mcwl.communityCenter.domain.dto.QuestionRes;
import com.mcwl.communityCenter.domain.vo.QuestionVo;
import com.mcwl.communityCenter.service.QuestionService;
import com.mcwl.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 提问
 */
@Api(tags = "社区提问")
@RestController
@RequestMapping("question")
@RequiredArgsConstructor
public class QuestionController {


    private final QuestionService questionService;

    private final ISysUserService sysUserService;

    /**
     * 提问
     */
    @PostMapping("addQuestion")
    @ApiOperation(value = "提问")
    public R<Object> addQuestion(@RequestBody @Valid QuestionRes questionRes) {

        // 提问类型
        Integer type = questionRes.getType();

        // 付费金额
        Double amount = questionRes.getAmount();

        // 类型为1时，付费金额不能为空
        if (Objects.equals(type, 1) && Objects.isNull(amount)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"付费金额不能为空");
        }

        // 付费类型为1时，判断钱包余额是否充足
        if (Objects.equals(type, 1)) {
            if (amount < 10) {
                return R.fail(HttpStatus.SHOW_ERROR_MSG,"付费金额不能小于10");
            }
            if (amount > 1000) {
                return R.fail(HttpStatus.SHOW_ERROR_MSG,"付费金额不能大于1000");
            }
            Long userId = SecurityUtils.getUserId();
            SysUser sysUser = sysUserService.selectUserById(userId);
            Double wallet = sysUser.getWallet();
            if (wallet < amount) {
                return R.fail(HttpStatus.SHOW_ERROR_MSG,"钱包余额不足");
            }
        } else {
            questionRes.setAmount(0.0);
        }

        return questionService.addQuestion(questionRes);

    }


    /**
     * 获取提问列表
     */
    @PostMapping("list")
    @ApiOperation(value = "获取提问列表")
    public TableDataInfo list(@RequestBody @Valid QuestionPageRes questionPageRes) {

        return questionService.list(questionPageRes);
    }

    /**
     * 获取提问图片列表
     */
    @PostMapping("listImage")
    @ApiOperation(value = "获取提问图片列表")
    public TableDataInfo listImage(@RequestBody @Valid QuestionPageRes questionPageRes) {

        return questionService.listImage(questionPageRes);
    }

    /**
     * 获取问题详情
     */
    @PostMapping("detail")
    @ApiOperation(value = "获取问题详情")
    public R<QuestionVo> getQuestionDetail(@RequestBody @Valid QuestionDetailRes questionDetailRes) {
        QuestionVo questionVo = questionService.getDetail(questionDetailRes);
        if (Objects.isNull(questionVo)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"获取详情失败");
        }
        return R.ok(questionVo);
    }

//    /**
//     * 回复
//     */
//    @PostMapping("reply")
//    @ApiOperation(value = "回复")
//    public AjaxResult reply(@RequestBody @Valid QuestionReplyRes questionReplyRes) {
//
//        return questionService.reply(questionReplyRes);
//
//    }


}
