package com.tashow.cloud.trade.framework.order.core.utils;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tashow.cloud.common.util.json.JsonUtils;
import com.tashow.cloud.trade.dal.dataobject.order.TradeOrderItemDO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TransOrderByTempUtils {

    private final static String tempUrl = "temp/order_serve_info.json";


    public static JSONObject trans(TradeOrderItemDO item) {
        JSONObject result = new JSONObject();
        String json = ResourceUtil.readUtf8Str(tempUrl);
        //解析json
        JSONObject orderInfo = JSON.parseObject(json);
        JSONArray serveInfoTempObj = orderInfo.getJSONArray("serveInfo");
        JSONArray serveExtInfoTempObj = orderInfo.getJSONArray("selectedServeInfo");
        JSONArray priceExtInfoTempObj = orderInfo.getJSONArray("priceExtInfo");
        //获取服务信息
        result.put("serveInfo", getServeInfo(serveInfoTempObj, item.getServeInfo()));
        //可选服务
        result.put("selectedServeInfo", getServeInfo(serveExtInfoTempObj, item.getServeExtInfo()));
        //服务附加费
        result.put("priceExtInfo", getServeInfo(priceExtInfoTempObj, item.getPriceExtInfo()));
        return result;
    }

    private static List<List<JSONObject>> getServeInfo(JSONArray tempObj, String valueInfo) {
        List<List<JSONObject>> result = new ArrayList<>();
        //数据json  [{},{}]
        JSONArray valueInfoObj = JSON.parseArray(valueInfo);
        for (Object val : valueInfoObj) {
            List<JSONObject> valResult = new ArrayList<>();
            Map valMap = JsonUtils.parseObject(val.toString(), Map.class);
            if (valMap == null) {
                continue;
            }
            //模版json
            for (Object tem : tempObj) {
                JSONObject resultMap = new JSONObject();
                Map<String, Object> tempMap = JsonUtils.parseObject(tem.toString(), Map.class);
                resultMap.put("key", MapUtil.getStr(tempMap, "key"));
                resultMap.put("name", MapUtil.getStr(tempMap, "name"));
                resultMap.put("value", valMap.get(MapUtil.getStr(tempMap, "key")));
                resultMap.put("sort", MapUtil.getStr(tempMap, "sort"));
                valResult.add(resultMap);
            }
            result.add(valResult);
        }
        return result;
    }

    public static void main(String[] args) {
        TradeOrderItemDO itemDO = new TradeOrderItemDO();
        JSONArray tempObj = new JSONArray();
        JSONObject orderInfo = new JSONObject();
        orderInfo.put("petName", "aaa");
        orderInfo.put("petType", "aaa");
        orderInfo.put("weight", "aaa");
        orderInfo.put("diedTime", "aaa");
        orderInfo.put("diedReason", "aaa");
        orderInfo.put("remainsPhoneUrl", "aaa");
        orderInfo.put("subType", "aaa");
        orderInfo.put("changeRule", "aaa");
        orderInfo.put("subOrder", "aaa");
        orderInfo.put("userRemark", "aaa");
        orderInfo.put("merchantRemark", "aaa");
        orderInfo.put("pickUpAddress", "aaa");
        orderInfo.put("sendAddress", "aaa");
        tempObj.add(orderInfo);
        orderInfo = new JSONObject();
        orderInfo.put("petName", "bbb");
        orderInfo.put("petType", "bbb");
        orderInfo.put("weight", "bbb");
        orderInfo.put("diedTime", "bbb");
        orderInfo.put("diedReason", "bbb");
        orderInfo.put("remainsPhoneUrl", "bbb");
        orderInfo.put("subType", "bbb");
        orderInfo.put("changeRule", "bbb");
        orderInfo.put("subOrder", "bbb");
        orderInfo.put("userRemark", "bbb");
        orderInfo.put("merchantRemark", "bbb");
        orderInfo.put("pickUpAddress", "bbb");
        orderInfo.put("sendAddress", "bbb");
        tempObj.add(orderInfo);
        String jsonString = JSON.toJSONString(tempObj);
        System.out.println("serveInfo:" + jsonString);
        itemDO.setServeInfo(jsonString);
        JSONArray tempObj1 = new JSONArray();
        JSONObject extraInfo = new JSONObject();
        extraInfo.put("serveUrl", "aaa");
        extraInfo.put("serveName", "aaa");
        extraInfo.put("serveDesc", "aaa");
        extraInfo.put("prodType", "aaa");
        extraInfo.put("parentActive", "aaa");
        extraInfo.put("count", "aaa");
        extraInfo.put("price", "aaa");
        extraInfo.put("handPrice", "aaa");
        extraInfo.put("totalPrice", "aaa");
        extraInfo.put("discountPrice", "aaa");
        extraInfo.put("payPrice", "aaa");
        extraInfo.put("refundMoney", "aaa");
        extraInfo.put("refundCount", "aaa");
        extraInfo.put("deliveryType", "aaa");
        extraInfo.put("receiptUser", "aaa");
        extraInfo.put("userMobile", "aaa");
        extraInfo.put("receiveAddress", "aaa");
        tempObj1.add(extraInfo);
        extraInfo = new JSONObject();
        extraInfo.put("serveUrl", "bbb");
        extraInfo.put("serveName", "bbb");
        extraInfo.put("serveDesc", "bbb");
        extraInfo.put("prodType", "bbb");
        extraInfo.put("parentActive", "bbb");
        extraInfo.put("count", "bbb");
        extraInfo.put("price", "bbb");
        extraInfo.put("handPrice", "bbb");
        extraInfo.put("totalPrice", "bbb");
        extraInfo.put("discountPrice", "bbb");
        extraInfo.put("payPrice", "bbb");
        extraInfo.put("refundMoney", "bbb");
        extraInfo.put("refundCount", "bbb");
        extraInfo.put("deliveryType", "bbb");
        extraInfo.put("receiptUser", "bbb");
        extraInfo.put("userMobile", "bbb");
        extraInfo.put("receiveAddress", "bbb");
        tempObj1.add(extraInfo);
        String jsonString1 = JSON.toJSONString(tempObj1);
        System.out.println("selected:" + jsonString1);
        itemDO.setServeExtInfo(jsonString1);
        JSONArray priceExtObj = new JSONArray();
        JSONObject priceExtInfo = new JSONObject();
        priceExtInfo.put("costName", "aaa");
        priceExtInfo.put("serveArea", "aaa");
        priceExtInfo.put("targetArea", "aaa");
        priceExtInfo.put("chargeType", "aaa");
        priceExtInfo.put("respMode", "aaa");
        priceExtInfo.put("respTime", "aaa");
        priceExtInfo.put("weight", "aaa");
        priceExtInfo.put("chargeTime", "aaa");
        priceExtInfo.put("totalPrice", "aaa");
        priceExtInfo.put("discountPrice", "aaa");
        priceExtInfo.put("payPrice", "aaa");
        priceExtInfo.put("refundPrice", "aaa");
        priceExtObj.add(priceExtInfo);
        priceExtInfo = new JSONObject();
        priceExtInfo.put("costName", "bbb");
        priceExtInfo.put("serveArea", "bbb");
        priceExtInfo.put("targetArea", "bbb");
        priceExtInfo.put("chargeType", "bbb");
        priceExtInfo.put("respMode", "bbb");
        priceExtInfo.put("respTime", "bbb");
        priceExtInfo.put("weight", "bbb");
        priceExtInfo.put("chargeTime", "bbb");
        priceExtInfo.put("totalPrice", "bbb");
        priceExtInfo.put("discountPrice", "bbb");
        priceExtInfo.put("payPrice", "bbb");
        priceExtInfo.put("refundPrice", "bbb");
        priceExtObj.add(priceExtInfo);
        String jsonString2 = JSON.toJSONString(priceExtObj);
        System.out.println("price:" + jsonString2);
        itemDO.setPriceExtInfo(jsonString2);
        Map<String, Object> trans = trans(itemDO);
        System.out.println(JSON.toJSONString(trans));
    }

}
