Authored by chenchao

fix and optimized

@@ -31,7 +31,7 @@ @@ -31,7 +31,7 @@
31 where status = #{status,jdbcType=INTEGER} 31 where status = #{status,jdbcType=INTEGER}
32 and attributes = #{attributes,jdbcType=INTEGER} 32 and attributes = #{attributes,jdbcType=INTEGER}
33 <if test="tailId !=null"> and id &gt; #{tailId,jdbcType=INTEGER}</if> 33 <if test="tailId !=null"> and id &gt; #{tailId,jdbcType=INTEGER}</if>
34 - order by id limit {offset} 34 + order by id limit #{offset}
35 </select> 35 </select>
36 36
37 <select id="selectAllCntByAttr" resultType="java.lang.Integer"> 37 <select id="selectAllCntByAttr" resultType="java.lang.Integer">
  1 +package com.yohoufo.order.model.dto;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Data;
  5 +import lombok.NoArgsConstructor;
  6 +import lombok.experimental.Builder;
  7 +
  8 +import java.util.concurrent.TimeUnit;
  9 +
  10 +/**
  11 + * Created by chao.chen on 2019/2/19.
  12 + */
  13 +@Data
  14 +@Builder
  15 +@AllArgsConstructor
  16 +@NoArgsConstructor
  17 +public class PreSaleOrderConfig {
  18 +
  19 + @Builder
  20 + @Data
  21 + public static class Range{
  22 + private int min;
  23 + private int max;
  24 + TimeUnit timeUnit = TimeUnit.DAYS;
  25 + }
  26 +
  27 + private Range noticeRange;
  28 +
  29 + private Range autoCancelRange;
  30 +
  31 +
  32 + public static PreSaleOrderConfig getDefault(){
  33 + PreSaleOrderConfig psoc = new PreSaleOrderConfig();
  34 + Range noticeRange = Range.builder().min(31).max(34).build();
  35 + psoc.noticeRange = noticeRange;
  36 + Range autoCancelRange = Range.builder().min(35).build();
  37 + psoc.autoCancelRange = autoCancelRange;
  38 + return psoc;
  39 + }
  40 +}
@@ -11,6 +11,7 @@ import com.yohoufo.dal.order.MetaConfigMapper; @@ -11,6 +11,7 @@ import com.yohoufo.dal.order.MetaConfigMapper;
11 import com.yohoufo.dal.order.model.MetaConfig; 11 import com.yohoufo.dal.order.model.MetaConfig;
12 import com.yohoufo.order.model.dto.BuyerPenalty; 12 import com.yohoufo.order.model.dto.BuyerPenalty;
13 import com.yohoufo.order.model.dto.EarnestMoney; 13 import com.yohoufo.order.model.dto.EarnestMoney;
  14 +import com.yohoufo.order.model.dto.PreSaleOrderConfig;
14 import com.yohoufo.order.service.cache.CacheKeyBuilder; 15 import com.yohoufo.order.service.cache.CacheKeyBuilder;
15 import com.yohoufo.order.service.cache.ExpiredTime; 16 import com.yohoufo.order.service.cache.ExpiredTime;
16 import org.apache.commons.lang3.StringUtils; 17 import org.apache.commons.lang3.StringUtils;
@@ -23,6 +24,7 @@ import java.math.BigDecimal; @@ -23,6 +24,7 @@ import java.math.BigDecimal;
23 import java.util.HashMap; 24 import java.util.HashMap;
24 import java.util.Map; 25 import java.util.Map;
25 import java.util.Objects; 26 import java.util.Objects;
  27 +import java.util.concurrent.TimeUnit;
