guoyuji
2024-10-23 52efeebf5115a2a2a69773694663617d1a23071b
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
package com.example.erp.service.sd;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.erp.entity.sd.GlassPriceBasic;
import com.example.erp.entity.sd.ProductDetail;
import com.example.erp.mapper.sd.GlassPriceBasicMapper;
import com.example.erp.mapper.sd.ProductDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
@Service
@DS("sd")
public class GlassPriceBasicService {
    private final GlassPriceBasicMapper glassPriceBasicMapper;
    private final ProductDetailMapper productDetailMapper;
 
    public GlassPriceBasicService(GlassPriceBasicMapper glassPriceBasicMapper, ProductDetailMapper productDetailMapper) {
        this.glassPriceBasicMapper = glassPriceBasicMapper;
        this.productDetailMapper = productDetailMapper;
    }
 
    //保存玻璃价格
    public Boolean save(Map<String, Object> prams) {
        GlassPriceBasic glassPriceBasic = new GlassPriceBasic();
        glassPriceBasic.setThickness(
                Double.parseDouble(prams.get("thickness").toString().replaceAll("[^0-9\\.]", ""))
        );
        glassPriceBasic.setPrice(Double.parseDouble(prams.get("price").toString()));
        glassPriceBasic.setJson(prams.toString());
        glassPriceBasic.setType(prams.get("type").toString());
        String type = prams.get("type").toString();
        //判断 是哪种类型
        if(Objects.equals(type, "glass")) {
            glassPriceBasic.setName(prams.get("thickness").toString() + prams.get("color").toString());
        }else if(Objects.equals(type, "hollow")){
            glassPriceBasic.setName(
                    prams.get("thickness").toString()
                            + prams.get("gasType").toString()
                            + prams.get("types").toString());
        } else if (Objects.equals(type, "interlayer"))  {
            glassPriceBasic.setName(
                    prams.get("thickness").toString()
                            + prams.get("color").toString()
                            + prams.get("types").toString());
        } else if (Objects.equals(type, "process")) {
            glassPriceBasic.setName(prams.get("thickness").toString() + prams.get("color").toString()+prams.get("process").toString());
            glassPriceBasic.setProcess(prams.get("process").toString());
        }
        GlassPriceBasic glassPriceBasic1 = glassPriceBasicMapper.selectOne(
                new QueryWrapper<GlassPriceBasic>()
                        .eq("name", glassPriceBasic.getName())
        );
        if(glassPriceBasic1 != null){
            return false;
        }
        return glassPriceBasicMapper.insert(glassPriceBasic)>0;
    }
 
    public Double glassPriceComputed(String productId) {
        List<ProductDetail> productDetails = productDetailMapper
                .selectList(new QueryWrapper<ProductDetail>().eq("prod_id", productId));
 
        final Double[] money = {0.0};
        for (ProductDetail productDetail : productDetails){
            Map<String,String> separation =  JSON.parseObject(
                    productDetail.getSeparation(), new TypeReference<Map<String, String>>(){});
            String name = "";
            switch (productDetail.getDetailType()) {
                case "glass":
                    name = separation.get("thickness") + separation.get("color");
                    String[] process = productDetail.getProcess().split("->");
                    for (String s : process) {
                        money[0] += glassPriceBasicMapper.selectOne(
                                new QueryWrapper<GlassPriceBasic>()
                                        .eq("name", name+s)).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 = glassPriceBasicMapper
                    .selectOne(new QueryWrapper<GlassPriceBasic>().eq("name", name));
            if(glassPriceBasic == null) {
                money[0]= 0.0;
                return money[0];
            }
            money[0] += glassPriceBasic.getPrice();
        }
        return money[0];
    }
 
    public List<GlassPriceBasic> searchGlassPrice() {
        return glassPriceBasicMapper.selectList(null);
    }
 
    public Boolean updateGlassPriceById(GlassPriceBasic glassPriceBasic) {
        return glassPriceBasicMapper.update(
                null,
                new LambdaUpdateWrapper<>(GlassPriceBasic.class)
                        .setSql("price = " + glassPriceBasic.getPrice())
                        .eq(GlassPriceBasic::getId, glassPriceBasic.getId())
                )>0;
    }
 
    public Boolean deleteGlassPriceById(String id) {
        return glassPriceBasicMapper.deleteById(id)>0;
    }
}