diff --git a/mall-account/src/main/java/com/suisung/mall/account/service/impl/AccountUserBaseServiceImpl.java b/mall-account/src/main/java/com/suisung/mall/account/service/impl/AccountUserBaseServiceImpl.java index b6fa65b9..67f8d7f0 100644 --- a/mall-account/src/main/java/com/suisung/mall/account/service/impl/AccountUserBaseServiceImpl.java +++ b/mall-account/src/main/java/com/suisung/mall/account/service/impl/AccountUserBaseServiceImpl.java @@ -1667,7 +1667,7 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("user_id", user_id).eq("bind_type", bind_type) - .eq("user_type", user_type) - .eq("bind_active", CommonConstant.Enable); - - return getOne(queryWrapper); - } - /** * 获取有效绑定 * @@ -80,9 +61,30 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("user_id", user_id) + .eq("bind_type", bind_type) + .eq("user_type", user_type) + .eq("bind_active", CommonConstant.Enable) + .orderByAsc("bind_time"); + + return findOne(queryWrapper); + } + @Override public AccountUserBindConnect getBindByBindId(String bind_id, Integer bind_type, Integer user_type) { - if (StrUtil.isEmpty(bind_id)) { + if (StrUtil.isBlank(bind_id)) { return null; } @@ -94,13 +96,15 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("bind_id", bind_id).eq("bind_type", bind_type); - AccountUserBindConnect bind_row = findOne(queryWrapper); + // 判断是否已经绑定 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("bind_id", bind_id).eq("bind_type", bind_type).orderByAsc("bind_time"); + AccountUserBindConnect bind_row = findOne(queryWrapper); + //getBindByBindId(bind_id, bind_type, user_id, CommonConstant.USER_TYPE_NORMAL); //findOne(queryWrapper); - if (bind_row != null) { - Integer bind_active = bind_row.getBind_active(); + if (bind_row != null) { + Integer bind_active = bind_row.getBind_active(); - if (bind_active == 1) { - Integer bind_user_id = bind_row.getUser_id(); - if (ObjectUtil.equal(bind_user_id, user_id)) { - return true; - } else { - throw new ApiException(I18nUtil._("其它用户已绑定!")); - } + if (bind_active == CommonConstant.Enable) { + Integer bind_user_id = bind_row.getUser_id(); + if (ObjectUtil.equal(bind_user_id, user_id)) { + return true; } else { - if (!edit(bind_data)) { - throw new ApiException(I18nUtil._("绑定修改失败!")); - } + throw new ApiException(I18nUtil._("其它用户已绑定!")); + } + } else { + if (!edit(bind_data)) { + throw new ApiException(I18nUtil._("绑定修改失败!")); } } + } - AccountUserInfo userInfo = new AccountUserInfo(); - switch (bind_type) { - case 1: - String user_intl = ObjectUtil.defaultIfNull(getParameter("user_intl"), CommonConstant.IDD_ZH_CN); - String sub_user_intl = bind_id.substring(0, user_intl.length()); - if (StrUtil.equals(user_intl, sub_user_intl)) { - userInfo.setUser_mobile(StrUtil.removePrefix(bind_id, sub_user_intl)); - userInfo.setUser_intl(user_intl); - } else { - userInfo.setUser_mobile(bind_id); - } - bind_data.setBind_openid(bind_id); - break; - case 2: - userInfo.setUser_email(bind_id); - bind_data.setBind_openid(bind_id); - break; - } + AccountUserInfo userInfo = new AccountUserInfo(); + switch (bind_type) { + case 1: + String user_intl = ObjectUtil.defaultIfNull(getParameter("user_intl"), CommonConstant.IDD_ZH_CN); + String sub_user_intl = bind_id.substring(0, user_intl.length()); + if (StrUtil.equals(user_intl, sub_user_intl)) { + userInfo.setUser_mobile(StrUtil.removePrefix(bind_id, sub_user_intl)); + userInfo.setUser_intl(user_intl); + } else { + userInfo.setUser_mobile(bind_id); + } + bind_data.setBind_openid(bind_id); + break; + case 2: + userInfo.setUser_email(bind_id); + bind_data.setBind_openid(bind_id); + break; + } - userInfo.setUser_id(user_id); - if (!accountUserInfoService.edit(userInfo)) { - throw new ApiException(I18nUtil._("保存用户数据失败!")); - } + userInfo.setUser_id(user_id); + if (!accountUserInfoService.edit(userInfo)) { + throw new ApiException(I18nUtil._("保存用户数据失败!")); + } - if (!saveOrUpdate(bind_data)) { - throw new ApiException(I18nUtil._("保存用户绑定失败!")); - } - } else { - throw new ApiException(I18nUtil._("验证码错误!")); + if (!saveOrUpdate(bind_data)) { + throw new ApiException(I18nUtil._("保存用户绑定失败!")); } return true; } + + /** + * 用户绑定连接处理(支持自动注册) + * + * @param accountUserBindConnect 绑定连接信息对象 + * @param activity_id 活动ID(可为空) + * @param needRegNow 是否需要立即注册新用户 + * @return 用户ID(绑定/注册成功返回用户ID,失败返回null) + */ @Override public Integer doUserBind(AccountUserBindConnect accountUserBindConnect, String activity_id, boolean needRegNow) { - Integer user_id = null; + String bind_id = accountUserBindConnect.getBind_id(); // 获取绑定ID(如微信openid) - String bind_id = accountUserBindConnect.getBind_id(); + // 1. 首先尝试通过绑定ID查找已有用户 AccountUserBase accountUserBase = accountUserBaseService.getByAccount(bind_id); - //unionId 获取,如果存在 + // 2. 处理unionId绑定逻辑(微信生态专用) if (StrUtil.isNotBlank(accountUserBindConnect.getBind_unionid()) && ObjectUtil.isNull(accountUserBase)) { - //检测unionId, 判断用户 - //根据unionId,判断已经绑定的用户 + // 通过unionId查询最早绑定的记录(微信同一用户在不同公众号下unionId相同) QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("bind_unionid", accountUserBindConnect.getBind_unionid()); + queryWrapper.eq("bind_unionid", accountUserBindConnect.getBind_unionid()).orderByAsc("bind_time"); AccountUserBindConnect find_bind_row = findOne(queryWrapper); + if (find_bind_row != null) { + // 获取unionId关联的主账号 accountUserBase = accountUserBaseService.get(find_bind_row.getUser_id()); - //运行到此处 - //判断bind openid是否存在,不存在则需要添加 - //支付地方需要调用到openid, 此处必须冗余存放openid + // 检查当前openid是否已绑定,未绑定则新增绑定关系(支付等场景需要openid) AccountUserBindConnect open_bind_row = get(bind_id); - if (null == open_bind_row) { + if (open_bind_row == null) { accountUserBindConnect.setBind_active(CommonConstant.Enable); accountUserBindConnect.setBind_type(accountUserBindConnect.getBind_type()); save(accountUserBindConnect); } + // 更新绑定关系并返回用户ID AccountUserBase updAccountUserBase = accountUserBaseService.get(find_bind_row.getUser_id()); if (updAccountUserBase != null) { user_id = updAccountUserBase.getUser_id(); @@ -269,39 +279,43 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl respMap = new HashMap<>(); -// // 订单分页数据 -// Long expireSeconds = 1500L; // 60秒*25分钟 = 1500秒 -// respMap.put("order_page_list", shopOrderBaseService.selectMchOrderPageList(params.getInt("storeId"), params.getStr("keyword"), params.getInt("delivery"), params.getInt("status"), expireSeconds, params.getInt("pageNum"), params.getInt("pageSize"))); -// // 订单数量 -// respMap.put("order_count", shopOrderBaseService.mchOrderCountByStoreId(params.getInt("storeId"))); -// return CommonResult.success(respMap); -// } - @ApiOperation(value = "查询移动商家端订单详情数据", notes = "查询移动商家端订单详情数据") @RequestMapping(value = "/getOrderDetail", method = RequestMethod.GET) public CommonResult getOrderDetail(@RequestParam(name = "order_id") String order_id) { diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderReturnServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderReturnServiceImpl.java index 6d1a195a..38122dfa 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderReturnServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderReturnServiceImpl.java @@ -2186,145 +2186,146 @@ public class ShopOrderReturnServiceImpl extends BaseServiceImpl returnQuery = new QueryWrapper<>(); - returnQuery.eq("order_id", orderId) - .ne("return_state_id", StateCode.RETURN_PROCESS_CANCEL); - if (CollectionUtil.isNotEmpty(find(returnQuery))) { - return CommonResult.failed("订单已有处理中的退货单"); - } - - // 获取订单商品项 - List orderItems = shopOrderItemService.find( - new QueryWrapper().eq("order_id", orderId)); - if (CollectionUtil.isEmpty(orderItems)) { - return CommonResult.failed("订单没有可退货的商品"); - } - - // 准备退货数据 - String reason = requestParams.getStr("reason"); - reason = StrUtil.isBlank(reason) ? "商家整单退货" : reason; - - OrderReturnInputVo refundRequest = new OrderReturnInputVo(); - refundRequest.setOrder_id(orderId); - refundRequest.setReturn_tel(""); - refundRequest.setReturn_buyer_message(I18nUtil._(reason)); - refundRequest.setUser_id(orderInfo.getBuyer_user_id()); - refundRequest.setSystem_opear(false); - - // 添加退货商品项 - orderItems.forEach(item -> { - OrderReturnItemInputVo itemVo = new OrderReturnItemInputVo(); - itemVo.setOrder_item_id(item.getOrder_item_id()); - itemVo.setReturn_item_num(item.getOrder_item_quantity()); - itemVo.setReturn_refund_amount(item.getOrder_item_payment_amount()); - refundRequest.getReturn_items().add(itemVo); - }); - - // 处理部分退货情况 - OrderReturnInputVo partialRefund = null; try { - partialRefund = JSONUtil.toBean(requestParams.getStr("order_return_vo"), OrderReturnInputVo.class); + // === 1. 参数基础校验 === + if (requestParams == null || StrUtil.isBlank(requestParams.getStr("order_id"))) { + return CommonResult.failed("退货请求参数不完整"); + } + + // 获取当前登录用户 + final UserDto currentUser = getCurrentUser(); + if (currentUser == null) { + throw new ApiException(ResultCode.NEED_LOGIN); + } + + // === 2. 验证订单有效性 === + final String orderId = requestParams.getStr("order_id"); + ShopOrderInfo orderInfo = shopOrderInfoService.get(orderId); + if (orderInfo == null) { + return CommonResult.failed("订单不存在"); + } + if (StateCode.ORDER_PAID_STATE_NO == orderInfo.getOrder_is_paid()) { + return CommonResult.failed("订单未付款,无法退货"); + } + // 验证店铺权限 + if (orderInfo.getStore_id() != null + && !orderInfo.getStore_id().equals(Convert.toInt(currentUser.getStore_id()))) { + return CommonResult.failed("无权处理其他店铺订单"); + } + + // === 3. 检查是否存在处理中的退货单 === + Boolean hasRefundRecord = CollectionUtil.isNotEmpty( + find(new QueryWrapper() + .eq("order_id", orderId) + .ne("return_state_id", StateCode.RETURN_PROCESS_CANCEL))); + + // === 4. 处理退货商品信息 === + OrderReturnInputVo partialRefund = null; + Boolean isPartialRefund = false; + if (StrUtil.isNotBlank(requestParams.getStr("order_return_vo"))) { + try { + partialRefund = JSONUtil.toBean(requestParams.getStr("order_return_vo"), OrderReturnInputVo.class); + isPartialRefund = partialRefund != null && CollectionUtil.isNotEmpty(partialRefund.getReturn_items()); + } catch (Exception e) { + return CommonResult.failed("退货商品参数格式错误"); + } + } + + // === 5. 创建退货单(如不存在) === + ShopOrderReturn refundOrder = null; + if (!hasRefundRecord) { + // 5.1 获取订单商品项 + List orderItems = shopOrderItemService.find( + new QueryWrapper().eq("order_id", orderId)); + if (CollectionUtil.isEmpty(orderItems)) { + return CommonResult.failed("订单没有可退货的商品"); + } + + // 5.2 准备退货请求数据 + OrderReturnInputVo refundRequest = new OrderReturnInputVo(); + refundRequest.setOrder_id(orderId); + refundRequest.setReturn_buyer_message( + StrUtil.isBlank(requestParams.getStr("reason")) ? + (isPartialRefund ? "商家部分商品退货" : "商家整单退货") : + requestParams.getStr("reason")); + refundRequest.setUser_id(orderInfo.getBuyer_user_id()); + + // 5.3 处理退货商品项 + if (isPartialRefund) { + // 校验部分退货商品有效性 + List validItemIds = orderItems.stream().map(ShopOrderItem::getOrder_item_id).collect(Collectors.toList()); + if (!partialRefund.getReturn_items().stream() + .allMatch(item -> validItemIds.contains(item.getOrder_item_id()) + && item.getReturn_refund_amount().compareTo(BigDecimal.ZERO) > 0 + && item.getReturn_item_num() > 0)) { + return CommonResult.failed("退货商品或金额无效"); + } + refundRequest.setReturn_items(partialRefund.getReturn_items()); + } else { + // 默认整单退货 + orderItems.forEach(item -> { + OrderReturnItemInputVo itemVo = new OrderReturnItemInputVo(); + itemVo.setOrder_item_id(item.getOrder_item_id()); + itemVo.setReturn_item_num(item.getOrder_item_quantity()); + itemVo.setReturn_refund_amount(item.getOrder_item_payment_amount()); + refundRequest.getReturn_items().add(itemVo); + }); + } + + // 5.4 创建退货单 + CommonResult createResult = addItem(refundRequest); + if (createResult.getStatus() != 200) { + return createResult; + } + refundOrder = findOne(new QueryWrapper().eq("order_id", orderId)); + if (refundOrder == null) { + return CommonResult.failed("退货单创建失败"); + } + refundOrder.setReturn_buyer_message(refundRequest.getReturn_buyer_message()); + } + + // === 6. 处理退货 === + refundOrder = refundOrder != null ? refundOrder : + findOne(new QueryWrapper().eq("order_id", orderId)); + refundOrder.setReturn_flag(0); + String reason = StrUtil.isBlank(requestParams.getStr("reason")) ? + (isPartialRefund ? "商家部分商品退货" : "商家整单退货") : + requestParams.getStr("reason"); + refundOrder.setReturn_store_message(reason); + refundOrder.setReturn_buyer_message(reason); + + if (!processReviewList(refundOrder, 0)) { + return CommonResult.failed("退货处理失败"); + } + + // === 7. 特殊场景:同城配送订单取消 === + if (orderInfo.getDelivery_type_id() != null + && StateCode.DELIVERY_TYPE_SAME_CITY == orderInfo.getDelivery_type_id().intValue() + && !isPartialRefund) { + try { + sfExpressApiService.cancelOrder(orderId, 313, refundOrder.getReturn_store_message()); + } catch (Exception e) { + log.error("顺丰同城取消订单失败", e); + } + } + + return CommonResult.success(); } catch (Exception e) { - return CommonResult.failed("退货商品参数格式错误"); + log.error("商家处理退货异常", e); + return CommonResult.failed("系统处理退货时发生异常"); } - - if (partialRefund != null && CollectionUtil.isNotEmpty(partialRefund.getReturn_items())) { - // 校验退货商品是否属于当前订单 - List validItemIds = orderItems.stream() - .map(ShopOrderItem::getOrder_item_id) - .collect(Collectors.toList()); - boolean allItemsValid = partialRefund.getReturn_items().stream() - .allMatch(item -> validItemIds.contains(item.getOrder_item_id())); - if (!allItemsValid) { - return CommonResult.failed("退货商品不属于当前订单"); - } - - // 校验退款金额和数量 - boolean amountsValid = partialRefund.getReturn_items().stream() - .allMatch(item -> item.getReturn_refund_amount().compareTo(BigDecimal.ZERO) > 0 && item.getReturn_item_num() > 0); - if (!amountsValid) { - return CommonResult.failed("退货数量或金额无效"); - } - - refundRequest.setReturn_items(partialRefund.getReturn_items()); - reason = StrUtil.isBlank(reason) ? "商家部分商品退货" : reason; - } - - // 验证退货商品项 - if (CollectionUtil.isEmpty(refundRequest.getReturn_items())) { - return CommonResult.failed("没有可退货的商品"); - } - - // 创建退货单 - CommonResult createResult = addItem(refundRequest); - if (createResult.getCode() != 200) { - return createResult; - } - - // 获取并处理退货单 - ShopOrderReturn refundOrder = findOne( - new QueryWrapper().eq("order_id", orderId)); - if (refundOrder == null) { - return CommonResult.failed("退货单创建失败"); - } - - refundOrder.setReturn_flag(0); - refundOrder.setReturn_buyer_message(reason); - refundOrder.setReturn_store_message(reason); - - boolean success = processReviewList(refundOrder, 0); - if (!success) { - return CommonResult.failed("退货处理失败"); - } - - // 如果是同城配送,通知顺丰同城取消订单 - if (orderInfo.getDelivery_type_id() != null - && orderInfo.getDelivery_type_id().equals(StateCode.DELIVERY_TYPE_SAME_CITY) - && partialRefund == null) { - try { - sfExpressApiService.cancelOrder(orderId, 313, reason); - } catch (Exception e) { - log.error("顺丰同城取消订单失败: {}", e.getMessage()); - } - } - - return CommonResult.success(); } } \ No newline at end of file