package com.clx.performance.service.impl.settle;

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.SettlementDriverDetailDao;
import com.clx.performance.dao.settle.SettlementOwnerDetailDao;
import com.clx.performance.dto.OrderChildSyncDTO;
import com.clx.performance.enums.PerformanceResultEnum;
import com.clx.performance.enums.settle.SettlementOwnerEnum;
import com.clx.performance.feign.TransportFeignService;
import com.clx.performance.model.OrderChild;
import com.clx.performance.model.OrderGoods;
import com.clx.performance.model.settle.SettlementDriverDetail;
import com.clx.performance.model.settle.SettlementOwnerDetail;
import com.clx.performance.param.feign.transport.ThirdOrderChildBrokerParam;
import com.clx.performance.service.TransportSyncService;
import com.clx.performance.service.settle.SettlementMqService;
import com.clx.performance.service.settle.SettlementService;
import com.clx.performance.vo.feign.transport.ThirdOrderChildBrokerResultVO;
import com.msl.common.dto.HttpDTO;
import com.msl.common.exception.ServiceSystemException;
import com.msl.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Objects;

@Slf4j
@Service
public class SettlementServiceImpl implements SettlementService {


    @Autowired
    private OrderChildDao orderChildDao;
    @Autowired
    private SettlementOwnerDetailDao settlementOwnerDetailDao;
    @Autowired
    private SettlementDriverDetailDao settlementDriverDetailDao;
    @Autowired
    private SettlementMqService settlementMqService;
    @Autowired
    private TransportSyncService transportSyncService;
    @Autowired
    private OrderGoodsDao orderGoodsDao;

    @Autowired
    private TransportFeignService transportFeignService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSettlementDetailInvoiceType(String childNo, Integer invoiceType) {
        OrderChild orderChild = orderChildDao.getByChildNo(childNo).orElseThrow(PerformanceResultEnum.ORDER_CHILD_NO_FOUND);
        SettlementOwnerDetail settlementOwnerDetail = settlementOwnerDetailDao.getByChildNo(childNo).orElseThrow(PerformanceResultEnum.DATA_NOT_FIND);
        SettlementDriverDetail settlementDriverDetail = settlementDriverDetailDao.getByChildNo(childNo).orElseThrow(PerformanceResultEnum.DATA_NOT_FIND);

        if (settlementOwnerDetail.getInvoiceType() != null) {return;}

        //设置司机预付运费标示
        if (settlementOwnerDetail.getPrepayFreight().compareTo(BigDecimal.ZERO) > 0) {
            settlementDriverDetail.setPrepayFreightFlag(1);
        } else {
            settlementDriverDetail.setPrepayFreightFlag(0);
        }
        // 货主结算金额<0走普通单
        if (settlementOwnerDetail.getSettlementFreight().compareTo(BigDecimal.ZERO) < 0){
            invoiceType = SettlementOwnerEnum.InvoiceType.ORDINARY.getCode();
            settlementDriverDetail.setPrepayFreightFlag(0);
        } else {
            OrderGoods orderGoods = orderGoodsDao.getByOrderGoodsNo(orderChild.getOrderGoodsNo()).get();
            //是否通过风控,调用网络货运
            ThirdOrderChildBrokerParam param = transportSyncService.generateOrderChildSync(orderChild, orderGoods, settlementOwnerDetail, settlementDriverDetail);
            Result<HttpDTO> httpDTOResult = transportFeignService.orderChildSync(param);
            String decrypt = ThirdComponent.decrypt(httpDTOResult.getData().getData());
            OrderChildSyncDTO bean = JSONUtil.toBean(decrypt, OrderChildSyncDTO.class);
            if (bean.getCode() == 0) {
                Integer status = bean.getData().getStatus();
                if (status == 1) {
                    //通过风控
                    invoiceType = SettlementOwnerEnum.InvoiceType.ONLINE.getCode();
                } else {
                    invoiceType = SettlementOwnerEnum.InvoiceType.ORDINARY.getCode();
                }
            } else {
                throw new ServiceSystemException(PerformanceResultEnum.ORDER_CHILD_SYNC_ERROR);

            }

            if (SettlementOwnerEnum.InvoiceType.ONLINE.getCode().equals(invoiceType) && settlementOwnerDetail.getPrepayFreight().compareTo(BigDecimal.ZERO) > 0) {
                settlementDriverDetail.setPrepayFreightFlag(1);
            }
        }

        settlementOwnerDetail.setInvoiceType(invoiceType);
        settlementDriverDetail.setInvoiceType(invoiceType);

//        // 网运单更新预付运费
//        if (Objects.equals(settlementOwnerDetail.getInvoiceType(), SettlementOwnerEnum.InvoiceType.ONLINE.getCode())) {
//            settlementOwnerDetail.setPrepayFreight(updatePrepayFreightPay(settlementOwnerDetail.getChildNo(),
//                    settlementOwnerDetail.getFreight(), settlementOwnerDetail.getLossFreight()));
//        }

        settlementOwnerDetailDao.updateInvoiceType(settlementOwnerDetail);
        settlementDriverDetailDao.updateInvoiceTypeAndPrepayFreightFlag(settlementDriverDetail);

        // 发送mq 通过开票标识
        settlementMqService.invoiceTypeSync(childNo, invoiceType);

    }

    /**
     * 预付运费支付
     */
    private BigDecimal updatePrepayFreightPay(String childNo, BigDecimal freight, BigDecimal lossFreight){
        //开票金额
        BigDecimal prepayFreight = freight.subtract(lossFreight);
        if (prepayFreight.compareTo(BigDecimal.ZERO) <= 0) {return BigDecimal.ZERO;}

        //扣减预付运费



        return prepayFreight;

    }
}
