xiaoyong931011
2023-01-10 73b3813c2d110bf446f251350f120bb1e2b51d0c
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
package cc.mrbird.febs.common.enumerates;
 
import lombok.Getter;
 
import java.math.BigDecimal;
//商品星级
@Getter
public enum StarRatingEnum {
    /**
     * 定级规则
     *      大于最小值,小于等于最大值
     */
    NORMAL("普通",0,30,1),
    ONE_STAR("一星",30,50,2),
    TWO_STAR("二星",50,100,3),
    THREE_STAR("三星",100,200,4);
 
    private String name;
 
    private Integer minValue;
 
    private Integer maxValue;
 
    private Integer code;
 
    StarRatingEnum(String name,Integer minValue, Integer maxValue,Integer code) {
        this.name = name;
        this.minValue = minValue;
        this.maxValue = maxValue;
        this.code = code;
    }
 
    /**
     * 根据商品原价获取对应的商品星级
     * @param price
     * @return
     */
    public String belongStarRating(String price){
        String name = StarRatingEnum.NORMAL.name;
        BigDecimal priceBig = new BigDecimal(price).setScale(2,BigDecimal.ROUND_DOWN);
        for (StarRatingEnum starRatingEnum : StarRatingEnum.values()) {
            BigDecimal minValue = new BigDecimal(starRatingEnum.minValue).setScale(2, BigDecimal.ROUND_DOWN);
            BigDecimal maxValue = new BigDecimal(starRatingEnum.maxValue).setScale(2, BigDecimal.ROUND_DOWN);
            if(priceBig.compareTo(minValue) > 0 && priceBig.compareTo(maxValue) <= 0){
                name = starRatingEnum.name;
            }
        }
        return name;
    }
 
    /**
     * 根据输入的商品星级获取对应的Code
     * @param name
     * @return
     */
    public Integer getGoodsStarCode(String name){
        Integer code = 0;
        for(StarRatingEnum starRatingEnum : StarRatingEnum.values()){
            if(starRatingEnum.name.equals(name)){
                code = starRatingEnum.code;
            }
        }
        return  code;
    }
 
    /**
     * 获取商品可以设置的最小价格
     * @return
     */
    public Integer getMinValue(){
        Integer totalMinvalue = 0;
        for(StarRatingEnum starRatingEnum : StarRatingEnum.values()){
            if(starRatingEnum.minValue < totalMinvalue){
                totalMinvalue = starRatingEnum.minValue;
            }
        }
        return totalMinvalue;
    }
 
    /**
     * 获取商品可以设置的最大价格
     * @return
     */
    public Integer getMaxValue(){
        Integer totalMaxValue = 0;
        for(StarRatingEnum starRatingEnum : StarRatingEnum.values()){
            if(starRatingEnum.maxValue > totalMaxValue){
                totalMaxValue = starRatingEnum.maxValue;
            }
        }
        return totalMaxValue;
    }
 
    public static void main(String[] args) {
        String s = StarRatingEnum.NORMAL.belongStarRating(String.valueOf(100));
        System.out.println(s);
    }
}