package com.simuwang.manage.task;

import com.simuwang.base.common.enums.TaskType;
import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.mapper.CompanyEmailConfigMapper;
import com.simuwang.base.mapper.EmailTaskInfoMapper;
import com.simuwang.base.pojo.dos.CompanyEmailConfigDO;
import com.simuwang.base.pojo.dos.EmailTaskInfoDO;
import com.simuwang.manage.service.CompanyEmailConfigService;
import com.simuwang.shiro.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * FileName: SendCompanyEmailTask
 * Author:   chenjianhua
 * Date:     2024/9/20 22:45
 * Description: ${DESCRIPTION}
 */
@Component
public class SendCompanyEmailTask {
    private static final Logger log = LoggerFactory.getLogger(SendCompanyEmailTask.class);
    @Autowired
    private CompanyEmailConfigMapper companyEmailConfigMapper;
    @Autowired
    private CompanyEmailConfigService companyEmailConfigService;
    @Autowired
    private EmailTaskInfoMapper emailTaskInfoMapper;
    public void sendEmail(){
        EmailTaskInfoDO emailTaskInfoDO = startEmailTask(null, 1);
        try{
            List<CompanyEmailConfigDO> configDOList = companyEmailConfigMapper.getAllCompanyConfig();
            Map<String,List<CompanyEmailConfigDO>> companyListMap = configDOList.stream().collect(Collectors.groupingBy(e -> e.getCompanyId()));
            for(String companyId : companyListMap.keySet()){
                List<CompanyEmailConfigDO> configDOS = companyListMap.get(companyId);
                StringBuffer emails = new StringBuffer();
                for(CompanyEmailConfigDO configDO : configDOS){
                    emails.append(configDO.getEmail()).append(";");
                }
                companyEmailConfigService.sendEmail(companyId,emails.toString());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            endEmailTask(emailTaskInfoDO.getId(),-1);
            return;
        }
        endEmailTask(emailTaskInfoDO.getId(),2);
    }
    private void endEmailTask(Integer id, Integer taskStatus) {
        try{
            EmailTaskInfoDO emailTaskInfoDO = new EmailTaskInfoDO();
            emailTaskInfoDO.setId(id);
            emailTaskInfoDO.setTaskStatus(taskStatus);
            emailTaskInfoDO.setUpdateTime(DateUtils.getNowDate());
            emailTaskInfoDO.setEndTime(DateUtils.getNowDate());
            emailTaskInfoMapper.updateTaskStatusById(emailTaskInfoDO);
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

    private EmailTaskInfoDO startEmailTask(String email,Integer taskStatus) {
        EmailTaskInfoDO  emailTaskInfoDO = new EmailTaskInfoDO();
        try{
            emailTaskInfoDO.setTaskName(TaskType.DELETION_NOTIFICATIONS.getInfo());
            emailTaskInfoDO.setTaskType(TaskType.DELETION_NOTIFICATIONS.getType());
            emailTaskInfoDO.setTaskStatus(taskStatus);
            emailTaskInfoDO.setStartTime(DateUtils.getNowDate());
            emailTaskInfoDO.setIsvalid(1);
            emailTaskInfoDO.setEmail(email);
            emailTaskInfoDO.setCreateTime(DateUtils.getNowDate());
            emailTaskInfoDO.setUpdateTime(DateUtils.getNowDate());
            try{
                emailTaskInfoDO.setCreatorId(UserUtils.getLoginUser().getUserId());
                emailTaskInfoDO.setUpdaterId(UserUtils.getLoginUser().getUserId());
            }catch (Exception e){
                emailTaskInfoDO.setCreatorId(1);
                emailTaskInfoDO.setUpdaterId(1);
            }
            emailTaskInfoMapper.insert(emailTaskInfoDO);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return emailTaskInfoDO;
    }
}
