1
|
package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
|
1
|
package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
|
2
|
|
2
|
|
|
|
3
|
+import java.math.BigDecimal;
|
|
|
4
|
+import java.math.RoundingMode;
|
|
|
5
|
+import java.text.DecimalFormat;
|
|
|
6
|
+import java.util.Date;
|
|
|
7
|
+import java.util.HashMap;
|
|
|
8
|
+import java.util.List;
|
|
|
9
|
+import java.util.Map;
|
|
|
10
|
+import java.util.stream.Collectors;
|
|
|
11
|
+
|
|
|
12
|
+import org.slf4j.Logger;
|
|
|
13
|
+import org.slf4j.LoggerFactory;
|
|
|
14
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
15
|
+import org.springframework.stereotype.Component;
|
|
|
16
|
+
|
3
|
import com.yoho.search.base.utils.DateUtil;
|
17
|
import com.yoho.search.base.utils.DateUtil;
|
4
|
import com.yoho.search.base.utils.MathUtils;
|
18
|
import com.yoho.search.base.utils.MathUtils;
|
|
|
19
|
+import com.yoho.search.base.utils.ProductIndexEsField;
|
5
|
import com.yoho.search.consumer.service.bo.ProductIndexBO;
|
20
|
import com.yoho.search.consumer.service.bo.ProductIndexBO;
|
6
|
import com.yoho.search.consumer.service.bo.ProductPriceBO;
|
21
|
import com.yoho.search.consumer.service.bo.ProductPriceBO;
|
7
|
import com.yoho.search.consumer.service.logic.SpecialDealLogicService;
|
22
|
import com.yoho.search.consumer.service.logic.SpecialDealLogicService;
|
8
|
import com.yoho.search.consumer.service.logic.VipPriceLogicService;
|
23
|
import com.yoho.search.consumer.service.logic.VipPriceLogicService;
|
9
|
import com.yoho.search.dal.ProductPriceMapper;
|
24
|
import com.yoho.search.dal.ProductPriceMapper;
|
10
|
import com.yoho.search.dal.model.ProductPrice;
|
25
|
import com.yoho.search.dal.model.ProductPrice;
|
11
|
-import org.slf4j.Logger;
|
|
|
12
|
-import org.slf4j.LoggerFactory;
|
|
|
13
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
14
|
-import org.springframework.stereotype.Component;
|
|
|
15
|
-
|
|
|
16
|
-import java.math.BigDecimal;
|
|
|
17
|
-import java.math.RoundingMode;
|
|
|
18
|
-import java.text.DecimalFormat;
|
|
|
19
|
-import java.util.Date;
|
|
|
20
|
-import java.util.List;
|
|
|
21
|
-import java.util.Map;
|
|
|
22
|
-import java.util.stream.Collectors;
|
|
|
23
|
|
26
|
|
24
|
/**
|
27
|
/**
|
25
|
* Created by wangnan on 2017/1/6.
|
28
|
* Created by wangnan on 2017/1/6.
|
|
@@ -27,150 +30,183 @@ import java.util.stream.Collectors; |
|
@@ -27,150 +30,183 @@ import java.util.stream.Collectors; |
27
|
@Component
|
30
|
@Component
|
28
|
public class ProductPriceBuilder implements ViewBuilder {
|
31
|
public class ProductPriceBuilder implements ViewBuilder {
|
29
|
|
32
|
|
30
|
- private final Logger logger = LoggerFactory.getLogger(ProductPriceBuilder.class);
|
33
|
+ private final Logger logger = LoggerFactory.getLogger(ProductPriceBuilder.class);
|
31
|
|
34
|
|
32
|
- @Autowired
|
|
|
33
|
- private ProductPriceMapper productPriceMapper;
|
|
|
34
|
- @Autowired
|
|
|
35
|
- private VipPriceLogicService vipPriceLogicService;
|
|
|
36
|
- @Autowired
|
|
|
37
|
- private SpecialDealLogicService specialSearchFieldLogicService;
|
35
|
+ @Autowired
|
|
|
36
|
+ private ProductPriceMapper productPriceMapper;
|
|
|
37
|
+ @Autowired
|
|
|
38
|
+ private VipPriceLogicService vipPriceLogicService;
|
|
|
39
|
+ @Autowired
|
|
|
40
|
+ private SpecialDealLogicService specialSearchFieldLogicService;
|
38
|
|
41
|
|
39
|
- @Override
|
|
|
40
|
- public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
|
|
|
41
|
- List<ProductPrice> productPrices = productPriceMapper.selectBySknList(sknList);
|
|
|
42
|
- Map<Integer, ProductPrice> productPricesMap = productPrices.stream().collect(Collectors.toMap(ProductPrice::getProductSkn, (p) -> p));
|
|
|
43
|
- for (ProductIndexBO productIndexBO : productIndexBOs) {
|
|
|
44
|
- try {
|
|
|
45
|
- // 如果该skn对应价格对象不存在,直接跳过
|
|
|
46
|
- ProductPrice productPrice = productPricesMap.get(productIndexBO.getProductSkn());
|
|
|
47
|
- if (productPrice == null) {
|
|
|
48
|
- continue;
|
|
|
49
|
- }
|
|
|
50
|
- // 构造ProductPriceBO
|
|
|
51
|
- ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
|
|
|
52
|
- this.buildProductPriceBO(productPriceBO);
|
|
|
53
|
- // 构造ProductIndexBO
|
|
|
54
|
- this.buildProductIndexBO(productIndexBO, productPriceBO);
|
|
|
55
|
- } catch (Exception e) {
|
|
|
56
|
- logger.error(e.getMessage(), e);
|
|
|
57
|
- }
|
|
|
58
|
- }
|
|
|
59
|
- }
|
42
|
+ @Override
|
|
|
43
|
+ public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> sknList) {
|
|
|
44
|
+ List<ProductPrice> productPrices = productPriceMapper.selectBySknList(sknList);
|
|
|
45
|
+ Map<Integer, ProductPrice> productPricesMap = productPrices.stream().collect(Collectors.toMap(ProductPrice::getProductSkn, (p) -> p));
|
|
|
46
|
+ for (ProductIndexBO productIndexBO : productIndexBOs) {
|
|
|
47
|
+ try {
|
|
|
48
|
+ // 如果该skn对应价格对象不存在,直接跳过
|
|
|
49
|
+ ProductPrice productPrice = productPricesMap.get(productIndexBO.getProductSkn());
|
|
|
50
|
+ if (productPrice == null) {
|
|
|
51
|
+ continue;
|
|
|
52
|
+ }
|
|
|
53
|
+ // 构造ProductPriceBO
|
|
|
54
|
+ ProductPriceBO productPriceBO = new ProductPriceBO(productPrice);
|
|
|
55
|
+ this.buildProductPriceBO(productPriceBO);
|
|
|
56
|
+ // 构造ProductIndexBO
|
|
|
57
|
+ this.buildProductIndexBO(productIndexBO, productPriceBO);
|
|
|
58
|
+ } catch (Exception e) {
|
|
|
59
|
+ logger.error(e.getMessage(), e);
|
|
|
60
|
+ }
|
|
|
61
|
+ }
|
|
|
62
|
+ }
|
60
|
|
63
|
|
61
|
- /**
|
|
|
62
|
- * 增量+全量都调用
|
|
|
63
|
- */
|
|
|
64
|
- public void buildProductPriceBO(ProductPriceBO productPriceBO) {
|
|
|
65
|
- // 1、计算并重设vip相关价格
|
|
|
66
|
- vipPriceLogicService.fillProductPriceVipPrice(productPriceBO);
|
|
|
67
|
- // 2、计算折扣相关
|
|
|
68
|
- double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());
|
|
|
69
|
- double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice());
|
64
|
+ /**
|
|
|
65
|
+ * 增量+全量都调用
|
|
|
66
|
+ */
|
|
|
67
|
+ public void buildProductPriceBO(ProductPriceBO productPriceBO) {
|
|
|
68
|
+ // 1、计算并重设vip相关价格
|
|
|
69
|
+ vipPriceLogicService.fillProductPriceVipPrice(productPriceBO);
|
|
|
70
|
+ // 2、计算折扣相关
|
|
|
71
|
+ double salesPrice = this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice());
|
|
|
72
|
+ double marketPrice = this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice());
|
70
|
productPriceBO.setIsDiscount(salesPrice < marketPrice ? "Y" : "N");
|
73
|
productPriceBO.setIsDiscount(salesPrice < marketPrice ? "Y" : "N");
|
71
|
- double specialOffer = this.divide(1, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
72
|
- if (specialOffer <= 0.5) {
|
|
|
73
|
- productPriceBO.setSpecialoffer("Y");
|
|
|
74
|
- } else {
|
|
|
75
|
- productPriceBO.setSpecialoffer("N");
|
|
|
76
|
- }
|
|
|
77
|
- double promotionDiscountInt = this.divide(1, productPriceBO.getSalesPrice().multiply(new BigDecimal(10)), productPriceBO.getMarketPrice());
|
|
|
78
|
- productPriceBO.setPromotionDiscountInt((long) promotionDiscountInt);
|
|
|
79
|
- double promotionDiscount = this.divide(3, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
80
|
- productPriceBO.setPromotionDiscount(this.getBigDecimalFromDouble(promotionDiscount));
|
|
|
81
|
- // 3、设置学生价相关属性
|
|
|
82
|
- productPriceBO.setIsStudentPrice(productPriceBO.getStudentPrice() != null ? "Y" : "N");
|
|
|
83
|
- productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
|
|
|
84
|
- }
|
74
|
+ double specialOffer = this.divide(1, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
75
|
+ if (specialOffer <= 0.5) {
|
|
|
76
|
+ productPriceBO.setSpecialoffer("Y");
|
|
|
77
|
+ } else {
|
|
|
78
|
+ productPriceBO.setSpecialoffer("N");
|
|
|
79
|
+ }
|
|
|
80
|
+ double promotionDiscountInt = this.divide(1, productPriceBO.getSalesPrice().multiply(new BigDecimal(10)), productPriceBO.getMarketPrice());
|
|
|
81
|
+ productPriceBO.setPromotionDiscountInt((long) promotionDiscountInt);
|
|
|
82
|
+ double promotionDiscount = this.divide(3, productPriceBO.getSalesPrice(), productPriceBO.getMarketPrice());
|
|
|
83
|
+ productPriceBO.setPromotionDiscount(this.getBigDecimalFromDouble(promotionDiscount));
|
|
|
84
|
+ // 3、设置学生价相关属性
|
|
|
85
|
+ productPriceBO.setIsStudentPrice(productPriceBO.getStudentPrice() != null ? "Y" : "N");
|
|
|
86
|
+ productPriceBO.setIsstudentrebate(productPriceBO.getStudentCoinRate() == null || productPriceBO.getStudentCoinRate().compareTo(BigDecimal.ZERO) == 0 ? "N" : "Y");
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ private void buildProductIndexBO(ProductIndexBO productIndexBO, ProductPriceBO productPriceBO) {
|
|
|
90
|
+ productIndexBO.setIsDiscount("N");
|
|
|
91
|
+ productIndexBO.setSpecialoffer("N");
|
|
|
92
|
+ productIndexBO.setPromotionDiscount(new BigDecimal(1));
|
|
|
93
|
+ productIndexBO.setPromotionDiscountInt(10L);
|
|
|
94
|
+ productIndexBO.setIsStudentPrice("N");
|
|
|
95
|
+ productIndexBO.setIsStudentRebate("N");
|
|
|
96
|
+ productIndexBO.setIsnew("N");
|
|
|
97
|
+ productIndexBO.setMarketPrice(productPriceBO.getMarketPrice());
|
|
|
98
|
+ productIndexBO.setVipDiscountType(productPriceBO.getVipDiscountType());
|
|
|
99
|
+ productIndexBO.setSalesPrice(productPriceBO.getSalesPrice());
|
|
|
100
|
+ productIndexBO.setVipPrice(productPriceBO.getVipPrice());
|
|
|
101
|
+ productIndexBO.setVip1Price(productPriceBO.getVip1Price());
|
|
|
102
|
+ productIndexBO.setVip2Price(productPriceBO.getVip2Price());
|
|
|
103
|
+ productIndexBO.setVip3Price(productPriceBO.getVip3Price());
|
|
|
104
|
+ productIndexBO.setStudentPrice(productPriceBO.getStudentPrice());
|
|
|
105
|
+ // 折扣相关
|
|
|
106
|
+ productIndexBO.setIsDiscount(productPriceBO.getIsDiscount());
|
|
|
107
|
+ productIndexBO.setSpecialoffer(productPriceBO.getSpecialoffer());
|
|
|
108
|
+ productIndexBO.setPromotionDiscountInt(productPriceBO.getPromotionDiscountInt());
|
|
|
109
|
+ productIndexBO.setPromotionDiscount(productPriceBO.getPromotionDiscount());
|
|
|
110
|
+ // 学生价相关字段
|
|
|
111
|
+ productIndexBO.setIsStudentPrice(productPriceBO.getIsStudentPrice());
|
|
|
112
|
+ productIndexBO.setIsStudentRebate(productPriceBO.getIsstudentrebate());
|
|
|
113
|
+ // 设置vipLevels
|
|
|
114
|
+ productIndexBO.setVipLevels(productPriceBO.getVipLevels());
|
|
|
115
|
+ // 设置价格更新时间
|
|
|
116
|
+ productIndexBO.setPriceUpdateTime(productPriceBO.getUpdateTime());
|
|
|
117
|
+ // 设置最后一次降价时间
|
|
|
118
|
+ productIndexBO.setLastReducePriceTime(productPriceBO.getLastReducePriceTime());
|
|
|
119
|
+ // 计算isNew(规则:首次上架7天内展示且折扣率大于等于88折)
|
|
|
120
|
+ productIndexBO.setIsnew("N");
|
|
|
121
|
+ if (this.isNew(productIndexBO.getFirstShelveTime(), productIndexBO.getMarketPrice(), productIndexBO.getSalesPrice())) {
|
|
|
122
|
+ productIndexBO.setIsnew("Y");
|
|
|
123
|
+ }
|
|
|
124
|
+ // 计算specialSearchFieldPrice
|
|
|
125
|
+ productIndexBO.setSpecialSearchFieldPrice(specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
|
|
|
126
|
+ }
|
85
|
|
127
|
|
86
|
- private void buildProductIndexBO(ProductIndexBO productIndexBO, ProductPriceBO productPriceBO) {
|
|
|
87
|
- productIndexBO.setIsDiscount("N");
|
|
|
88
|
- productIndexBO.setSpecialoffer("N");
|
|
|
89
|
- productIndexBO.setPromotionDiscount(new BigDecimal(1));
|
|
|
90
|
- productIndexBO.setPromotionDiscountInt(10L);
|
|
|
91
|
- productIndexBO.setIsStudentPrice("N");
|
|
|
92
|
- productIndexBO.setIsStudentRebate("N");
|
|
|
93
|
- productIndexBO.setIsnew("N");
|
|
|
94
|
- productIndexBO.setMarketPrice(productPriceBO.getMarketPrice());
|
|
|
95
|
- productIndexBO.setVipDiscountType(productPriceBO.getVipDiscountType());
|
|
|
96
|
- productIndexBO.setSalesPrice(productPriceBO.getSalesPrice());
|
|
|
97
|
- productIndexBO.setVipPrice(productPriceBO.getVipPrice());
|
|
|
98
|
- productIndexBO.setVip1Price(productPriceBO.getVip1Price());
|
|
|
99
|
- productIndexBO.setVip2Price(productPriceBO.getVip2Price());
|
|
|
100
|
- productIndexBO.setVip3Price(productPriceBO.getVip3Price());
|
|
|
101
|
- productIndexBO.setStudentPrice(productPriceBO.getStudentPrice());
|
|
|
102
|
- // 折扣相关
|
|
|
103
|
- productIndexBO.setIsDiscount(productPriceBO.getIsDiscount());
|
|
|
104
|
- productIndexBO.setSpecialoffer(productPriceBO.getSpecialoffer());
|
|
|
105
|
- productIndexBO.setPromotionDiscountInt(productPriceBO.getPromotionDiscountInt());
|
|
|
106
|
- productIndexBO.setPromotionDiscount(productPriceBO.getPromotionDiscount());
|
|
|
107
|
- // 学生价相关字段
|
|
|
108
|
- productIndexBO.setIsStudentPrice(productPriceBO.getIsStudentPrice());
|
|
|
109
|
- productIndexBO.setIsStudentRebate(productPriceBO.getIsstudentrebate());
|
|
|
110
|
- // 设置vipLevels
|
|
|
111
|
- productIndexBO.setVipLevels(productPriceBO.getVipLevels());
|
|
|
112
|
- // 设置价格更新时间
|
|
|
113
|
- productIndexBO.setPriceUpdateTime(productPriceBO.getUpdateTime());
|
|
|
114
|
- // 设置最后一次降价时间
|
|
|
115
|
- productIndexBO.setLastReducePriceTime(productPriceBO.getLastReducePriceTime());
|
|
|
116
|
- // 计算isNew(规则:首次上架7天内展示且折扣率大于等于88折)
|
|
|
117
|
- productIndexBO.setIsnew("N");
|
|
|
118
|
- if (this.isNew(productIndexBO.getFirstShelveTime(), productIndexBO.getMarketPrice(), productIndexBO.getSalesPrice())) {
|
|
|
119
|
- productIndexBO.setIsnew("Y");
|
|
|
120
|
- }
|
|
|
121
|
- // 计算specialSearchFieldPrice
|
|
|
122
|
- productIndexBO.setSpecialSearchFieldPrice(specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
|
|
|
123
|
- }
|
128
|
+ public Map<String, Object> getProductPriceModelMap(Integer productId, Integer firstShelveTime, ProductPrice pp) {
|
|
|
129
|
+ Map<String, Object> indexData = new HashMap<String, Object>();
|
|
|
130
|
+ indexData.put("isnew", "N");
|
|
|
131
|
+ if (pp == null) {
|
|
|
132
|
+ return indexData;
|
|
|
133
|
+ }
|
|
|
134
|
+ // 构造ProductPriceBO
|
|
|
135
|
+ ProductPriceBO productPriceBO = new ProductPriceBO(pp);
|
|
|
136
|
+ this.buildProductPriceBO(productPriceBO);
|
|
|
137
|
+ indexData.put("productId", productId);
|
|
|
138
|
+ indexData.put("marketPrice", this.getDoubleFromBigDecimal(productPriceBO.getMarketPrice()));
|
|
|
139
|
+ indexData.put("salesPrice", this.getDoubleFromBigDecimal(productPriceBO.getSalesPrice()));
|
|
|
140
|
+ indexData.put("vipPrice", this.getDoubleFromBigDecimal(productPriceBO.getVipPrice()));
|
|
|
141
|
+ indexData.put("vipDiscountType", productPriceBO.getVipDiscountType());
|
|
|
142
|
+ indexData.put("vip1Price", this.getDoubleFromBigDecimal(productPriceBO.getVip1Price()));
|
|
|
143
|
+ indexData.put("vip2Price", this.getDoubleFromBigDecimal(productPriceBO.getVip2Price()));
|
|
|
144
|
+ indexData.put("vip3Price", this.getDoubleFromBigDecimal(productPriceBO.getVip3Price()));
|
|
|
145
|
+ indexData.put("studentPrice", this.getDoubleFromBigDecimal(productPriceBO.getStudentPrice()));
|
124
|
|
146
|
|
|
|
147
|
+ indexData.put("isDiscount", productPriceBO.getIsDiscount());
|
|
|
148
|
+ indexData.put("specialoffer", productPriceBO.getSpecialoffer());
|
|
|
149
|
+ indexData.put("promotionDiscountInt", productPriceBO.getPromotionDiscountInt());
|
|
|
150
|
+ indexData.put("promotionDiscount", this.getDoubleFromBigDecimal(productPriceBO.getPromotionDiscount()));
|
|
|
151
|
+ indexData.put("isStudentPrice", productPriceBO.getIsStudentPrice());
|
|
|
152
|
+ indexData.put("isstudentrebate", productPriceBO.getIsstudentrebate());
|
|
|
153
|
+ indexData.put("vipLevels", productPriceBO.getVipLevels());
|
|
|
154
|
+ indexData.put("priceUpdateTime", productPriceBO.getUpdateTime());
|
|
|
155
|
+ if (firstShelveTime != null) {
|
|
|
156
|
+ indexData.put("isnew", this.isNew(firstShelveTime, productPriceBO.getMarketPrice(), productPriceBO.getSalesPrice()));
|
|
|
157
|
+ }
|
|
|
158
|
+ indexData.put(ProductIndexEsField.specialSearchFieldPrice, specialSearchFieldLogicService.getSpecialSearchFieldPrice(productPriceBO));
|
|
|
159
|
+ return indexData;
|
|
|
160
|
+ }
|
125
|
|
161
|
|
126
|
- /**
|
|
|
127
|
- * isNew 标签逻辑
|
|
|
128
|
- */
|
|
|
129
|
- public boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {
|
|
|
130
|
- try {
|
|
|
131
|
- if (firstShelveTime == null || firstShelveTime <= 0 || salesPrice == null || marketPrice == null) {
|
|
|
132
|
- return false;
|
|
|
133
|
- }
|
|
|
134
|
- long dateCount = DateUtil.daysBetween(new Date(firstShelveTime * 1000L), new Date());
|
|
|
135
|
- if (dateCount > 7) {
|
|
|
136
|
- return false;
|
|
|
137
|
- }
|
|
|
138
|
- // 计算折扣
|
|
|
139
|
- Double discount = MathUtils.getDevideValue(salesPrice, marketPrice, 2, RoundingMode.DOWN);
|
|
|
140
|
- if (discount >= 0.88) {
|
|
|
141
|
- return true;
|
|
|
142
|
- }
|
|
|
143
|
- return false;
|
|
|
144
|
- } catch (Exception e) {
|
|
|
145
|
- return false;
|
|
|
146
|
- }
|
|
|
147
|
- }
|
162
|
+ /**
|
|
|
163
|
+ * isNew 标签逻辑
|
|
|
164
|
+ */
|
|
|
165
|
+ public boolean isNew(Integer firstShelveTime, BigDecimal marketPrice, BigDecimal salesPrice) {
|
|
|
166
|
+ try {
|
|
|
167
|
+ if (firstShelveTime == null || firstShelveTime <= 0 || salesPrice == null || marketPrice == null) {
|
|
|
168
|
+ return false;
|
|
|
169
|
+ }
|
|
|
170
|
+ long dateCount = DateUtil.daysBetween(new Date(firstShelveTime * 1000L), new Date());
|
|
|
171
|
+ if (dateCount > 7) {
|
|
|
172
|
+ return false;
|
|
|
173
|
+ }
|
|
|
174
|
+ // 计算折扣
|
|
|
175
|
+ Double discount = MathUtils.getDevideValue(salesPrice, marketPrice, 2, RoundingMode.DOWN);
|
|
|
176
|
+ if (discount >= 0.88) {
|
|
|
177
|
+ return true;
|
|
|
178
|
+ }
|
|
|
179
|
+ return false;
|
|
|
180
|
+ } catch (Exception e) {
|
|
|
181
|
+ return false;
|
|
|
182
|
+ }
|
|
|
183
|
+ }
|
148
|
|
184
|
|
149
|
- private double divide(int scale, BigDecimal a, BigDecimal b) {
|
|
|
150
|
- if (b == null || b.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
151
|
- return 0;
|
|
|
152
|
- }
|
|
|
153
|
- StringBuilder sb = new StringBuilder("0.0");
|
|
|
154
|
- for (int i = 1; i < scale; i++) {
|
|
|
155
|
- sb.append('0');
|
|
|
156
|
- }
|
|
|
157
|
- BigDecimal divideResult = a.divide(b, scale, RoundingMode.DOWN);
|
|
|
158
|
- DecimalFormat decimalFormat = new DecimalFormat(sb.toString());
|
|
|
159
|
- String result = decimalFormat.format(divideResult);
|
|
|
160
|
- return Double.parseDouble(result);
|
|
|
161
|
- }
|
185
|
+ private double divide(int scale, BigDecimal a, BigDecimal b) {
|
|
|
186
|
+ if (b == null || b.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
187
|
+ return 0;
|
|
|
188
|
+ }
|
|
|
189
|
+ StringBuilder sb = new StringBuilder("0.0");
|
|
|
190
|
+ for (int i = 1; i < scale; i++) {
|
|
|
191
|
+ sb.append('0');
|
|
|
192
|
+ }
|
|
|
193
|
+ BigDecimal divideResult = a.divide(b, scale, RoundingMode.DOWN);
|
|
|
194
|
+ DecimalFormat decimalFormat = new DecimalFormat(sb.toString());
|
|
|
195
|
+ String result = decimalFormat.format(divideResult);
|
|
|
196
|
+ return Double.parseDouble(result);
|
|
|
197
|
+ }
|
162
|
|
198
|
|
163
|
- private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
|
|
|
164
|
- if (bigDecimal == null) {
|
|
|
165
|
- return 0d;
|
|
|
166
|
- }
|
|
|
167
|
- return bigDecimal.doubleValue();
|
|
|
168
|
- }
|
199
|
+ private double getDoubleFromBigDecimal(BigDecimal bigDecimal) {
|
|
|
200
|
+ if (bigDecimal == null) {
|
|
|
201
|
+ return 0d;
|
|
|
202
|
+ }
|
|
|
203
|
+ return bigDecimal.doubleValue();
|
|
|
204
|
+ }
|
169
|
|
205
|
|
170
|
- private BigDecimal getBigDecimalFromDouble(Double d) {
|
|
|
171
|
- if (d == null) {
|
|
|
172
|
- return new BigDecimal(0);
|
|
|
173
|
- }
|
|
|
174
|
- return new BigDecimal(d);
|
|
|
175
|
- }
|
206
|
+ private BigDecimal getBigDecimalFromDouble(Double d) {
|
|
|
207
|
+ if (d == null) {
|
|
|
208
|
+ return new BigDecimal(0);
|
|
|
209
|
+ }
|
|
|
210
|
+ return new BigDecimal(d);
|
|
|
211
|
+ }
|
176
|
} |
212
|
} |