package com.simuwang.manage.service.impl;

import com.simuwang.base.common.conts.UserConstants;
import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.common.util.StringUtil;
import com.simuwang.base.mapper.EmailTemplateApplicationRuleMapper;
import com.simuwang.base.mapper.EmailTemplateDataRuleMapper;
import com.simuwang.base.mapper.EmailTemplateInfoMapper;
import com.simuwang.base.pojo.dos.EmailTemplateApplicationRuleDO;
import com.simuwang.base.pojo.dos.EmailTemplateDataRuleDO;
import com.simuwang.base.pojo.dos.EmailTemplateInfoDO;
import com.simuwang.base.pojo.dos.SysConfigDO;
import com.simuwang.base.pojo.dto.query.EmailTemplateInfoPageQuery;
import com.simuwang.base.pojo.vo.*;
import com.simuwang.manage.service.EmailTemplateInfoService;
import com.simuwang.shiro.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * FileName: EmailTemplateInfoServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/25 18:50
 * Description: ${DESCRIPTION}
 */
@Service
public class EmailTemplateInfoServiceImpl implements EmailTemplateInfoService {

    @Autowired
    private EmailTemplateInfoMapper emailTemplateInfoMapper;

    @Autowired
    private EmailTemplateDataRuleMapper emailTemplateDataRuleMapper;

    @Autowired
    private EmailTemplateApplicationRuleMapper emailTemplateApplicationRuleMapper;

    @Override
    public MybatisPage<EmailTemplateInfoVO> searchTemplateList(EmailTemplateInfoPageQuery emailTemplateInfoPageQuery) {
        List<EmailTemplateInfoDO> emailTemplateInfoDOList = emailTemplateInfoMapper.searchTemplateList(emailTemplateInfoPageQuery);
        List<EmailTemplateInfoVO> emailTemplateInfoVOList = emailTemplateInfoDOList.stream().map(EmailTemplateInfoDO::toVO).collect(Collectors.toList());
        long total = emailTemplateInfoMapper.countTemplateList(emailTemplateInfoPageQuery);
        return MybatisPage.of(total,emailTemplateInfoVOList);
    }

    @Override
    public void deleteTemplateList(IdListVO idListVO) {
        Integer userId = UserUtils.getLoginUser().getUserId();
        List<Integer> idList = idListVO.getIdList();
        emailTemplateInfoMapper.deleteTemplateList(idList,userId);
        emailTemplateApplicationRuleMapper.deleteByTemplateId(idList,userId);
        emailTemplateDataRuleMapper.deleteByTemplateId(idList,userId);
    }

