Commit 7596697d by wuwenlong

unpay order aotuchancel task dev;

parent 2357c337
......@@ -35,6 +35,10 @@
<artifactId>share-common</artifactId>
</dependency>
<dependency>
<groupId>share</groupId>
<artifactId>share-system</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package share.quartz.task;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;
import share.common.constant.Constants;
import share.common.core.redis.RedisUtil;
import share.common.enums.CouponStatusEnum;
import share.common.enums.YesNoEnum;
import share.common.exception.base.BaseException;
import share.system.domain.SConsumerCoupon;
import share.system.domain.SOrder;
import share.system.service.ISConsumerCouponService;
import share.system.service.ISOrderService;
import share.system.service.ISysConfigService;
/**
* @Author wwl
* @Date 2023/10/25 13:48
*/
@Component("orderTask")
public class OrderTask {
private static final Logger logger = LoggerFactory.getLogger(OrderTask.class);
@Autowired
private RedisUtil redisUtil;
@Autowired
private ISysConfigService sysConfigService;
@Autowired
private ISOrderService orderService;
@Autowired
private ISConsumerCouponService consumerCouponService;
@Autowired
private TransactionTemplate transactionTemplate;
public void autoCancel() {
String redisKey = Constants.ORDER_AUTO_CANCEL_KEY;
Long size = redisUtil.getListSize(redisKey);
logger.info("orderTask.autoCancel | size:" + size);
if (size < 1) {
return;
}
for (int i = 0; i < size; i++) {
//如果10秒钟拿不到一个数据,那么退出循环
Object data = redisUtil.getRightPop(redisKey, 10L);
if (null == data) {
continue;
}
try {
SOrder sOrder = orderService.getByOrderNo(String.valueOf(data));
if (ObjectUtil.isNull(sOrder)) {
logger.error("orderTask.autoCancel | 订单不存在,orderNo: " + data);
throw new BaseException("订单不存在,orderNo: " + data);
}
boolean result = autoCancel(sOrder);
if (!result) {
redisUtil.lPush(redisKey, data);
}
} catch (Exception e) {
e.printStackTrace();
redisUtil.lPush(redisKey, data);
}
}
}
public Boolean autoCancel(SOrder sOrder) {
// 判断订单是否支付
if (YesNoEnum.yes.getIndex().equals(sOrder.getPayStatus())) {
return Boolean.TRUE;
}
if (YesNoEnum.yes.getIndex().equals(sOrder.getIsDelete())) {
return Boolean.TRUE;
}
// 获取过期时间
String cancelStr;
DateTime cancelTime;
cancelStr = sysConfigService.selectConfigByKey("order_cancel_time");
if (StrUtil.isBlank(cancelStr)) {
cancelStr = "1";
}
cancelTime = cn.hutool.core.date.DateUtil.offset(sOrder.getCreateTime(), DateField.HOUR_OF_DAY, Integer.parseInt(cancelStr));
long between = cn.hutool.core.date.DateUtil.between(cancelTime, cn.hutool.core.date.DateUtil.date(), DateUnit.SECOND, false);
if (between < 0) {// 未到过期时间继续循环
return Boolean.FALSE;
}
sOrder.setIsDelete(YesNoEnum.yes.getIndex());
Boolean execute = false;
execute = transactionTemplate.execute(e -> {
orderService.updateById(sOrder);
// 退优惠券
if (sOrder.getCouponId() > 0L ) {
SConsumerCoupon couponUser = consumerCouponService.getById(sOrder.getCouponId());
couponUser.setUseStatus(CouponStatusEnum.NORMAL.getValue());
consumerCouponService.updateById(couponUser);
}
return Boolean.TRUE;
});
return execute;
}
}
......@@ -105,7 +105,7 @@ public class SConsumerCoupon extends BaseEntity
/** 使用状态(0:待使用,1:已使用,2:已失效) */
@Excel(name = "使用状态(0:待使用,1:已使用,2:已失效)")
private String useStatus;
private Integer useStatus;
/** 删除状态(0:未删除,1:已删除) */
//逻辑删除注解(0 未删除 1 已删除)
......
......@@ -72,7 +72,7 @@ public class SOrder extends BaseEntity
/** 优惠券id */
@Excel(name = "优惠券id")
private String couponId;
private Long couponId;
@Excel(name = "优惠券金额")
private BigDecimal couponPrice;
......
......@@ -89,4 +89,11 @@ public interface ISOrderService extends IService<SOrder>
* @return ComputedOrderPriceResponse
*/
ComputedOrderPriceResponse computedOrderPrice(OrderComputedPriceRequest request);
/**
* 通过订单编号查询订单
* @param valueOf
* @return
*/
SOrder getByOrderNo(String orderNo);
}
......@@ -11,6 +11,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -261,6 +262,13 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper,SOrder> implemen
return computedPrice(request, user);
}
@Override
public SOrder getByOrderNo(String orderNo) {
LambdaQueryWrapper<SOrder> lqw = Wrappers.lambdaQuery();
lqw.eq(SOrder::getOrderNo, orderNo);
return getOne(lqw);
}
private BigDecimal computeTotalPrice(BigDecimal unitPrice, Date startTime, Date endTime){
return DateUtils.differentHour(startTime,endTime).multiply(unitPrice);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment