package com.zd.exam.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.exam.domain.ElExam;
import com.zd.exam.domain.ElExamCertRelation;
import com.zd.exam.domain.ElUserCert;
import com.zd.exam.domain.vo.ElExamVO;
import com.zd.exam.service.ElExamService;
import com.zd.exam.service.IElUserCertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 课程(ElExam)表控制层
 *
 * @author liubo
 * @since 2021-08-31 09:09:15
 */
@RestController
@RequestMapping("/exam")
@Api(tags = {"考试管理"})
public class ElExamController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ElExamService elExamService;
    @Resource
    private TokenService tokenService;
    @Autowired
    private IElUserCertService userCertService;


    /**
     * 主键查询
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EXAM + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据主键查询考试明细",notes="权限字符："+PerPrefix.EXAM_EXAM+ PerFun.QUERY)
    public AjaxResult selectOne(@PathVariable("id") Long id) {
        return AjaxResult.success(this.elExamService.queryById(id));
    }

    /**
     * 获取考试列表
     */
    @GetMapping("/list")
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EXAM + PerFun.LIST)
    @ApiOperation(value = "分页查询考试列表",notes="权限字符："+PerPrefix.EXAM_EXAM+ PerFun.LIST)
    public TableDataInfo list(ElExamVO examvo) {
        startPage();
        List<ElExam> list = elExamService.queryList(examvo);
        return getDataTable(list);
    }

    /**
     * 获取当前用户院系的违规考试列表
     */
    @GetMapping("/my/deptViolations/list")
    @ApiOperation(value = "获取当前用户院系的违规考试列表")
    public TableDataInfo myDeptViolations(ElExamVO examvo) {
        startPage();
        Long deptId = tokenService.getLoginUser().getSysUser().getDeptId();
        examvo.setDeptId(deptId);
        // 1.0 的需求，2.1已经没有违规类型的考试了
        examvo.setScopeType(2); //违规
        examvo.setOpenType(2); //部门

        List<ElExam> list = elExamService.queryList(examvo);
        return getDataTable(list);
    }

    /**
     * 查询在线考试列表（用户相关，区分列表接口）
     */
    @ApiOperation(value = "在线考试列表")
    @RequestMapping(value = "/onlineExamList")
    public TableDataInfo queryMyExamList(ElExam exam) {
        startPage();
        //分页查询并转换
        List<ElExam> list = elExamService.queryMyExamList(exam);
        return getDataTable(list);
    }

    /**
     * 获取考试下拉列表
     */
    @PostMapping("/queryOption")
    @ApiOperation(value = "查询考试下拉列表")
    public AjaxResult queryOption(@RequestBody ElExamVO examvo) {
        List<ElExam> list = elExamService.queryList(examvo);
        return AjaxResult.success(list);
    }

    /**
     * 添加考试
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EXAM + PerFun.ADD)
    @Log(title = "考试", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "添加考试",notes="权限字符："+PerPrefix.EXAM_EXAM+ PerFun.ADD)
    public AjaxResult add(@RequestBody @Valid ElExam exam) {
        return toAjax(elExamService.insert(exam));
    }

    /**
     * 修改考试
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EXAM + PerFun.EDIT)
    @Log(title = "考试", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改考试",notes="权限字符："+PerPrefix.EXAM_EXAM+ PerFun.EDIT)
    public AjaxResult edit(@RequestBody ElExam exam) {
        return toAjax(elExamService.update(exam));
    }

    /**
     * 批量删除
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EXAM + PerFun.REMOVE)
    @Log(title = "考试", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除",notes="权限字符："+PerPrefix.EXAM_EXAM+ PerFun.REMOVE)
    public AjaxResult delete(@PathVariable Long[] ids) {
        //根据ID删除
        return toAjax(elExamService.deleteByIds(Arrays.asList(ids)));
    }

    /**
     * 查询考试关联的证书
     */
    @PostMapping("/queryCertById")
    @ApiOperation(value = "查询考试下拉列表")
    public AjaxResult queryCertById(@RequestBody ElExam exam) {
        List<ElExamCertRelation> list = elExamService.queryCertById(exam);
        return AjaxResult.success(list);
    }

    /**
     * 保存考试关联的培训
     */
    @PostMapping("/saveSafeTrainById")
    @ApiOperation(value = "保存考试关联的培训")
    public AjaxResult saveSafeTrainById(@RequestBody ElExam exam) {
        if (exam.getId() == null && exam.getClassifyIds() == null) {
            return error("参数异常！");
        }
        elExamService.saveSafeTrainById(exam);
        return AjaxResult.success();
    }

    /**
     * 保存考试关联的证书
     */
    @PostMapping("/saveCertById")
    @ApiOperation(value = "保存考试关联的证书")
    public AjaxResult saveCertById(@RequestBody ElExam exam) {
        elExamService.saveCertById(exam);
        return AjaxResult.success();
    }

    /**
     * 根据登录用户查询 我的证书列表
     */
    @PostMapping("/queryMyCert")
    @ApiOperation(value = "根据登录用户查询我的证书列表")
    public TableDataInfo queryMyCert(@RequestBody ElUserCert userCert) {
        userCert.setJoinUserId(SecurityUtils.getUserId());
        startPage();
        List<ElUserCert> userCerts = userCertService.queryMyCert(userCert);
        return getDataTable(userCerts);
    }
}
