package com.clx.performance.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.clx.performance.component.ThirdComponent;
import com.clx.performance.dao.OrderChildDao;
import com.clx.performance.dao.OrderGoodsDao;
import com.clx.performance.dao.settle.OrderChildSyncTransportRecordDao;
import com.clx.performance.dao.settle.SettlementDriverDetailDao;
import com.clx.performance.dto.OrderChildSyncDTO;
import com.clx.performance.enums.PerformanceResultEnum;
import com.clx.performance.enums.settle.SettlementDriverEnum;
import com.clx.performance.enums.settle.SettlementOwnerEnum;
import com.clx.performance.extranal.user.OwnerInfoService;
import com.clx.performance.feign.TransportFeignService;
import com.clx.performance.model.OrderChild;
import com.clx.performance.model.OrderGoods;
import com.clx.performance.model.settle.OrderChildSyncTransportRecord;
import com.clx.performance.model.settle.SettlementDriverDetail;
import com.clx.performance.model.settle.SettlementOwner;
import com.clx.performance.model.settle.SettlementOwnerDetail;
import com.clx.performance.param.feign.transport.OrderChildPaySyncParam;
import com.clx.performance.service.settle.SettlementDriverDetailService;
import com.clx.performance.service.settle.SettlementMqHandlerService;
import com.clx.performance.service.settle.SettlementMqService;
import com.clx.performance.service.settle.SettlementOwnerDetailService;
import com.clx.performance.utils.LocalDateTimeUtils;
import com.clx.performance.vo.mq.SettlementDetailInvoiceTypeSyncMqParam;
import com.clx.user.vo.feign.OwnerInfoFeignVO;
import com.msl.common.dto.HttpDTO;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Component
@AllArgsConstructor
public class OrderChildPaySyncTransportJob {

    private final OrderChildSyncTransportRecordDao orderChildSyncTransportRecordDao;

    private final TransportFeignService transportFeignService;

    private final SettlementMqHandlerService settlementMqHandlerService;

    private final SettlementDriverDetailService settlementDriverDetailService;

    private final SettlementOwnerDetailService settlementOwnerDetailService;

    private final SettlementMqService settlementMqService;

    private final OrderChildDao orderChildDao;

    private final OrderGoodsDao orderGoodsDao;

    /**
     * 同步网络货运
     */
    @XxlJob("orderChildPaySyncTransportJob")
    public void orderChildPaySyncTransportJob() {
        List<OrderChildSyncTransportRecord> recordList = orderChildSyncTransportRecordDao.selectListForJob();
        if (CollectionUtil.isEmpty(recordList)) {
            return;
        }

        List<String> orderChilList = recordList.stream().map(OrderChildSyncTransportRecord::getChildNo).collect(Collectors.toList());
        OrderChildPaySyncParam param = new OrderChildPaySyncParam();
        param.setChildNoList(orderChilList);
        HttpDTO httpDTO = transportFeignService.orderChildPaySync(param);
        String decrypt = ThirdComponent.decrypt(httpDTO.getData());
        OrderChildSyncDTO bean = JSONUtil.toBean(decrypt, OrderChildSyncDTO.class);
        if (bean.getCode() == 0) {
            for (OrderChildSyncTransportRecord record : recordList) {
                OrderChildSyncTransportRecord update = new OrderChildSyncTransportRecord();
                update.setId(record.getId());
                update.setStatus(1);
                orderChildSyncTransportRecordDao.updateEntityByKey(update);
            }
        }

    }


    /**
     * 结算信息job处理
     * */
    @XxlJob("settlementDetailInvoiceTypeSyncJob")
    public void settlementDetailInvoiceTypeSyncJob() {
        String jobParam = XxlJobHelper.getJobParam();
        log.info("XxlJobHelper jobParam:{}", jobParam);
        SettlementDetailInvoiceTypeSyncMqParam mqParam = new SettlementDetailInvoiceTypeSyncMqParam();
        mqParam.setChildNo(jobParam);
        settlementMqHandlerService.settlementDetailInvoiceTypeSync(mqParam);
    }


    @XxlJob("testJob")
    public void test2() {
        String jobParam = XxlJobHelper.getJobParam();
        log.info("XxlJobHelper jobParam:{}", jobParam);
        OrderChild orderChild = orderChildDao.getByChildNo(jobParam).get();
        OrderGoods orderGoods = orderGoodsDao.getByOrderGoodsNo(orderChild.getOrderGoodsNo()).get();
        Integer settlementDriverDetailId = settlementDriverDetailService.saveSettlementDetail(orderChild);
        Integer settlementOwnerDetailId = settlementOwnerDetailService.saveSettlementDetail(orderChild, orderGoods);

        settlementMqService.settlementDetailAdd(settlementDriverDetailId, settlementOwnerDetailId);

    }

}
