package com.storlead.sales.task.dispatch;

import com.storlead.mail.service.EmailsService;
import com.storlead.mail.service.SmtpPopSettingsService;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @program: sp-sales-platform
 * @description: 绑定客户关系(英文要实时，可以不需要这个)
 * @author: chenkq
 * @create: 2024-11-15 18:32
 */
@Slf4j
@Component
@NoArgsConstructor
@DisallowConcurrentExecution
public class BindCustomerMailTaskJob implements Job {

    @Resource
    private SmtpPopSettingsService smtpPopSettingsService;

    @Resource
    private EmailsService emailsService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        // 绑定邮件客户信息
//        LambdaQueryWrapper<SmtpPopSettingsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SmtpPopSettingsEntity::getIsDelete,0);
//        lambdaQueryWrapper.eq(SmtpPopSettingsEntity::getUseDefault,Integer.valueOf(1));
//        lambdaQueryWrapper.eq(SmtpPopSettingsEntity::getEnabled,1);
//        List<SmtpPopSettingsEntity> settingsEntities = smtpPopSettingsService.list(lambdaQueryWrapper);
//        if (CollectionUtils.isEmpty(settingsEntities)) {
//            return;
//        }
//        for (SmtpPopSettingsEntity smtpPop : settingsEntities) {
//            LambdaQueryWrapper<EmailsEntity> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.select(EmailsEntity::getId,EmailsEntity::getFrom,EmailsEntity::getRecipient,EmailsEntity::getRecipientCc,EmailsEntity::getFolder,EmailsEntity::getOwnerBy);
//            queryWrapper.eq(EmailsEntity::getIsDelete, Integer.valueOf(0));
//            queryWrapper.eq(EmailsEntity::getSmtpPopId, smtpPop.getId());
//            queryWrapper.eq(EmailsEntity::getSourceType,Integer.valueOf(0));
//            queryWrapper.eq(EmailsEntity::getOwnerBy, smtpPop.getOwnerBy());
//            List<EmailsEntity> emails = emailsService.list(queryWrapper);
//
//            if (CollectionUtils.isEmpty(emails)) {
//                return ;
//            }
//            for (EmailsEntity entity : emails) {
//                if (Integer.valueOf(1).equals(entity.getInOutMark())) {
//                    emailsService.bindLiaisonMailCustomerIdByMailIdAndUserId(entity.getId(), entity.getFrom(), entity.getOwnerBy(), 1);
//                } else if (Integer.valueOf(2).equals(entity.getInOutMark())) {
//                    String recipient = entity.getRecipient();
//                    if (StrUtil.isNotBlank(entity.getRecipientCc())) {
//                        if (StrUtil.isNotBlank(recipient)) {
//                            recipient = recipient + "," + entity.getRecipientCc();
//                        } else {
//                            recipient = entity.getRecipientCc();
//                        }
//                    }
//                    emailsService.bindLiaisonMailCustomerIdByMailIdAndUserId(entity.getId(), recipient, entity.getOwnerBy(), 2);
//                }
//            }
//        }
    }
}
