| @@ -1,4 +1,4 @@ | |||
| package com.iformall.schedule; | |||
| package com.iformall.config; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.scheduling.annotation.EnableScheduling; | |||
| @@ -8,6 +8,7 @@ import com.iformall.domain.po.*; | |||
| import com.iformall.enums.*; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.mapper.*; | |||
| import com.iformall.service.WxOrderService; | |||
| import com.iformall.service.WxPayOrderService; | |||
| import com.iformall.service.WxProfitSharingOrderService; | |||
| import com.iformall.service.WxRefundOrderService; | |||
| @@ -43,13 +44,25 @@ public class CouponOrderExpiringSchedule { | |||
| private WxAppinfoMapper wxAppinfoMapper; | |||
| @Autowired | |||
| private WxPayOrderMapper wxPayOrderMapper; | |||
| private WxPayOrderMapper wxPayOrderMapper; | |||
| @Autowired | |||
| private WxOrderMapper wxOrderMapper; | |||
| @Autowired | |||
| private WxOrderService wxOrderService; | |||
| @Scheduled(cron = "0 5 0 * * ?") // 每天凌晨00:05 | |||
| //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void couponOrderExpiringSchedule() { | |||
| wxCouponOrderMapper.offExpiredCouponOrderByValidDate(); | |||
| List<WxCouponOrder> list = wxCouponOrderMapper.findExpiredFreeCouponOrderByValidDate(); | |||
| list.stream().forEach(co -> { | |||
| // 券过期 | |||
| couponOrderExpire(co); | |||
| }); | |||
| } | |||
| @Scheduled(cron = "0 5 0 * * ?") // 每天凌晨00:05 | |||
| @@ -58,13 +71,13 @@ public class CouponOrderExpiringSchedule { | |||
| List<WxCouponOrder> list = wxCouponOrderMapper.findExpiredCouponOrderByValidDate(); | |||
| list.parallelStream().forEach(co -> { | |||
| list.stream().forEach(co -> { | |||
| // 券过期与退款分离 | |||
| couponOrderExpire(co); | |||
| // 非储值卡, 可以退款 | |||
| if(!co.getCouponType().equals(EnumCouponType.CARD_MULTIMCH.getCode()) | |||
| && !co.getAutoRefund().equals(EnumCouponAutoRefund.UNSUPPORTED.getCode())) { | |||
| // 非储值卡, 可以退款 | |||
| WxAppinfo appinfo = new WxAppinfo(); | |||
| appinfo.setTenantId(co.getTenantId()); | |||
| appinfo.setType(EnumAppType.B.getCode()); | |||
| @@ -110,18 +123,21 @@ public class CouponOrderExpiringSchedule { | |||
| private void couponOrderExpire(WxCouponOrder co) { | |||
| // 标记 couponOrder过期 | |||
| WxCouponOrder wxCouponOrder = new WxCouponOrder(); | |||
| wxCouponOrder.setId(co.getId()); | |||
| WxCouponOrder updateRecord = new WxCouponOrder(); | |||
| updateRecord.setId(co.getId()); | |||
| if(co.getCouponType().equals(EnumCouponType.CARD_MULTIMCH.getCode())) { | |||
| wxCouponOrder.setCouponOrderStatus(EnumCouponOrderStatus.CARD_OVER_TIME.getCode()); // 卡线下退款 | |||
| updateRecord.setCouponOrderStatus(EnumCouponOrderStatus.CARD_OVER_TIME.getCode()); // 卡线下退款 | |||
| } else { | |||
| wxCouponOrder.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_OVER_TIME.getCode()); //券已过期 | |||
| updateRecord.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_OVER_TIME.getCode()); //券已过期 | |||
| } | |||
| wxCouponOrder.setUpdateDate(new Date()); | |||
| try { | |||
| wxCouponOrderMapper.updateByPrimaryKeySelective(wxCouponOrder); | |||
| } catch (Exception e) { | |||
| logger.error("db failed: couponOrder-" + wxCouponOrder.getId() + ", e:" + e.getMessage()); | |||
| updateRecord.setUpdateDate(new Date()); | |||
| wxCouponOrderMapper.updateByPrimaryKeySelective(updateRecord); | |||
| // 库存退回 | |||
| WxOrder order = wxOrderMapper.selectByPrimaryKey(co.getOrderId()); | |||
| if(order != null) { | |||
| wxOrderService.stockBack(order); | |||
| } | |||
| } | |||
| } | |||
| @@ -48,7 +48,7 @@ public class OrderExpiringSchedule { | |||
| Date curDate = new Date(); | |||
| Date expireDate = new Date(curDate.getTime() - TIME_OUT_VALUE ); | |||
| // 券相关订单 | |||
| // 券相关订单, 15分钟未支付, 订单过期 | |||
| dateMap.put("type", EnumOrderType.COUPON.getCode()); | |||
| dateMap.put("endDate", expireDate); | |||
| List<WxOrder> orderCoupons = wxOrderMapper.findListOfUnpaidOrderByDate(dateMap); | |||
| @@ -56,7 +56,7 @@ public class OrderExpiringSchedule { | |||
| orderExpired(o); | |||
| }); | |||
| // 砍价相关订单 | |||
| // 砍价相关订单, 已超过砍价结束时间, 订单过期 | |||
| dateMap.put("type", EnumOrderType.COUPON.getCode()); | |||
| dateMap.put("endDate", curDate); | |||
| List<WxOrder> orderPresss = wxOrderMapper.findListOfUnpaidPressOrderByDate(dateMap); | |||
| @@ -79,6 +79,9 @@ public class OrderExpiringSchedule { | |||
| updateOrder.setOrderStatus(EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL.getCode()); | |||
| wxOrderMapper.updateByPrimaryKeySelective(updateOrder); | |||
| // 库存退回 | |||
| wxOrderService.stockBack(order); | |||
| // 微信 关单 | |||
| wxOrderService.orderClose(updateOrder); | |||
| @@ -96,6 +99,8 @@ public class OrderExpiringSchedule { | |||
| updateOrder.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PRESS_OVERTIME.getCode()); | |||
| updateOrder.setUpdateDate(curDate); | |||
| wxOrderMapper.updateByPrimaryKeySelective(updateOrder); | |||
| // 库存退回 | |||
| wxOrderService.stockBack(order); | |||
| logger.info("\n砍价订单: " + order.getId() + " create at " + order.getCreateDate() + " expired at " + curDate); | |||
| } | |||
| @@ -1,6 +1,5 @@ | |||
| package com.iformall.schedule; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.domain.po.WxProfitSharingOrder; | |||
| import com.iformall.enums.EnumProfitSharingOrderStatus; | |||
| import com.iformall.mapper.*; | |||
| @@ -24,9 +23,6 @@ public class SharingOrderResultSchedule { | |||
| @Autowired | |||
| private WxProfitSharingOrderMapper wxProfitSharingOrderMapper; | |||
| @Autowired | |||
| private WxPayOrderMapper wxPayOrderMapper; | |||
| @Scheduled(cron = "0 15 0 * * ?") // 每天凌晨00:15更新分账结果 | |||
| //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 | |||
| public void sharingOrderResultSchedule() { | |||
| @@ -61,7 +61,7 @@ public class WxOrder implements Serializable { | |||
| @io.swagger.annotations.ApiModelProperty(value="渠道ID",name="couponChannelId") | |||
| private Long couponChannelId; | |||
| /**产品ID,根据order type**/ | |||
| @io.swagger.annotations.ApiModelProperty(value="产品ID",name="productId") | |||
| @io.swagger.annotations.ApiModelProperty(value="产品ID(type-0:couponId, 1:buserId, 3:merchantId)",name="productId") | |||
| private Long productId; | |||
| /**类型0:券,1:B刷卡支付,2:C扫码支付,3:储值卡**/ | |||
| @io.swagger.annotations.ApiModelProperty(value="类型",name="type") | |||
| @@ -94,7 +94,7 @@ public interface WxCouponOrderMapper extends CommonMapper<WxCouponOrder, Long> { | |||
| void offExpiredCouponOrderByValidDate(); | |||
| List<WxCouponOrder> findExpiredFreeCouponOrderByValidDate(); | |||
| List<WxCouponOrder> findExpiredCouponOrderByValidDate(); | |||
| @@ -243,15 +243,18 @@ public class WxOrderServiceImpl implements WxOrderService { | |||
| try { | |||
| WxCoupon updateRecord = new WxCoupon(); | |||
| updateRecord.setId(coupon.getId()); | |||
| updateRecord.setRemainInventory(coupon.getRemainInventory() - 1); | |||
| updateRecord.setUpdateDate(new Date()); | |||
| // 减库存 | |||
| wxCouponMapper.reduceInventory(coupon.getId(), 1); | |||
| //解锁 | |||
| redisLock.unlock(couponIdStr, timeStr); | |||
| wxCouponMapper.updateByPrimaryKeySelective(updateRecord); | |||
| } catch (RuntimeException e) { | |||
| //解锁 | |||
| redisLock.unlock(couponIdStr, timeStr); | |||
| logger.error("此券减库存失败, couponId: " + couponIdStr); | |||
| throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | |||
| } finally { | |||
| //解锁 | |||
| redisLock.unlock(couponIdStr, timeStr); | |||
| } | |||
| } | |||
| @@ -281,10 +284,13 @@ public class WxOrderServiceImpl implements WxOrderService { | |||
| throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); | |||
| } | |||
| coupon.setRemainInventory(coupon.getRemainInventory() + 1); | |||
| WxCoupon updateRecord = new WxCoupon(); | |||
| updateRecord.setId(coupon.getId()); | |||
| updateRecord.setRemainInventory(coupon.getRemainInventory() + 1); | |||
| updateRecord.setUpdateDate(new Date()); | |||
| try { | |||
| wxCouponMapper.updateByPrimaryKeySelective(coupon); | |||
| wxCouponMapper.updateByPrimaryKeySelective(updateRecord); | |||
| } catch (Exception e) { | |||
| logger.error("数据库更新失败,库存+1失败, e:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "库存恢复失败"); | |||
| @@ -254,9 +254,9 @@ | |||
| where tenant_id=#{tenantId} AND create_date >= #{startTime} and create_date < #{endTime} | |||
| </select> | |||
| <update id="offExpiredCouponOrderByValidDate"> | |||
| update wx_coupon_order SET coupon_order_status = 2, update_date = now() | |||
| where expired_time < now() and coupon_price = 0 and coupon_order_status = 0 | |||
| <update id="findExpiredFreeCouponOrderByValidDate"> | |||
| select <include refid="allColumns" /> from wx_coupon_order | |||
| where expired_time < now() and coupon_price = 0 and coupon_order_status = 0 | |||
| </update> | |||
| <select id="findExpiredCouponOrderByValidDate" parameterType="com.iformall.domain.po.WxCouponOrder" resultMap="BaseResultMap"> | |||