package com.jwipc.job.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jwipc.api.constant.RestCode;
import com.jwipc.constant.FreezeConst;
import com.jwipc.inquiry.entity.InquiryMaterialSupplier;
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.constant.JobSapClient;
import com.jwipc.job.dto.SupplierFreezeParamDTO;
import com.jwipc.job.dto.SupplierFreezeResultSapDTO;
import com.jwipc.job.logic.impl.EmailSendLogicLogicImpl;
import com.jwipc.job.prop.JobProp;
import com.jwipc.supplier.clent.dto.SupplierDTO;
import com.jwipc.supplier.constant.AuthenticationState;
import com.jwipc.supplier.entity.Supplier;
import com.jwipc.supplier.entity.SupplierPurchaseCategory;
import com.jwipc.supplier.service.ISupplierPurchaseCategoryService;
import com.jwipc.supplier.service.ISupplierService;
import lombok.RequiredArgsConstructor;
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.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p> </p>
 *
 * @author shenyang.ou
 * @version 1.0
 * @since JDK 1.8
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class InquirySupplierQuotaJob 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.getSupplierQuotaJob(new Date(),DateUtil.offsetHour(new Date(),1));
        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("title",inquiryMaterialSupplierQuotaJobPO.getInquiryTitle());
                        params.put("url", jobProp.getHostUrl());
                        params.put("publicDate",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                        emailSendLogicLogic.syncSend(email,"报价不足1小时提醒","JW_POST_116",params,1L,null);
                    }
                }

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

    }
}
