package com.template.controller;


import com.template.annotation.DESRespondSecret;
import com.template.api.SmartSemesterControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.evaluate.teacher.SmartEvaluateTdetail;
import com.template.model.evaluate.teacher.SmartEvaluateTeacher;
import com.template.model.evaluate.teacher.SmartScoreManage;
import com.template.model.pojo.SmartSemester;
import com.template.model.pojo.SmartIdentity;
import com.template.model.pojo.SmartSemester;
import com.template.model.pojo.SmartTeaching;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.SemesterVo;
import com.template.model.vo.UserSubjectVo;
import com.template.services.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 应用管理 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-03-07
 */
@RestController
//返回参数加密注解
@DESRespondSecret
public class SmartSemesterController implements SmartSemesterControllerAPI {

    @Autowired
    private SmartSemesterService smartSemesterService;

    @Autowired
    private SmartScoreManageService smartScoreManageService;

    @Autowired
    private SmartTeachingService smartTeachingService;

    @Autowired
    private SmartEvaluateTdetailService smartEvaluateTdetailService;

    @Autowired
    private SmartEvaluateTeacherService smartEvaluateTeacherService;

    /**
     * 新增学期
     *
     * @param smartApply    学期数据
     * @param bindingResult
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult insertSmartSemester(SmartSemester smartApply, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        Date startTime = smartApply.getStartTime();
        Date endTime = smartApply.getEndTime();
        SmartSemester semester = smartSemesterService.querySemesterByName(smartApply.getName());
        if(semester != null){
            return CommonResult.fail(smartApply.getName()+"学期数据在系统中已存在");
        }

        //一天里不能是多个学期
        List<SmartSemester> list = smartSemesterService.getSemesterTime(startTime, endTime);
        if (ObjectUtils.isNotEmpty(list) && list.size() > 0) {
            return CommonResult.fail("该时间段已包含其他学期");
        }

        try{
            int result = smartSemesterService.insertSmartSemester(smartApply);
            if(result <= 0){
                //return CommonResult.fail("添加失败");
                throw new Exception("添加失败！");
            }

            //region 添加对应的默认教师评分项
            List<SmartScoreManage> scoreManages = new ArrayList<>();
            //region 德
            SmartScoreManage morals = new SmartScoreManage();
            morals.setTermId(result);
            morals.setTerm(smartApply.getName());
            morals.setScoreItem("德");
            morals.setParentId(0);
            morals.setScoreNum(12.0);
            scoreManages.add(morals);
            //endregion
            //region 能
            SmartScoreManage ability = new SmartScoreManage();
            ability.setTermId(result);
            ability.setTerm(smartApply.getName());
            ability.setScoreItem("能");
            ability.setParentId(0);
            ability.setScoreNum(28.0);
            scoreManages.add(ability);
            //endregion
            //region 勤
            SmartScoreManage diligent = new SmartScoreManage();
            diligent.setTermId(result);
            diligent.setTerm(smartApply.getName());
            diligent.setScoreItem("勤");
            diligent.setParentId(0);
            diligent.setScoreNum(6.0);
            scoreManages.add(diligent);
            //endregion
            //region 绩
            SmartScoreManage merit = new SmartScoreManage();
            merit.setTermId(result);
            merit.setTerm(smartApply.getName());
            merit.setScoreItem("绩");
            merit.setParentId(0);
            merit.setScoreNum(54.0);
            scoreManages.add(merit);
            //endregion
            boolean inserSManages = smartScoreManageService.saveBatch(scoreManages);
            if (!inserSManages) {
                //return CommonResult.fail("添加失败");
                throw new Exception("添加失败！");
            }

            List<SmartScoreManage> scoreManagesData = smartScoreManageService.queryDatasByTermId(result);

            List<SmartScoreManage> scoreManageDetails = new ArrayList<>();
            for (SmartScoreManage data : scoreManagesData) {
                switch (data.getScoreItem()) {
                    case "德":
                        SmartScoreManage one = new SmartScoreManage();
                        one.setTermId(result);
                        one.setTerm(smartApply.getName());
                        one.setScoreItem("为人师表");
                        one.setParentId(data.getId());
                        one.setScoreNum(3.0);
                        scoreManageDetails.add(one);
                        SmartScoreManage two = new SmartScoreManage();
                        two.setTermId(result);
                        two.setTerm(smartApply.getName());
                        two.setScoreItem("关爱学生");
                        two.setParentId(data.getId());
                        two.setScoreNum(3.0);
                        scoreManageDetails.add(two);
                        SmartScoreManage three = new SmartScoreManage();
                        three.setTermId(result);
                        three.setTerm(smartApply.getName());
                        three.setScoreItem("爱岗敬业");
                        three.setParentId(data.getId());
                        three.setScoreNum(6.0);
                        scoreManageDetails.add(three);
                        break;
                    case "能":
                        SmartScoreManage none = new SmartScoreManage();
                        none.setTermId(result);
                        none.setTerm(smartApply.getName());
                        none.setScoreItem("基础能力");
                        none.setParentId(data.getId());
                        none.setScoreNum(1.0);
                        scoreManageDetails.add(none);
                        SmartScoreManage ntwo = new SmartScoreManage();
                        ntwo.setTermId(result);
                        ntwo.setTerm(smartApply.getName());
                        ntwo.setScoreItem("教学能力");
                        ntwo.setParentId(data.getId());
                        ntwo.setScoreNum(16.0);
                        scoreManageDetails.add(ntwo);
                        SmartScoreManage nthree = new SmartScoreManage();
                        nthree.setTermId(result);
                        nthree.setTerm(smartApply.getName());
                        nthree.setScoreItem("学习能力");
                        nthree.setParentId(data.getId());
                        nthree.setScoreNum(7.0);
                        scoreManageDetails.add(nthree);
                        SmartScoreManage nfour = new SmartScoreManage();
                        nfour.setTermId(result);
                        nfour.setTerm(smartApply.getName());
                        nfour.setScoreItem("创新能力");
                        nfour.setParentId(data.getId());
                        nfour.setScoreNum(4.0);
                        scoreManageDetails.add(nfour);
                        break;
                    case "勤":
                        SmartScoreManage qone = new SmartScoreManage();
                        qone.setTermId(result);
                        qone.setTerm(smartApply.getName());
                        qone.setScoreItem("考勤");
                        qone.setParentId(data.getId());
                        qone.setScoreNum(6.0);
                        scoreManageDetails.add(qone);
                        break;
                    case "绩":
                        SmartScoreManage jone = new SmartScoreManage();
                        jone.setTermId(result);
                        jone.setTerm(smartApply.getName());
                        jone.setScoreItem("工作量");
                        jone.setParentId(data.getId());
                        jone.setScoreNum(8.0);
                        scoreManageDetails.add(jone);
                        SmartScoreManage jtwo = new SmartScoreManage();
                        jtwo.setTermId(result);
                        jtwo.setTerm(smartApply.getName());
                        jtwo.setScoreItem("教学成绩");
                        jtwo.setParentId(data.getId());
                        jtwo.setScoreNum(30.0);
                        scoreManageDetails.add(jtwo);
                        SmartScoreManage jthree = new SmartScoreManage();
                        jthree.setTermId(result);
                        jthree.setTerm(smartApply.getName());
                        jthree.setScoreItem("教研成果");
                        jthree.setParentId(data.getId());
                        jthree.setScoreNum(10.0);
                        scoreManageDetails.add(jthree);
                        SmartScoreManage jfour = new SmartScoreManage();
                        jfour.setTermId(result);
                        jfour.setTerm(smartApply.getName());
                        jfour.setScoreItem("专业成长");
                        jfour.setParentId(data.getId());
                        jfour.setScoreNum(6.0);
                        scoreManageDetails.add(jfour);
                        break;
                    default:
                        break;
                }
            }

            boolean inserSDetails = smartScoreManageService.saveBatch(scoreManageDetails);
            if (!inserSDetails) {
                //return CommonResult.fail("添加失败");
                throw new Exception("添加失败！");
            }
            //endregion

            //region 任课老师赋值
            List<UserSubjectVo> teachings = smartTeachingService.querySmartTeachings(null);
            if(teachings != null && teachings.size() > 0){
                List<SmartEvaluateTeacher> sets = new ArrayList<>();
                for (UserSubjectVo usv:teachings) {
                    if(usv.getUserName() != null){
                        SmartEvaluateTeacher set = new SmartEvaluateTeacher();
                        set.setCardNo(usv.getCardNo());
                        set.setName(usv.getUserName());
                        set.setUserId(usv.getUserId());
                        set.setGradeId(usv.getGradeId());
                        set.setGradeName(usv.getGradeName());
                        set.setTermId(smartApply.getId());
                        set.setTerm(smartApply.getName());
                        set.setSubjectId(usv.getSubjectId());
                        set.setSubject(usv.getSubjectName());
                        set.setClassId(usv.getClassId());
                        set.setClassName(usv.getClassName());
                        set.setTotalScore(0.0);
                        set.setLowingScore(0.0);
                        sets.add(set);
                    }
                }
                if(sets != null && sets.size() > 0){
                    boolean teacherBatch = smartEvaluateTeacherService.saveBatch(sets);
                    if(!teacherBatch){
                        throw new Exception("添加失败！");
                    }
                }
            }
            //endregion
        }catch (Exception e) {
            throw new Exception("添加失败！");
        }

        return CommonResult.ok("添加成功");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult testSmartSemester(int id) throws Exception {

        SmartSemester smartApply = smartSemesterService.getSmartById(id);

        try{
            int result = smartApply.getId();

            //region 添加对应的默认教师评分项
            List<SmartScoreManage> scoreManages = new ArrayList<>();
            //region 德
            SmartScoreManage morals = new SmartScoreManage();
            morals.setTermId(result);
            morals.setTerm(smartApply.getName());
            morals.setScoreItem("德");
            morals.setParentId(0);
            morals.setScoreNum(12.0);
            scoreManages.add(morals);
            //endregion
            //region 能
            SmartScoreManage ability = new SmartScoreManage();
            ability.setTermId(result);
            ability.setTerm(smartApply.getName());
            ability.setScoreItem("能");
            ability.setParentId(0);
            ability.setScoreNum(28.0);
            scoreManages.add(ability);
            //endregion
            //region 勤
            SmartScoreManage diligent = new SmartScoreManage();
            diligent.setTermId(result);
            diligent.setTerm(smartApply.getName());
            diligent.setScoreItem("勤");
            diligent.setParentId(0);
            diligent.setScoreNum(6.0);
            scoreManages.add(diligent);
            //endregion
            //region 绩
            SmartScoreManage merit = new SmartScoreManage();
            merit.setTermId(result);
            merit.setTerm(smartApply.getName());
            merit.setScoreItem("绩");
            merit.setParentId(0);
            merit.setScoreNum(54.0);
            scoreManages.add(merit);
            //endregion
            boolean inserSManages = smartScoreManageService.saveBatch(scoreManages);
            if (!inserSManages) {
                //return CommonResult.fail("添加失败");
                throw new Exception("添加失败！");
            }

            List<SmartScoreManage> scoreManagesData = smartScoreManageService.queryDatasByTermId(result);

            List<SmartScoreManage> scoreManageDetails = new ArrayList<>();
            for (SmartScoreManage data : scoreManagesData) {
                switch (data.getScoreItem()) {
                    case "德":
                        SmartScoreManage one = new SmartScoreManage();
                        one.setTermId(result);
                        one.setTerm(smartApply.getName());
                        one.setScoreItem("为人师表");
                        one.setParentId(data.getId());
                        one.setScoreNum(3.0);
                        scoreManageDetails.add(one);
                        SmartScoreManage two = new SmartScoreManage();
                        two.setTermId(result);
                        two.setTerm(smartApply.getName());
                        two.setScoreItem("关爱学生");
                        two.setParentId(data.getId());
                        two.setScoreNum(3.0);
                        scoreManageDetails.add(two);
                        SmartScoreManage three = new SmartScoreManage();
                        three.setTermId(result);
                        three.setTerm(smartApply.getName());
                        three.setScoreItem("爱岗敬业");
                        three.setParentId(data.getId());
                        three.setScoreNum(6.0);
                        scoreManageDetails.add(three);
                        break;
                    case "能":
                        SmartScoreManage none = new SmartScoreManage();
                        none.setTermId(result);
                        none.setTerm(smartApply.getName());
                        none.setScoreItem("基础能力");
                        none.setParentId(data.getId());
                        none.setScoreNum(1.0);
                        scoreManageDetails.add(none);
                        SmartScoreManage ntwo = new SmartScoreManage();
                        ntwo.setTermId(result);
                        ntwo.setTerm(smartApply.getName());
                        ntwo.setScoreItem("教学能力");
                        ntwo.setParentId(data.getId());
                        ntwo.setScoreNum(16.0);
                        scoreManageDetails.add(ntwo);
                        SmartScoreManage nthree = new SmartScoreManage();
                        nthree.setTermId(result);
                        nthree.setTerm(smartApply.getName());
                        nthree.setScoreItem("学习能力");
                        nthree.setParentId(data.getId());
                        nthree.setScoreNum(7.0);
                        scoreManageDetails.add(nthree);
                        SmartScoreManage nfour = new SmartScoreManage();
                        nfour.setTermId(result);
                        nfour.setTerm(smartApply.getName());
                        nfour.setScoreItem("创新能力");
                        nfour.setParentId(data.getId());
                        nfour.setScoreNum(4.0);
                        scoreManageDetails.add(nfour);
                        break;
                    case "勤":
                        SmartScoreManage qone = new SmartScoreManage();
                        qone.setTermId(result);
                        qone.setTerm(smartApply.getName());
                        qone.setScoreItem("考勤");
                        qone.setParentId(data.getId());
                        qone.setScoreNum(6.0);
                        scoreManageDetails.add(qone);
                        break;
                    case "绩":
                        SmartScoreManage jone = new SmartScoreManage();
                        jone.setTermId(result);
                        jone.setTerm(smartApply.getName());
                        jone.setScoreItem("工作量");
                        jone.setParentId(data.getId());
                        jone.setScoreNum(8.0);
                        scoreManageDetails.add(jone);
                        SmartScoreManage jtwo = new SmartScoreManage();
                        jtwo.setTermId(result);
                        jtwo.setTerm(smartApply.getName());
                        jtwo.setScoreItem("教学成绩");
                        jtwo.setParentId(data.getId());
                        jtwo.setScoreNum(30.0);
                        scoreManageDetails.add(jtwo);
                        SmartScoreManage jthree = new SmartScoreManage();
                        jthree.setTermId(result);
                        jthree.setTerm(smartApply.getName());
                        jthree.setScoreItem("教研成果");
                        jthree.setParentId(data.getId());
                        jthree.setScoreNum(10.0);
                        scoreManageDetails.add(jthree);
                        SmartScoreManage jfour = new SmartScoreManage();
                        jfour.setTermId(result);
                        jfour.setTerm(smartApply.getName());
                        jfour.setScoreItem("专业成长");
                        jfour.setParentId(data.getId());
                        jfour.setScoreNum(6.0);
                        scoreManageDetails.add(jfour);
                        break;
                    default:
                        break;
                }
            }

            boolean inserSDetails = smartScoreManageService.saveBatch(scoreManageDetails);
            if (!inserSDetails) {
                //return CommonResult.fail("添加失败");
                throw new Exception("添加失败！");
            }
            //endregion

            //region 任课老师赋值
            List<UserSubjectVo> teachings = smartTeachingService.querySmartTeachings(null);
            if(teachings != null && teachings.size() > 0){
                List<SmartEvaluateTeacher> sets = new ArrayList<>();
                for (UserSubjectVo usv:teachings) {
                    if(usv.getUserName() != null){
                        SmartEvaluateTeacher set = new SmartEvaluateTeacher();
                        set.setCardNo(usv.getCardNo());
                        set.setName(usv.getUserName());
                        set.setUserId(usv.getUserId());
                        set.setGradeId(usv.getGradeId());
                        set.setGradeName(usv.getGradeName());
                        set.setTermId(smartApply.getId());
                        set.setTerm(smartApply.getName());
                        set.setSubjectId(usv.getSubjectId());
                        set.setSubject(usv.getSubjectName());
                        set.setClassId(usv.getClassId());
                        set.setClassName(usv.getClassName());
                        set.setTotalScore(0.0);
                        set.setLowingScore(0.0);
                        sets.add(set);
                    }
                }
                if(sets != null && sets.size() > 0){
                    boolean teacherBatch = smartEvaluateTeacherService.saveBatch(sets);
                    if(!teacherBatch){
                        throw new Exception("添加失败！");
                    }
                }
            }
            //endregion
        }catch (Exception e) {
            throw new Exception("添加失败！");
        }

        return CommonResult.ok("添加成功");
    }

    /**
     * 更新学期
     *
     * @param sa            学期数据
     * @param bindingResult
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult updateSmartSemesterById(SmartSemester sa, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        Date startTime = sa.getStartTime();
        Date endTime = sa.getEndTime();
        //        一天里不能是多个学期
        List<SmartSemester> list = smartSemesterService.getSemesterTime(startTime, endTime);
        if (ObjectUtils.isNotEmpty(list) && list.size() > 0) {
            return CommonResult.fail("该时间段已包含其他学期");
        }

        int result = smartSemesterService.updateSmartSemester(sa);
        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 学期分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    @DESRespondSecret(validated = true)
    public CommonResult queryPageSmartSemesters(int currentPage, int pageCount, String name) {
        PageUtils<SmartSemester> result = smartSemesterService.queryPageSmartSemesters(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }


    @Override
    @DESRespondSecret(validated = true)
    public CommonResult deleteSmartSemesterById(int id) {

        SmartSemester data = smartSemesterService.getSmartById(id);

        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = smartSemesterService.deleteSmartSemesterById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

}