    @Override
    public void saveTemplateList(SaveTemplateInfoVO saveTemplateInfoVO) {
        EmailTemplateInfoVO emailTemplateInfoVO = saveTemplateInfoVO.getEmailTemplateInfoVO();
        EmailTemplateInfoDO emailDeletionInfoDO = toEmailTemplateInfoDO(emailTemplateInfoVO);
        Integer templateId;
        if(StringUtil.isNull(emailDeletionInfoDO.getId())){
            //保存
            emailTemplateInfoMapper.saveTemplateInfo(emailDeletionInfoDO);
            templateId = emailDeletionInfoDO.getId();
            List<EmailTemplateDataRuleVO> emailTemplateDataRuleVOList = saveTemplateInfoVO.getEmailTemplateDataRuleVOList();
            if(emailTemplateDataRuleVOList.size() > 0){
                emailTemplateDataRuleVOList.forEach(e -> e.setTemplateId(templateId));
                List<EmailTemplateDataRuleDO> emailTemplateDataRuleDOList = new ArrayList<>();
                for(EmailTemplateDataRuleVO dataRuleVO : emailTemplateDataRuleVOList){
                    EmailTemplateDataRuleDO emailTemplateDataRuleDO = toEmailTemplateDataRuleDO(dataRuleVO);
                    emailTemplateDataRuleDOList.add(emailTemplateDataRuleDO);
                }
                emailTemplateDataRuleMapper.batchInsert(emailTemplateDataRuleDOList);
            }
            List<EmailTemplateApplicationRuleVO> emailTemplateApplicationRuleVOList = saveTemplateInfoVO.getEmailTemplateApplicationRuleVOList();
            if(emailTemplateApplicationRuleVOList.size() > 0){
                emailTemplateApplicationRuleVOList.forEach(e -> e.setTemplateId(templateId));
                List<EmailTemplateApplicationRuleDO> templateApplicationRuleDOList = new ArrayList<>();
                for(EmailTemplateApplicationRuleVO applicationRuleVO : emailTemplateApplicationRuleVOList){
                    EmailTemplateApplicationRuleDO applicationRuleDO = toEmailTemplateApplicationRuleDO(applicationRuleVO);
                    templateApplicationRuleDOList.add(applicationRuleDO);
                }
                emailTemplateApplicationRuleMapper.batchInsert(templateApplicationRuleDOList);
            }
        }else{
            templateId = emailDeletionInfoDO.getId();
            emailTemplateInfoMapper.updateTemplateInfo(emailDeletionInfoDO);
            List<EmailTemplateDataRuleVO> emailTemplateDataRuleVOList = saveTemplateInfoVO.getEmailTemplateDataRuleVOList();
            if(emailTemplateDataRuleVOList.size() > 0){
                emailTemplateDataRuleVOList.forEach(e -> e.setTemplateId(templateId));
                List<EmailTemplateDataRuleDO> addList = new ArrayList<>();
                List<EmailTemplateDataRuleDO> editList = new ArrayList<>();
                for(EmailTemplateDataRuleVO dataRuleVO : emailTemplateDataRuleVOList){
                    EmailTemplateDataRuleDO emailTemplateDataRuleDO = toEmailTemplateDataRuleDO(dataRuleVO);
                    if(StringUtil.isNull(emailTemplateDataRuleDO.getId())){
                        addList.add(emailTemplateDataRuleDO);
                    }else{
                        editList.add(emailTemplateDataRuleDO);
                    }
                }
                if(addList.size() > 0){
                    emailTemplateDataRuleMapper.batchInsert(addList);
                }
                if(editList.size() > 0){
                    emailTemplateDataRuleMapper.batchUpdate(editList);
                }
            }
            List<EmailTemplateApplicationRuleVO> emailTemplateApplicationRuleVOList = saveTemplateInfoVO.getEmailTemplateApplicationRuleVOList();
            if(emailTemplateApplicationRuleVOList.size() > 0){
                emailTemplateApplicationRuleVOList.forEach(e -> e.setTemplateId(templateId));
                List<Integer> templateIdList = new ArrayList<>();
                templateIdList.add(templateId);
                Integer userId = UserUtils.getLoginUser().getUserId();
                emailTemplateApplicationRuleMapper.deleteByTemplateId(templateIdList,userId);
                List<EmailTemplateApplicationRuleDO> addList = new ArrayList<>();
                for(EmailTemplateApplicationRuleVO applicationRuleVO : emailTemplateApplicationRuleVOList){
                    EmailTemplateApplicationRuleDO applicationRuleDO = toEmailTemplateApplicationRuleDO(applicationRuleVO);
                    addList.add(applicationRuleDO);
                }
                if(addList.size() > 0){
                    emailTemplateApplicationRuleMapper.batchInsert(addList);
                }
            }

        }
    }

    @Override
    public void deleteApplicationRuleList(IdListVO idListVO) {
        Integer userId = UserUtils.getLoginUser().getUserId();
        emailTemplateApplicationRuleMapper.deleteByIdList(idListVO.getIdList(),userId);
    }

    @Override
    public void controlTemplateConfig(StatusVO statusVO) {
        EmailTemplateInfoDO emailTemplateInfoDO = emailTemplateInfoMapper.searchTemplateById(statusVO.getId());
        emailTemplateInfoDO.setStatus(statusVO.getStatus());
        Integer userId = UserUtils.getLoginUser().getUserId();
        emailTemplateInfoDO.setUpdaterId(userId);
        emailTemplateInfoDO.setUpdateTime(DateUtils.getNowDate());
        emailTemplateInfoMapper.updateTemplateInfo(emailTemplateInfoDO);
    }

    @Override
    public SaveTemplateInfoVO searchTemplateInfo(Integer id) {
        SaveTemplateInfoVO vo = new SaveTemplateInfoVO();
        EmailTemplateInfoDO emailTemplateInfoDO = emailTemplateInfoMapper.searchTemplateById(id);
        vo.setEmailTemplateInfoVO(emailTemplateInfoDO.toVO());
        List<Integer> idList = new ArrayList<>();
        idList.add(id);
        List<EmailTemplateDataRuleDO> ruleDOList = emailTemplateDataRuleMapper.queryByTemplateId(idList);
        List<EmailTemplateDataRuleVO> ruleVOList = ruleDOList.stream().map(EmailTemplateDataRuleDO::toVO).collect(Collectors.toList());
        vo.setEmailTemplateDataRuleVOList(ruleVOList);
        List<EmailTemplateApplicationRuleDO> applicationRuleDOList = emailTemplateApplicationRuleMapper.queryByTemplateId(idList);
        List<EmailTemplateApplicationRuleVO> applicationRuleVOList = applicationRuleDOList.stream().map(EmailTemplateApplicationRuleDO::toVO).collect(Collectors.toList());
        vo.setEmailTemplateApplicationRuleVOList(applicationRuleVOList);
        return vo;
    }

