package com.jwipc.job.handler;

import com.jwipc.job.logic.impl.EmailSendLogicLogicImpl;
import com.jwipc.job.prop.JobProp;
import com.jwipc.spe.model.po.QmNoticeOrderPO;
import com.jwipc.spe.service.IQmNoticeOrderService;
import com.jwipc.supplier.service.ISupplierMaterialDemoService;
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 javax.annotation.Resource;
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 QmRealeaseEmailJob extends QuartzJobBean implements InterruptableJob {

    @Autowired
    private ISupplierMaterialDemoService supplierMaterialDemoService;

    @Autowired
    private IQmNoticeOrderService qmNoticeOrderService;

    @Autowired
    private EmailSendLogicLogicImpl emailSendLogicLogic;

    @Resource
    private  JobProp jobProp;


    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    /**
     * 这个不需要了，发送邮件太过频繁了。
     * @param context
     */
    @Override
    protected void executeInternal(JobExecutionContext context) {
        List<QmNoticeOrderPO> qmNoticeOrderPOS = qmNoticeOrderService.selectWarnList();
        if (CollectionUtils.isNotEmpty(qmNoticeOrderPOS)){
            for (QmNoticeOrderPO qmNoticeOrderPO : qmNoticeOrderPOS) {
                if (StringUtils.isNotEmpty(qmNoticeOrderPO.getEmail())){

                    Map<String,Object> params = new HashMap<>();
                    params.put("code",qmNoticeOrderPO.getNoticeOrderCode());
                    params.put("supplierName",qmNoticeOrderPO.getSupplierName());
                    //外网地址
                    params.put("url", jobProp.getWebsiteUrl());
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    params.put("publicDate",formatter.format(qmNoticeOrderPO.getReleaseTime()));
                    emailSendLogicLogic.syncSend(qmNoticeOrderPO.getEmail(),"质量整改单确认预警","JW_POST_145",params,1L,null);
                }
            }
        }
        log.info("{}", qmNoticeOrderPOS);
    }
}

