Authored by chenchao

fix and optimized

... ... @@ -31,7 +31,7 @@
where status = #{status,jdbcType=INTEGER}
and attributes = #{attributes,jdbcType=INTEGER}
<if test="tailId !=null"> and id &gt; #{tailId,jdbcType=INTEGER}</if>
order by id limit {offset}
order by id limit #{offset}
</select>
<select id="selectAllCntByAttr" resultType="java.lang.Integer">
... ...
package com.yohoufo.order.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Builder;
import java.util.concurrent.TimeUnit;
/**
* Created by chao.chen on 2019/2/19.
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PreSaleOrderConfig {
@Builder
@Data
public static class Range{
private int min;
private int max;
TimeUnit timeUnit = TimeUnit.DAYS;
}
private Range noticeRange;
private Range autoCancelRange;
public static PreSaleOrderConfig getDefault(){
PreSaleOrderConfig psoc = new PreSaleOrderConfig();
Range noticeRange = Range.builder().min(31).max(34).build();
psoc.noticeRange = noticeRange;
Range autoCancelRange = Range.builder().min(35).build();
psoc.autoCancelRange = autoCancelRange;
return psoc;
}
}
... ...
... ... @@ -11,6 +11,7 @@ import com.yohoufo.dal.order.MetaConfigMapper;
import com.yohoufo.dal.order.model.MetaConfig;
import com.yohoufo.order.model.dto.BuyerPenalty;
import com.yohoufo.order.model.dto.EarnestMoney;
import com.yohoufo.order.model.dto.PreSaleOrderConfig;
import com.yohoufo.order.service.cache.CacheKeyBuilder;
import com.yohoufo.order.service.cache.ExpiredTime;
import org.apache.commons.lang3.StringUtils;
... ... @@ -23,6 +24,7 @@ import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* Created by chao.chen on 2019/1/3.
... ... @@ -184,4 +186,38 @@ public class MetaConfigService {
return sellerPenalty;
}
/**
* 预售订单阈值
* {"notice":{"timeRange":{"min":31,"max":34}},"autoCancel":{"timeRange":{"min":35}}}
* @return
*/
public PreSaleOrderConfig getPreSaleOrderConfig(){
String key = MetaConfigKey.PRESALE_THRESHOLD;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
String metaVal = metaConfig.getValue();
PreSaleOrderConfig psoc;
try {
JSONObject psocJO = JSONObject.parseObject(metaVal);
JSONObject ntrJO = psocJO.getJSONObject("notice").getJSONObject("timeRange");
TimeUnit ntrtu = "h".equalsIgnoreCase(ntrJO.getString("timeUnit")) ? TimeUnit.HOURS : TimeUnit.DAYS;
PreSaleOrderConfig.Range ntr = PreSaleOrderConfig.Range.builder()
.min(ntrJO.getIntValue("min"))
.timeUnit(ntrtu)
.max(ntrJO.getIntValue("max")).build();
//
JSONObject actrJO = psocJO.getJSONObject("autoCancel").getJSONObject("timeRange");
TimeUnit actrtu = "h".equalsIgnoreCase(actrJO.getString("timeUnit")) ? TimeUnit.HOURS : TimeUnit.DAYS;
PreSaleOrderConfig.Range actr = PreSaleOrderConfig.Range.builder()
.min(actrJO.getIntValue("min"))
.timeUnit(actrtu)
.build();
psoc = PreSaleOrderConfig.builder().autoCancelRange(actr)
.noticeRange(ntr).build();
}catch (Exception ex){
logger.warn("getPreSaleOrderConfig fail ,use default in Process self");
psoc = PreSaleOrderConfig.getDefault();
}
return psoc;
}
}
... ...
... ... @@ -7,7 +7,6 @@ import com.yoho.quartz.domain.JobProcessResult;
import com.yoho.quartz.job.YhJob;
import com.yohobuy.ufo.model.order.common.OrderAttributes;
import com.yohobuy.ufo.model.order.common.OrderStatus;
import com.yohobuy.ufo.model.order.constants.ConfirmDesc;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.common.utils.PageHelper;
import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
... ... @@ -19,8 +18,10 @@ import com.yohoufo.dal.order.model.BuyerOrderGoods;
import com.yohoufo.dal.order.model.OrdersPay;
import com.yohoufo.dal.order.model.SellerOrderGoods;
import com.yohoufo.order.model.dto.DTNode;
import com.yohoufo.order.model.dto.PreSaleOrderConfig;
import com.yohoufo.order.service.cache.OrderCacheService;
import com.yohoufo.order.service.impl.BuyerOrderCancelService;
import com.yohoufo.order.service.impl.MetaConfigService;
import com.yohoufo.order.service.proxy.InBoxFacade;
import com.yohoufo.order.utils.LoggerUtils;
import lombok.experimental.Builder;
... ... @@ -30,6 +31,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
... ... @@ -64,6 +66,9 @@ public class PreSaleOrderJob implements YhJob {
@Autowired
private InBoxFacade inBoxFacade;
@Autowired
private MetaConfigService metaConfigService;
private static final long CACHE_EXPIRED_TIME = 24*3600L;
... ... @@ -77,6 +82,7 @@ public class PreSaleOrderJob implements YhJob {
int cnt = buyerOrderViewMapper.selectAllCntByAttr(status, oa);
if (cnt==0){
logger.info("in PreSaleOrderJob.process no paid pre-sale order");
return null;
}
final int pageSize = 10;
... ... @@ -85,20 +91,22 @@ public class PreSaleOrderJob implements YhJob {
Integer lastId = null;
List<BuyerOrder> bos;
//TODO 第35天 自动超时取消
// 查询条件 预售 + 支付完成(待卖家发货)+ 支付时间到当前时间的差值在(0,35] 单位:天
//TODO 第31天开始 到34天,通知剩余发货时间
List<BuyerOrder> over35DaysBOs = new ArrayList<>(cnt);
List<BuyerOrderWrapper> from30To34BOs = new ArrayList<>(cnt);
final int currentSencond = DateUtil.getCurrentTimeSecond();
PreSaleOrderConfig psoc = metaConfigService.getPreSaleOrderConfig();
PreSaleOrderConfig.Range cancelRange = psoc.getAutoCancelRange(),
noticeRange = psoc.getNoticeRange();
int minCancelTR;
final int secondsOfCancelDays = secondsFromTimeRange(minCancelTR=cancelRange.getMin(), cancelRange.getTimeUnit()),
secondsOfNoticeDays = secondsFromTimeRange(noticeRange.getMin(), noticeRange.getTimeUnit());
for(int i=0; i<pageTotal; i++){
bos = buyerOrderViewMapper.selectAllByAttr(status, oa, lastId, pageSize);
if (Objects.nonNull(bos)){
Node node = processSub(bos, currentSencond);
Node node = processSub(bos, currentSencond, secondsOfCancelDays, secondsOfNoticeDays);
over35DaysBOs.addAll(node.over35DaysBOs);
from30To34BOs.addAll(node.from30To34BOs);
lastId = node.lastId;
... ... @@ -121,9 +129,9 @@ public class PreSaleOrderJob implements YhJob {
Integer times = orderCacheService.getOrderDeliverNoticeTimes(orderCode, nowDateStr);
if (times==null){
//todo 发送通知
int leftTime = 35*3600 - needNoticBO.diffSeconds;
int leftTime = calLeftTime(minCancelTR ,needNoticBO.diffSeconds);
DTNode node = DTNode.calBySeconds(leftTime);
//todo 35天走配置
//35天走配置
int leftHours = node.days * 24 + node.hours + node.minutes/30;
BuyerOrderGoods bog = buyerOrderGoodsMapper.selectOnlyByOrderCode(orderCode);
SellerOrderGoods sog = sellerOrderGoodsMapper.selectByPrimaryKey(bog.getSkup());
... ... @@ -138,10 +146,18 @@ public class PreSaleOrderJob implements YhJob {
return null;
}
private int calLeftTime(int minCancelTR, int diffSeconds){
int leftTime = minCancelTR*24*3600 - diffSeconds;
if (leftTime<0){
leftTime = new BigDecimal(leftTime).abs().intValue();
}
return leftTime;
}
static final int secondsOf35Days = secondsOfDays(35), secondsOf31Days = secondsOfDays(31);
Node processSub(List<BuyerOrder> bos, int currentSencond){
Node processSub(List<BuyerOrder> bos, int currentSencond, int secondsOfCancelDays, int secondsOfNoticeDays){
BuyerOrder lastBO = null;
Node node = new Node(bos.size());
List<BuyerOrder> over35DaysBOs = node.over35DaysBOs;
... ... @@ -157,11 +173,11 @@ public class PreSaleOrderJob implements YhJob {
Integer payTime = op.getCreateTime();
int diff = currentSencond-payTime;
//超过35天
if (diff>=secondsOf35Days){
if (diff>=secondsOfCancelDays){
over35DaysBOs.add(orderCodes2BoMap.get(op.getOrderCode()));
}else {
//超过30天(第31天),不到35天
if (diff>=secondsOf31Days) {
if (diff>=secondsOfNoticeDays) {
BuyerOrder buyerOrder = orderCodes2BoMap.get(op.getOrderCode());
BuyerOrderWrapper bow = BuyerOrderWrapper.builder().buyerOrder(buyerOrder)
.diffSeconds(diff).build();
... ... @@ -188,6 +204,23 @@ public class PreSaleOrderJob implements YhJob {
}
}
private static int secondsFromTimeRange(int time, TimeUnit timeUnit){
int seconds = 0;
switch (timeUnit){
case DAYS:
seconds = secondsOfDays(time);
break;
case HOURS:
seconds = secondsOfHours(time);
break;
}
return seconds;
}
private static int secondsOfHours(int hours){
return hours * 3600;
}
private static int secondsOfDays(int days){
return days * 24 * 3600;
}
... ...