商家主动退款,优化

This commit is contained in:
Jack 2025-07-11 23:38:39 +08:00
parent d5826cc93e
commit a16e8e4c42
4 changed files with 249 additions and 245 deletions

View File

@ -1667,7 +1667,7 @@ public class AccountUserBaseServiceImpl extends BaseServiceImpl<AccountUserBaseM
String verify_token = (String) userInfo.get("verify_token");
String rand_key = (String) userInfo.get("rand_key");
Integer userIsAdmin = Convert.toInt(userInfo.getOrDefault("user_is_admin", CommonConstant.USER_TYPE_NORMAL)); // 用户类型:0-普通买家; 1-管理员2-入驻商家
Integer userType = userIsAdmin; // 用户身份:0-普通买家; 1-管理员2-入驻商家(有歧义废弃)
Integer userType = userIsAdmin; // 用户身份:0-普通买家; 1-管理员2-入驻商家
String user_email = (String) userInfo.get("user_email");
Integer bind_type = getParameter("bind_type", BindCode.MOBILE);

View File

@ -49,25 +49,6 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
@Autowired
private AccountUserBaseService accountUserBaseService;
/**
* 根据用户 ID 和绑定类型获取一条记录
*
* @param user_id
* @param bind_type
* @param user_type
* @return
*/
@Override
public AccountUserBindConnect getBindByUserId(Integer user_id, Integer bind_type, Integer user_type) {
user_type = ObjectUtil.isNotEmpty(user_type) ? user_type : CommonConstant.USER_TYPE_NORMAL;
QueryWrapper<AccountUserBindConnect> 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<AccountUs
return Convert.toMap(String.class, Object.class, getBindByUserId(user_id, bind_type, user_type));
}
/**
* 根据用户 ID 和绑定类型获取一条记录
*
* @param user_id
* @param bind_type
* @param user_type
* @return
*/
@Override
public AccountUserBindConnect getBindByUserId(Integer user_id, Integer bind_type, Integer user_type) {
user_type = ObjectUtil.isNotEmpty(user_type) ? user_type : CommonConstant.USER_TYPE_NORMAL;
QueryWrapper<AccountUserBindConnect> 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<AccountUs
queryWrapper.eq("bind_id", bind_id)
.eq("bind_type", bind_type)
.eq("user_type", user_type)
.eq("bind_active", CommonConstant.Enable);
return getOne(queryWrapper);
.eq("bind_active", CommonConstant.Enable)
.orderByAsc("bind_time");
return findOne(queryWrapper);
}
@Override
public AccountUserBindConnect getBindByBindId(String bind_id, Integer bind_type, Integer user_id, Integer user_type) {
if (StrUtil.isEmpty(bind_id)) {
if (StrUtil.isBlank(bind_id) || ObjectUtil.isEmpty(user_id)) {
return null;
}
@ -113,8 +117,9 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
.eq("bind_type", bind_type)
.eq("user_type", user_type)
.eq("user_id", user_id)
.eq("bind_active", CommonConstant.Enable);
return getOne(queryWrapper);
.eq("bind_active", CommonConstant.Enable)
.orderByAsc("bind_time");
return findOne(queryWrapper);
}
@Override
@ -122,7 +127,6 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
// 如果是手机号码则进行转换带 +86 的手机号码
bind_id = bind_type == BindCode.MOBILE ? PhoneNumberUtils.convZhPhoneNumber(bind_id) : bind_id;
// AccountUserBindConnect currAccountUserBindConnect = get(bind_id);
AccountUserBindConnect currAccountUserBindConnect = getBindByBindId(bind_id, bind_type, user_id, user_type);
if (currAccountUserBindConnect == null) {
accountUserBindConnect.setBind_id(bind_id);
@ -168,95 +172,101 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
String bind_id = bind_data.getBind_id();
// todo 验证验证码
if (true) {
bind_data.setBind_active(1);
bind_data.setUser_id(user_id);
bind_data.setBind_type(bind_type);
bind_data.setBind_active(CommonConstant.Enable);
bind_data.setUser_id(user_id);
bind_data.setBind_type(bind_type);
// 判断是否已经绑定
QueryWrapper<AccountUserBindConnect> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("bind_id", bind_id).eq("bind_type", bind_type);
AccountUserBindConnect bind_row = findOne(queryWrapper);
// 判断是否已经绑定
QueryWrapper<AccountUserBindConnect> 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<AccountUserBindConnect> 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<AccountUs
}
}
//自动注册用户
// 3. 处理用户注册/绑定逻辑
if (accountUserBase != null) {
// 已有用户直接返回ID
user_id = accountUserBase.getUser_id();
} else {
//需要注册用户
if (needRegNow) {
// 3.1 需要立即注册新用户
Map reg_info = new HashMap();
reg_info.put("user_account", bind_id);
reg_info.put("user_password", "Ss@123" + UUID.randomUUID());
reg_info.put("user_nickname", accountUserBindConnect.getBind_nickname());
reg_info.put("activity_id", activity_id);
reg_info.put("user_account", bind_id); // 使用绑定ID作为账号
reg_info.put("user_password", "Ss@123" + UUID.randomUUID()); // 生成随机密码
reg_info.put("user_nickname", accountUserBindConnect.getBind_nickname()); // 从绑定信息获取昵称
reg_info.put("activity_id", activity_id); // 关联活动
// 注册新用户
accountUserBase = accountUserBaseService.register(reg_info);
if (accountUserBase != null) {
user_id = accountUserBase.getUser_id();
//添加user info
// 完善用户信息
Map info = new HashMap();
info.put("user_nickname", accountUserBindConnect.getBind_nickname());// 名称
info.put("user_nickname", accountUserBindConnect.getBind_nickname());
info.put("user_avatar", accountUserBindConnect.getBind_icon());
info.put("user_gender", accountUserBindConnect.getBind_gender()); // 性别 1: 2:
info.put("user_gender", accountUserBindConnect.getBind_gender());
accountUserInfoService.editAccount(user_id, info);
checkBind(bind_id, accountUserBindConnect.getBind_type(), user_id, accountUserBindConnect.getUser_type(), accountUserBindConnect);
// 执行绑定操作
checkBind(bind_id, accountUserBindConnect.getBind_type(), user_id,
accountUserBindConnect.getUser_type(), accountUserBindConnect);
}
} else {
//读取用户信息
// 3.2 不需要注册则直接绑定用于已登录用户绑定新渠道
user_id = accountUserBindConnect.getUser_id();
checkBind(bind_id, accountUserBindConnect.getBind_type(), user_id, accountUserBindConnect.getUser_type(), accountUserBindConnect);
// 执行绑定操作
checkBind(bind_id, accountUserBindConnect.getBind_type(), user_id,
accountUserBindConnect.getUser_type(), accountUserBindConnect);
}
}
return user_id;
@ -329,7 +343,7 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
queryWrapper.eq("user_type", userType);
}
queryWrapper.select("user_id").orderByAsc("user_id");
queryWrapper.select("user_id").orderByAsc("bind_time");
AccountUserBindConnect accountUserBindConnect = findOne(queryWrapper);
return accountUserBindConnect != null ? accountUserBindConnect.getUser_id() : null;
@ -365,7 +379,8 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
.eq("bind_type", BindCode.MOBILE)
.eq("user_type", userType)
.eq("user_id", userId)
.eq("bind_active", CommonConstant.Enable);
.eq("bind_active", CommonConstant.Enable)
.orderByAsc("bind_time");
AccountUserBindConnect accountUserBindConnect = findOne(queryWrapper);
if (accountUserBindConnect != null) {
@ -413,8 +428,8 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
// 1. 校验入参任何一个为空直接返回null
if (StrUtil.isBlank(bindId)
|| ObjectUtil.isNull(bindType)
|| ObjectUtil.isNull(bindType)
|| ObjectUtil.isNull(bindType)) {
|| ObjectUtil.isNull(userId)
|| ObjectUtil.isNull(userType)) {
log.warn("初始化账户用户绑定信息失败参数存在空值bindId={}, bindType={}, userId={}, userType={}", bindId, bindType, userId, userType);
return null;
}
@ -425,7 +440,7 @@ public class AccountUserBindConnectServiceImpl extends BaseServiceImpl<AccountUs
.eq("bind_type", bindType)
.eq("user_type", userType)
.eq("user_id", userId)
.eq("bind_active", CommonConstant.Enable);
.eq("bind_active", CommonConstant.Enable).orderByAsc("bind_time");
AccountUserBindConnect existingBindConnect = findOne(queryWrapper);
if (existingBindConnect != null) {

View File

@ -219,18 +219,6 @@ public class UserOrderController extends BaseControllerImpl {
return CommonResult.success(shopOrderBaseService.listsOrderAndsProduct(pageNum, pageSize));
}
// @ApiOperation(value = "查询移动商家端订单数据(新)", notes = "查询移动商家端订单数据(新)")
// @RequestMapping(value = "/mch/order/list", method = RequestMethod.POST)
// public CommonResult selectMchOrderPageList(@RequestBody JSONObject params) {
// Map<String, Object> 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) {

View File

@ -2186,145 +2186,146 @@ public class ShopOrderReturnServiceImpl extends BaseServiceImpl<ShopOrderReturnM
}
/**
* 商家处理退货退款支持全单或部分商品退货
* 商家处理退货退款支持全单/部分商品退货
*
* @param requestParams JSON格式请求参数包含:
* @param requestParams JSON参数包含:
* - order_id: 订单ID (必填)
* - order_return_vo: 退货商品信息 (可选用于部分商品退货)
* - reason: 退货理由说明 (可选)
* @return CommonResult 处理结果
* @throws ApiException 当用户未登录时抛出
* - order_return_vo: 退货商品信息 (可选)
* - reason: 退货理由 (可选)
* @return CommonResult 统一返回结果
* @throws ApiException 当用户未登录时抛出
*/
@Transactional
@Override
public CommonResult doRefundForMch(JSONObject requestParams) {
// 参数基础校验
if (requestParams == null || StrUtil.isBlank(requestParams.getStr("order_id"))) {
return CommonResult.failed("退货请求参数不完整");
}
final String orderId = requestParams.getStr("order_id");
final UserDto currentUser = getCurrentUser();
if (currentUser == null) {
throw new ApiException(ResultCode.NEED_LOGIN);
}
// 验证订单有效性
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().equals(currentUser.getStore_id())) {
return CommonResult.failed("无权处理其他店铺的订单");
}
// 检查是否存在处理中的退货单
QueryWrapper<ShopOrderReturn> 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<ShopOrderItem> orderItems = shopOrderItemService.find(
new QueryWrapper<ShopOrderItem>().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<ShopOrderReturn>()
.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<ShopOrderItem> orderItems = shopOrderItemService.find(
new QueryWrapper<ShopOrderItem>().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<Long> 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<ShopOrderReturn>().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<ShopOrderReturn>().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<Long> 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<ShopOrderReturn>().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();
}
}