/*
 * The Hefei JingTong RDC(Research and Development Centre) Group.
 * __________________
 *
 *    Copyright 2015-2019
 *    All Rights Reserved.
 *
 *    NOTICE:  All information contained herein is, and remains
 *    the property of JingTong Company and its suppliers,
 *    if any.
 */

package com.jwipc.job.handler;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jwipc.job.logic.impl.EmailSendLogicLogicImpl;
import com.jwipc.order.model.po.PurchaseOrderPO;
import com.jwipc.order.service.IPurchaseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p> </p>
 *
 * @author
 * @version 1.0
 * @since JDK 1.8
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class SupplierConfirmOrderJob extends QuartzJobBean implements InterruptableJob {

    @Autowired
    private IPurchaseOrderService iPurchaseOrderService;


    @Autowired
    private EmailSendLogicLogicImpl emailSendLogicLogic;

    public final static String url = "http://srm.jwipc.com:8888/home";



    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("获取待供应商确认状态采购订单任务开始");
        List<PurchaseOrderPO> purchaseOrderPOS = iPurchaseOrderService.supplierConfirmOrderList();
        if(CollectionUtils.isNotEmpty(purchaseOrderPOS)){
            for (PurchaseOrderPO purchaseOrderPO : purchaseOrderPOS) {
                pushEmail(purchaseOrderPO, "JW_POST_149");
            }
            log.info("获取待供应商确认状态采购订单任务结束");
        }
    }

    public void pushEmail(PurchaseOrderPO purchaseOrderPO, String emailTmpCode) {
        if (ObjectUtil.isNotEmpty(purchaseOrderPO)) {
            try {
                Map<String, Object> params = new HashMap<>();
                params.put("supplierName",purchaseOrderPO.getSupplierName());
                params.put("orderCode",purchaseOrderPO.getOrderCode());
                params.put("date", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                params.put("url", url);
                emailSendLogicLogic.syncSend(purchaseOrderPO.getSupplierEmail(),"待确认订单提醒",emailTmpCode,params,1L,null);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }
}


