shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.ishop.merchant.util;
 
import com.ishop.merchant.Constants;
import com.ishop.model.po.EbProduct;
import com.ishop.model.po.EbProductAttr;
import com.ishop.model.po.EbProductAttrValue;
import com.ishop.model.vo.ProductAddVo;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
public class ProductAttrUtils {
 
    protected static final Logger logger = LoggerFactory.getLogger(ProductAttrUtils.class);
 
    /**
     * 根据价格规格,返回自定义属性中要存放的价格信息。
     * <pre>
     *
     * </pre>
     * @param priceAttr 几个规格定义,如:[100,200,300,500,其他金额]
     * @param attrValueSku 自定义属性值,如:98#,其他金额 或者 98#,500
     * @return
     * @date 2023-09-10
     * @date 2023-09-11 目前该方法未使用,因为通过规格分组已经可以配置出来金额了。
     */
    public static final double acquirePriceAttrMoney(String[] priceAttr, String attrValueSku){
        String[] skuArray = StringUtils.commaDelimitedListToStringArray(attrValueSku);
        for(String skuValue : skuArray){
            if(skuValue.equals(Constants.ATTR_VALUE_OTHER_PRICE) || skuValue.equals(Constants.ATTR_VALUE_CUSTOM_PRICE)){
                return 0;
            }
            for(String price : priceAttr){
                if(skuValue.equals(price)){
                    return Double.parseDouble(price);
                }
            }
        }
        return -1;
    }
 
    /**
     * 返回价格规格,对应的价格数组,如果存在的话。
     * <pre>
     *    格式如下:
     *    [100, 200, 其他金额(或自定义)]
     * </pre>
     * @param attrList
     * @return
     * @date 2023-09-10
     */
    public static final String[] hasPriceAttr(List<EbProductAttr> attrList){
        for(EbProductAttr attr : attrList){
            if(attr.getAttrName().equals(Constants.ATTR_NAME_PRICE)){
                return StringUtils.commaDelimitedListToStringArray(attr.getAttrValues());
            }
        }
        return null;
    }
 
    /**
     * 设置添加(或编辑)商品价格信息。
     * @param request
     * @param product
     * @date 2023-08-02
     */
    public static final void combineProductAttrValue(ProductAddVo request, EbProduct product){
        //计算价格
        List<EbProductAttrValue> attrValueAddRequestList = request.getAttrValue();
        EbProductAttrValue minAttrValue = attrValueAddRequestList.stream().min(Comparator.comparing(EbProductAttrValue::getPrice)).get();
        product.setPrice(minAttrValue.getPrice());
        product.setOtPrice(minAttrValue.getOtPrice());
        product.setCost(minAttrValue.getCost());
        product.setStock(attrValueAddRequestList.stream().mapToInt(EbProductAttrValue::getStock).sum());
    }
 
    /**
     * 商品sku。
     * <p>返回数据格式,如:92汽油,95汽油,98汽油,规格值拼接了一下,要存储数据库</p>
     *
     * @param attrValue json字符串,如:{"92号":"92号", "93号":"93号"}
     * @return sku
     */
    public static final String getSku(String attrValue) {
//        LinkedHashMap<String, String> linkedHashMap = JSONObject.parseObject(attrValue, LinkedHashMap.class, Feature.OrderedField);
//        LinkedHashMap<String, String> linkedHashMap = null;
//        logger.debug("======= " + attrValue);
        if(attrValue.startsWith("\"")){
            attrValue = attrValue.substring(1, attrValue.length()-1);
            // 前端传来的包含双引号转移反斜杠,老报错
            attrValue = attrValue.replace("\\", StringUtils.EMPTY_STRING);
        }
        Map<String, String> map = null;
        try {
            map = JsonUtils.jsonStringToObject(attrValue, Map.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        List<String> strings = new ArrayList<>(map.size());
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            strings.add(next.getValue());
        }
        return StringUtils.collectionToCommaDelimitedString(strings);
    }
 
    /**
     * 把给定的 Key,分隔为:type | productId
     * @param key
     * @return
     * @date 2023-06-14
     */
    public static final String[] splitTypeAndProductId(String key){
        return StringUtils.commaDelimitedListToStringArray(key);
    }
 
    public static final String combineKey(int type, long productId){
        return new StringBuilder().append(type)
                .append(StringUtils.DEFAULT_SPLIT_SEPARATOR).append(productId).toString();
    }
}