package com.ruoyi.logistics.service.impl;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.logistics.domain.*;
import com.ruoyi.logistics.service.IBizFinancialFeeItemsService;
import com.ruoyi.logistics.service.IBizWaybillCostDetailsService;
import com.ruoyi.logistics.service.IBizWaybillOrderService;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.List;


/**
 * 费用明细回调入库
 *
 * @author zxf
 * @date 2026-02-03
 */

@Service
public class FeePushService {
    private static final Logger logger = LoggerFactory.getLogger(FeePushService.class);
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IBizWaybillOrderService bizWaybillOrderService;
    @Autowired
    private IBizFinancialFeeItemsService BizFinancialFeeItemsService;
    @Autowired
    private IBizWaybillCostDetailsService bizWaybillCostDetailsService;

    public boolean processFeePush(SfFeePushRequest request) {
        try {
            String waybillNo = request.getWaybillNo();
            String orderNo = request.getOrderNo();
            BigDecimal totalWeight = request.getMeterageWeightQty();
            Integer quantity = request.getQuantity();
            String productName = request.getProductName();
            List<SfFeeInfo> feeList = request.getFeeList();
            logger.info("开始处理顺丰运费清单 - 运单号: {}, 订单号: {}, 产品: {}, 计费重量: {}kg",
                    waybillNo, orderNo, productName, totalWeight);
            // 1. 根据订单号查询本地订单 如果不存在该订单号  则不进行明细的入库
            BizWaybillOrder bizWaybillOrder= new BizWaybillOrder();
            bizWaybillOrder.setWaybillNo(orderNo);
            bizWaybillOrder.setExternalWaybillNo(waybillNo);
            List<BizWaybillOrder> orderList=bizWaybillOrderService.selectBizWaybillOrderList(bizWaybillOrder);
            if (orderList.size()==0) {
                logger.error("订单不存在: {}", orderNo);
                return false;
            }
            /**
             * 存在的话 就去拿到这个部门id 要知道这个供应商的费率
             * 还要拿到订单创建Id  数据权限可能到个人
             */
            Long deptId=orderList.get(0).getDeptId();
            Long userId=orderList.get(0).getUserId();
            Long waybillId=orderList.get(0).getWaybillId();

            SysDept sysDept = sysDeptService.selectDeptById(orderList.get(0).getDeptId());

            // TODO:先写死  这里差一个查询供应商费率的查询
            String  Rate=sysDept.getRateValue();
            /*处理费用明细*/
            if (feeList != null && !feeList.isEmpty()) {
                for (SfFeeInfo fee : feeList) {
                    BigDecimal feeAmt = fee.getFeeAmt();
                    /**
                     * 回调回来的 费用类型  还需与系统 费用类型表-biz_inancial_fee_items
                     *
                     */
                    String feeTypeCode = fee.getFeeTypeCode();
                    BizFinancialFeeItems bizFinancialFeeItems = new BizFinancialFeeItems();
                    bizFinancialFeeItems.setFeeItemCategory("sf");
                    bizFinancialFeeItems.setExtFeeCode(feeTypeCode);
                    List<BizFinancialFeeItems> feeItemsList=BizFinancialFeeItemsService.selectBizFinancialFeeItemsList(bizFinancialFeeItems);
                    BizWaybillCostDetails bizWaybillCostDetails= new BizWaybillCostDetails();
                    /**
                     * 回顺丰的费用类型代码无法在费用类型表中匹配时
                     * 匹配到---给到对应名称
                     * 匹配不到 --- 还叫顺丰的自己的费用名称和编码
                     */
                    bizWaybillCostDetails.setFeeName(feeItemsList.get(0).getExtFeeName());
                    if(StringUtils.isBlank(feeItemsList.get(0).getFeeItemCode())){
                        bizWaybillCostDetails.setFeeItemCode(feeItemsList.get(0).getExtFeeCode());
                        bizWaybillCostDetails.setFeeItemName(feeItemsList.get(0).getExtFeeName());
                    }
                    else{
                        bizWaybillCostDetails.setFeeItemCode(feeItemsList.get(0).getFeeItemCode());
                        bizWaybillCostDetails.setFeeItemName(feeItemsList.get(0).getFeeItemName());
                    }
                    bizWaybillCostDetails.setAmount(feeAmt);
                    BigDecimal deptRate = new BigDecimal(Rate);
                    BigDecimal result = feeAmt.multiply(deptRate);
                    bizWaybillCostDetails.setRateAmount(result);
                    bizWaybillCostDetails.setDeptId(deptId);
                    bizWaybillCostDetails.setUserId(userId);
                    bizWaybillCostDetails.setWaybillId(waybillId);
                    bizWaybillCostDetails.setExternalWaybillNo(waybillNo);
                    // 记录费用明细
                    bizWaybillCostDetailsService.insertBizWaybillCostDetails(bizWaybillCostDetails);
                }
            }
            return true;

        } catch (Exception e) {
            logger.error("处理运费清单失败: {}", e.getMessage(), e);
            return false;
        }
    }


}
