Authored by mali

价格走势,180天补充零价格

1 package com.yohoufo.dal.product.model; 1 package com.yohoufo.dal.product.model;
2 2
  3 +import com.alibaba.fastjson.JSONObject;
  4 +
3 import java.math.BigDecimal; 5 import java.math.BigDecimal;
4 6
5 public class PriceTrendModel { 7 public class PriceTrendModel {
@@ -13,6 +15,17 @@ public class PriceTrendModel { @@ -13,6 +15,17 @@ public class PriceTrendModel {
13 15
14 private BigDecimal skuPrice; 16 private BigDecimal skuPrice;
15 17
  18 + public PriceTrendModel() {
  19 + }
  20 +
  21 + public PriceTrendModel(Integer productId, Integer sizeId, BigDecimal sknPrice, BigDecimal skuPrice, Integer createTime) {
  22 + this.productId = productId;
  23 + this.sizeId = sizeId;
  24 + this.sknPrice = sknPrice;
  25 + this.skuPrice = skuPrice;
  26 + this.createTime = createTime;
  27 + }
  28 +
16 public Integer getEndTime() { 29 public Integer getEndTime() {
17 return endTime; 30 return endTime;
18 } 31 }
@@ -82,4 +95,9 @@ public class PriceTrendModel { @@ -82,4 +95,9 @@ public class PriceTrendModel {
82 public void setCreateTime(Integer createTime) { 95 public void setCreateTime(Integer createTime) {
83 this.createTime = createTime; 96 this.createTime = createTime;
84 } 97 }
  98 +
  99 + @Override
  100 + public String toString() {
  101 + return JSONObject.toJSONString(this);
  102 + }
85 } 103 }
@@ -4,10 +4,14 @@ import com.google.common.collect.Lists; @@ -4,10 +4,14 @@ import com.google.common.collect.Lists;
4 import com.yohoufo.common.utils.DateUtil; 4 import com.yohoufo.common.utils.DateUtil;
5 import com.yohoufo.dal.product.PriceTrendHalfYearMapper; 5 import com.yohoufo.dal.product.PriceTrendHalfYearMapper;
6 import com.yohoufo.dal.product.model.PriceTrendModel; 6 import com.yohoufo.dal.product.model.PriceTrendModel;
  7 +import org.apache.commons.collections.CollectionUtils;
7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Service; 9 import org.springframework.stereotype.Service;
9 10
  11 +import java.math.BigDecimal;
10 import java.util.List; 12 import java.util.List;
  13 +import java.util.Objects;
  14 +import java.util.Optional;
11 15
12 /** 16 /**
13 * Created by li.ma on 2019/1/8. 17 * Created by li.ma on 2019/1/8.
@@ -17,9 +21,15 @@ public class PriceTrendHalfYearService implements PriceTrendServiceInf{ @@ -17,9 +21,15 @@ public class PriceTrendHalfYearService implements PriceTrendServiceInf{
17 @Autowired 21 @Autowired
18 private PriceTrendHalfYearMapper priceTrendHalfYearMapper; 22 private PriceTrendHalfYearMapper priceTrendHalfYearMapper;
19 23
  24 + private BigDecimal zeroPrice = new BigDecimal(0);
  25 +
  26 + // 180天(最小颗粒度:3天);
20 public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) { 27 public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) {
21 if (null != productId) { 28 if (null != productId) {
22 - List<PriceTrendModel> priceTrendModels = priceTrendHalfYearMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-180), DateUtil.getTimeSecondOfDay(0)); 29 + int timeSecondOfHalfYear = DateUtil.getTimeSecondOfDay(-180);
  30 + List<PriceTrendModel> priceTrendModels = priceTrendHalfYearMapper.selectByProductId(productId, sizeId, timeSecondOfHalfYear, DateUtil.getTimeSecondOfDay(0));
  31 +
  32 + compeleteZeroPrice(priceTrendModels, timeSecondOfHalfYear, productId, sizeId);
23 33
24 priceTrendModels.stream().forEach(item -> { 34 priceTrendModels.stream().forEach(item -> {
25 item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime()); 35 item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime());
@@ -27,10 +37,31 @@ public class PriceTrendHalfYearService implements PriceTrendServiceInf{ @@ -27,10 +37,31 @@ public class PriceTrendHalfYearService implements PriceTrendServiceInf{
27 }); 37 });
28 38
29 return priceTrendModels; 39 return priceTrendModels;
  40 + }
30 41
  42 + return Lists.newArrayList();
  43 + }
31 44
  45 + //产品紧急变更需求 补全180天的数据,价格用零补充
  46 + private void compeleteZeroPrice(List<PriceTrendModel> priceTrendModels, int timeSecondOfHalfYear, Integer productId, Integer sizeId) {
  47 + if (CollectionUtils.isEmpty(priceTrendModels) || priceTrendModels.size() <= 1) {
  48 + return;
32 } 49 }
33 50
34 - return Lists.newArrayList(); 51 + Integer firstCreateTime = Optional.ofNullable(priceTrendModels.get(0)).map(PriceTrendModel::getCreateTime).orElse(null);
  52 + if (Objects.isNull(firstCreateTime)) { // 有脏数据 不用记日志,查询数据可以看出来
  53 + return;
  54 + }
  55 + List<PriceTrendModel> zeroPriceList = Lists.newArrayList();
  56 + for (int i = firstCreateTime - 259200; i >= timeSecondOfHalfYear; i -= 259200) {
  57 + zeroPriceList.add(new PriceTrendModel(productId, sizeId, zeroPrice, zeroPrice, i));
  58 + }
  59 +
  60 + if (CollectionUtils.isEmpty(zeroPriceList)) {
  61 + return;
  62 + }
  63 +
  64 + List<PriceTrendModel> reverse = Lists.reverse(zeroPriceList);
  65 + reverse.addAll(priceTrendModels);
35 } 66 }
36 } 67 }