package com.ruoyi.logistics.callback;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.logistics.domain.BizFinancialFeeItems;
import com.ruoyi.logistics.domain.BizWaybillCostDetails;
import com.ruoyi.logistics.domain.BizWaybillOrder;
import com.ruoyi.logistics.dto.JDOrderFeeRequestDTO;
import com.ruoyi.logistics.service.FeeItemsCacheService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/callback/jd")
public class JDPushOrderFeeCallback {

    @Autowired
    private IBizWaybillOrderService bizWaybillOrderService;

    @Autowired
    private IBizWaybillCostDetailsService bizWaybillCostDetailsService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private FeeItemsCacheService feeItemsCacheService;

    @RequestMapping("/fee")
    public AjaxResult fee(@RequestBody JDOrderFeeRequestDTO request) {
        log.info("京东物流运单费用回调，参数：{}", request);

        // 根据运单号查询订单
        BizWaybillOrder query = new BizWaybillOrder();
        query.setExternalWaybillNo(request.getWaybillCode());
        BizWaybillOrder bizWaybillOrder = bizWaybillOrderService.selectBizWaybillOrderByQuery(query);
        if (bizWaybillOrder == null) {
            return AjaxResult.error("订单不存在");
        }
        SysDept sysDept = sysDeptService.selectDeptById(bizWaybillOrder.getDeptId());

        List<BizWaybillCostDetails> details = new ArrayList<>();
        request.getCommonActualFeeInfoDetails().forEach(item -> {
            BizWaybillCostDetails costDetail = new BizWaybillCostDetails();
            costDetail.setWaybillId(bizWaybillOrder.getWaybillId());
            costDetail.setExternalWaybillNo(bizWaybillOrder.getExternalWaybillNo());

            // 匹配费用类型项
            BizFinancialFeeItems feeItemByExtFeeCode = feeItemsCacheService.getFeeItemByExtFeeCode(item.getFeeType());
            if (feeItemByExtFeeCode != null) {
                costDetail.setFeeItemCode(feeItemByExtFeeCode.getFeeItemCode());
                costDetail.setFeeItemName(feeItemByExtFeeCode.getFeeItemName());
                costDetail.setFeeName(item.getFeeTypeName());
                costDetail.setAmount(item.getStandardAmount());
                costDetail.setRateAmount(item.getStandardAmount().multiply(new BigDecimal(StringUtils.isNotBlank(sysDept.getRateValue()) ? sysDept.getRateValue() : "1")).setScale(2, BigDecimal.ROUND_HALF_UP));
                costDetail.setDeptId(bizWaybillOrder.getDeptId());
                costDetail.setUserId(bizWaybillOrder.getUserId());
            }

            details.add(costDetail);
        });

        details.forEach(detail -> {
            bizWaybillCostDetailsService.insertBizWaybillCostDetails(detail);
        });

        return AjaxResult.success().put("message", "成功");
    }
}
