package com.jwipc.job.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.jwipc.inquiry.entity.InquirySupplier;
import com.jwipc.inquiry.model.po.InquiryMaterialSupplierQuotaJobPO;
import com.jwipc.inquiry.service.IInquiryMaterialSupplierService;
import com.jwipc.inquiry.service.IInquirySupplierService;
import com.jwipc.job.logic.impl.EmailSendLogicLogicImpl;
import com.jwipc.job.prop.JobProp;
import lombok.extern.slf4j.Slf4j;
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.util.*;

/**
 * 询报价-报价结束的前一天，通知供应商登录系统进行系统内报价
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class QuotationEndNoticeEmailJob extends QuartzJobBean implements InterruptableJob {

    @Autowired
    private IInquiryMaterialSupplierService inquiryMaterialSupplierService;
    @Autowired
    private IInquirySupplierService inquirySupplierService;
    @Autowired
    private EmailSendLogicLogicImpl emailSendLogicLogic;
    @Resource
    private JobProp jobProp;

    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    @Override
    protected void executeInternal(JobExecutionContext context){
        log.info("供应商报价时间剩余一天提醒任务开始");
        //查询
        List<InquiryMaterialSupplierQuotaJobPO> poList = inquiryMaterialSupplierService.getQuotationEndNoticeJob();
        if (CollUtil.isNotEmpty(poList)) {
            for (InquiryMaterialSupplierQuotaJobPO inquiryMaterialSupplierQuotaJobPO : poList){
                if (StringUtils.isNotEmpty(inquiryMaterialSupplierQuotaJobPO.getSupplierEmail())){
                    List<String> emailList = Arrays.asList(StringUtils.split(inquiryMaterialSupplierQuotaJobPO.getSupplierEmail(), ","));
                    for (String email : emailList){
                        Map<String,Object> params = new HashMap<>();
                        params.put("supplierName",inquiryMaterialSupplierQuotaJobPO.getSupplierName());
                        params.put("supplierCode",inquiryMaterialSupplierQuotaJobPO.getSupplierCode());
                        params.put("title",inquiryMaterialSupplierQuotaJobPO.getInquiryTitle());
                        params.put("url", jobProp.getWebsiteUrl());
                        params.put("publicDate",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                        emailSendLogicLogic.syncSend(email,"报价日期剩余一天提醒","JW_POST_117",params,1L,null);
                    }
                }

                InquirySupplier inquirySupplier = new InquirySupplier();
                inquirySupplier.setId(inquiryMaterialSupplierQuotaJobPO.getInquirySupplierId());
                inquirySupplier.setRemindQuoteFlag(1);
                inquirySupplierService.updateById(inquirySupplier);
            }
        }else {
            log.info("供应商报价时间供应商报价时间剩余一天提醒任务-----未查询到需要提醒的数据");
        }
    }
}

