package com.ruoyi.web.controller.train;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dto.PaperDto;
import com.ruoyi.dto.TrainingPlanDto;
import com.ruoyi.entity.*;
import com.ruoyi.service.TrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;

/**
 * @program: RuoYi-Vue-master
 * @description: 培训控制层
 * @author: niujie
 * @create: 2023-08-03 20:29
 */
@Api(tags = "培训")
@RestController
@RequestMapping("/train")
@Anonymous
@CrossOrigin
public class TrainController {
    @Autowired
    private TrainService trainService;

    /**
     * APP首页统计
     * @return
     */
    @ApiOperation("培训计划")
    @GetMapping("/trainStatistics")
    public AjaxResult trainStatistics() {
        return AjaxResult.success(trainService.trainStatistics());
    }

    /**
     * 查询培训计划分类列表
     * @param categoriesName
     * @return
     */
    @ApiOperation("查询培训计划分类列表")
    @GetMapping("/getTrainingPlanClassificationList")
    public AjaxResult getTrainingPlanClassificationList(@RequestParam("categoriesName") String categoriesName) {
        return AjaxResult.success(trainService.getTrainingPlanClassificationList(categoriesName));
    }

    /**
     * 新增/修改培训计划分类
     * @param trainingPlanClassification
     * @return
     */
    @ApiOperation("新增/修改培训计划分类")
    @PostMapping("/saveTrainingPlanClassification")
    public AjaxResult saveTrainingPlanClassificationList(@RequestBody TrainingPlanClassification trainingPlanClassification) {
        if (StringUtils.isBlank(trainingPlanClassification.getCategoriesName())) {
            return AjaxResult.error("类目名称不可为空");
        }
        /*if (null == trainingPlanClassification.getClassificationSort()) {
            return AjaxResult.error("排序不可为空");
        }*/
        return AjaxResult.success(trainService.saveTrainingPlanClassificationList(trainingPlanClassification));
    }

