package com.xjrsoft.module.assessment.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.common.utils.WeChatUtil;
import com.xjrsoft.config.CommonPropertiesConfig;
import com.xjrsoft.module.assessment.dto.AddAssessmentTemplatePlanDto;
import com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerPageDto;
import com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerStudentDto;
import com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerSubmitDto;
import com.xjrsoft.module.assessment.dto.AssessmentPlanSubmitDto;
import com.xjrsoft.module.assessment.dto.AssessmentQuestionResultDto;
import com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanPageDto;
import com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanSureDto;
import com.xjrsoft.module.assessment.dto.UpdateAssessmentTemplatePlanDto;
import com.xjrsoft.module.assessment.dto.UpdateAssessmentTemplatePlanTimeDto;
import com.xjrsoft.module.assessment.entity.AssessmentPlanAnswerClass;
import com.xjrsoft.module.assessment.entity.AssessmentPlanAnswerResult;
import com.xjrsoft.module.assessment.entity.AssessmentPlanAnswerStudent;
import com.xjrsoft.module.assessment.entity.AssessmentPlanQuestion;
import com.xjrsoft.module.assessment.entity.AssessmentQuestion;
import com.xjrsoft.module.assessment.entity.AssessmentTemplate;
import com.xjrsoft.module.assessment.entity.AssessmentTemplatePlan;
import com.xjrsoft.module.assessment.mapper.AssessmentPlanAnswerClassMapper;
import com.xjrsoft.module.assessment.mapper.AssessmentPlanAnswerResultMapper;
import com.xjrsoft.module.assessment.mapper.AssessmentPlanAnswerStudentMapper;
import com.xjrsoft.module.assessment.mapper.AssessmentPlanQuestionMapper;
import com.xjrsoft.module.assessment.mapper.AssessmentQuestionMapper;
import com.xjrsoft.module.assessment.mapper.AssessmentTemplateMapper;
import com.xjrsoft.module.assessment.mapper.AssessmentTemplatePlanMapper;
import com.xjrsoft.module.assessment.service.IAssessmentTemplatePlanService;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerClassResultVo;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerDetailVo;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerStudentPageVo;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerStudentVo;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerTeacherPageVo;
import com.xjrsoft.module.assessment.vo.AssessmentQuestionOptionResultVo;
import com.xjrsoft.module.assessment.vo.AssessmentQuestionResultVo;
import com.xjrsoft.module.assessment.vo.AssessmentQuestionVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanPageVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanQuestionVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanResultVo;
import com.xjrsoft.module.assessment.vo.OneQuestionVo;
import com.xjrsoft.module.assessment.vo.TwoQuestionVo;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.mapper.BaseClassMapper;
import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
import com.xjrsoft.module.organization.service.IWeChatService;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.teacher.mapper.XjrUserMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
* @title: 考核计划
* @Author dzx
* @Date: 2024-04-01
* @Version 1.0
*/
@Service
@AllArgsConstructor
public
class AssessmentTemplatePlanServiceImpl extends MPJBaseServiceImpl<AssessmentTemplatePlanMapper, AssessmentTemplatePlan> implements IAssessmentTemplatePlanService {
    private final AssessmentTemplatePlanMapper templatePlanMapper;

