package com.jkcredit.invoice.task;

import com.jkcredit.invoice.mapper.Binvoce.SelfCarApplMapper;
import com.jkcredit.invoice.mapper.customer.CustomerCarRecMapper;
import com.jkcredit.invoice.mapper.waybill.NoCarWaybillMapper;
import com.jkcredit.invoice.model.entity.customer.CustomerCarRec;
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 com.jkcredit.invoice.service.lowerService.vo.SelfCarDueQueryVo;
import com.jkcredit.invoice.service.lowerService.vo.TradeRequestVo;
import com.jkcredit.invoice.service.manager.ParamService;
import com.jkcredit.invoice.util.DateUtil;
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.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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;

    @Autowired
    CustomerCarRecMapper customerCarRecMapper;

    @Autowired
    ParamService paramService;
    /**
     * 实时运单开票（每天23点开始）
     */
    @Scheduled(cron = "0 0 23 * * ?", zone = "Asia/Shanghai")
    public void updateNocarBillInfo() throws Exception{
        InetAddress addr = InetAddress.getLocalHost();
        String ip  = paramService.getParamsByParamName("CURR_SELF_IP").getParamValue();

        log.info("CURR_SELF_IP"+ip +",address:"+addr);
        if(!addr.getHostAddress().equals(ip)){
          log.info("！bengjiqikaipao");
          return;
        }else{
            log.info("bengjiqikaipao");
        }
        try {
            delGetInvoce(0);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void delGetInvoce(Integer historyFlag){
        NoCarWayBill carWayBill = new NoCarWayBill();
        carWayBill.setHisFlag(historyFlag);
        carWayBill.setInterType(1);
        carWayBill.setBillwayStatus(2);
        List<NoCarWayBill> noCarWayBillsMdEnd = noCarWaybillMapper.selectByHisFlagAndBillStatus(carWayBill);
        if(noCarWayBillsMdEnd!=null && noCarWayBillsMdEnd.size()>0){
            noCarWayBillsMdEnd.parallelStream().forEach(noCarWayBill -> {
                noCarService.getInvoiceByWayBillNumReal(noCarWayBill,false);
            });
        }
    }
    /**
     * 历史运单开票（每天整点点开始） todo 改成动态设置
     */
    @Scheduled(cron = "0 0 0/1 * * ?", zone = "Asia/Shanghai")
    public void updateHistoryNocarBillInfo() throws  Exception{
        InetAddress addr = InetAddress.getLocalHost();
        String ip  = paramService.getParamsByParamName("HISTORY_IP").getParamValue();

        log.info("HISTORY_IP"+ip +",address:"+addr);
        if(!addr.getHostAddress().equals(ip)){
            log.info("！bengjiqikaipao");
            return;
        }else{
            log.info("bengjiqikaipao");
        }
        try {
            delGetInvoce(1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

   @Scheduled(cron = "0 0 23 * * ?", zone = "Asia/Shanghai")
    public void getSefCarInvoice() throws Exception{
       InetAddress addr = InetAddress.getLocalHost();
       String ip  = paramService.getParamsByParamName("CURR_SELF_IP").getParamValue();

       log.info("CURR_SELF_IP"+ip +",address:"+addr);
       if(!addr.getHostAddress().equals(ip)){
           log.info("！bengjiqikaipao");
           return;
       }else{
           log.info("bengjiqikaipao");
       }
       /**
        * 1.查询所有的etc卡
        * 2.按照etc卡号取最近两天的发票
        */
       List<CustomerCarRec> lst =  customerCarRecMapper.selectAllBindEtcNoGetInvoice();

       lst.stream().forEach(customerCarRec -> {
          try {
              SelfCarDueQueryVo selfCarDueQueryVo = new SelfCarDueQueryVo();
              selfCarDueQueryVo.setCompanyName(customerCarRec.getCompanyName());
              selfCarDueQueryVo.setCardId(customerCarRec.getEtcNum());
              selfCarDueQueryVo.setCustomername(customerCarRec.getCustomerName());
              DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
              String curr = dateFormat.format(new Date());
              String befor = DateUtil.getDateAfterDays(curr,-1);
              String after = DateUtil.getDateAfterDays(curr,+1);
              selfCarDueQueryVo.setStartTime(befor+" 00:00:00");
              selfCarDueQueryVo.setEndTime(after+"00:00:00");
              //查询交易id
              TradeRequestVo tradeRequestVo = new TradeRequestVo();
              tradeRequestVo.setStartTime(befor+" 00:00:00");
              tradeRequestVo.setEndTime(after+"00:00:00");
              tradeRequestVo.setCompanyName(customerCarRec.getCustomerName());
              tradeRequestVo.setEtcId(customerCarRec.getEtcNum());
              tradeRequestVo.setTradeStatus(3);
              selfCarService.getTradeList(tradeRequestVo);
              selfCarService.getSelfCarInvoicesByTime(selfCarDueQueryVo);
          }catch (Exception e){
              e.printStackTrace();
              log.info(customerCarRec.getEtcNum());
          }
       });
      /*  try {
            List<SelfCarAppl> selfCarAppls = selfCarApplMapper.selectAllNoRec();
            selfCarAppls.forEach(selfCarAppl -> {
                selfCarService.getSelfCarInvoicesByApplyIds(selfCarAppl);
            });
        }catch (Exception e){
            e.printStackTrace();
        }*/
    }
}
