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.UserBankRecordAddBo;
import com.zhongzheng.modules.user.bo.UserBankRecordEditBo;
import com.zhongzheng.modules.user.bo.UserBankRecordQueryBo;
import com.zhongzheng.modules.user.bo.UserStudyRecordPhotoAddBo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import com.zhongzheng.modules.user.service.IUserBankRecordService;
import com.zhongzheng.modules.user.service.IUserStudyRecordPhotoService;
import com.zhongzheng.modules.user.vo.UserBankRecordVo;
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 ruoyi
 * @date 2021-06-24
 */
@Api(value = "视频商品用户的做题历史控制器", tags = {"视频商品用户的做题历史管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/bank/record")
public class UserBankRecordController extends BaseController {

    private final IUserBankRecordService iUserBankRecordService;

    private final WxTokenService wxTokenService;

    private final IUserStudyRecordPhotoService userStudyRecordPhotoService;

    /**
     * 查询用户的做题历史列表
     */
    @ApiOperation("视频商品查询用户的做题历史列表")
    @GetMapping("/list")
    public TableDataInfo<UserBankRecordVo> list(UserBankRecordQueryBo bo) {
        startPage();
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        List<UserBankRecordVo> list = iUserBankRecordService.queryList(bo);
        return getDataTable(list);
    }

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

    /**
     * 修改用户的做题历史
     */
    @ApiOperation("视频商品做题拍照上传照片")
    @Log(title = "视频商品做题拍照上传照片", businessType = BusinessType.UPDATE)
    @PostMapping("/addPhoto")
    public AjaxResult<Void> addPhoto(@RequestBody UserStudyRecordPhotoAddBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(userStudyRecordPhotoService.insertExamByAddBo(bo) ? 1 : 0);
    }

    /**
     * 获取用户的做题历史详细信息
     */
    @ApiOperation("视频商品获取用户的做题历史详细信息")
    @GetMapping("/{recordId}")
    public AjaxResult<UserBankRecordVo> getInfo(@PathVariable("recordId" ) Long recordId) {
        return AjaxResult.success(iUserBankRecordService.queryById(recordId));
    }

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

    /**
     * 获取用户的做题历史详细信息
     */
    @ApiOperation("视频商品获取用户的做题历史次数")
    @GetMapping("/doNum")
    public AjaxResult<Long> getUserExamDoNum(UserBankRecordQueryBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return AjaxResult.success(iUserBankRecordService.getUserExamDoNum(bo));
    }

}
