package com.storlead.sales.task.dispatch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2025-08-05 11:06
 */
@Component
@Slf4j
public class TestAutoSendDelayEmailTaskJob {
//    @Resource
//    private EmailsService emailsService;
//
//    @Resource
//    private SmtpPopSettingsService smtpPopSettingsService;
//    @Scheduled(cron = "0 0/1 * * * *")
//    public void autoExecute() {
//        log.info("定时发送邮件任务开始");
//        LambdaQueryWrapper<EmailsEntity> wrapper = new LambdaQueryWrapper<>();
//        LocalDateTime now = LocalDateTime.now();
//        LocalDateTime triggerTime = now;
//        wrapper.select(EmailsEntity::getId,EmailsEntity::getSmtpPopId,EmailsEntity::getOwnerBy);
//        wrapper.le(EmailsEntity::getDelaySendTime,triggerTime);
//        wrapper.eq(EmailsEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
//        wrapper.eq(EmailsEntity::getStatus,-1);
//        wrapper.eq(EmailsEntity::getFolder, EmailBoxEnum.SENTING.code);
//        wrapper.isNotNull(EmailsEntity::getDelaySendTime);
//        List<EmailsEntity> emailsls = emailsService.list(wrapper);
//        if (CollectionUtils.isEmpty(emailsls)) {
//            return;
//        }
//        LambdaUpdateWrapper<EmailsEntity> upEntity = new LambdaUpdateWrapper<>();
//        upEntity.set(EmailsEntity::getStatus,0);
//        upEntity.in(EmailsEntity::getId,emailsls.stream().map(EmailsEntity::getId).collect(Collectors.toList()));
//        emailsService.update(upEntity);
//
//        List<SmtpPopSettingsEntity> smtpPopls = smtpPopSettingsService.listByIds(emailsls.stream().map(EmailsEntity::getSmtpPopId).collect(Collectors.toList()));
//        if (CollectionUtils.isEmpty(smtpPopls)) {
//            return;
//        }
//        Map<Long,SmtpPopSettingsEntity> smtpPopMap = smtpPopls.stream().collect(Collectors.toMap(SmtpPopSettingsEntity::getId, a ->a));
//        log.info("定时发送邮件任务，共{}条",emailsls.size());
//        for (EmailsEntity emailVal : emailsls) {
//            EmailsEntity email = emailsService.getById(emailVal.getId());
//            SmtpPopSettingsEntity smtpPop = smtpPopMap.get(email.getSmtpPopId());
//            AccessKeyEncryptor accessKeyEncryptor = AccessKeyEncryptor.getAccessKeyEncryptor("");
//            String pass = accessKeyEncryptor.decrypt(smtpPop.getEmailPassword());
//            smtpPop.setEmailPassword(pass);
//            emailsService.sendEmail(email,smtpPop);
//        }
//    }
}
