package com.ms.biz.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ms.biz.consts.PurchaseOrderConst;
import com.ms.api.dto.dsorder.AppendVenderRemarkTplItemDTO;

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

public class PurchaseTool {

//    /**
//     * 替换掉电话号码相关标签
//     * 如去掉 <span contenteditable="false" data-field="receiver_mobile">13366667777</span>
//     * @param tpl
//     * @return
//     */
//    public static String removeRemarkMobileTplHashTag(String tpl) {
////        String tpl = "asdasdas<span contenteditable=\"false\" data-field=\"receiver_mobile\">#+861222352531#</span>afafafafafafaf";
//        return tpl.replaceAll("<span contenteditable=\"false\" data-field=\"receiver_mobile\">#(?:[^#]+)#</span>", "");
//    }

//    /**
//     * 生成默认备注模板
//     * @return
//     */
//    public static String getDefaultAppendVenderRemarkTpl() {
//        String[] arr = {
//            String.format("<span contenteditable=\"false\" data-field=\"%s\">#%s#</span>", PurchaseOrderConst.PURCHASE_PLATFORM2_FIELD, "采购平台").toString(),
//            String.format("<span contenteditable=\"false\" data-field=\"%s\">#%s#</span>", PurchaseOrderConst.PURCHASE_ORDER_SN_FIELD, "采购单号").toString(),
//            String.format("<span contenteditable=\"false\" data-field=\"%s\">#%s#</span>", PurchaseOrderConst.LOGISTICS_INFO_FIELD, "快递及运单号").toString(),
//            String.format("<span contenteditable=\"false\" data-field=\"%s\">#%s#</span>", PurchaseOrderConst.PURCHASE_ORDER_BUYER, "采购账号").toString(),
//        };
//        return String.join("#", arr);
//    }

    public static String getRemarkTplRegPattern() {
        return "([^</span>]*)<span.*?data-field=\"([^\"]+)\"[^>]*?>#(?:[^#]+)#</span>([^<span>]*)";
    }

    /**
     * 生成默认备注模板
     * @return
     */
    public static String getDefaultAppendVenderRemarkTpl() {
        List<AppendVenderRemarkTplItemDTO> list = new ArrayList<>();
        list.add(new AppendVenderRemarkTplItemDTO(PurchaseOrderConst.APPEND_VENDER_REMARK_TPL_ITEM_TYPE_FIELD, PurchaseOrderConst.PURCHASE_PLATFORM2_FIELD,"#采购平台#"));
        list.add(new AppendVenderRemarkTplItemDTO(PurchaseOrderConst.APPEND_VENDER_REMARK_TPL_ITEM_TYPE_TEXT, null,"#"));
        list.add(new AppendVenderRemarkTplItemDTO(PurchaseOrderConst.APPEND_VENDER_REMARK_TPL_ITEM_TYPE_FIELD, PurchaseOrderConst.PURCHASE_ORDER_SN_FIELD,"#采购单号#"));
        list.add(new AppendVenderRemarkTplItemDTO(PurchaseOrderConst.APPEND_VENDER_REMARK_TPL_ITEM_TYPE_TEXT, null,"#"));
        list.add(new AppendVenderRemarkTplItemDTO(PurchaseOrderConst.APPEND_VENDER_REMARK_TPL_ITEM_TYPE_FIELD, PurchaseOrderConst.LOGISTICS_INFO_FIELD,"#快递及运单号#"));
        list.add(new AppendVenderRemarkTplItemDTO(PurchaseOrderConst.APPEND_VENDER_REMARK_TPL_ITEM_TYPE_TEXT, null,"#"));
        list.add(new AppendVenderRemarkTplItemDTO(PurchaseOrderConst.APPEND_VENDER_REMARK_TPL_ITEM_TYPE_FIELD, PurchaseOrderConst.PURCHASE_ORDER_BUYER,"#采购账号#"));
        return JSON.toJSONString(list);
    }

    /**
     * 替换掉电话号码项
     * @param tpl
     * @return
     */
    public static String removeRemarkMobileTplItem(String tpl) {
        List<AppendVenderRemarkTplItemDTO> list = JSON.parseObject(tpl, new TypeReference<List<AppendVenderRemarkTplItemDTO>>() {});
        List<AppendVenderRemarkTplItemDTO> newList = new ArrayList<>();
        for (AppendVenderRemarkTplItemDTO item : list) {
            if (!"receiver_mobile".equals(item.getName())) {
                newList.add(item);
            }
        }
        return JSON.toJSONString(newList);
    }

