Prechádzať zdrojové kódy

1.骑手转单代码重构;
2.骑手转单新增按真实姓名查询骑手功能;

codingliang 2 rokov pred
rodič
commit
1a43bd634d

+ 9 - 0
src/main/java/com/sqx/modules/app/service/UserService.java

@@ -10,6 +10,7 @@ import com.sqx.modules.app.entity.UserEntity;
 import com.sqx.modules.errand.entity.Feedback;
 import org.springframework.web.bind.annotation.RequestParam;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -29,6 +30,14 @@ public interface UserService extends IService<UserEntity> {
     UserEntity queryByPhone(String phone);
 
     /**
+     * 根据真实姓名查询用户
+     *
+     * @param realName 真实姓名
+     * @return
+     */
+    List<UserEntity> queryByRealName(String realName);
+
+    /**
      * 根据手机号和用户类型查询用户
      * @param phone  手机号
      * @param type  用户类型

+ 8 - 0
src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java

@@ -70,6 +70,7 @@ import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -111,6 +112,13 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
     }
 
     @Override
+    public List<UserEntity> queryByRealName(String realName) {
+        LambdaQueryWrapper<UserEntity> queryWrapper = Wrappers.lambdaQuery();
+        queryWrapper.eq(UserEntity::getUserName, realName);
+        return baseMapper.selectList(queryWrapper);
+    }
+
+    @Override
     public UserEntity queryByPhoneAndType(String phone) {
         return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("phone", phone));
     }

+ 13 - 45
src/main/java/com/sqx/modules/errand/controller/app/AppTbIndentController.java

@@ -1,5 +1,7 @@
 package com.sqx.modules.errand.controller.app;
 
+import cn.hutool.core.util.ObjectUtil;
+import com.sqx.common.exception.SqxException;
 import com.sqx.common.utils.DateUtils;
 import com.sqx.common.utils.PageUtils;
 import com.sqx.common.utils.Result;
@@ -9,11 +11,11 @@ import com.sqx.modules.app.service.UserService;
 import com.sqx.modules.common.entity.CommonInfo;
 import com.sqx.modules.common.service.CommonInfoService;
 import com.sqx.modules.errand.dto.RiderDeliveryDTO;
+import com.sqx.modules.errand.dto.RiderTransferOrderDTO;
 import com.sqx.modules.errand.dto.WaitForAcceptOrderQueryDTO;
 import com.sqx.modules.errand.entity.*;
 import com.sqx.modules.errand.service.ErrandComplaintService;
 import com.sqx.modules.errand.service.TbIndentService;
-import com.sqx.modules.order.entity.Evaluate;
 import com.sqx.modules.utils.SenInfoCheckUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -333,52 +335,18 @@ public class AppTbIndentController {
     @Login
     @ApiOperation("转单")
     @PostMapping("/transferOfOrder")
-    public Result transferOfOrder(Long indentId,String phone){
-        UserEntity userEntity = userService.queryByPhone(phone);
-        if(userEntity==null){
-            return Result.error("用户不存在!");
-        }
-        CommonInfo one1 = commonInfoService.findOne(273);
-        Double cashDeposit = Double.parseDouble(one1.getValue());
-        if(!"1".equals(userEntity.getCheckCertification())){
-            return Result.error("骑手未进行实名认证!");
-        }
-        if(userEntity.getCashDeposit().doubleValue()<cashDeposit){
-            return Result.error("骑手保证金不足!");
-        }
+    public Result transferOfOrder(@RequestAttribute Long userId, @Valid RiderTransferOrderDTO transferOrderDTO){
+        // 检查入参
+        checkTransferOrderParam(transferOrderDTO);
+
+        tbIndentService.transferOfOrder(userId, transferOrderDTO);
 
-        TbIndent indent = tbIndentService.getById(indentId);
-        if(userEntity.getUserId().equals(indent.getRiderUserId())){
-            return Result.error("不能转给自己!");
-        }
-        if(!"3".equals(indent.getIndentState())){
-            return Result.error("订单状态发生变更,请刷新后重试!");
-        }
-        indent.setRiderUserId(userEntity.getUserId());
-        indent.setIsRider(1);
-        tbIndentService.updateById(indent);
-        UserEntity user = userService.getById(indent.getUserId());
-        CommonInfo one = commonInfoService.findOne(335);
-        List<String> msgList=new ArrayList<>();
-        msgList.add(0,indent.getIndentNumber());
-        if("3".equals(indent.getIndentState())){
-            msgList.add(1,"已接单");
-        }else if("4".equals(indent.getIndentState())){
-            msgList.add(1,"已取货");
-        }else{
-            msgList.add(1,"进行中");
-        }
-        msgList.add(2,"骑手已更换,新骑手将尽快为您送达");
-        msgList.add(3, DateUtils.format(new Date()));
-        if(StringUtils.isNotEmpty(user.getOpenId())){
-            SenInfoCheckUtil.sendMsg(user.getOpenId(),one.getValue(),msgList,5);
-        }
-        if(StringUtils.isNotEmpty(user.getOpenId())){
-            userService.pushToSingle("更换骑手","骑手已更换,新骑手将尽快为您送达",user.getClientid());
-        }
         return Result.success();
     }
 
-
-
+    private void checkTransferOrderParam(RiderTransferOrderDTO transferOrderDTO) {
+        if (StringUtils.isBlank(transferOrderDTO.getPhone()) && StringUtils.isBlank(transferOrderDTO.getRealName())) {
+            throw new SqxException("待分配骑手手机号和待分配骑手真实姓名不能同时为空");
+        }
+    }
 }

+ 22 - 0
src/main/java/com/sqx/modules/errand/dto/RiderTransferOrderDTO.java

@@ -0,0 +1,22 @@
+package com.sqx.modules.errand.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+
+@Data
+@ApiModel("骑手转单dto")
+public class RiderTransferOrderDTO {
+
+    @ApiModelProperty(value = "配送单id", required = true)
+    @NotNull(message = "配送单id不能为空")
+    private Long indentId;
+
+    @ApiModelProperty("待分配骑手手机号,待分配骑手手机号和待分配骑手真实姓名不能同时为空")
+    private String phone;
+
+    @ApiModelProperty("待分配骑手真实姓名,待分配骑手手机号和待分配骑手真实姓名不能同时为空")
+    private String realName;
+}

+ 8 - 0
src/main/java/com/sqx/modules/errand/service/TbIndentService.java

@@ -5,6 +5,7 @@ import com.sqx.common.utils.PageUtils;
 import com.sqx.common.utils.Result;
 import com.sqx.modules.app.entity.UserEntity;
 import com.sqx.modules.errand.dto.RiderDeliveryDTO;
+import com.sqx.modules.errand.dto.RiderTransferOrderDTO;
 import com.sqx.modules.errand.dto.WaitForAcceptOrderQueryDTO;
 import com.sqx.modules.errand.entity.ErrandAddress;
 import com.sqx.modules.errand.entity.ErrandEvaluate;
@@ -105,4 +106,11 @@ public interface TbIndentService extends IService<TbIndent> {
     Result userCancleIndent(Long userId, String indentNumber);
 
     TbIndent indentMessageByOrderId(Long orderId, double ol, double od);
+
+    /**
+     * 骑手转单
+     * @param userId 当前用户id
+     * @param transferOrderDTO dto
+     */
+    void transferOfOrder(Long userId, RiderTransferOrderDTO transferOrderDTO);
 }

