package com.zhongzheng.controller.user;

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

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhongzheng.common.core.domain.model.LoginUser;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.bank.vo.QuestionImport;
import com.zhongzheng.modules.exam.vo.ExamApplyVo;
import com.zhongzheng.modules.user.bo.*;
import com.zhongzheng.modules.user.service.IUserExamGoodsService;
import com.zhongzheng.modules.user.vo.UserExamGoodsVo;
import com.zhongzheng.modules.user.vo.UserSubscribeImport;
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.modules.user.vo.UserSubscribeVo;
import com.zhongzheng.modules.user.service.IUserSubscribeService;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户预约考试Controller
 * 
 * @author ruoyi
 * @date 2021-12-07
 */
@Api(value = "报考数据控制器", tags = {"报考数据控制器"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/subscribe")
public class UserSubscribeController extends BaseController {

    private final IUserSubscribeService iUserSubscribeService;

    private final IUserExamGoodsService iUserExamGoodsService;

    /**
     * 查询用户预约考试列表
     */
    @ApiOperation("查询报考数据列表")
    @PreAuthorize("@ss.hasPermi('system:subscribe:list')")
    @GetMapping("/list")
    public TableDataInfo<UserSubscribeVo> listSubscribe(UserSubscribeQueryBo bo) {
        startPage();
        List<UserSubscribeVo> list = iUserSubscribeService.listSubscribe(bo);
        return getDataTable(list);
    }



    /**
     * 修改用户预约考试
     */
    @ApiOperation("批量取消预约，批量选考试登记状态,批量修改预约状态，批量前培账号开通")
    @PreAuthorize("@ss.hasPermi('system:subscribe:edit')")
    @Log(title = "用户预约考试", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult<Void> edit(@RequestBody UserSubscribeEditBo bo) {
        return toAjax(iUserSubscribeService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户预约考试
     */
    @ApiOperation("批量改证书状态")
    @PreAuthorize("@ss.hasPermi('system:subscribe:edit')")
    @Log(title = "用户预约考试", businessType = BusinessType.UPDATE)
    @PostMapping()
    public AjaxResult<Void> editCertificate(@RequestBody List<UserSubscribeCertificateEditBo> bo) {
        return toAjax(iUserSubscribeService.editCertificate(bo) ? 1 : 0);
    }

    /**
     * 查询用户下这个商品的考试次数，前培次数列表
     */
    @ApiOperation("查询用户下商品的考试次数，前培次数列表")
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/listUserExam")
    public TableDataInfo<UserExamGoodsVo> listUserExam(UserExamGoodsQueryBo bo) {
        startPage();
        List<UserExamGoodsVo> list = iUserExamGoodsService.listUserExam(bo);
        return getDataTable(list);
    }


    /**
     * 查询详细商品考试次数，前培次数
     */
    @ApiOperation("查询详细商品考试次数，前培次数")
    @PreAuthorize("@ss.hasPermi('system:apply:query')")
    @GetMapping("/UserExam")
    public AjaxResult<UserExamGoodsVo> getUserExamInfo(UserExamGoodsQueryBo userExamGoodsQueryBo) {
        UserExamGoodsVo userExamGoodsVo = iUserExamGoodsService.getUserExamInfo(userExamGoodsQueryBo);
        return AjaxResult.success(userExamGoodsVo);
    }

    @Log(title = "导入预约考试模板", businessType = BusinessType.IMPORT)
    @ApiOperation("导入新增预约考试模板")
    @PreAuthorize("@ss.hasPermi('system:apply:import')")
    @PostMapping("/importData")
    public AjaxResult<UserSubscribeImport> importData(MultipartFile file) throws Exception {
        ExcelUtil<UserSubscribeImport> util = new ExcelUtil<UserSubscribeImport>(UserSubscribeImport.class);
        List<UserSubscribeImport> userSubscribeImportList = util.importExcel(file.getInputStream());
        List<UserSubscribeImport> userSubscribeImport = iUserSubscribeService.importData(userSubscribeImportList);

        if (!CollectionUtils.isEmpty(userSubscribeImport)) {
            return util.exportExcel(userSubscribeImport, "导入失败原因，请勿将此excel修改后再次上传，请在导入模板修改上传");
        }
        UserSubscribeImport userSubscribeImport1 = null;
        return AjaxResult.successNoMsg(userSubscribeImport1);
    }

    @Log(title = "导入修改预约考试模板", businessType = BusinessType.IMPORT)
    @ApiOperation("导入修改预约考试模板")
    @PreAuthorize("@ss.hasPermi('system:apply:import')")
    @PostMapping("/importUpdateData")
    public AjaxResult<UserSubscribeImport> importUpdateData(MultipartFile file) throws Exception {
        ExcelUtil<UserSubscribeImport> util = new ExcelUtil<UserSubscribeImport>(UserSubscribeImport.class);
        List<UserSubscribeImport> userSubscribeImportList = util.importExcel(file.getInputStream());
        List<UserSubscribeImport> userSubscribeImport = iUserSubscribeService.importUpdateData(userSubscribeImportList);
        if (!CollectionUtils.isEmpty(userSubscribeImport)) {
            return util.exportExcel(userSubscribeImport, "导入失败原因，请勿将此excel修改后再次上传，请在导入模板修改上传");
        }
        UserSubscribeImport userSubscribeImport1 = null;
        return AjaxResult.successNoMsg(userSubscribeImport1);
    }

}