26 28
27 /** 29 /**
28 * Created by chao.chen on 2019/1/3. 30 * Created by chao.chen on 2019/1/3.
@@ -184,4 +186,38 @@ public class MetaConfigService { @@ -184,4 +186,38 @@ public class MetaConfigService {
184 return sellerPenalty; 186 return sellerPenalty;
185 } 187 }
186 188
  189 + /**
  190 + * 预售订单阈值
  191 + * {"notice":{"timeRange":{"min":31,"max":34}},"autoCancel":{"timeRange":{"min":35}}}
  192 + * @return
  193 + */
  194 + public PreSaleOrderConfig getPreSaleOrderConfig(){
  195 + String key = MetaConfigKey.PRESALE_THRESHOLD;
  196 + MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
  197 + String metaVal = metaConfig.getValue();
  198 + PreSaleOrderConfig psoc;
  199 + try {
  200 + JSONObject psocJO = JSONObject.parseObject(metaVal);
  201 + JSONObject ntrJO = psocJO.getJSONObject("notice").getJSONObject("timeRange");
  202 + TimeUnit ntrtu = "h".equalsIgnoreCase(ntrJO.getString("timeUnit")) ? TimeUnit.HOURS : TimeUnit.DAYS;
  203 + PreSaleOrderConfig.Range ntr = PreSaleOrderConfig.Range.builder()
  204 + .min(ntrJO.getIntValue("min"))
  205 + .timeUnit(ntrtu)
  206 + .max(ntrJO.getIntValue("max")).build();
  207 + //
  208 + JSONObject actrJO = psocJO.getJSONObject("autoCancel").getJSONObject("timeRange");
  209 + TimeUnit actrtu = "h".equalsIgnoreCase(actrJO.getString("timeUnit")) ? TimeUnit.HOURS : TimeUnit.DAYS;
  210 + PreSaleOrderConfig.Range actr = PreSaleOrderConfig.Range.builder()
  211 + .min(actrJO.getIntValue("min"))
  212 + .timeUnit(actrtu)
  213 + .build();
  214 + psoc = PreSaleOrderConfig.builder().autoCancelRange(actr)
  215 + .noticeRange(ntr).build();
  216 + }catch (Exception ex){
  217 + logger.warn("getPreSaleOrderConfig fail ,use default in Process self");
  218 + psoc = PreSaleOrderConfig.getDefault();
  219 + }
  220 + return psoc;
  221 + }
  222 +
187 } 223 }
@@ -7,7 +7,6 @@ import com.yoho.quartz.domain.JobProcessResult; @@ -7,7 +7,6 @@ import com.yoho.quartz.domain.JobProcessResult;
7 import com.yoho.quartz.job.YhJob; 7 import com.yoho.quartz.job.YhJob;
8 import com.yohobuy.ufo.model.order.common.OrderAttributes; 8 import com.yohobuy.ufo.model.order.common.OrderAttributes;
9 import com.yohobuy.ufo.model.order.common.OrderStatus; 9 import com.yohobuy.ufo.model.order.common.OrderStatus;
10 -import com.yohobuy.ufo.model.order.constants.ConfirmDesc;  
11 import com.yohoufo.common.utils.DateUtil; 10 import com.yohoufo.common.utils.DateUtil;
12 import com.yohoufo.common.utils.PageHelper; 11 import com.yohoufo.common.utils.PageHelper;
13 import com.yohoufo.dal.order.BuyerOrderGoodsMapper; 12 import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
@@ -19,8 +18,10 @@ import com.yohoufo.dal.order.model.BuyerOrderGoods; @@ -19,8 +18,10 @@ import com.yohoufo.dal.order.model.BuyerOrderGoods;
19 import com.yohoufo.dal.order.model.OrdersPay; 18 import com.yohoufo.dal.order.model.OrdersPay;
20 import com.yohoufo.dal.order.model.SellerOrderGoods; 19 import com.yohoufo.dal.order.model.SellerOrderGoods;
21 import com.yohoufo.order.model.dto.DTNode; 20 import com.yohoufo.order.model.dto.DTNode;
  21 +import com.yohoufo.order.model.dto.PreSaleOrderConfig;
22 import com.yohoufo.order.service.cache.OrderCacheService; 22 import com.yohoufo.order.service.cache.OrderCacheService;
23 import com.yohoufo.order.service.impl.BuyerOrderCancelService; 23 import com.yohoufo.order.service.impl.BuyerOrderCancelService;
  24 +import com.yohoufo.order.service.impl.MetaConfigService;
24 import com.yohoufo.order.service.proxy.InBoxFacade; 25 import com.yohoufo.order.service.proxy.InBoxFacade;
25 import com.yohoufo.order.utils.LoggerUtils; 26 import com.yohoufo.order.utils.LoggerUtils;
26 import lombok.experimental.Builder; 27 import lombok.experimental.Builder;
@@ -30,6 +31,7 @@ import org.springframework.stereotype.Service; @@ -30,6 +31,7 @@ import org.springframework.stereotype.Service;
30 31
31 import java.math.BigDecimal; 32 import java.math.BigDecimal;
32 import java.util.*; 33 import java.util.*;
  34 +import java.util.concurrent.TimeUnit;