    private final AssessmentPlanQuestionMapper planQuestionMapper;
    private final AssessmentPlanAnswerClassMapper answerClassMapper;
    private final AssessmentPlanAnswerStudentMapper studentMapper;
    private final AssessmentTemplateMapper assessmentTemplateMapper;
    private final AssessmentQuestionMapper questionMapper;
    private final AssessmentPlanAnswerResultMapper resultMapper;
    private final XjrUserMapper xjrUserMapper;
    private final IWeChatService weChatService;
    private final CommonPropertiesConfig commonPropertiesConfig;
    private final BaseClassMapper baseClassMapper;
    private final WeChatUtil weChatUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AddAssessmentTemplatePlanDto dto) {
        AssessmentTemplate template = assessmentTemplateMapper.selectById(dto.getAssessmentTemplateId());
        AssessmentTemplatePlan assessmentTemplatePlan = BeanUtil.toBean(dto, AssessmentTemplatePlan.class);
        assessmentTemplatePlan.setScore(template.getScore());
        templatePlanMapper.insert(assessmentTemplatePlan);

        //查询模板下面的题目信息
        List<AssessmentQuestion> questionList = questionMapper.selectList(
            new QueryWrapper<AssessmentQuestion>().lambda()
            .eq(AssessmentQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
            .eq(AssessmentQuestion::getAssessmentTemplateId, dto.getAssessmentTemplateId())
        );
        Map<Long, Long> parentMap = new HashMap<>();
        for (AssessmentQuestion assessmentQuestion : questionList) {
            parentMap.put(assessmentQuestion.getId(), IdUtil.getSnowflakeNextId());
        }
        for (AssessmentQuestion assessmentQuestion : questionList) {
            AssessmentPlanQuestion planQuestion = BeanUtil.toBean(assessmentQuestion, AssessmentPlanQuestion.class);
            planQuestion.setId(parentMap.get(assessmentQuestion.getId()));
            planQuestion.setParentId(parentMap.get(assessmentQuestion.getParentId()));
            planQuestion.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
            planQuestionMapper.insert(planQuestion);
        }
        List<BaseClass> classList = baseClassMapper.selectList(
            new QueryWrapper<BaseClass>().lambda()
            .eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())
            .eq(BaseClass::getIsGraduate, 1)
        );
        List<Long> classIds = new ArrayList<>();
        if("all".equals(dto.getType())){
            for (BaseClass baseClass : classList) {
                classIds.add(baseClass.getId());
            }
        }else if("random".equals(dto.getType())){
            List<Integer> randomList = new ArrayList<>();
            Random random = new Random();
            while(randomList.size() < dto.getRandomNumber()){
                int nextInt = random.nextInt(classList.size());
                if(!randomList.contains(nextInt) && nextInt >= 0){
                    randomList.add(nextInt);

                }
            }
            for (Integer i : randomList) {
                classIds.add(classList.get(i).getId());
            }
        }
        dto.setClassIds(classIds);
        //存入班级，并根据选人方式存入答题学生
        for (Long classId : dto.getClassIds()) {
            AssessmentPlanAnswerClass answerClass = new AssessmentPlanAnswerClass();
            answerClass.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
            answerClass.setClassId(classId);
            answerClassMapper.insert(answerClass);

            MPJLambdaWrapper<XjrUser> query = new MPJLambdaWrapper<>();
            query.leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
            .eq(BaseStudentSchoolRoll::getClassId, classId);
            List<XjrUser> studentList = xjrUserMapper.selectJoinList(XjrUser.class, query);
            for (XjrUser xjrUser : studentList) {
                AssessmentPlanAnswerStudent answer = new AssessmentPlanAnswerStudent();
                answer.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
                answer.setStudentUserId(xjrUser.getId());
                studentMapper.insert(answer);
            }
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(UpdateAssessmentTemplatePlanDto dto) {
        AssessmentTemplate template = assessmentTemplateMapper.selectById(dto.getAssessmentTemplateId());
        AssessmentTemplatePlan assessmentTemplatePlan = BeanUtil.toBean(dto, AssessmentTemplatePlan.class);
        assessmentTemplatePlan.setScore(template.getScore());
        templatePlanMapper.updateById(assessmentTemplatePlan);

        //删除以前的题目
        planQuestionMapper.deleteByPlanId(assessmentTemplatePlan.getId(), StpUtil.getLoginIdAsLong());
        //查询模板下面的题目信息
        List<AssessmentQuestion> questionList = questionMapper.selectList(
            new QueryWrapper<AssessmentQuestion>().lambda()
            .eq(AssessmentQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
            .eq(AssessmentQuestion::getAssessmentTemplateId, dto.getAssessmentTemplateId())
            .orderByAsc(AssessmentQuestion::getCategory)
        );
        Map<Long, Long> parentMap = new HashMap<>();
        for (AssessmentQuestion assessmentQuestion : questionList) {
            parentMap.put(assessmentQuestion.getId(), IdUtil.getSnowflakeNextId());
        }
        for (AssessmentQuestion assessmentQuestion : questionList) {
            AssessmentPlanQuestion planQuestion = BeanUtil.toBean(assessmentQuestion, AssessmentPlanQuestion.class);
            planQuestion.setId(parentMap.get(assessmentQuestion.getId()));
            planQuestion.setParentId(parentMap.get(assessmentQuestion.getParentId()));
            planQuestion.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
            planQuestionMapper.insert(planQuestion);
        }
        //删除班级和学生
        answerClassMapper.deleteByPlanId(assessmentTemplatePlan.getId(), StpUtil.getLoginIdAsLong());
        studentMapper.deleteByPlanId(assessmentTemplatePlan.getId(), StpUtil.getLoginIdAsLong());
        List<BaseClass> classList = baseClassMapper.selectList(
            new QueryWrapper<BaseClass>().lambda()
            .eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())
            .eq(BaseClass::getIsGraduate, 1)
        );
        List<Long> classIds = new ArrayList<>();
        if("all".equals(dto.getType())){
            for (BaseClass baseClass : classList) {
                classIds.add(baseClass.getId());
            }
        }else if("random".equals(dto.getType())){
            List<Integer> randomList = new ArrayList<>();
            Random random = new Random();
            while(randomList.size() < dto.getRandomNumber()){
                int nextInt = random.nextInt(classList.size());
                if(!randomList.contains(nextInt) && nextInt >= 0){
                    randomList.add(nextInt);
                }
            }
            for (Integer i : randomList) {
                classIds.add(classList.get(i).getId());
            }
        }
        dto.setClassIds(classIds);
        //存入班级，并根据选人方式存入答题学生
        for (Long classId : dto.getClassIds()) {
            AssessmentPlanAnswerClass answerClass = new AssessmentPlanAnswerClass();
            answerClass.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
            answerClass.setClassId(classId);
            answerClassMapper.insert(answerClass);

            MPJLambdaWrapper<XjrUser> query = new MPJLambdaWrapper<>();
            query.leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
                    .eq(BaseStudentSchoolRoll::getClassId, classId);
            List<XjrUser> studentList = xjrUserMapper.selectJoinList(XjrUser.class, query);
            for (XjrUser xjrUser : studentList) {
                AssessmentPlanAnswerStudent answer = new AssessmentPlanAnswerStudent();
                answer.setAssessmentTemplatePlanId(assessmentTemplatePlan.getId());
                answer.setStudentUserId(xjrUser.getId());
                studentMapper.insert(answer);
            }
        }
        return true;
    }

    @Override
    public Boolean updateTime(UpdateAssessmentTemplatePlanTimeDto dto) {
        AssessmentTemplatePlan assessmentTemplatePlan = BeanUtil.toBean(dto, AssessmentTemplatePlan.class);
        templatePlanMapper.updateById(assessmentTemplatePlan);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        templatePlanMapper.deleteBatchIds(ids);
        planQuestionMapper.delete(Wrappers.lambdaQuery(AssessmentPlanQuestion.class).in(AssessmentPlanQuestion::getAssessmentTemplatePlanId, ids));
        answerClassMapper.delete(Wrappers.lambdaQuery(AssessmentPlanAnswerClass.class).in(AssessmentPlanAnswerClass::getAssessmentTemplatePlanId, ids));
        studentMapper.delete(Wrappers.lambdaQuery(AssessmentPlanAnswerStudent.class).in(AssessmentPlanAnswerStudent::getAssessmentTemplatePlanId, ids));

        return true;
    }

    @Override
    public Page<AssessmentTemplatePlanPageVo> getPage(Page<AssessmentTemplatePlanPageVo> page, AssessmentTemplatePlanPageDto dto) {
        return templatePlanMapper.getPage(page, dto);
    }

    @Override
    public Page<AssessmentPlanAnswerStudentPageVo> getStudentPage(Page<AssessmentPlanAnswerStudentPageVo> page, AssessmentPlanAnswerPageDto dto) {
        return templatePlanMapper.getStudentPage(page, dto);
    }

    @Override
    public Page<AssessmentPlanAnswerTeacherPageVo> getTeacherPage(Page<AssessmentPlanAnswerTeacherPageVo> page, AssessmentPlanAnswerPageDto dto) {
        return templatePlanMapper.getTeacherPage(page, dto);
    }

    @Override
    public Boolean sure(AssessmentTemplatePlanSureDto dto) {
        long teacherId = StpUtil.getLoginIdAsLong();
        answerClassMapper.sure(dto, teacherId);
        studentMapper.sure(dto, teacherId);
        return true;
    }

    @Override
    public AssessmentTemplatePlanQuestionVo getQuestionsById(Long id) {
        AssessmentTemplatePlan templatePlan = templatePlanMapper.selectById(id);
        AssessmentTemplatePlanQuestionVo vo = BeanUtil.toBean(templatePlan, AssessmentTemplatePlanQuestionVo.class);
        List<AssessmentPlanQuestion> questionList = planQuestionMapper.selectList(
            new QueryWrapper<AssessmentPlanQuestion>().lambda()
            .eq(AssessmentPlanQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
            .eq(AssessmentPlanQuestion::getAssessmentTemplatePlanId, id)
        );

        Integer submitStatus = studentMapper.getSubmitStatus(id, StpUtil.getLoginIdAsLong());
        vo.setSubmitStatus(submitStatus);

        AssessmentQuestionResultDto resultDto = new AssessmentQuestionResultDto();
        resultDto.setAssessmentTemplatePlanId(id);
        resultDto.setUserId(StpUtil.getLoginIdAsLong());
        List<AssessmentQuestionResultVo> resultList = resultMapper.getResultList(resultDto);
        Map<Long, String> resultMap = new HashMap<>();
        for (AssessmentQuestionResultVo resultVo : resultList) {
            resultMap.put(resultVo.getQuestionId(), resultVo.getAnswer());
        }

        //循环大题
        List<AssessmentQuestionVo> oneList = new ArrayList<>();
        for (AssessmentPlanQuestion assessmentQuestion : questionList) {
            if(assessmentQuestion.getCategory() == 1){
                oneList.add(BeanUtil.toBean(assessmentQuestion, AssessmentQuestionVo.class));
            }
        }

        //循环小题
        for (AssessmentQuestionVo oneQuestion : oneList) {
            List<AssessmentQuestionVo> twoList = new ArrayList<>();
            for (AssessmentPlanQuestion assessmentQuestion : questionList) {
                if(assessmentQuestion.getCategory() == 2 && assessmentQuestion.getParentId().equals(oneQuestion.getId())){
                    AssessmentQuestionVo questionVo = BeanUtil.toBean(assessmentQuestion, AssessmentQuestionVo.class);
                    questionVo.setValue(resultMap.get(questionVo.getId()));
                    twoList.add(questionVo);
                }
            }
            oneQuestion.setChildren(twoList);
        }

        //循环选项
        for (AssessmentQuestionVo oneQuestion : oneList) {
            for (AssessmentQuestionVo twoQuestion : oneQuestion.getChildren()) {
                List<AssessmentQuestionVo> optionList = new ArrayList<>();
                for (AssessmentPlanQuestion assessmentQuestion : questionList) {
                    if(assessmentQuestion.getCategory() == 3 && assessmentQuestion.getParentId().equals(twoQuestion.getId())){
                        optionList.add(BeanUtil.toBean(assessmentQuestion, AssessmentQuestionVo.class));
                    }
                }
                twoQuestion.setOptions(optionList);
            }
        }

        vo.setChildren(oneList);
        return vo;
    }

    @Override
    public Boolean saveAnswer(AssessmentPlanSubmitDto dto) {
        long studentUserId = StpUtil.getLoginIdAsLong();
        Date date = new Date();

        List<Long> questionIds = new ArrayList<>();
        for (AssessmentPlanAnswerSubmitDto answerSubmitDto : dto.getAnswerList()) {
            questionIds.add(answerSubmitDto.getQuestionId());
        }
        Map<Long, String> questionTypeMaps = new HashMap<>();
        planQuestionMapper.selectList(
            new QueryWrapper<AssessmentPlanQuestion>().lambda()
            .in(AssessmentPlanQuestion::getId, questionIds)
        ).forEach(question ->{
            questionTypeMaps.put(question.getId(), question.getType());
        });

        // 学生提交
        studentMapper.submitAnswer(dto.getAssessmentTemplatePlanId(), studentUserId);
        for (AssessmentPlanAnswerSubmitDto answerSubmitDto : dto.getAnswerList()) {
            if("multi_question".equals(questionTypeMaps.get(answerSubmitDto.getQuestionId()))){
                String[] split = answerSubmitDto.getAnswerId().split(",");
                for (String answerId : split) {
                    AssessmentPlanAnswerResult answerStudent = new AssessmentPlanAnswerResult();
                    answerStudent.setAssessmentTemplatePlanId(dto.getAssessmentTemplatePlanId());
                    answerStudent.setStudentUserId(studentUserId);
                    answerStudent.setCreateDate(date);
                    answerStudent.setQuestionId(answerSubmitDto.getQuestionId());
                    answerStudent.setAnswerId(answerId);
                    resultMapper.insert(answerStudent);
                }
            }else{
                AssessmentPlanAnswerResult answerStudent = new AssessmentPlanAnswerResult();
                answerStudent.setAssessmentTemplatePlanId(dto.getAssessmentTemplatePlanId());
                answerStudent.setStudentUserId(studentUserId);
                answerStudent.setCreateDate(date);
                answerStudent.setQuestionId(answerSubmitDto.getQuestionId());
                answerStudent.setAnswerId(answerSubmitDto.getAnswerId());
                resultMapper.insert(answerStudent);
            }
        }

        return true;
    }

    @Override
    public List<AssessmentTemplatePlanPageVo> getMobilePage(AssessmentTemplatePlanPageDto dto) {
        return templatePlanMapper.getMobilePage(dto);
    }

    @Override
    public List<AssessmentPlanAnswerStudentVo> getAnswerStudent(AssessmentPlanAnswerStudentDto dto) {
        List<String> roleList = StpUtil.getRoleList();
        if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){
            dto.setTeacherId(StpUtil.getLoginIdAsLong());
        }
        return templatePlanMapper.getAnswerStudent(dto);
    }

    @Override
    public List<Long> getSemesterClass(Long id) {
        return templatePlanMapper.getSemesterClass(id);
    }

    @Override
    public List<AssessmentPlanAnswerClassResultVo> getClassResult(Long id) {
        return resultMapper.getClassResult(id);
    }

    @Override
    public AssessmentTemplatePlanResultVo getQuestionResult(Long id) {
        AssessmentTemplatePlan templatePlan = templatePlanMapper.selectById(id);
        AssessmentTemplatePlanResultVo vo = BeanUtil.toBean(templatePlan, AssessmentTemplatePlanResultVo.class);
        vo.setStudentCount(studentMapper.getStudentCount(id));
        List<AssessmentPlanQuestion> questionList = planQuestionMapper.selectList(
            new QueryWrapper<AssessmentPlanQuestion>().lambda()
            .eq(AssessmentPlanQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
            .eq(AssessmentPlanQuestion::getAssessmentTemplatePlanId, id)
        );

        //循环大题
        List<OneQuestionVo> oneList = new ArrayList<>();
        for (AssessmentPlanQuestion assessmentQuestion : questionList) {
            if(assessmentQuestion.getCategory() == 1){
                oneList.add(BeanUtil.toBean(assessmentQuestion, OneQuestionVo.class));
            }
        }
        //循环小题
        for (OneQuestionVo oneQuestion : oneList) {
            List<TwoQuestionVo> twoList = new ArrayList<>();
            for (AssessmentPlanQuestion assessmentQuestion : questionList) {
                if(assessmentQuestion.getCategory() == 2 && assessmentQuestion.getParentId().equals(oneQuestion.getId())){
                    TwoQuestionVo questionVo = BeanUtil.toBean(assessmentQuestion, TwoQuestionVo.class);
                    twoList.add(questionVo);
                }
            }
            oneQuestion.setChildren(twoList);
        }
        List<AssessmentPlanAnswerDetailVo> questionResult = resultMapper.getQuestionResult(id);
        Map<String, Integer> answerCountMap = new HashMap<>();
        for (AssessmentPlanAnswerDetailVo countVo : questionResult) {
            answerCountMap.put(countVo.getQuestionId().toString() + countVo.getAnswerId(), countVo.getChooseCount());
        }

        //量表
        List<AssessmentPlanAnswerDetailVo> scaleQuestionResult = resultMapper.getScaleQuestionResult(id);

        //循环选项
        for (OneQuestionVo oneQuestion : oneList) {
            for (TwoQuestionVo twoQuestion : oneQuestion.getChildren()) {
                List<AssessmentQuestionOptionResultVo> optionList = new ArrayList<>();
                if("radio_question".equals(twoQuestion.getType()) || "multi_question".equals(twoQuestion.getType())){
                    for (AssessmentPlanQuestion assessmentQuestion : questionList) {
                        if(assessmentQuestion.getCategory() == 3 && assessmentQuestion.getParentId().equals(twoQuestion.getId())){
                            AssessmentQuestionOptionResultVo questionVo = BeanUtil.toBean(assessmentQuestion, AssessmentQuestionOptionResultVo.class);
                            Integer count = answerCountMap.get(assessmentQuestion.getParentId().toString() + assessmentQuestion.getId().toString());
                            questionVo.setCount(count == null?0:count);

                            questionVo.setPercentage(vo.getStudentCount());
                            optionList.add(questionVo);
                        }
                    }
                }else if("judge_question".equals(twoQuestion.getType())){
                    optionList.add(new AssessmentQuestionOptionResultVo(){{
                        setName("是");
                        Integer count = answerCountMap.get(twoQuestion.getId().toString() + "1");
                        setPercentage(vo.getStudentCount());
                        setCount(count == null?0:count);
                    }});
                    optionList.add(new AssessmentQuestionOptionResultVo(){{
                        setName("否");
                        Integer count = answerCountMap.get(twoQuestion.getId().toString() + "2");
                        setPercentage(vo.getStudentCount());
                        setCount(count == null?0:count);
                    }});
                }else if("scale_question".equals(twoQuestion.getType())) {
                    for (AssessmentPlanAnswerDetailVo detailVo : scaleQuestionResult) {
                        if(detailVo.getQuestionId().equals(twoQuestion.getId())){
                            optionList.add(new AssessmentQuestionOptionResultVo(){{
                                setName(detailVo.getAnswerId());
                                Integer count = detailVo.getChooseCount();
                                setPercentage(vo.getStudentCount());
                                setCount(count == null?0:count);
                            }});
                        }
                    }
                }else if("text_question".equals(twoQuestion.getType())) {
                    for (AssessmentPlanQuestion assessmentQuestion : questionList) {
                        if(assessmentQuestion.getCategory() == 3 && assessmentQuestion.getParentId().equals(twoQuestion.getId())){
                            AssessmentQuestionOptionResultVo questionVo = BeanUtil.toBean(assessmentQuestion, AssessmentQuestionOptionResultVo.class);
                            Integer count = answerCountMap.get(assessmentQuestion.getParentId().toString() + assessmentQuestion.getId().toString());
                            questionVo.setCount(count == null?0:count);

                            questionVo.setPercentage(vo.getStudentCount());
                            optionList.add(questionVo);
                        }
                    }
                }
                twoQuestion.setResultList(optionList);
            }
        }
        vo.setChildren(oneList);
        return vo;
    }

    @Override
    public Boolean sendMsg(AssessmentTemplatePlanSureDto dto) {
        //先查询班上的所有学生
        MPJLambdaWrapper<XjrUser> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                .innerJoin(AssessmentPlanAnswerStudent.class, AssessmentPlanAnswerStudent::getStudentUserId,XjrUser::getId)
                .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId,XjrUser::getId)
                .eq(BaseStudentSchoolRoll::getClassId , dto.getClassId())
                .eq(AssessmentPlanAnswerStudent::getAssessmentTemplatePlanId , dto.getId())
                .disableSubLogicDel();
        List<XjrUser> userList = xjrUserMapper.selectJoinList(XjrUser.class, queryWrapper);
        AssessmentTemplatePlan templatePlan = templatePlanMapper.selectById(dto.getId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        for (XjrUser user : userList) {
            WeChatSendMessageDto weChatSendMessageDto = new WeChatSendMessageDto();
            weChatSendMessageDto.setUserId(user.getOpenId());

            weChatSendMessageDto.setTemplateId(weChatUtil.getAssessmentTemplate());
            weChatSendMessageDto.setUrl(StrUtil.format("{}/pages/student/canteen/evaluate?id={}", commonPropertiesConfig.getDomainApp(), dto.getId()));
            weChatSendMessageDto.setMsgId(dto.getId().toString());

            JSONObject data = new JSONObject();
            data.put("thing1.DATA", templatePlan.getName());
            data.put("time2.DATA", sdf.format(templatePlan.getStartTime()));
            data.put("time3.DATA", sdf.format(templatePlan.getEndTime()));
            data.put("thing5.DATA", "test");
            data.put("thing8.DATA", "test");
            weChatSendMessageDto.setContent(data);
            weChatService.sendTemplateMessage(weChatSendMessageDto);
        }
        return true;
    }
}
