Просмотр исходного кода

商家自配订单完成订单时加锁

codingliang 1 год назад
Родитель
Сommit
cff5a62368

+ 58 - 50
src/main/java/com/sqx/modules/order/service/impl/AppAppOrderServiceImpl.java

@@ -652,71 +652,79 @@ public class AppAppOrderServiceImpl extends ServiceImpl<AppOrderDao, TbOrder> im
 
     @Override
     public void finishOrderByShop(OrderFinishByShopDTO orderFinishByShopDTO) {
-        Long orderId = orderFinishByShopDTO.getOrderId();
-        TbOrder order = getById(orderId);
-
-        if (ObjectUtil.isNull(order)) {
-            throw new SqxException("无效的订单id");
-        }
-        if(order.getAutoSendOrder() != null && order.getAutoSendOrder() == 0){
-            throw new SqxException("当前订单为自动派单,不能手动完成!");
-        }
-
-        if (order.getStatus().equals(4)) {
-            return;
-        }
+        RLock lock = redissonClient.getLock(String.format(RedisKey.FINISH_ORDER_LOCK, orderId));
+        lock.lock();
+        try {
+            Long orderId = orderFinishByShopDTO.getOrderId();
+            TbOrder order = getById(orderId);
 
-        Long shopId = order.getShopId();
-        SysUserEntity currentUser = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
-        List<SysUserShop> sysUserShops = sysUserService.selectShopId(currentUser.getUserId());
-        if (CollUtil.isEmpty(sysUserShops) || !sysUserShops.stream().filter(e -> e.getShopId().longValue() == shopId.longValue()).findFirst().isPresent()) {
-            throw new SqxException("没有操作权限,只能操作完成自己店铺订单!");
-        }
+            if (ObjectUtil.isNull(order)) {
+                throw new SqxException("无效的订单id");
+            }
+            if(order.getAutoSendOrder() != null && order.getAutoSendOrder() == 0){
+                throw new SqxException("当前订单为自动派单,不能手动完成!");
+            }
 
-        if (Constant.YES.equals(orderFinishByShopDTO.getSendSmsFlag())) {
-            TbIndent indent = tbIndentService.getOne(new QueryWrapper<TbIndent>().eq("order_id", orderId));
-            if (ObjectUtil.isNull(indent)) {
+            if (order.getStatus().equals(4)) {
                 return;
             }
 
-            // 如果是外卖订单
-            if (ObjectUtil.equal(indent.getIndentType(), "5")) {
-                Long smsTemplateId = orderFinishByShopDTO.getSmsTemplateId();
-                if (ObjectUtil.isNull(smsTemplateId)) {
-                    throw new SqxException("短信模板id不能为空");
-                }
-
-                // 收货人手机
-                String userPhone = indent.getUserPhone();
+            Long shopId = order.getShopId();
+            SysUserEntity currentUser = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
+            List<SysUserShop> sysUserShops = sysUserService.selectShopId(currentUser.getUserId());
+            if (CollUtil.isEmpty(sysUserShops) || !sysUserShops.stream().filter(e -> e.getShopId().longValue() == shopId.longValue()).findFirst().isPresent()) {
+                throw new SqxException("没有操作权限,只能操作完成自己店铺订单!");
+            }
 
-                if (StrUtil.isBlank(userPhone)) {
+            if (Constant.YES.equals(orderFinishByShopDTO.getSendSmsFlag())) {
+                TbIndent indent = tbIndentService.getOne(new QueryWrapper<TbIndent>().eq("order_id", orderId));
+                if (ObjectUtil.isNull(indent)) {
                     return;
                 }
 
-                // 店铺信息
-                GoodsShop goodsShop = goodsShopDao.selectById(shopId);
-                // 查询短信发送费用
-                CommonInfo commonInfo = commonInfoService.findOne(421);
-                // 店铺钱包
-                UserMoney userMoney = userMoneyService.selectUserMoneyByUserId(goodsShop.getUserId());
-                if (userMoney.getMoney().compareTo(new BigDecimal(commonInfo.getValue())) < 0) {
-                    throw new SqxException("当前店铺余额不够,短信发送失败!");
-                }
+                // 如果是外卖订单
+                if (ObjectUtil.equal(indent.getIndentType(), "5")) {
+                    Long smsTemplateId = orderFinishByShopDTO.getSmsTemplateId();
+                    if (ObjectUtil.isNull(smsTemplateId)) {
+                        throw new SqxException("短信模板id不能为空");
+                    }
+
+                    // 收货人手机
+                    String userPhone = indent.getUserPhone();
+
+                    if (StrUtil.isBlank(userPhone)) {
+                        return;
+                    }
+
+                    // 店铺信息
+                    GoodsShop goodsShop = goodsShopDao.selectById(shopId);
+                    // 查询短信发送费用
+                    CommonInfo commonInfo = commonInfoService.findOne(421);
+                    // 店铺钱包
+                    UserMoney userMoney = userMoneyService.selectUserMoneyByUserId(goodsShop.getUserId());
+                    if (userMoney.getMoney().compareTo(new BigDecimal(commonInfo.getValue())) < 0) {
+                        throw new SqxException("当前店铺余额不够,短信发送失败!");
+                    }
 
-                // 发送短信并记录短信发送记录
-                SmsSendResult smsSendResult = smsTemplateService.sendSms(smsTemplateId, goodsShop.getPhone(), goodsShop.getShopName(), goodsShop.getShopId(), userPhone);
+                    // 发送短信并记录短信发送记录
+                    SmsSendResult smsSendResult = smsTemplateService.sendSms(smsTemplateId, goodsShop.getPhone(), goodsShop.getShopName(), goodsShop.getShopId(), userPhone);
 
-                // sourceType 1骑手、2商家
-                smsSendLogService.saveLog(indent.getIndentId(), "2", smsSendResult);
+                    // sourceType 1骑手、2商家
+                    smsSendLogService.saveLog(indent.getIndentId(), "2", smsSendResult);
+                }
             }
-        }
 
-        if (StrUtil.isNotBlank(orderFinishByShopDTO.getImgs())) {
-            order.setDeliveryImgs(orderFinishByShopDTO.getImgs());
-            updateById(order);
+            if (StrUtil.isNotBlank(orderFinishByShopDTO.getImgs())) {
+                order.setDeliveryImgs(orderFinishByShopDTO.getImgs());
+                updateById(order);
+            }
+
+            log.info("商户自配完成订单:{}", orderId);
+            accomplishOrder(orderId);
+        } finally {
+            lock.unlock();
         }
 
-        accomplishOrder(orderId);
     }
 
     @Override