package com.jkcredit.invoice.hub.task;

import com.jkcredit.invoice.hub.model.po.carFreeCarrierBillStart.CarFreeCarrierBillStartPo;
import com.jkcredit.invoice.hub.service.apiCarFree.ApiCarFreeChargeService;
import com.jkcredit.invoice.hub.service.carFreeCarrierBillStart.CarFreeCarrierBillStartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/18 15:49
 * @version: V1.0
 **/
@Component
@Slf4j
public class WayBillTask {
    @Autowired
    CarFreeCarrierBillStartService startService;
    @Autowired
    ApiCarFreeChargeService chargeService;

    // 实时运单
    @Scheduled(cron = "0 1 23 * * ?", zone = "Asia/Shanghai")
    public void realTimeWayBill() {
        log.info("实时运单定时处理开始");
        // 查询实时订单，且状态为已结束和开票中
        // 调用运单查询发票接口
        // 解析结果
        // 如果result不为[]，则将结果入库
        // 根据返回transationID去重
        // 每一条放入消息队列，计费
        List<CarFreeCarrierBillStartPo> startPoList = startService.getRealTimeBills();
        for (CarFreeCarrierBillStartPo po : startPoList) {
            chargeService.charge(po);
        }
    }

    // 历史运单
    @Scheduled(cron = "0 1 * * * ?", zone = "Asia/Shanghai")
//    @Scheduled(cron = "0 11 17 * * ?", zone = "Asia/Shanghai")
    public void historyWayBill() {
        log.info("历史运单定时处理开始");
        // 查询历史订单，且状态为已结束和开票中
        // 调用运单查询发票接口
        // 解析结果
        // 如果result不为[]，则将结果入库
        // 根据返回transationID去重
        // 每一条放入消息队列，计费
        List<CarFreeCarrierBillStartPo> startPoList = startService.getHistoryBills();
        for (CarFreeCarrierBillStartPo po : startPoList) {
            chargeService.charge(po);
        }
    }

    // // 查询状态为空的运单（状态为空为老平台遗留数据，通过此项目接口上传的运单状态不为空）,is_history为空则不能区分是历史运单还是实时运单
    @Scheduled(cron = "0 0 0/2 * * ?", zone = "Asia/Shanghai")
//    @Scheduled(cron = "0 1/3 * * * ?", zone = "Asia/Shanghai")
    public void billsWithoutIsHistoryFlag() {
        // 查询实时/历史订单，且状态为已结束和开票中
        // 调用运单查询发票接口
        // 解析结果
        // 如果result不为[]，则将结果入库
        // 根据返回transationID去重
        // 每一条放入消息队列，计费
        List<CarFreeCarrierBillStartPo> startPoList = startService.getBillsWithoutIsHistoryFlag();
        for (CarFreeCarrierBillStartPo po : startPoList) {
            chargeService.charge(po);
        }
    }
}
