guoyuji
2024-10-22 020fcb1311aa00e29f473c001104ee3756321b03
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
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.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.List;
import java.util.Map;
import java.util.Objects;
 
@Service
@DS("sd")
public class GlassPriceBasicService {
    private final GlassPriceBasicMapper glassPriceBasicMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
 
    public GlassPriceBasicService(GlassPriceBasicMapper glassPriceBasicMapper) {
        this.glassPriceBasicMapper = glassPriceBasicMapper;
    }
 
    //保存玻璃价格
    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());
        }
        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};
        productDetails.forEach(productDetail -> {
            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");
                    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] += glassPriceBasic.getPrice();
 
        });
        return money[0];
    }
}