    public static Map<String, String> getAppendShopRemarkFieldsMap() {
        Map<String, String> map = new HashMap<>();
        map.put(PurchaseOrderConst.PURCHASE_PLATFORM2_FIELD, "#采购平台#");
        map.put(PurchaseOrderConst.PURCHASE_ORDER_SN_FIELD, "#采购单号#");
        map.put(PurchaseOrderConst.PURCHASE_ORDER_BUYER, "#采购账号#");
        map.put(PurchaseOrderConst.PURCHASE_PAYMENT_FIELD, "#采购成本#");
        map.put(PurchaseOrderConst.PURCHASE_ORDER_SELLER_FIELD, "#上家店铺名称#");
        map.put(PurchaseOrderConst.PURCHASE_ORDER_NUM_FIELD, "#采购数量#");
        map.put(PurchaseOrderConst.LOGISTICS_INFO_FIELD, "#快递及运单号#");
        map.put(PurchaseOrderConst.SHOP_NAME_FIELD, "#抖店店铺名称#");
        map.put(PurchaseOrderConst.TOTAL_PROFIT_FIELD, "#总利润#");
        map.put(PurchaseOrderConst.PROFIT_RATIO_FIELD, "#利润率#");
        return map;
    }

    public static Map<String, String> getAppendShopNoteFieldsMap() {
        Map<String, String> map = new HashMap<>();
        map.put(PurchaseOrderConst.PURCHASE_PLATFORM2_FIELD, "#采购平台#");
        map.put(PurchaseOrderConst.PURCHASE_ORDER_SN_FIELD, "#采购单号#");
        map.put(PurchaseOrderConst.PURCHASE_ORDER_BUYER, "#采购账号#");
        map.put(PurchaseOrderConst.PURCHASE_PAYMENT_FIELD, "#采购成本#");
        map.put(PurchaseOrderConst.LOGISTICS_INFO_FIELD, "#快递及运单号#");
        map.put(PurchaseOrderConst.PURCHASE_ORDER_SELLER_FIELD, "#上家店铺名称#");
        map.put(PurchaseOrderConst.PURCHASE_ORDER_NUM_FIELD, "#采购数量#");
        map.put(PurchaseOrderConst.SHOP_NAME_FIELD, "#抖店店铺名称#");
        map.put(PurchaseOrderConst.TOTAL_PROFIT_FIELD, "#总利润#");
        map.put(PurchaseOrderConst.PROFIT_RATIO_FIELD, "#利润率#");
        return map;
    }

    public static Map<String, String> getAutoFillRemarkFieldsMap() {
        Map<String, String> map = new HashMap<>();
        map.put(PurchaseOrderConst.AUTO_FILL_REMARK_TYPE_SKU, "#抖店商品规格#");
        map.put(PurchaseOrderConst.AUTO_FILL_REMARK_TYPE_ONLY_VENDER_REMARK, "#抖店商家备注#");
        map.put(PurchaseOrderConst.AUTO_FILL_REMARK_TYPE_ONLY_ORDER_REMARK, "#抖店买家留言#");
        map.put(PurchaseOrderConst.AUTO_FILL_REMARK_TYPE_PLATFORM_NAME, "#平台名称#");
        map.put(PurchaseOrderConst.AUTO_FILL_REMARK_TYPE_PLATFORM_SHOP_NAME, "#抖店店铺名称#");
        map.put(PurchaseOrderConst.AUTO_FILL_REMARK_TYPE_PLATFORM_ORDER_ID, "#抖店订单号#");
        return map;
    }

    public static Map<String, String> getAutoFillOrderMemoFieldsMap() {
        Map<String, String> map = new HashMap<>();
        map.put(PurchaseOrderConst.AUTO_FILL_ORDER_MEMO_TYPE_SHOP_NAME, "#抖店店铺名称#");
        map.put(PurchaseOrderConst.AUTO_FILL_ORDER_MEMO_TYPE_ORDER_ID, "#抖店订单号#");
        map.put(PurchaseOrderConst.AUTO_FILL_ORDER_MEMO_TYPE_PLATFORM_NAME, "#抖店平台#");
        map.put(PurchaseOrderConst.AUTO_FILL_ORDER_MEMO_TYPE_SELLER_REMARK, "#抖店商家备注#");
        map.put(PurchaseOrderConst.AUTO_FILL_ORDER_MEMO_TYPE_ORDER_REMARK, "#抖店买家留言#");
        return map;
    }

    public static Map<String, String> dsAndDdLogisticsNameMap() {
        return new HashMap<>();
    }

    public static String getDdLogisticsNameByDsLogisticsName(String dsLogisticsName) {
        return dsAndDdLogisticsNameMap().getOrDefault(dsLogisticsName, dsLogisticsName);
    }
}
