From 4f7e506eee4d91a9f491572a376288515875569b Mon Sep 17 00:00:00 2001 From: xiaohanzi Date: Wed, 17 Jun 2020 19:30:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8B=BC=E5=9B=A2bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WxPayOrderController.java | 40 +++++++--- .../iformall/service/WxOrderGroupService.java | 5 ++ .../service/impl/WxOrderGroupServiceImpl.java | 79 +++++++++++++------ 3 files changed, 89 insertions(+), 35 deletions(-) diff --git a/mallinkCApi/src/main/java/com/iformall/controller/WxPayOrderController.java b/mallinkCApi/src/main/java/com/iformall/controller/WxPayOrderController.java index 50241c85c..e8ceb32e8 100644 --- a/mallinkCApi/src/main/java/com/iformall/controller/WxPayOrderController.java +++ b/mallinkCApi/src/main/java/com/iformall/controller/WxPayOrderController.java @@ -1,32 +1,47 @@ package com.iformall.controller; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + import com.github.pagehelper.PageInfo; import com.iformall.common.ErrorCode; import com.iformall.common.IdWorker; import com.iformall.common.Result; import com.iformall.common.ResultData; -import com.iformall.domain.po.*; +import com.iformall.domain.po.WxAppinfo; +import com.iformall.domain.po.WxCUser; +import com.iformall.domain.po.WxCUserBasicInfo; +import com.iformall.domain.po.WxOrder; +import com.iformall.domain.po.WxPayAccount; +import com.iformall.domain.po.WxPayOrder; import com.iformall.enums.EnumCreditLockedStatus; import com.iformall.enums.EnumPayStatus; import com.iformall.enums.EnumPayWay; import com.iformall.exception.MallinkException; import com.iformall.service.WxCUserBasicInfoService; +import com.iformall.service.WxOrderGroupService; import com.iformall.service.WxOrderService; import com.iformall.service.WxPayAccountService; import com.iformall.service.WxPayOrderService; import com.iformall.utils.IPUtil; + import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - -import javax.servlet.http.HttpServletRequest; -import java.util.Map; @RestController @RequestMapping("/api/pay") @@ -45,6 +60,9 @@ public class WxPayOrderController extends BaseController { @Autowired private WxPayAccountService wxPayAccountService; + + @Autowired + private WxOrderGroupService orderGroupService; //@ApiOperation(value = "发起微信小程序支付订单", notes = "{\"orderId\":\"string\"}") @RequestMapping(value = "/create", method = RequestMethod.POST) @@ -142,6 +160,10 @@ public class WxPayOrderController extends BaseController { } if (payOrder.getPayOrderStatus().equals(EnumPayStatus.PAY_STATUS_SUCCESS.getCode())) { order = wxOrderService.getById(orderId); + //如果是拼团,需要处理拼团状态和人数 + if(order.getOrderGroupId() != null && !order.getOrderGroupId().equals(0L)) { + orderGroupService.afterPaySuccessUpdate(order); + } return new ResultData(Result.SUCCESS, "支付状态更新成功", order); } else{ //查询微信订单状态并更新 diff --git a/mallinkService/src/main/java/com/iformall/service/WxOrderGroupService.java b/mallinkService/src/main/java/com/iformall/service/WxOrderGroupService.java index 2672ecd11..c1aaf7ecb 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxOrderGroupService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxOrderGroupService.java @@ -32,5 +32,10 @@ public interface WxOrderGroupService { List findList(WxOrderGroup group); PageInfo queryOrderGroupMarketingList(WxOrderGroupMarketing marketing, Integer pageNum, Integer pageSize); + + /** + * 支付成功之后,更新接口调用 + */ + void afterPaySuccessUpdate(WxOrder order); } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxOrderGroupServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxOrderGroupServiceImpl.java index 6ad332fd4..3508d84bf 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxOrderGroupServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxOrderGroupServiceImpl.java @@ -65,6 +65,56 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { @Autowired WxCouponChannelMapper wxCouponChannelMapper; + @Override + public void afterPaySuccessUpdate(WxOrder order) { + // 参与拼团 + WxOrderGroup wxOrderGroup = wxOrderGroupMapper.selectById(order.getOrderGroupId()); + if (null == wxOrderGroup) { + //错误,更新状态,定时任务会发起退款,回滚库存 + order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode()); + wxOrderMapper.updateById(order); + throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"WxOrderGroup is null."+order.getOrderGroupId()); + } + WxCoupon wxCoupon = wxCouponMapper.selectById(order.getProductId()); + if (null == wxCoupon) { + throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"WxCoupon is null. orderId:"+order.getId()); + } + int remainPeople = updateOrderGroup(wxOrderGroup,order,wxCoupon); + if (remainPeople == 0) { + actionAfterOrderGroupSuccess(order, order.getOrderGroupId()); + } + } + + + private int updateOrderGroup(WxOrderGroup wxOrderGroup,WxOrder order, WxCoupon wxCoupon) { + //锁住人数 + long time = System.currentTimeMillis() + RedisLock.LONG_TIMEOUT; + String timeStr = String.valueOf(time); + boolean grouppeopleLock = redisLock.lock("grouppeopleLock_"+order.getOrderGroupId(), timeStr); + if (!grouppeopleLock) { + throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团锁定失败,请稍后再试。"); + } + //此处拼团数量应从订单总数查询 + int ordercount = wxOrderService.countGroupOrder(order.getOrderGroupId(),true); + Integer remainPeople = wxCoupon.getPressLimitNum()-ordercount; + //Integer remainPeople = wxOrderGroup.getRemainPeople()-1; + wxOrderGroup.setStatus(remainPeople <= 0 ? EnumOrderStatus.ORDER_STATUS_COOPERATING_COMPLETE.getCode() : wxOrderGroup.getStatus()); + wxOrderGroup.setUpdateDate(new Date()); + wxOrderGroup.setRemainPeople(remainPeople); + logger.info("非首次拼团,更新拼团信息, 剩余人数-1"); + int row = wxOrderGroupMapper.updateOrderGroup(wxOrderGroup); + if (row != 1) { + //错误,更新状态,定时任务会发起退款,回滚库存 + order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode()); + wxOrderMapper.updateById(order); + redisLock.unlock("grouppeopleLock_"+order.getOrderGroupId(), timeStr); + throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团失败,将稍后退款."); + }else { + redisLock.unlock("grouppeopleLock_"+order.getOrderGroupId(), timeStr); + } + return remainPeople; + } + @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) public void createOrderGroup(WxOrder order, WxCoupon wxCoupon) { @@ -87,32 +137,8 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团人数已满,将稍后退款。"); } - //锁住人数 - long time = System.currentTimeMillis() + RedisLock.LONG_TIMEOUT; - String timeStr = String.valueOf(time); - boolean grouppeopleLock = redisLock.lock("grouppeopleLock_"+orderGroupId, timeStr); - if (!grouppeopleLock) { - throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团锁定失败,请稍后再试。"); - } - //此处拼团数量应从订单总数查询 - int ordercount = wxOrderService.countGroupOrder(orderGroupId,true); - Integer remainPeople = wxCoupon.getPressLimitNum()-ordercount; - //Integer remainPeople = wxOrderGroup.getRemainPeople()-1; - wxOrderGroup.setStatus(remainPeople <= 0 ? EnumOrderStatus.ORDER_STATUS_COOPERATING_COMPLETE.getCode() : wxOrderGroup.getStatus()); - wxOrderGroup.setUpdateDate(new Date()); - wxOrderGroup.setRemainPeople(remainPeople); - logger.info("非首次拼团,更新拼团信息, 剩余人数-1"); - int row = wxOrderGroupMapper.updateOrderGroup(wxOrderGroup); - if (row != 1) { - //错误,更新状态,定时任务会发起退款,回滚库存 - order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode()); - wxOrderMapper.updateById(order); - redisLock.unlock("grouppeopleLock_"+orderGroupId, timeStr); - throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团失败,将稍后退款."); - }else { - redisLock.unlock("grouppeopleLock_"+orderGroupId, timeStr); - } - + int remainPeople = updateOrderGroup(wxOrderGroup,order,wxCoupon); + // 更新订单信息 order.setOrderStatus(wxOrderGroup.getStatus()); order.setPressEndDate(wxOrderGroup.getExpiredDate()); @@ -399,4 +425,5 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { } } } + }