package com.zhongzheng.controller.user;

import java.util.List;
import java.util.Arrays;

import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.bank.vo.QuestionVo;
import com.zhongzheng.modules.collect.bo.CollectQuestionQueryBo;
import com.zhongzheng.modules.collect.vo.CollectQuestionVo;
import com.zhongzheng.modules.user.bo.UserExamWrongRecordAddBo;
import com.zhongzheng.modules.user.bo.UserExamWrongRecordEditBo;
import com.zhongzheng.modules.user.bo.UserExamWrongRecordQueryBo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import com.zhongzheng.modules.user.service.IUserExamWrongRecordService;
import com.zhongzheng.modules.user.vo.UserExamWrongRecordVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户我的题库错题记录Controller
 *
 * @author hjl
 * @date 2021-12-15
 */
@Api(value = "用户我的题库错题记录控制器", tags = {"我的题库通-用户我的题库错题记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/exam/wwrong/record")
public class UserExamWrongRecordController extends BaseController {

    private final IUserExamWrongRecordService iUserExamWrongRecordService;

    private final WxTokenService wxTokenService;

    /**
     * 查询用户我的题库错题记录列表
     */
    @ApiOperation("查询用户我的题库错题记录列表-按试卷")
    @GetMapping("/list")
    public TableDataInfo<UserExamWrongRecordVo> list(UserExamWrongRecordQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<UserExamWrongRecordVo> list = iUserExamWrongRecordService.selectList(bo);
        return getDataTable(list);
    }

    @ApiOperation("按类型获取收藏数量")
    @GetMapping("/type_list")
    public TableDataInfo<UserExamWrongRecordVo> type_list(UserExamWrongRecordQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<UserExamWrongRecordVo> list = iUserExamWrongRecordService.selectTypeNum(bo);
        return getDataTable(list);
    }

    @ApiOperation("试卷获取题目列表")
    @GetMapping("/exam_question_list")
    public TableDataInfo<UserExamWrongRecordVo> selectQuestionList(UserExamWrongRecordQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<UserExamWrongRecordVo> list = iUserExamWrongRecordService.selectQuestionList(bo);
        return getDataTable(list);
    }

    @ApiOperation("类型获取题目列表")
    @GetMapping("/type_question_list")
    public TableDataInfo<UserExamWrongRecordVo> type_question_list(UserExamWrongRecordQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<UserExamWrongRecordVo> list = iUserExamWrongRecordService.selectTypeQuestionList(bo);
        return getDataTable(list);
    }

    /**
     *
     * 导出用户我的题库错题记录列表
     */
    /*@ApiOperation("导出用户我的题库错题记录列表")
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "用户我的题库错题记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<UserExamWrongRecordVo> export(UserExamWrongRecordQueryBo bo) {
        List<UserExamWrongRecordVo> list = iUserExamWrongRecordService.queryList(bo);
        ExcelUtil<UserExamWrongRecordVo> util = new ExcelUtil<UserExamWrongRecordVo>(UserExamWrongRecordVo.class);
        return util.exportExcel(list, "用户我的题库错题记录");
    }*/

    /**
     * 获取用户我的题库错题记录详细信息
     */
    @ApiOperation("获取用户我的题库错题记录详细信息")
    @GetMapping("/{wrongId}")
    public AjaxResult<UserExamWrongRecordVo> getInfo(@PathVariable("wrongId" ) Long wrongId) {
        return AjaxResult.success(iUserExamWrongRecordService.queryById(wrongId));
    }

    /**
     * 新增用户我的题库错题记录
     */
    @ApiOperation("新增用户我的题库错题记录")
    @Log(title = "用户我的题库错题记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody UserExamWrongRecordAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(iUserExamWrongRecordService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户我的题库错题记录
     */
    @ApiOperation("修改用户我的题库错题记录")
    @Log(title = "用户我的题库错题记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@RequestBody UserExamWrongRecordEditBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(iUserExamWrongRecordService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户我的题库错题记录
     */
    @ApiOperation("删除用户我的题库错题记录")
    @Log(title = "用户我的题库错题记录" , businessType = BusinessType.DELETE)
    @PostMapping("/delete/{wrongIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] wrongIds) {
        return toAjax(iUserExamWrongRecordService.deleteWithValidByIds(Arrays.asList(wrongIds), true) ? 1 : 0);
    }

    /**
     * 删除用户我的题库错题记录
     */
    @ApiOperation("删除用户我的题库错题记录")
    @Log(title = "用户我的题库错题记录" , businessType = BusinessType.DELETE)
    @PostMapping("/delete/question")
    public AjaxResult<Void> remove_question(@RequestBody UserExamWrongRecordQueryBo bo) {

        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(iUserExamWrongRecordService.remove_question(bo));
    }
}
