package com.jwipc.job.handler;

import com.jwipc.job.logic.impl.EmailSendLogicLogicImpl;
import com.jwipc.supplier.model.po.SupplierMaterialDemoPO;
import com.jwipc.supplier.service.ISupplierMaterialDemoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  物料认证承认书预警：超过三天供应商没有确认 发送邮件提醒 每日凌晨
 * （1）查询 记录状态 = 【待供应商处理】的记录的 更新时间字段；
 * （2）当前时间 - 记录时间 >=  3天（系统参数配置supplierMaterialPageDeadInterval），发送邮件给供应商；
 * </p>
 *
 * @author wcb
 * @version 1.0
 * @since JDK 17
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class MaterialAuthToSupplierEmailJob extends QuartzJobBean implements InterruptableJob {

    @Autowired
    private ISupplierMaterialDemoService supplierMaterialDemoService;
    @Autowired
    private EmailSendLogicLogicImpl emailSendLogicLogic;

    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    @Override
    protected void executeInternal(JobExecutionContext context) {
        List<SupplierMaterialDemoPO> supplierMaterialDemoPOS = supplierMaterialDemoService.selectWarnList();
        if (CollectionUtils.isNotEmpty(supplierMaterialDemoPOS)){
            for (SupplierMaterialDemoPO supplierMaterialDemoPO : supplierMaterialDemoPOS) {
                if (StringUtils.isNotEmpty(supplierMaterialDemoPO.getEmail())){

                    Map<String,Object> params = new HashMap<>();
                    params.put("code",supplierMaterialDemoPO.getCode());
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    params.put("publicDate",formatter.format(supplierMaterialDemoPO.getUpdateTime()));
                    emailSendLogicLogic.syncSend(supplierMaterialDemoPO.getEmail(),"物料认证承认书预警","JW_POST_010",params,1L,null);
                }
            }
        }
        log.info("{}", supplierMaterialDemoPOS);
    }
}