+ 113 - 0
src/main/java/com/sqx/modules/errand/service/impl/TbIndentServiceImpl.java

@@ -25,6 +25,7 @@ import com.sqx.modules.common.service.CommonInfoService;
 import com.sqx.modules.errand.dao.ErrandEvaluateDao;
 import com.sqx.modules.errand.dao.TbIndentDao;
 import com.sqx.modules.errand.dto.RiderDeliveryDTO;
+import com.sqx.modules.errand.dto.RiderTransferOrderDTO;
 import com.sqx.modules.errand.dto.WaitForAcceptOrderQueryDTO;
 import com.sqx.modules.errand.entity.ErrandAddress;
 import com.sqx.modules.errand.entity.ErrandEvaluate;
@@ -795,6 +796,22 @@ public class TbIndentServiceImpl extends ServiceImpl<TbIndentDao, TbIndent> impl
         return tbIndent;
     }
 
+    @Override
+    public void transferOfOrder(Long userId, RiderTransferOrderDTO transferOrderDTO) {
+        // 获取待转单
+        TbIndent indent = getTransferOrder(userId, transferOrderDTO);
+
+        // 获取新的骑手
+        UserEntity userEntity = getNewRicher(userId, transferOrderDTO);
+
+        // 开始转单
+        indent.setRiderUserId(userEntity.getUserId());
+        indent.setIsRider(1);
+        updateById(indent);
+
+        // 消息通知
+        noticeAfterTransfer(indent);
+    }
 
     @Transactional
     @Override