    /**
     * 查看培训计划分类详情
     * @param id
     * @return
     */
    @ApiOperation("查看培训计划分类详情")
    @GetMapping("/getTrainingPlanClassificationById")
    public AjaxResult getTrainingPlanClassificationById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.getTrainingPlanClassificationById(id));
    }

    /**
     * 删除培训计划分类
     * @param id
     * @return
     */
    @ApiOperation("删除培训计划分类")
    @GetMapping("/delTrainingPlanClassificationById")
    public AjaxResult delTrainingPlanClassificationById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.delTrainingPlanClassificationById(id));
    }

    /**
     * 查询末级培训计划分类
     * @return
     */
    @ApiOperation("查询末级培训计划分类")
    @GetMapping("/queryLastLevelClassification")
    public AjaxResult queryLastLevelClassification() {
        return AjaxResult.success(trainService.queryLastLevelClassification());
    }

    /**
     * 查询题库列表
     * @param questionTitle 题目标题
     * @param questionTypes 题目类型
     * @param trainingPlanClassificationId 分类
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation("查询题库列表")
    @GetMapping("/getQuestionBankList")
    public AjaxResult getQuestionBankList(String questionTitle,
                                          String questionTypes,
                                          String trainingPlanClassificationId,
                                          @RequestParam("pageNo") Integer pageNo,
                                          @RequestParam("pageSize") Integer pageSize) {
        return AjaxResult.success(trainService.getQuestionBankList(questionTitle, questionTypes, trainingPlanClassificationId, pageNo, pageSize));
    }

    /**
     * 查询题库详情
     * @param id
     * @return
     */
    @ApiOperation("查询题库详情")
    @GetMapping("/getQuestionBankById")
    public AjaxResult getQuestionBankById(@RequestParam("id") String id) {
        QuestionBank questionBank = trainService.getQuestionBankById(id);
        questionBank.setQuestionAnschas(Arrays.asList(questionBank.getQuestionAnscha().split("##")));
        questionBank.setQuestionOptions(Arrays.asList(questionBank.getQuestionOption().split("##")));
        return AjaxResult.success(questionBank);
    }

    /**
     * 删除题库详情
     * @param id
     * @return
     */
    @ApiOperation("删除题库详情")
    @GetMapping("/delQuestionBankById")
    public AjaxResult delQuestionBankById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.delQuestionBankById(id));
    }

    /**
     * 新增/修改题库
     * @param questionBank
     * @return
     */
    @ApiOperation("新增/修改题库")
    @PostMapping("/saveQuestionBank")
    public AjaxResult saveQuestionBank(@RequestBody QuestionBank questionBank) {
        if (StringUtils.isBlank(questionBank.getQuestionTitle())) {
            return AjaxResult.error("题目标题不可为空");
        }
        if (questionBank.getQuestionOptions().size() <= 0) {
            return AjaxResult.error("题目选项不可为空");
        }
        if (questionBank.getQuestionAnschas().size() <= 0) {
            return AjaxResult.error("题目答案不可为空");
        }
        if (StringUtils.isBlank(questionBank.getQuestionTypes())) {
            return AjaxResult.error("题目类型不可为空");
        }
        if (StringUtils.isBlank(questionBank.getTrainingPlanClassificationId())) {
            return AjaxResult.error("培训计划分类不可为空");
        }
        questionBank.setQuestionOption(String.join("##", questionBank.getQuestionOptions()));
        questionBank.setQuestionAnscha(String.join("##", questionBank.getQuestionAnschas()));
        return AjaxResult.success(trainService.saveQuestionBank(questionBank));
    }

    /**
     * 查询题库select
     * @return
     */
    @ApiOperation("查询题库select")
    @GetMapping("/getQuestionBankSelect")
    public AjaxResult getQuestionBankSelect(String trainingPlanClassificationId) {
        if (StringUtils.isBlank(trainingPlanClassificationId)) {
            return AjaxResult.error("培训计划分类不可为空");
        }
        return AjaxResult.success(trainService.getQuestionBankSelect(trainingPlanClassificationId));
    }

    /**
     * 查询课程列表
     * @param courseTitle 标题
     * @param categoriesAuthor 作者
     * @param categoriesLecturer 主讲老师
     * @param trainingPlanClassificationId 分类
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation("查询课程列表")
    @GetMapping("/getCourseList")
    public AjaxResult getCourseList(String courseTitle,
                                    String categoriesAuthor,
                                    String categoriesLecturer,
                                    String trainingPlanClassificationId,
                                    @RequestParam("pageNo") Integer pageNo,
                                    @RequestParam("pageSize") Integer pageSize) {
        return AjaxResult.success(trainService.getCourseList(courseTitle, categoriesAuthor, categoriesLecturer, trainingPlanClassificationId, pageNo, pageSize));
    }

    /**
     * 查询全部课程列表
     * @return
     */
    @ApiOperation("查询全部课程列表")
    @GetMapping("/getCourseSelect")
    public AjaxResult getCourseSelect(String trainingPlanClassificationId) {
        if (StringUtils.isBlank(trainingPlanClassificationId)) {
            return AjaxResult.error("培训计划分类不可为空");
        }
        return AjaxResult.success(trainService.getCourseSelect(trainingPlanClassificationId));
    }

    /**
     * 查询证书列表
     * @return
     */
    @ApiOperation("查询证书列表")
    @GetMapping("/getCertificateSelect")
    public AjaxResult getCertificateSelect(String trainingPlanClassificationId) {
        if (StringUtils.isBlank(trainingPlanClassificationId)) {
            return AjaxResult.error("培训计划分类不可为空");
        }
        return AjaxResult.success(trainService.getCertificateSelect(trainingPlanClassificationId));
    }

    /**
     * 查询课程详情
     * @param id
     * @return
     */
    @ApiOperation("查询课程详情")
    @GetMapping("/getCourseById")
    public AjaxResult getCourseById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.getCourseById(id));
    }

    /**
     * 删除课程详情
     * @param id
     * @return
     */
    @ApiOperation("删除课程详情")
    @GetMapping("/delCourseById")
    public AjaxResult delCourseById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.delCourseById(id));
    }

    /**
     * 新增/修改课程
     * @param course
     * @return
     */
    @ApiOperation("新增/修改课程")
    @PostMapping("/saveCourse")
    public AjaxResult saveCourse(@RequestBody Course course) {
        if (StringUtils.isBlank(course.getCourseTitle())) {
            return AjaxResult.error("题目标题不可为空");
        }
        if (StringUtils.isBlank(course.getCategoriesBriefIntroduction())) {
            return AjaxResult.error("简介不可为空");
        }
        if (StringUtils.isBlank(course.getCategoriesAuthor())) {
            return AjaxResult.error("作者不可为空");
        }
        if (StringUtils.isBlank(course.getCategoriesLecturer())) {
            return AjaxResult.error("主讲老师不可为空");
        }
        if (StringUtils.isBlank(course.getImportantContent())) {
            return AjaxResult.error("重要内容不可为空");
        }
        if (StringUtils.isBlank(course.getCoursewareUrl())) {
            return AjaxResult.error("课件地址不可为空");
        }
        if (StringUtils.isBlank(course.getCoverPhoto())) {
            return AjaxResult.error("封面图片不可为空");
        }
        if (StringUtils.isBlank(course.getTrainingPlanClassificationId())) {
            return AjaxResult.error("培训计划分类不可为空");
        }
        return AjaxResult.success(trainService.saveCourse(course));
    }

    /**
     * 查询培训计划列表
     * @param trainingPlanName 名称
     * @param lessonScheduleState 状态
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation("查询培训计划列表")
    @GetMapping("/getTrainingPlanList")
    public AjaxResult getTrainingPlanList(String trainingPlanName,
                                    String lessonScheduleState,
                                    @RequestParam("pageNo") Integer pageNo,
                                    @RequestParam("pageSize") Integer pageSize) {
        return AjaxResult.success(trainService.getTrainingPlanList(trainingPlanName, lessonScheduleState, pageNo, pageSize));
    }

    /**
     * 删除培训计划
     * @param id
     * @return
     */
    @ApiOperation("删除培训计划")
    @GetMapping("/delTrainingPlanById")
    public AjaxResult delTrainingPlanById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.delTrainingPlanById(id));
    }

    /**
     * 获取培训计划
     * @param id
     * @return
     */
    @ApiOperation("获取培训计划")
    @GetMapping("/getTrainingPlanById")
    public AjaxResult getTrainingPlanById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.getTrainingPlanById(id));
    }

    /**
     * 新增/修改培训计划
     * @param trainingPlanDto
     * @return
     */
    @ApiOperation("新增/修改培训计划")
    @PostMapping("/saveTrainingPlan")
    public AjaxResult saveTrainingPlan(@RequestBody TrainingPlanDto trainingPlanDto) {
        return AjaxResult.success(trainService.saveTrainingPlan(trainingPlanDto));
    }

    /**
     * 培训计划审核
     * @param trainingPlan
     * @return
     */
    @ApiOperation("培训计划审核")
    @PostMapping("/auditTrainingPlan")
    public AjaxResult auditTrainingPlan(@RequestBody TrainingPlan trainingPlan) {
        if (StringUtils.isBlank(trainingPlan.getId())) {
            return AjaxResult.error("id不可为空");
        }
        if (StringUtils.isBlank(trainingPlan.getLessonScheduleState())) {
            return AjaxResult.error("状态不可为空");
        }
        return AjaxResult.success(trainService.auditTrainingPlan(trainingPlan));
    }

    /**
     * 查询证书列表
     * @param certificateName 名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation("查询证书列表")
    @GetMapping("/getCertificateList")
    public AjaxResult getCertificateList(String certificateName,
                                          @RequestParam("pageNo") Integer pageNo,
                                          @RequestParam("pageSize") Integer pageSize) {
        return AjaxResult.success(trainService.getCertificateList(certificateName, pageNo, pageSize));
    }

    /**
     * 删除证书
     * @param id
     * @return
     */
    @ApiOperation("删除证书")
    @GetMapping("/delCertificateById")
    public AjaxResult delCertificateById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.delCertificateById(id));
    }

    /**
     * 获取证书
     * @param id
     * @return
     */
    @ApiOperation("获取证书")
    @GetMapping("/getCertificateById")
    public AjaxResult getCertificateById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.getCertificateById(id));
    }

    /**
     * 新增/修改证书
     * @param certificate
     * @return
     */
    @ApiOperation("新增/修改证书")
    @PostMapping("/saveCertificate")
    public AjaxResult saveCertificate(@RequestBody Certificate certificate) {
        return AjaxResult.success(trainService.saveCertificate(certificate));
    }

    /**
     * 查询试卷列表
     * @param paperName 名称
     * @param paperStatus 试卷状态 0、启用；1、停用
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation("查询试卷列表")
    @GetMapping("/getPaperList")
    public AjaxResult getPaperList(String paperName,
                                         String paperStatus,
                                         @RequestParam("pageNo") Integer pageNo,
                                         @RequestParam("pageSize") Integer pageSize) {
        return AjaxResult.success(trainService.getPaperList(paperName, paperStatus, pageNo, pageSize));
    }

    /**
     * 停用/删除试卷
     * @param id
     * @param type 1、停用，2、删除
     * @return
     */
    @ApiOperation("停用/删除试卷")
    @GetMapping("/deactivatePaperById")
    public AjaxResult deactivatePaperById(@RequestParam("id") String id, @RequestParam("type") String type) {
        if (StringUtils.isBlank(type)) {
            return AjaxResult.error("类型不可为空");
        }
        return AjaxResult.success(trainService.deactivatePaperById(id, type));
    }

    /**
     * 获取试卷
     * @param id
     * @return
     */
    @ApiOperation("获取试卷")
    @GetMapping("/getPaperById")
    public AjaxResult getPaperById(@RequestParam("id") String id) {
        return AjaxResult.success(trainService.getPaperById(id));
    }

    /**
     * 新增/修改试卷
     * @param paperDto
     * @return
     */
    @ApiOperation("新增/修改试卷")
    @PostMapping("/savePaper")
    public AjaxResult savePaper(@RequestBody PaperDto paperDto) {
        if (Integer.parseInt(paperDto.getParperPassingMark()) > Integer.parseInt(paperDto.getParperFraction())) {
            return AjaxResult.error("及格分数不能大于试卷分数。");
        }
        if (CollectionUtils.isEmpty(paperDto.getParperQuestionBankList())) {
            return AjaxResult.error("试题不可为空。");
        }

        return AjaxResult.success(trainService.savePaper(paperDto));
    }
}
