package com.jkcredit.invoice.task;

import com.jkcredit.invoice.mapper.Binvoce.SelfCarApplMapper;
import com.jkcredit.invoice.mapper.waybill.NoCarWaybillMapper;
import com.jkcredit.invoice.model.entity.invoice.SelfCarAppl;
import com.jkcredit.invoice.model.entity.waybill.NoCarWayBill;
import com.jkcredit.invoice.service.lowerService.NoCarService;
import com.jkcredit.invoice.service.lowerService.SelfCarServiceL;
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: sunzhaoning
 * @create: 2019-06-27 16:36
 * @version: V1.0
 **/
@Component
@Slf4j
public class ScheduledBillTask {
    @Autowired
    NoCarWaybillMapper noCarWaybillMapper;

    @Autowired
    SelfCarApplMapper selfCarApplMapper;

    @Autowired
    NoCarService noCarService;

    @Autowired
    SelfCarServiceL selfCarService;
    /**
     * 实时运单开票（每天23点开始）
     */
    @Scheduled(cron = "0 0 0/1 * * ?", zone = "Asia/Shanghai")
    public void updateNocarBillInfo() {
        try {
            delGetInvoce(0);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void delGetInvoce(Integer historyFlag){
        NoCarWayBill carWayBill = new NoCarWayBill();
        carWayBill.setHisFlag(historyFlag);
        carWayBill.setBillwayStatus(2);
        List<NoCarWayBill> noCarWayBillsMdEnd = noCarWaybillMapper.selectByHisFlagAndBillStatus(carWayBill);
        if(noCarWayBillsMdEnd!=null && noCarWayBillsMdEnd.size()>0){
            noCarWayBillsMdEnd.stream().forEach(noCarWayBill -> {
                noCarService.getInvoiceByWayBillNumReal(noCarWayBill,true);
            });
        }
    }
    /**
     * 历史运单开票（每天整点点开始） todo 改成动态设置
     */
    @Scheduled(cron = "0 0 0/1 * * ?", zone = "Asia/Shanghai")
    public void updateHistoryNocarBillInfo() {
        try {
            delGetInvoce(1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

   /* @Scheduled(cron = "0 0 23 * * ?", zone = "Asia/Shanghai")
    public void getSefCarInvoice() {
        try {
            List<SelfCarAppl> selfCarAppls = selfCarApplMapper.selectAllNoRec();
            selfCarAppls.forEach(selfCarAppl -> {
                selfCarService.getSelfCarInvoicesByApplyIds(selfCarAppl);
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }*/
}