    @Override
    public boolean checkConfigKeyUnique(SaveTemplateInfoVO saveTemplateInfoVO) {
        EmailTemplateInfoVO infoVO = saveTemplateInfoVO.getEmailTemplateInfoVO();
        Integer id = StringUtil.isNull(infoVO.getId()) ? -1 : infoVO.getId();
        EmailTemplateInfoDO info = emailTemplateInfoMapper.searchTemplateByName(infoVO.getName());
        if (StringUtil.isNotNull(info) && info.getId().intValue() != id.intValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    private EmailTemplateApplicationRuleDO toEmailTemplateApplicationRuleDO(EmailTemplateApplicationRuleVO applicationRuleVO) {
        Integer userId = UserUtils.getLoginUser().getUserId();
        EmailTemplateApplicationRuleDO applicationRuleDO = new EmailTemplateApplicationRuleDO();
        applicationRuleDO.setId(applicationRuleVO.getId());
        applicationRuleDO.setTemplateId(applicationRuleVO.getTemplateId());
        applicationRuleDO.setColumn(applicationRuleVO.getColumn());
        applicationRuleDO.setRow(applicationRuleVO.getRow());
        applicationRuleDO.setContainKeyword(applicationRuleVO.getContainKeyword());
        applicationRuleDO.setNotContainKeyword(applicationRuleVO.getNotContainKeyword());
        applicationRuleDO.setIsvalid(1);
        applicationRuleDO.setType(applicationRuleVO.getType());
        applicationRuleDO.setUpdaterId(userId);
        applicationRuleDO.setUpdateTime(DateUtils.getNowDate());
        applicationRuleDO.setCreateTime(DateUtils.getNowDate());
        applicationRuleDO.setCreatorId(userId);
        return applicationRuleDO;
    }

    private EmailTemplateDataRuleDO toEmailTemplateDataRuleDO(EmailTemplateDataRuleVO dataRuleVO) {
        Integer userId = UserUtils.getLoginUser().getUserId();
        EmailTemplateDataRuleDO emailTemplateDataRuleDO = new EmailTemplateDataRuleDO();
        emailTemplateDataRuleDO.setTemplateId(dataRuleVO.getTemplateId());
        emailTemplateDataRuleDO.setId(dataRuleVO.getId());
        emailTemplateDataRuleDO.setFieldRule(dataRuleVO.getFieldRule());
        emailTemplateDataRuleDO.setColumn(dataRuleVO.getColumn());
        emailTemplateDataRuleDO.setRow(dataRuleVO.getRow());
        emailTemplateDataRuleDO.setPosition(dataRuleVO.getPosition());
        emailTemplateDataRuleDO.setUnitConvert(dataRuleVO.getUnitConvert());
        emailTemplateDataRuleDO.setFieldName(dataRuleVO.getFieldName());
        emailTemplateDataRuleDO.setIsvalid(1);
        emailTemplateDataRuleDO.setUpdaterId(userId);
        emailTemplateDataRuleDO.setUpdateTime(DateUtils.getNowDate());
        if(StringUtil.isNull(dataRuleVO.getId())){
            emailTemplateDataRuleDO.setCreateTime(DateUtils.getNowDate());
            emailTemplateDataRuleDO.setCreatorId(userId);
        }
        return emailTemplateDataRuleDO;
    }

    private EmailTemplateInfoDO toEmailTemplateInfoDO(EmailTemplateInfoVO emailTemplateInfoVO) {
        Integer userId = UserUtils.getLoginUser().getUserId();
        EmailTemplateInfoDO emailDeletionInfoDO = new EmailTemplateInfoDO();
        emailDeletionInfoDO.setId(emailTemplateInfoVO.getId());
        emailDeletionInfoDO.setName(emailTemplateInfoVO.getName());
        emailDeletionInfoDO.setStatus(emailTemplateInfoVO.getStatus());
        emailDeletionInfoDO.setDirection(emailTemplateInfoVO.getDirection());
        emailDeletionInfoDO.setStartIndex(emailTemplateInfoVO.getStartIndex());
        emailDeletionInfoDO.setEndIndex(emailTemplateInfoVO.getEndIndex());
        emailDeletionInfoDO.setDescription(emailTemplateInfoVO.getDescription());
        emailDeletionInfoDO.setType(emailTemplateInfoVO.getType());
        emailDeletionInfoDO.setIsvalid(1);
        emailDeletionInfoDO.setUpdateTime(DateUtils.getNowDate());
        emailDeletionInfoDO.setUpdaterId(userId);
        if(StringUtil.isNull(emailTemplateInfoVO.getId())){
            emailDeletionInfoDO.setCreateTime(DateUtils.getNowDate());
            emailDeletionInfoDO.setCreatorId(userId);
        }
        return emailDeletionInfoDO;
    }
}
