package com.clx.performance.job.settlement;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.clx.performance.dao.OrderChildSyncTransportInvoiceRecordDao;
import com.clx.performance.enums.TransportSyncTypeEnum;
import com.clx.performance.enums.settle.SettlementOwnerEnum;
import com.clx.performance.extranal.broker.BrokerService;
import com.clx.performance.model.OrderChildSyncTransportInvoiceRecord;
import com.clx.performance.service.settle.SettlementMqService;
import com.clx.performance.vo.pc.broker.TransportVO;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@AllArgsConstructor

public class SettlementJob {
    private final OrderChildSyncTransportInvoiceRecordDao orderChildSyncTransportInvoiceRecordDao;

    private final BrokerService brokerService;

    private final SettlementMqService settlementMqService;

    /**
     * 同步无车承运运单状态
     */
    @XxlJob("settlementInvoiceTypeSync")
    public void settlementInvoiceTypeSync() {
        List<OrderChildSyncTransportInvoiceRecord> recordList = orderChildSyncTransportInvoiceRecordDao
                .selectListForJob();
        log.info("待同步无车承运运单状态，size:{}", recordList.size());
        if (recordList.isEmpty()) {return;}

        List<Integer> ids = new LinkedList<>();
        for (OrderChildSyncTransportInvoiceRecord item : recordList) {
            log.info("同步无车承运运单状态：{}", JSONUtil.parse(item));

            try {
                // 获取状态
                TransportVO result = brokerService.getTransportByChildNo(item.getChildNo());

                Integer invoiceType;

                if (Objects.equals(result.getRiskStatus(), 1) || Objects.equals(result.getRiskStatus(), 2)) {
                    invoiceType = SettlementOwnerEnum.InvoiceType.ORDINARY.getCode();
                } else if (Objects.equals(result.getSyncStatus(), 2)) {
                    invoiceType = SettlementOwnerEnum.InvoiceType.ORDINARY.getCode();
                } else if (Objects.equals(result.getSyncStatus(), 0)) {
                    OrderChildSyncTransportInvoiceRecord entity = new OrderChildSyncTransportInvoiceRecord();
                    entity.setChildNo(item.getChildNo());
                    entity.setType(TransportSyncTypeEnum.Type.EXCEPTION.getCode());
                    entity.setRemark("处理中");
                    orderChildSyncTransportInvoiceRecordDao.saveEntity(entity);
                    continue;
                } else {
                    invoiceType = SettlementOwnerEnum.InvoiceType.ONLINE.getCode();
                }

                // 发送mq （结算）
                settlementMqService.settle(item.getChildNo(), invoiceType);
            }catch (Exception e){
                OrderChildSyncTransportInvoiceRecord entity = new OrderChildSyncTransportInvoiceRecord();
                entity.setChildNo(item.getChildNo());
                entity.setType(TransportSyncTypeEnum.Type.EXCEPTION.getCode());
                entity.setRemark("接口调用失败");
                orderChildSyncTransportInvoiceRecordDao.saveEntity(entity);
            }
        }

        orderChildSyncTransportInvoiceRecordDao.deleteByKeys(ids);
    }

}