@@ -1332,7 +1349,103 @@ public class TbIndentServiceImpl extends ServiceImpl<TbIndentDao, TbIndent> impl
         return Result.success().put("data", indent1);
     }
 
+    /**
+     * 转单后通知
+     * @param indent
+     */
+    private void noticeAfterTransfer(TbIndent indent) {
+        CommonInfo one = commonInfoService.findOne(335);
+        UserEntity user = userService.getById(indent.getUserId());
+        List<String> msgList=new ArrayList<>();
+        msgList.add(0, indent.getIndentNumber());
+        String indentState = indent.getIndentState();
+        if("3".equals(indentState)){
+            msgList.add(1,"已接单");
+        }else if("4".equals(indentState)){
+            msgList.add(1,"已取货");
+        }else{
+            msgList.add(1,"进行中");
+        }
+        msgList.add(2,"骑手已更换,新骑手将尽快为您送达");
+        msgList.add(3, DateUtils.format(new Date()));
+        if(org.apache.commons.lang.StringUtils.isNotEmpty(user.getOpenId())){
+            SenInfoCheckUtil.sendMsg(user.getOpenId(), one.getValue(), msgList,5);
+        }
+
+        if(org.apache.commons.lang.StringUtils.isNotEmpty(user.getOpenId())){
+            userService.pushToSingle("更换骑手","骑手已更换,新骑手将尽快为您送达", user.getClientid());
+        }
+    }
+
+    /**
+     * 获取待转单配送单
+     *
+     * @param currentUserId 当前用户
+     * @param transferOrderDTO
+     * @return
+     */
+    private TbIndent getTransferOrder(Long currentUserId, RiderTransferOrderDTO transferOrderDTO) {
+        // 开始转单
+        TbIndent indent = getById(transferOrderDTO.getIndentId());
+        if (ObjectUtil.isNull(indent)) {
+            throw new SqxException("无效的配送单id");
+        }
+
+        if (indent.getRiderUserId() != currentUserId) {
+            throw new SqxException("不能操作非自己的订单");
+        }
+
+        if(!"3".equals(indent.getIndentState())){
+            throw new SqxException("订单状态发生变更,请刷新后重试!");
+        }
+
+        return indent;
+    }
+
+    /**
+     * 获取新骑手
+     * @param currentUserId 当前用户id
+     * @param transferOrderDTO dto
+     * @return 新骑手
+     */
+    private UserEntity getNewRicher(Long currentUserId, RiderTransferOrderDTO transferOrderDTO) {
+        String phone = transferOrderDTO.getPhone();
+        String realName = transferOrderDTO.getRealName();
+
+        UserEntity userEntity = null;
+        if (org.apache.commons.lang.StringUtils.isNotBlank(phone)) {
+            userEntity = userService.queryByPhone(phone);
+        } else {
+            List<UserEntity> userEntities = userService.queryByRealName(realName);
+            if (userEntities.size() > 1) {
+                throw new SqxException("根据姓名查到了多个骑手,其输入手机号码精确查询");
+            }
+            if (userEntities.size() == 1) {
+                userEntity = userEntities.get(0);
+            }
+        }
+
+        if(userEntity == null){
+            throw new SqxException("用户不存在");
+        }
+
+        if (currentUserId == userEntity.getUserId()) {
+            throw new SqxException("不能转单给自己!");
+        }
+
+        // 校验当前骑手实名认证、保证金等信息
+        CommonInfo one1 = commonInfoService.findOne(273);
+        Double cashDeposit = Double.parseDouble(one1.getValue());
+        if(!"1".equals(userEntity.getCheckCertification())){
+            throw new SqxException("骑手未进行实名认证!");
+        }
 
+        if(userEntity.getCashDeposit().doubleValue() < cashDeposit){
+            throw new SqxException("骑手保证金不足!");
+        }
+
+        return userEntity;
+    }
 }