package com.simuwang.manage.service.impl;

import com.simuwang.base.common.enums.OpenStatusType;
import com.simuwang.base.common.util.StringUtil;
import com.simuwang.base.mapper.CompanyEmailConfigMapper;
import com.simuwang.base.pojo.dos.CompanyEmailConfigDO;
import com.simuwang.base.pojo.vo.CompanyEmailConfigVO;
import com.simuwang.manage.service.CompanyEmailConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * FileName: CompanyEmailConfigServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/7 12:55
 * Description: ${DESCRIPTION}
 */
@Service
public class CompanyEmailConfigServiceImpl implements CompanyEmailConfigService {
    @Autowired
    private CompanyEmailConfigMapper companyEmailConfigMapper;
    @Override
    public void saveCompanyEmailConfig(List<CompanyEmailConfigVO> companyEmailConfigVOS) {
        try{
            for(CompanyEmailConfigVO emailConfigVO : companyEmailConfigVOS){
                CompanyEmailConfigDO emailConfigDO = new CompanyEmailConfigDO();
                emailConfigDO.setCompanyId(emailConfigVO.getCompanyId());
                emailConfigDO.setCompanyName(emailConfigVO.getCompanyName());
                emailConfigDO.setEmail(emailConfigVO.getEmail());
                emailConfigDO.setOpenStatus(emailConfigVO.getOpenStatus());
                emailConfigDO.setRemark(emailConfigVO.getSendRemark());
                emailConfigDO.setIsvalid(1);
                emailConfigDO.setUpdateTime(new Date());
                CompanyEmailConfigDO oldEmailConfigDO = companyEmailConfigMapper.selectEmailConfig(emailConfigVO.getCompanyId(),emailConfigVO.getEmail());
                if(!StringUtil.isNull(oldEmailConfigDO)){
                    emailConfigVO.setId(oldEmailConfigDO.getId());
                }
                if(emailConfigVO.getId() != null){
                    emailConfigDO.setId(emailConfigVO.getId());
                    companyEmailConfigMapper.updateCompanyEmailConfig(emailConfigDO);
                }else{
                    emailConfigDO.setCreateTime(new Date());
                    companyEmailConfigMapper.saveCompanyEmailConfig(emailConfigDO);
                }
            }
        }catch (Exception e){

        }
    }

    @Override
    public void deleteCompanyEmailConfig(Integer id) {
        CompanyEmailConfigDO configDO = companyEmailConfigMapper.selectById(id);
        if (configDO != null) {
            configDO.setIsvalid(0);
            configDO.setUpdateTime(new Date());
            companyEmailConfigMapper.updateById(configDO);
        }
    }

    @Override
    public void updateCompanyEmailConfig(CompanyEmailConfigVO companyEmailConfigVO) {
        CompanyEmailConfigDO configDO = companyEmailConfigMapper.selectCompanyEmailConfigById(companyEmailConfigVO.getId());
        if(configDO != null){
            configDO.setEmail(companyEmailConfigVO.getEmail());
            configDO.setCompanyId(companyEmailConfigVO.getCompanyId());
            configDO.setCompanyName(companyEmailConfigVO.getCompanyName());
            configDO.setUpdateTime(new Date());
            companyEmailConfigMapper.updateCompanyEmailConfig(configDO);
        }
    }

    @Override
    public void sendCompanyEmail(List<Integer> idList) {
        for(Integer id : idList){
            CompanyEmailConfigDO configDO = companyEmailConfigMapper.selectCompanyEmailConfigById(id);
            //把缺失数据的邮件发送到该公司名下的邮箱地址
            sendEmail(configDO.getCompanyId(),configDO.getEmail());
        }
    }
    //邮件校验处理
    private void sendEmail(String companyId, String email) {

    }
}
