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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.module.assessment.dto.AddAssessmentQuestionDto;
import com.xjrsoft.module.assessment.dto.AddAssessmentQuestionOptionsDto;
import com.xjrsoft.module.assessment.dto.AddAssessmentTemplateDto;
import com.xjrsoft.module.assessment.dto.UpdateAssessmentTemplateDto;
import com.xjrsoft.module.assessment.entity.AssessmentQuestion;
import com.xjrsoft.module.assessment.entity.AssessmentTemplate;
import com.xjrsoft.module.assessment.mapper.AssessmentQuestionMapper;
import com.xjrsoft.module.assessment.mapper.AssessmentTemplateMapper;
import com.xjrsoft.module.assessment.service.IAssessmentTemplateService;
import com.xjrsoft.module.assessment.vo.AssessmentQuestionVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplateVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
* @title: 考核模板
* @Author dzx
* @Date: 2024-03-29
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class AssessmentTemplateServiceImpl extends MPJBaseServiceImpl<AssessmentTemplateMapper, AssessmentTemplate> implements IAssessmentTemplateService {
    private final AssessmentTemplateMapper assessmentTemplateMapper;

    private final AssessmentQuestionMapper assessmentQuestionMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AssessmentTemplate assessmentTemplate) {

        return true;
    }

    @Override
    public Boolean addCustom(AddAssessmentTemplateDto dto) {
        AssessmentTemplate assessmentTemplate = BeanUtil.toBean(dto, AssessmentTemplate.class);
        assessmentTemplate.setCreateDate(new Date());
        assessmentTemplateMapper.insert(assessmentTemplate);
        //插入答题
        for (AddAssessmentQuestionDto questionDto : dto.getChildren()) {
            AssessmentQuestion question = BeanUtil.toBean(questionDto, AssessmentQuestion.class);
            question.setAssessmentTemplateId(assessmentTemplate.getId());
            question.setCategory(1);
            question.setCreateDate(new Date());
            assessmentQuestionMapper.insert(question);
            //插入小题
            for (AddAssessmentQuestionDto child : questionDto.getChildren()) {
                AssessmentQuestion childQuestion = BeanUtil.toBean(child, AssessmentQuestion.class);
                childQuestion.setParentId(question.getId());
                childQuestion.setAssessmentTemplateId(assessmentTemplate.getId());
                childQuestion.setCategory(2);
                childQuestion.setCreateDate(new Date());
                assessmentQuestionMapper.insert(childQuestion);
                //插入选项
                for (AddAssessmentQuestionOptionsDto option : child.getOptions()) {
                    AssessmentQuestion optionQuestion = BeanUtil.toBean(option, AssessmentQuestion.class);
                    optionQuestion.setParentId(childQuestion.getId());
                    optionQuestion.setCreateDate(new Date());
                    optionQuestion.setAssessmentTemplateId(assessmentTemplate.getId());
                    optionQuestion.setCategory(3);
                    assessmentQuestionMapper.insert(optionQuestion);
                }
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(AssessmentTemplate assessmentTemplate) {
        return true;
    }

    @Override
    public Boolean update(UpdateAssessmentTemplateDto dto) {
        Date updateDate = new Date();
        AssessmentTemplate assessmentTemplate = BeanUtil.toBean(dto, AssessmentTemplate.class);
        assessmentTemplate.setModifyDate(updateDate);
        assessmentTemplateMapper.updateById(assessmentTemplate);
        //删除下面的子集重新添加
        assessmentQuestionMapper.deleteByAssessmentQuestionId(assessmentTemplate.getId(), StpUtil.getLoginIdAsLong());
        //插入答题
        for (AddAssessmentQuestionDto questionDto : dto.getChildren()) {
            AssessmentQuestion question = BeanUtil.toBean(questionDto, AssessmentQuestion.class);
            question.setModifyDate(updateDate);
            question.setAssessmentTemplateId(assessmentTemplate.getId());
            question.setCategory(1);
            assessmentQuestionMapper.insert(question);
            //插入小题
            for (AddAssessmentQuestionDto child : questionDto.getChildren()) {
                AssessmentQuestion childQuestion = BeanUtil.toBean(child, AssessmentQuestion.class);
                childQuestion.setParentId(question.getId());
                childQuestion.setAssessmentTemplateId(assessmentTemplate.getId());
                childQuestion.setModifyDate(updateDate);
                childQuestion.setCategory(2);
                assessmentQuestionMapper.insert(childQuestion);
                //插入选项
                for (AddAssessmentQuestionOptionsDto option : child.getOptions()) {
                    AssessmentQuestion optionQuestion = BeanUtil.toBean(option, AssessmentQuestion.class);
                    optionQuestion.setParentId(childQuestion.getId());
                    optionQuestion.setAssessmentTemplateId(assessmentTemplate.getId());
                    optionQuestion.setModifyDate(updateDate);
                    optionQuestion.setCategory(3);
                    assessmentQuestionMapper.insert(optionQuestion);
                }
            }
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        assessmentTemplateMapper.deleteBatchIds(ids);
        assessmentQuestionMapper.delete(Wrappers.lambdaQuery(AssessmentQuestion.class).in(AssessmentQuestion::getAssessmentTemplateId, ids));
        return true;
    }

    @Override
    public AssessmentTemplateVo getById(Long id) {
        AssessmentTemplate assessmentTemplate = assessmentTemplateMapper.selectById(id);
        AssessmentTemplateVo vo = BeanUtil.toBean(assessmentTemplate, AssessmentTemplateVo.class);
        List<AssessmentQuestion> questionList = assessmentQuestionMapper.selectList(
            new QueryWrapper<AssessmentQuestion>().lambda()
            .eq(AssessmentQuestion::getDeleteMark, DeleteMark.NODELETE.getCode())
            .eq(AssessmentQuestion::getAssessmentTemplateId, id)
        );

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

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

        //循环选项
        for (AssessmentQuestionVo oneQuestion : oneList) {
            for (AssessmentQuestionVo twoQuestion : oneQuestion.getChildren()) {
                List<AssessmentQuestionVo> optionList = new ArrayList<>();
                for (AssessmentQuestion 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;
    }
}