33 import java.util.function.Function; 35 import java.util.function.Function;
34 import java.util.stream.Collectors; 36 import java.util.stream.Collectors;
35 37
@@ -64,6 +66,9 @@ public class PreSaleOrderJob implements YhJob { @@ -64,6 +66,9 @@ public class PreSaleOrderJob implements YhJob {
64 @Autowired 66 @Autowired
65 private InBoxFacade inBoxFacade; 67 private InBoxFacade inBoxFacade;
66 68
  69 + @Autowired
  70 + private MetaConfigService metaConfigService;
  71 +
67 72
68 private static final long CACHE_EXPIRED_TIME = 24*3600L; 73 private static final long CACHE_EXPIRED_TIME = 24*3600L;
69 74
@@ -77,6 +82,7 @@ public class PreSaleOrderJob implements YhJob { @@ -77,6 +82,7 @@ public class PreSaleOrderJob implements YhJob {
77 int cnt = buyerOrderViewMapper.selectAllCntByAttr(status, oa); 82 int cnt = buyerOrderViewMapper.selectAllCntByAttr(status, oa);
78 83
79 if (cnt==0){ 84 if (cnt==0){
  85 + logger.info("in PreSaleOrderJob.process no paid pre-sale order");
80 return null; 86 return null;
81 } 87 }
82 final int pageSize = 10; 88 final int pageSize = 10;
@@ -85,20 +91,22 @@ public class PreSaleOrderJob implements YhJob { @@ -85,20 +91,22 @@ public class PreSaleOrderJob implements YhJob {
85 Integer lastId = null; 91 Integer lastId = null;
86 92
87 List<BuyerOrder> bos; 93 List<BuyerOrder> bos;
88 -  
89 //TODO 第35天 自动超时取消 94 //TODO 第35天 自动超时取消
90 -  
91 // 查询条件 预售 + 支付完成(待卖家发货)+ 支付时间到当前时间的差值在(0,35] 单位:天 95 // 查询条件 预售 + 支付完成(待卖家发货)+ 支付时间到当前时间的差值在(0,35] 单位:天
92 -  
93 //TODO 第31天开始 到34天,通知剩余发货时间 96 //TODO 第31天开始 到34天,通知剩余发货时间
94 List<BuyerOrder> over35DaysBOs = new ArrayList<>(cnt); 97 List<BuyerOrder> over35DaysBOs = new ArrayList<>(cnt);
95 List<BuyerOrderWrapper> from30To34BOs = new ArrayList<>(cnt); 98 List<BuyerOrderWrapper> from30To34BOs = new ArrayList<>(cnt);
96 final int currentSencond = DateUtil.getCurrentTimeSecond(); 99 final int currentSencond = DateUtil.getCurrentTimeSecond();
97 - 100 + PreSaleOrderConfig psoc = metaConfigService.getPreSaleOrderConfig();
  101 + PreSaleOrderConfig.Range cancelRange = psoc.getAutoCancelRange(),
  102 + noticeRange = psoc.getNoticeRange();
  103 + int minCancelTR;
  104 + final int secondsOfCancelDays = secondsFromTimeRange(minCancelTR=cancelRange.getMin(), cancelRange.getTimeUnit()),
  105 + secondsOfNoticeDays = secondsFromTimeRange(noticeRange.getMin(), noticeRange.getTimeUnit());
98 for(int i=0; i<pageTotal; i++){ 106 for(int i=0; i<pageTotal; i++){
99 bos = buyerOrderViewMapper.selectAllByAttr(status, oa, lastId, pageSize); 107 bos = buyerOrderViewMapper.selectAllByAttr(status, oa, lastId, pageSize);
100 if (Objects.nonNull(bos)){ 108 if (Objects.nonNull(bos)){
101 - Node node = processSub(bos, currentSencond); 109 + Node node = processSub(bos, currentSencond, secondsOfCancelDays, secondsOfNoticeDays);
102 over35DaysBOs.addAll(node.over35DaysBOs); 110 over35DaysBOs.addAll(node.over35DaysBOs);
103 from30To34BOs.addAll(node.from30To34BOs); 111 from30To34BOs.addAll(node.from30To34BOs);
104 lastId = node.lastId; 112 lastId = node.lastId;
@@ -121,9 +129,9 @@ public class PreSaleOrderJob implements YhJob { @@ -121,9 +129,9 @@ public class PreSaleOrderJob implements YhJob {
121 Integer times = orderCacheService.getOrderDeliverNoticeTimes(orderCode, nowDateStr); 129 Integer times = orderCacheService.getOrderDeliverNoticeTimes(orderCode, nowDateStr);
122 if (times==null){ 130 if (times==null){
123 //todo 发送通知 131 //todo 发送通知
124 - int leftTime = 35*3600 - needNoticBO.diffSeconds; 132 + int leftTime = calLeftTime(minCancelTR ,needNoticBO.diffSeconds);
125 DTNode node = DTNode.calBySeconds(leftTime); 133 DTNode node = DTNode.calBySeconds(leftTime);
126 - //todo 35天走配置 134 + //35天走配置
127 int leftHours = node.days * 24 + node.hours + node.minutes/30; 135 int leftHours = node.days * 24 + node.hours + node.minutes/30;
128 BuyerOrderGoods bog = buyerOrderGoodsMapper.selectOnlyByOrderCode(orderCode); 136 BuyerOrderGoods bog = buyerOrderGoodsMapper.selectOnlyByOrderCode(orderCode);
129 SellerOrderGoods sog = sellerOrderGoodsMapper.selectByPrimaryKey(bog.getSkup()); 137 SellerOrderGoods sog = sellerOrderGoodsMapper.selectByPrimaryKey(bog.getSkup());
@@ -138,10 +146,18 @@ public class PreSaleOrderJob implements YhJob { @@ -138,10 +146,18 @@ public class PreSaleOrderJob implements YhJob {
138 return null; 146 return null;
139 } 147 }
140 148
  149 + private int calLeftTime(int minCancelTR, int diffSeconds){
  150 + int leftTime = minCancelTR*24*3600 - diffSeconds;
  151 + if (leftTime<0){
  152 + leftTime = new BigDecimal(leftTime).abs().intValue();
  153 + }
  154 + return leftTime;
  155 + }
141 156
142 - static final int secondsOf35Days = secondsOfDays(35), secondsOf31Days = secondsOfDays(31);  
143 157
144 - Node processSub(List<BuyerOrder> bos, int currentSencond){ 158 +
  159 +
  160 + Node processSub(List<BuyerOrder> bos, int currentSencond, int secondsOfCancelDays, int secondsOfNoticeDays){
145 BuyerOrder lastBO = null; 161 BuyerOrder lastBO = null;
146 Node node = new Node(bos.size()); 162 Node node = new Node(bos.size());
147 List<BuyerOrder> over35DaysBOs = node.over35DaysBOs; 163 List<BuyerOrder> over35DaysBOs = node.over35DaysBOs;
@@ -157,11 +173,11 @@ public class PreSaleOrderJob implements YhJob { @@ -157,11 +173,11 @@ public class PreSaleOrderJob implements YhJob {
157 Integer payTime = op.getCreateTime(); 173 Integer payTime = op.getCreateTime();
158 int diff = currentSencond-payTime; 174 int diff = currentSencond-payTime;
159 //超过35天 175 //超过35天
160 - if (diff>=secondsOf35Days){ 176 + if (diff>=secondsOfCancelDays){
161 over35DaysBOs.add(orderCodes2BoMap.get(op.getOrderCode())); 177 over35DaysBOs.add(orderCodes2BoMap.get(op.getOrderCode()));
162 }else { 178 }else {
163 //超过30天(第31天),不到35天 179 //超过30天(第31天),不到35天
164 - if (diff>=secondsOf31Days) { 180 + if (diff>=secondsOfNoticeDays) {
165 BuyerOrder buyerOrder = orderCodes2BoMap.get(op.getOrderCode()); 181 BuyerOrder buyerOrder = orderCodes2BoMap.get(op.getOrderCode());
166 BuyerOrderWrapper bow = BuyerOrderWrapper.builder().buyerOrder(buyerOrder) 182 BuyerOrderWrapper bow = BuyerOrderWrapper.builder().buyerOrder(buyerOrder)
167 .diffSeconds(diff).build(); 183 .diffSeconds(diff).build();
@@ -188,6 +204,23 @@ public class PreSaleOrderJob implements YhJob { @@ -188,6 +204,23 @@ public class PreSaleOrderJob implements YhJob {
188 } 204 }
189 } 205 }
190 206
  207 + private static int secondsFromTimeRange(int time, TimeUnit timeUnit){
  208 + int seconds = 0;
  209 + switch (timeUnit){
  210 + case DAYS:
  211 + seconds = secondsOfDays(time);
  212 + break;
  213 + case HOURS:
  214 + seconds = secondsOfHours(time);
  215 + break;
  216 + }
  217 + return seconds;
  218 + }
  219 +
  220 + private static int secondsOfHours(int hours){
  221 + return hours * 3600;
  222 + }
  223 +
191 private static int secondsOfDays(int days){ 224 private static int secondsOfDays(int days){
192 return days * 24 * 3600; 225 return days * 24 * 3600;
193 } 226 }