guoyujie
2025-11-20 dbde84b9c89cb37f5e6cadbde182031576dec988
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
130
131
132
package com.example.northglasserpclient.service.sd.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.northglasserpclient.domain.po.sd.*;
import com.example.northglasserpclient.mapper.sd.ProductMapper;
import com.example.northglasserpclient.service.sd.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
 
    private final IProductDetailService productDetailService;
 
    private final IGlassPriceBasicService glassPriceBasicService;
 
    @Override
    public List<Product> getProduct() {
        return list(new LambdaQueryWrapper<Product>());
    }
 
    @Override
    public Map<String, Object> glassPriceComputed(String productId) {
        Map<String, Object> map = new HashMap<>();
 
        List<ProductDetail> productDetails  =  productDetailService.list(
                new LambdaQueryWrapper<ProductDetail>()
                        .eq(ProductDetail::getProdId, productId)
        );
 
        final Double[] money = {0.0};
        String mergedResult = productDetails.get(0).getProcess();
        outerLoop:
        for (ProductDetail productDetail : productDetails){
            Map<String,String> separation =  JSON.parseObject(
                    productDetail.getSeparation(), new TypeReference<Map<String, String>>(){});
            String name = "";
            switch (productDetail.getDetailType()) {
                case "glass":
                    mergedResult = mergeTwoProcesses(mergedResult, productDetail.getProcess());
 
                    name = separation.get("thickness") + separation.get("color");
                    String[] process = productDetail.getProcess().split("->");
                    for (String s : process) {
                        GlassPriceBasic glassPriceBasic = glassPriceBasicService.getOne(
                                new LambdaQueryWrapper<GlassPriceBasic>()
                                        .eq(GlassPriceBasic::getName, name+s));
                        if(glassPriceBasic == null) {
                            money[0] += 0.0;
                            break outerLoop;
                        }else{
                            money[0] += glassPriceBasic.getPrice();
                        }
 
                    }
                    break;
                case "hollow":
                    name = separation.get("thickness") + separation.get("gasType") + separation.get("Type");
                    break;
                case "interlayer":
                    name = separation.get("thickness") + separation.get("color") + separation.get("type");
                    break;
            }
 
            GlassPriceBasic glassPriceBasic = glassPriceBasicService
                    .getOne(new LambdaQueryWrapper<GlassPriceBasic>().eq(GlassPriceBasic::getName, name));
            if(glassPriceBasic == null) {
                money[0]= 0.0;
                break outerLoop;
            }else{
                money[0] += glassPriceBasic.getPrice();
            }
 
        }
        map.put("money", money[0]);
        map.put("workmanship", mergedResult);
        return map;
    }
 
    //合并产品的工艺流程
    private static String mergeTwoProcesses(String p1, String p2) {
        String[] arr1 = p1.split("->");
        String[] arr2 = p2.split("->");
 
        // 找到共同前缀长度
        int minLen = Math.min(arr1.length, arr2.length);
        int commonLen = 0;
        while (commonLen < minLen && arr1[commonLen].equals(arr2[commonLen])) {
            commonLen++;
        }
 
        // 拼接共同前缀
        StringBuilder commonPrefix = new StringBuilder();
        for (int i = 0; i < commonLen; i++) {
            if (i > 0) commonPrefix.append("->");
            commonPrefix.append(arr1[i]);
        }
 
        // 提取p1的差异部分
        StringBuilder diff1 = new StringBuilder();
        for (int i = commonLen; i < arr1.length; i++) {
            diff1.append("->").append(arr1[i]);
        }
 
        // 提取p2的差异部分
        StringBuilder diff2 = new StringBuilder();
        for (int i = commonLen; i < arr2.length; i++) {
            diff2.append("->").append(arr2[i]);
        }
 
        // 合并差异(用[]包裹,多个差异用|分隔)
        if (diff1.length() == 0 && diff2.length() == 0) {
            return commonPrefix.toString();
        } else if (diff1.length() == 0) {
            return commonPrefix + "" + diff2 ;
        } else if (diff2.length() == 0) {
            return commonPrefix + "" + diff1 ;
        } else {
            return commonPrefix + "" + diff1 + diff2;
        }
    }
}