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.user.bo.UserExamRecordAddBo;
import com.zhongzheng.modules.user.bo.UserExamRecordEditBo;
import com.zhongzheng.modules.user.bo.UserExamRecordQueryBo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import com.zhongzheng.modules.user.service.IUserExamRecordService;
import com.zhongzheng.modules.user.vo.UserExamRecordVo;
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/record")
public class UserExamRecordController extends BaseController {

    private final IUserExamRecordService iUserExamRecordService;
    private final WxTokenService wxTokenService;


    /**
     * 查询用户的题库试卷做题历史列表
     */
    @ApiOperation("查询用户的题库试卷做题历史列表")
    @GetMapping("/list")
    public TableDataInfo<UserExamRecordVo> list(UserExamRecordQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<UserExamRecordVo> list = iUserExamRecordService.selectList(bo);
        return getDataTable(list);
    }

    /**
     * 查询用户的题库试卷做题历史列表
     */
    @ApiOperation("查询用户的题库试卷做题历史次数")
    @GetMapping("/count")
    public AjaxResult<Integer> recordNum(UserExamRecordQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        Integer num = iUserExamRecordService.recordNum(bo);
        return AjaxResult.success(num);
    }

    /**
     * 导出用户的题库试卷做题历史列表
     */
   /* @ApiOperation("导出用户的题库试卷做题历史列表")
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "用户的题库试卷做题历史", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<UserExamRecordVo> export(UserExamRecordQueryBo bo) {
        List<UserExamRecordVo> list = iUserExamRecordService.queryList(bo);
        ExcelUtil<UserExamRecordVo> util = new ExcelUtil<UserExamRecordVo>(UserExamRecordVo.class);
        return util.exportExcel(list, "用户的题库试卷做题历史");
    }*/

    /**
     * 获取用户的题库试卷做题历史详细信息
     */
    @ApiOperation("获取用户的题库试卷做题历史详细信息")
    @GetMapping("/{recordId}")
    public AjaxResult<UserExamRecordVo> getInfo(@PathVariable("recordId" ) Long recordId) {
        UserExamRecordQueryBo bo = new UserExamRecordQueryBo();
        bo.setRecordId(recordId);
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iUserExamRecordService.selectDetail(bo));
    }

    /**
     * 新增用户的题库试卷做题历史
     */
    @ApiOperation("新增用户的题库试卷做题历史")
    @Log(title = "用户的题库试卷做题历史", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Long> add(@RequestBody UserExamRecordAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iUserExamRecordService.insertByAddBo(bo));
    }

    /**
     * 修改用户的题库试卷做题历史
     */
    @ApiOperation("修改用户的题库试卷做题历史")
    @Log(title = "用户的题库试卷做题历史", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@RequestBody UserExamRecordEditBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(iUserExamRecordService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户的题库试卷做题历史
     */
    /*@ApiOperation("删除用户的题库试卷做题历史")
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "用户的题库试卷做题历史" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] recordIds) {
        return toAjax(iUserExamRecordService.deleteWithValidByIds(Arrays.asList(recordIds), true) ? 1 : 0);
    }*/
}
