| @@ -1,9 +1,12 @@ | |||||
| package com.iformall.schedule; | package com.iformall.schedule; | ||||
| import com.iformall.domain.po.WxAppinfo; | |||||
| import com.iformall.domain.po.WxOrder; | import com.iformall.domain.po.WxOrder; | ||||
| import com.iformall.domain.po.WxOrderGroup; | import com.iformall.domain.po.WxOrderGroup; | ||||
| import com.iformall.enums.EnumAppType; | |||||
| import com.iformall.enums.EnumOrderStatus; | import com.iformall.enums.EnumOrderStatus; | ||||
| import com.iformall.enums.EnumOrderType; | import com.iformall.enums.EnumOrderType; | ||||
| import com.iformall.mapper.WxAppinfoMapper; | |||||
| import com.iformall.mapper.WxOrderGroupMapper; | import com.iformall.mapper.WxOrderGroupMapper; | ||||
| import com.iformall.mapper.WxOrderMapper; | import com.iformall.mapper.WxOrderMapper; | ||||
| import com.iformall.service.WxOrderGroupService; | import com.iformall.service.WxOrderGroupService; | ||||
| @@ -39,6 +42,11 @@ public class OrderExpiringSchedule { | |||||
| @Autowired | @Autowired | ||||
| WxOrderGroupService wxOrderGroupService; | WxOrderGroupService wxOrderGroupService; | ||||
| @Autowired | |||||
| WxAppinfoMapper wxAppinfoMapper; | |||||
| @Autowired | |||||
| com.iformall.service.WxRefundOrderService WxRefundOrderService; | |||||
| @Scheduled(cron = "0 */5 * * * *?") // 每5分钟检查一次 | @Scheduled(cron = "0 */5 * * * *?") // 每5分钟检查一次 | ||||
| //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 | //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 | ||||
| @@ -72,7 +80,7 @@ public class OrderExpiringSchedule { | |||||
| } | } | ||||
| }); | }); | ||||
| //拼团 | |||||
| //拼团 过期与失败 | |||||
| List<WxOrderGroup> orderGroups = wxOrderGroupService.findList(null); | List<WxOrderGroup> orderGroups = wxOrderGroupService.findList(null); | ||||
| orderGroups.stream().forEach(g -> { | orderGroups.stream().forEach(g -> { | ||||
| try { | try { | ||||
| @@ -81,6 +89,19 @@ public class OrderExpiringSchedule { | |||||
| logger.error(e.getMessage()); | logger.error(e.getMessage()); | ||||
| } | } | ||||
| }); | }); | ||||
| //参团失败 | |||||
| WxOrder order = new WxOrder(); | |||||
| order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_FAILD.getCode()); | |||||
| wxOrderMapper.select(order).parallelStream().forEach(o -> { | |||||
| try { | |||||
| returnMoney(o); | |||||
| } catch (Exception e) { | |||||
| logger.error("拼团订单退款失败" + e.getMessage()); | |||||
| } | |||||
| }); | |||||
| } | } | ||||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | ||||
| @@ -117,4 +138,16 @@ public class OrderExpiringSchedule { | |||||
| logger.info("\n砍价订单: " + order.getId() + " create at " + order.getCreateDate() + " expired at " + curDate); | logger.info("\n砍价订单: " + order.getId() + " create at " + order.getCreateDate() + " expired at " + curDate); | ||||
| } | } | ||||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||||
| public void returnMoney(WxOrder order) { | |||||
| //获取APP信息 | |||||
| WxAppinfo appinfo = new WxAppinfo(); | |||||
| appinfo.setTenantId(order.getTenantId()); | |||||
| appinfo.setType(EnumAppType.C.getCode()); | |||||
| appinfo = wxAppinfoMapper.findList(appinfo).get(0); | |||||
| //退款流程 | |||||
| WxRefundOrderService.returnMoney(appinfo, order.getId()); | |||||
| } | |||||
| } | } | ||||
| @@ -370,6 +370,12 @@ public enum ErrorCode{ | |||||
| */ | */ | ||||
| MSG_MODEL_NOT_FOUND(27000, "找不到短信模板"), | MSG_MODEL_NOT_FOUND(27000, "找不到短信模板"), | ||||
| MSG_CONFIG_NOT_FOUND(27001, "找不到短信配置"), | MSG_CONFIG_NOT_FOUND(27001, "找不到短信配置"), | ||||
| /** | |||||
| * 拼团 | |||||
| */ | |||||
| ORDER_GROUP_COOPERATING_FAILD(28001, "参团失败"), | |||||
| ; | ; | ||||
| private int code; | private int code; | ||||
| @@ -20,7 +20,8 @@ public enum EnumOrderStatus { | |||||
| ORDER_STATUS_COOPERATING(10, "拼团中"), | ORDER_STATUS_COOPERATING(10, "拼团中"), | ||||
| ORDER_STATUS_COOPERATING_COMPLETE(11, "拼团完成"), | ORDER_STATUS_COOPERATING_COMPLETE(11, "拼团完成"), | ||||
| ORDER_STATUS_COOPERATING_OVERTIME(12, "拼团已过期"), | ORDER_STATUS_COOPERATING_OVERTIME(12, "拼团已过期"), | ||||
| ORDER_STATUS_COOPERATING_CANCEL(13, "拼团失败") | |||||
| ORDER_STATUS_COOPERATING_CANCEL(13, "拼团失败"), | |||||
| ORDER_STATUS_COOPERATING_FAILD(14, "参团失败") | |||||
| ; | ; | ||||
| public static EnumOrderStatus getEnum(Integer code) { | public static EnumOrderStatus getEnum(Integer code) { | ||||
| @@ -16,6 +16,7 @@ import com.iformall.service.WxOrderGroupService; | |||||
| import com.iformall.service.WxOrderService; | import com.iformall.service.WxOrderService; | ||||
| import com.iformall.service.WxRefundOrderService; | import com.iformall.service.WxRefundOrderService; | ||||
| import com.iformall.utils.DateUtils; | import com.iformall.utils.DateUtils; | ||||
| import com.iformall.utils.RedisLock; | |||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
| @@ -57,6 +58,9 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { | |||||
| @Autowired | @Autowired | ||||
| WxRefundOrderService WxRefundOrderService; | WxRefundOrderService WxRefundOrderService; | ||||
| @Autowired | |||||
| RedisLock redisLock; | |||||
| @Override | @Override | ||||
| @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) | @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) | ||||
| public void createOrderGroup(WxOrder order, WxCoupon wxCoupon) { | public void createOrderGroup(WxOrder order, WxCoupon wxCoupon) { | ||||
| @@ -64,8 +68,29 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { | |||||
| Long notOrderGroupId = 0L; | Long notOrderGroupId = 0L; | ||||
| //查看是否首次拼团,orderGroupId不等于0代表不是首次拼团,因为第二个加入的拼团者肯定有团单号 | //查看是否首次拼团,orderGroupId不等于0代表不是首次拼团,因为第二个加入的拼团者肯定有团单号 | ||||
| if (!orderGroupId.equals(notOrderGroupId)) { | if (!orderGroupId.equals(notOrderGroupId)) { | ||||
| long time = System.currentTimeMillis() + RedisLock.TIMEOUT; | |||||
| String timeStr = String.valueOf(time); | |||||
| // 团单加锁 | |||||
| String orderGroupIdStr = String.valueOf(orderGroupId); | |||||
| WxOrderGroup wxOrderGroup = null; | |||||
| while (!redisLock.lock(orderGroupIdStr, timeStr)) { | |||||
| try { | |||||
| Thread.sleep(1000); | |||||
| } catch (InterruptedException e) { | |||||
| logger.error("订单休眠失败"); | |||||
| } | |||||
| } | |||||
| wxOrderGroup = wxOrderGroupMapper.selectByPrimaryKey(orderGroupId); | |||||
| if (wxOrderGroup.getRemainPeople() == 0) { | |||||
| //修改订单状态后定时退款 | |||||
| order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_FAILD.getCode()); | |||||
| wxOrderMapper.updateByPrimaryKeySelective(order); | |||||
| redisLock.unlock(orderGroupIdStr, timeStr); | |||||
| throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD); | |||||
| } | |||||
| redisLock.unlock(orderGroupIdStr, timeStr); | |||||
| logger.info("非首次拼团,更新拼团信息"); | logger.info("非首次拼团,更新拼团信息"); | ||||
| WxOrderGroup wxOrderGroup = wxOrderGroupMapper.selectByPrimaryKey(orderGroupId); | |||||
| int remainPeople = wxOrderGroup.getRemainPeople() - 1; | int remainPeople = wxOrderGroup.getRemainPeople() - 1; | ||||
| int endPeople = 0; | int endPeople = 0; | ||||
| wxOrderGroup.setStatus(remainPeople == endPeople ? EnumOrderStatus.ORDER_STATUS_COOPERATING_COMPLETE.getCode() : wxOrderGroup.getStatus()); | wxOrderGroup.setStatus(remainPeople == endPeople ? EnumOrderStatus.ORDER_STATUS_COOPERATING_COMPLETE.getCode() : wxOrderGroup.getStatus()); | ||||
| @@ -300,13 +325,7 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { | |||||
| || temp.getOrderStatus().equals(EnumOrderStatus.ORDER_STATUS_COOPERATING_CANCEL.getCode())) { | || temp.getOrderStatus().equals(EnumOrderStatus.ORDER_STATUS_COOPERATING_CANCEL.getCode())) { | ||||
| //去库存 | //去库存 | ||||
| wxOrderService.stockBack(temp); | wxOrderService.stockBack(temp); | ||||
| ResultData resultData = WxRefundOrderService.returnMoney(appinfo, temp.getId()); | |||||
| logger.info("拼团不成功时的退款返回信息:" + resultData + "resultData.code == ResultData.SUCCESS" + (resultData.code == ResultData.SUCCESS)); | |||||
| // if (resultData.code == ResultData.SUCCESS) { | |||||
| // logger.info("修改订单的状态为已退款"); | |||||
| // temp.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_REFUND.getCode()); | |||||
| // wxOrderMapper.updateByPrimaryKeySelective(temp); | |||||
| // } | |||||
| WxRefundOrderService.returnMoney(appinfo, temp.getId()); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||