package com.nasc.custom.service.impl;

import com.nasc.common.core.text.Convert;
import com.nasc.custom.domain.*;
import com.nasc.custom.mapper.*;
import com.nasc.custom.service.INascMatterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: Yujianq
 * @Date: 2024/6/4 16:36
 * @Description: 审查事项
 * @return:
 */
@Service
public class NascMatterServiceImpl implements INascMatterService {

    private static final Logger log = LoggerFactory.getLogger(NascMatterServiceImpl.class);

    @Autowired
    private NascMatterMapper matterMapper;

    @Autowired
    private NascMaterialMapper materialMapper;

    @Autowired
    private NascPrintMapper printMapper;

    @Autowired
    private NascPreQuestionMapper preQuestionMapper;

    @Autowired
    private NascPreQuestionOptionMapper preQuestionOptionMapper;

    @Autowired
    private NascMaterialListMapper materialListMapper;

    @Override
    public List<NascMatter> selectMatterList(NascMatter matter) {
        return matterMapper.selectMatterList(matter);
    }

    @Override
    public Long checkMatterDuplicates(NascMatter matter) {
        return matterMapper.checkMatterDuplicates(matter);
    }

    @Override
    @Transactional
    public int insertMatter(NascMatter matter) {
        // 保存审查事项基础信息
        int rows = matterMapper.insertMatter(matter);
        // 判断是否有前置问题
        if ("N".equals(matter.getPrefixQuestion())) {
            // 无前置问题直接存储材料信息
            saveMaterial(matter.getMaterial(), matter.getId(), null);
            // 保存打印模板数据
            savePrintTemplate(matter);
            return rows;
        }
        // 保存打印模板数据
        savePrintTemplate(matter);
        return rows;
    }

    @Override
    @Transactional
    public int updateMatter(NascMatter matter) {
        // 判断是否有前置问题
        if ("N".equals(matter.getPrefixQuestion())) {
            // 删除所有问题及相关的选项及选项相关的材料信息
            deletePreQuestion(matter);
            NascMaterial material = matter.getMaterial();
            // 如果材料不存在
            if (material.getId() == null || material.getId() <= 0) {
                // 无前置问题直接存储材料信息
                saveMaterial(matter.getMaterial(), matter.getId(), null);
            } else {
                // 修改材料信息
                materialMapper.updateMaterial(material);
                // 删除材料清单列表
                materialListMapper.deleteMaterialListByMaterialId(material.getId());
                // 重新存储材料清单列表
                saveMaterialList(material.getMaterialList(), material.getId());
            }
            // 修改一次性告知单打印模板信息
            updatePrintTemplate(matter);
            return matterMapper.updateMatter(matter);
        }
        // 修改一次性告知单打印模板信息
        updatePrintTemplate(matter);
        return matterMapper.updateMatter(matter);
    }

    @Override
    public int deleteMatterByIds(Long[] ids) {
        return matterMapper.deleteMatterByIds(ids);
    }

    @Override
    public NascMatter getMatterInfo(Long id) {
        // 根据ID获取事项详情
        NascMatter matter = matterMapper.getMatterInfo(id);
        if (matter == null) {
            return null;
        }
        // 是否有前置问题
        if ("Y".equals(matter.getPrefixQuestion())) {
            List<NascPreQuestion> questionList = preQuestionMapper.getQuestionByMatterId(id);
            for (NascPreQuestion question : questionList) {
                // 查询问题选项和选项对应的材料信息
                List<NascPreQuestionOption> optionList = preQuestionOptionMapper.getPreQuestionOptionListByQuestionId(question.getId());
                question.setPreQuestionOptionList(optionList);
                for (NascPreQuestionOption option : optionList) {
                    if (option.getJumpToMatterId() == 0) {
                        NascMaterial material = option.getMaterial();
                        material.setMaterialList(materialListMapper.getMaterialListByMaterialId(material.getId()));
                    }
                }
            }
            // 问题赋值
            matter.setPreQuestionList(questionList);
        } else {
            // 根据事项ID查询材料信息
            NascMaterial material = materialMapper.getMaterialByMatterId(id);
            material.setMaterialList(materialListMapper.getMaterialListByMaterialId(material.getId()));
            matter.setMaterial(material);
        }
        return matter;
    }

    @Override
    public Long checkDeptMatter(Long deptId) {
        return matterMapper.checkDeptMatter(deptId);
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/5 16:53
     * @Description: 删除所有问题及相关的选项及选项相关的材料信息
     * @param matter
     * @return: void
     */
    public void deletePreQuestion(NascMatter matter) {
        List<NascPreQuestion> preQuestionList = matter.getPreQuestionList();
        if (preQuestionList == null) {
            preQuestionList = preQuestionMapper.getQuestionByMatterId(matter.getId());
        }
        for (NascPreQuestion preQuestion : preQuestionList) {
            List<NascPreQuestionOption> preQuestionOptionList = preQuestion.getPreQuestionOptionList();
            if (preQuestionOptionList == null) {
                preQuestionOptionList = preQuestionOptionMapper.getPreQuestionOptionListByQuestionId(preQuestion.getId());
            }
            long[] optionIds = preQuestionOptionList.stream().mapToLong(NascPreQuestionOption::getId).toArray();
            // 删除材料清单列表
            for (NascPreQuestionOption option : preQuestionOptionList) {
                if (option.getMaterial() != null) {
                    materialListMapper.deleteMaterialListByMaterialId(option.getMaterial().getId());
                }
            }
            // 删除选项相关的材料信息
            materialMapper.deleteMaterialByOptionIds(optionIds);
            // 删除问题相关的选项
            preQuestionOptionMapper.deleteOptionByQuestionId(preQuestion.getId());
        }
        // 删除所有问题
        preQuestionMapper.deleteQuestionByMatterId(matter.getId());
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/5 15:53
     * @Description: 保存审查事项材料信息
     * @param material
     * @param matterId
     * @param optionId
     * @return: void
     */
    public void saveMaterial(NascMaterial material, Long matterId, Long optionId) {
        material.setMatterId(matterId);
        material.setOptionId(optionId);
        if (matterId != null) {
            // 删除材料信息（若有）
            materialMapper.deleteMaterialByMatterId(matterId);
        }
        // 存储材料信息
        materialMapper.insertMaterial(material);
        // 保存材料清单
        saveMaterialList(material.getMaterialList(), material.getId());
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/13 15:40
     * @Description: 保存材料清单列表
     * @param materialLists
     * @param materialId
     * @return: void
     */
    public void saveMaterialList(List<NascMaterialList> materialLists, Long materialId) {
        for (NascMaterialList materialList: materialLists) {
            materialList.setMaterialId(materialId);
            materialListMapper.insertMaterialList(materialList);
        }
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/5 15:47
     * @Description: 保存打印模板数据（一次性告知单）
     * @param matter
     * @return: void
     */
    public void savePrintTemplate(NascMatter matter) {
        NascPrint print = matter.getPrint();
        print.setPrintBy(Convert.toLong(matter.getCreateBy()));
        print.setMatterId(matter.getId());
        // 保存打印模板数据
        printMapper.insertPrint(print);
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/5 16:59
     * @Description: 修改打印模板数据（一次性告知单）
     * @param matter
     * @return: void
     */
    public void updatePrintTemplate(NascMatter matter) {
        NascPrint print = matter.getPrint();
        print.setPrintBy(Convert.toLong(matter.getUpdateBy()));
        // 修改打印模板数据
        printMapper.updatePrint(print);
    }

}
