package com.zhongzheng.controller.goods;

import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.core.page.TableDataInfo;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.bank.bo.QuestionChapterExamQueryBo;
import com.zhongzheng.modules.bank.bo.QuestionModuleChapterQueryBo;
import com.zhongzheng.modules.bank.service.IQuestionChapterExamService;
import com.zhongzheng.modules.bank.service.IQuestionModuleChapterService;
import com.zhongzheng.modules.bank.vo.ExamVo;
import com.zhongzheng.modules.bank.vo.QuestionChapterVo;
import com.zhongzheng.modules.collect.bo.CollectQuestionQueryBo;
import com.zhongzheng.modules.collect.service.ICollectQuestionService;
import com.zhongzheng.modules.course.bo.CourseQueryBo;
import com.zhongzheng.modules.goods.bo.*;
import com.zhongzheng.modules.goods.service.IGoodsAttachedService;
import com.zhongzheng.modules.goods.service.IGoodsAuditionConfigService;
import com.zhongzheng.modules.goods.service.IGoodsService;
import com.zhongzheng.modules.goods.vo.GoodsAttachedVo;
import com.zhongzheng.modules.goods.vo.GoodsVo;
import com.zhongzheng.modules.user.bo.UserExamRecordQueryBo;
import com.zhongzheng.modules.user.bo.UserExamWrongRecordQueryBo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import com.zhongzheng.modules.user.service.IUserExamRecordService;
import com.zhongzheng.modules.user.service.IUserExamWrongRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 商品Controller
 *
 * @author hjl
 * @date 2021-10-12
 */
@Api(value = "商品控制器", tags = {"商品管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    private final IGoodsService iGoodsService;

    private final IGoodsAttachedService iGoodsAttachedService;

    private final WxTokenService wxTokenService;

    private final IUserExamRecordService iUserExamRecordService;


    private final IUserExamWrongRecordService iUserExamWrongRecordService;

    private final ICollectQuestionService iCollectQuestionService;

    private final IQuestionModuleChapterService iQuestionModuleChapterService;

    private final IQuestionChapterExamService iQuestionChapterExamService;

    /**
     * 获取商品详细信息
     */
    @ApiOperation("获取商品详细信息")
    @GetMapping("/{goodsId}")
    public AjaxResult<GoodsVo> getInfo(@PathVariable("goodsId" ) Long goodsId) {
        CourseQueryBo bo = new CourseQueryBo();
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        bo.setGoodsId(goodsId);
        return AjaxResult.success(iGoodsService.selectUserDetail(bo));
    }


    /**
     * 获取商品详细信息
     */
    @ApiOperation("获取题库商品题目数统计信息")
    @GetMapping("/bank/questionNum/{goodsId}")
    public AjaxResult<Map<String,Object>> questionNum(@PathVariable("goodsId" ) Long goodsId) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());

        Map<String,Object> numMap = new HashMap<>();

        UserExamWrongRecordQueryBo wBo = new UserExamWrongRecordQueryBo();
        wBo.setGoodsId(goodsId);
        wBo.setUserId(loginUser.getUser().getUserId());
        Long wrongNum = iUserExamWrongRecordService.recordNum(wBo);
        numMap.put("wrongNum",wrongNum);

        CollectQuestionQueryBo collectQuestionQueryBo = new CollectQuestionQueryBo();
        collectQuestionQueryBo.setGoodsId(goodsId);
        collectQuestionQueryBo.setUserId(loginUser.getUser().getUserId());
        Integer collectNum = iCollectQuestionService.collectNum(collectQuestionQueryBo);
        numMap.put("collectNum",collectNum);

        UserExamRecordQueryBo userExamRecordQueryBo = new UserExamRecordQueryBo();
        userExamRecordQueryBo.setGoodsId(goodsId);
        userExamRecordQueryBo.setUserId(loginUser.getUser().getUserId());
        Long doNum = iUserExamRecordService.selectDoNum(userExamRecordQueryBo);
        numMap.put("doNum",doNum);

        userExamRecordQueryBo.setGoodsId(goodsId);
        userExamRecordQueryBo.setUserId(loginUser.getUser().getUserId());
        Long rightNum = iUserExamRecordService.selectRightNum(userExamRecordQueryBo);
        numMap.put("rightNum",rightNum);

        Long totalNum = iGoodsService.getQuestionNum(goodsId);

        numMap.put("totalNum",totalNum);

        return AjaxResult.success(numMap);
    }

    @ApiOperation("查询商品题库目录列表")
    @GetMapping("/bank/list")
    public AjaxResult<List<GoodsAttachedVo>> bankList(GoodsAttachedQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        List<GoodsAttachedVo> list = iGoodsAttachedService.selectList(bo);
        return AjaxResult.success(list);
    }

    @ApiOperation("查询商品题库做过目录列表")
    @GetMapping("/bank/dolist")
    public AjaxResult<List<GoodsAttachedVo>> bankDoList(GoodsAttachedQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        List<GoodsAttachedVo> list = iGoodsAttachedService.selectDoList(bo);
        return AjaxResult.success(list);
    }

    /**
     * 查询关联章卷列表
     */
    @ApiOperation("查询模块卷关联章卷做过列表")
    @GetMapping("/chapter/dolist")
    public AjaxResult<List<QuestionChapterVo>> chapterDoList(QuestionModuleChapterQueryBo bo) {
        List<QuestionChapterVo> list = iQuestionModuleChapterService.getDoList(bo);
        return AjaxResult.success(list);
    }

    /**
     * 查询关联试卷列表
     */
    @ApiOperation("查询章卷关联试卷做过列表")
    @GetMapping("/exam/dolist")
    public AjaxResult<List<ExamVo>> examDoList(QuestionChapterExamQueryBo bo) {
        List<ExamVo> list = iQuestionChapterExamService.getDoList(bo);
        return AjaxResult.success(list);
    }
}
