package com.simuwang.manage.task;

import com.simuwang.base.mapper.CompanyEmailConfigMapper;
import com.simuwang.base.pojo.dos.CompanyEmailConfigDO;
import com.simuwang.manage.service.CompanyEmailConfigService;
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}
 */
@EnableScheduling
@Component
public class SendCompanyEmailTask {


    @Autowired
    private CompanyEmailConfigMapper companyEmailConfigMapper;

    @Autowired
    private CompanyEmailConfigService companyEmailConfigService;
    @Scheduled(cron = "0 0/3 * * * ?")
    public void sendEmail(){
        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());
        }
    }
}
