package com.bohuikeji.delivery.backstage.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.service.third.HuolalaServer;
import com.bohuikeji.delivery.backstage.service.third.SfExpressServer;
import com.bohuikeji.delivery.backstage.util.capacity.HllUtils;
import com.bohuikeji.delivery.backstage.util.capacity.JdUtils;
import com.bohuikeji.delivery.backstage.util.capacity.SFCityUtils;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 第三方补充支付 工具方法类
 */
@Slf4j
public class ThirdSmUtils {

    /**
     * 包装订单信息
     * @param order
     * @return
     */
    public static JSONObject orderInfo(TOrder order){
        JSONObject orderInfo=new JSONObject();
        orderInfo.put("orderNum",order.getThirdNum());
        orderInfo.put("mailingNme",order.getMailingName());
        orderInfo.put("orderStatus",order.getOrderStatus());
        orderInfo.put("mailingAddress",order.getMailingAddress().getCity()+order.getMailingAddress().getArea());
        orderInfo.put("receiptName",order.getReceiptName());
        orderInfo.put("receiptAddress",order.getReceiptAddress().getCity()+order.getReceiptAddress().getArea());
        orderInfo.put("information",order.getCargoName()+"/"+order.getCargoWeight()+"公斤/"+order.getWorthName()+"元");
        orderInfo.put("wayName",order.getWayName());
        orderInfo.put("paymentType",order.getPaymentType());
        orderInfo.put("orderCreateTime",order.getOrderCreateTime());
        orderInfo.put("orderExpectTime",order.getOrderExpectTime());
        orderInfo.put("orderRemarks",order.getOrderRemarks());
        return orderInfo;
    }

    /**
     * 计算费用信息，包装数据结构
     * @param order
     * @return
     */
    public static JSONObject costInfo(TOrder order){
        //总费用
        BigDecimal allPrice=new BigDecimal("0.00");
        //耗材总费用，因为可能存在多项耗材计费，这里做个累加
        BigDecimal materialAllPrice=new BigDecimal("0.00");
        //快递保价费
        BigDecimal insuredPrice=new BigDecimal("0.00");
        if (SfExpressServer.NAME.equals(order.getThirdName())){
            JSONObject res= SFCityUtils.getSfOrderStatus(order.getThirdNum()).getJSONObject("result");
            if (res != null){
                allPrice=allPrice.add(res.getBigDecimal("total_price").divide(new BigDecimal("100")));
            }
        }else {
            //查询京东费用信息
            JSONObject res= JdUtils.queryOrderPrice(order.getThirdNum()).getJSONObject("data");
            JSONArray array=res.getJSONArray("feeInfoList");
            for (int i = 0; i < array.size(); i++) {
                JSONObject tmp=array.getJSONObject(i);
                //总费用累加
                allPrice=allPrice.add(tmp.getBigDecimal("standardAmount"));
                //只处理耗材费用，其他费用归类到体积重量费
                if ("QLHCF".equals(tmp.getString("costNo"))){
                    materialAllPrice=materialAllPrice.add(tmp.getBigDecimal("standardAmount"));
                }
                if("QLBJ".equals(tmp.getString("costNo"))){
                    insuredPrice=insuredPrice.add(tmp.getBigDecimal("standardAmount"));
                }
            }
        }
        //总费用直接减去优惠金额
        allPrice=allPrice.subtract(order.getPrefAmount());

        //组装数据结构
        JSONObject cost=new JSONObject();
        JSONArray detail=new JSONArray();
        //基础快递费
        JSONObject baseItem=new JSONObject();
        baseItem.put("title","快递运费");
        baseItem.put("desc","包含快递基础费用");
        baseItem.put("price",order.getOrderExpectPrice());
        detail.add(0,baseItem);
        //快递耗材费
        JSONObject item=new JSONObject();
        item.put("title","快递耗材费");
        item.put("desc","文件袋，包装等耗材使用产生的费用");
        item.put("price",materialAllPrice.setScale(2, RoundingMode.DOWN));
        detail.add(1,item);
        //快递保价费
        //快递耗材费
        JSONObject insuredItem=new JSONObject();
        insuredItem.put("title","快递保价费");
        insuredItem.put("desc","配送物品保价费用");
        insuredItem.put("price",insuredPrice.setScale(2, RoundingMode.DOWN));
        detail.add(2,insuredItem);
        //其他费用
        JSONObject otherItem=new JSONObject();
        otherItem.put("title","体积重量费");
        otherItem.put("desc","快递员取件确认物品体积、重量等增加的费用");
        //其他费用=总费用-基础费用-耗材费用
        otherItem.put("price",allPrice.subtract(order.getOrderExpectPrice()).subtract(materialAllPrice).subtract(insuredPrice));
        detail.add(otherItem);
        log.info("#################################:{}",detail);
        //费用明细
        cost.put("detail",detail);
        //总费用
        cost.put("totalPrice",allPrice);
        //已支付费用
        cost.put("paidPrice",order.getOrderFactPrice());
        //待支付费用
        cost.put("waitPrice",allPrice.subtract(order.getOrderFactPrice()));
        return cost;
    }

    /**
     * 货拉拉费用，包装数据结构
     * @param order
     * @return
     */
    public static JSONObject huolalaCostInfo(TOrder order,String token){
        JSONArray huoRes= HllUtils.getEOrderDetailtwo(token,order.getThirdNum());
        JSONObject huodetail=HllUtils.getEOrderDetail(token,order.getThirdNum());
        //总费用
        BigDecimal allPrice=new BigDecimal("0.00");
        if (HuolalaServer.NAME.equals(order.getThirdName())){//货拉拉确认账单补付
            allPrice=allPrice.add(order.getAllPrice());
        }
        //总费用直接减去优惠金额
        allPrice=allPrice.subtract(order.getPrefAmount());
        JSONArray detail=new JSONArray();
        if(huoRes.size()>0) {
            for (int i = 0; i < huoRes.size(); i++) {
                JSONObject baseItem=new JSONObject();
                //对应⾓标的所有数据（⼀条）
                JSONObject arr = huoRes.getJSONObject(i);
                //取出值，getstring是将name的值变成string类型
                baseItem.put("title",arr.getString("name"));
                baseItem.put("desc","");
                baseItem.put("price",BigDecimal.valueOf(arr.getFloatValue("price_fen")).divide(new BigDecimal("100")));
                detail.add(baseItem);
            }
        }

        //组装数据结构
        JSONObject cost=new JSONObject();
        //费用明细
        cost.put("detail",detail);
        //总费用
        cost.put("totalPrice",BigDecimal.valueOf(huodetail.getFloatValue("send_bill_price_fen")).divide(new BigDecimal("100")));
        //已支付费用
        cost.put("paidPrice",order.getOrderFactPrice());
        //待支付费用
        cost.put("waitPrice",allPrice);
        return cost;
    }

}
