修复:优化解绑优惠券和余额历史

This commit is contained in:
xiaowang
2026-03-27 11:34:53 +08:00
parent 7f92e302bb
commit b1fc6c3b79
9 changed files with 130 additions and 20 deletions
@@ -141,7 +141,7 @@ public class AdminUserController {
if (Integer.valueOf(4).equals(targetUser.getType())) {
throw new RuntimeException("管理员账号不允许解绑");
}
boolean success = userService.unbindUserInShop(id, currentUser.getShopId());
boolean success = userService.unbindUserInShopWithOperator(id, currentUser.getShopId(), currentUser.getType());
return Result.success(success);
}
@@ -117,7 +117,7 @@ public class UserController {
if (Integer.valueOf(4).equals(targetUser.getType())) {
throw new RuntimeException("管理员账号不允许解绑");
}
boolean success = userService.unbindUserInShop(id, currentUser.getShopId());
boolean success = userService.unbindUserInShopWithOperator(id, currentUser.getShopId(), currentUser.getType());
return Result.success(success);
}
@@ -1,9 +1,10 @@
package com.xiaowang.yuminailcore.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@@ -16,9 +17,11 @@ import java.time.LocalDateTime;
* @date 2026-02-13
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("t_balance_change_detail")
public class BalanceChangeDetailDO extends BaseEntity {
public class BalanceChangeDetailDO {
@TableId(type = IdType.AUTO)
private Long id;
/**
* 关联用户表主键
@@ -80,6 +80,8 @@ public interface UserService extends IService<UserDO> {
boolean deleteUserInShop(Long id, Long shopId);
boolean unbindUserInShop(Long id, Long shopId);
boolean unbindUserInShopWithOperator(Long id, Long shopId, Integer operatorType);
boolean updateUserAvatar(Long userId, String avatarUrl);
@@ -25,6 +25,7 @@ import com.xiaowang.yuminailcore.vo.UserCouponVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@@ -32,6 +33,7 @@ import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
/**
@@ -65,7 +67,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
public Long createCoupon(CouponDTO couponDTO, Long shopId) {
// 创建优惠券
CouponDO couponDO = new CouponDO();
couponDO.setCouponCode(couponDTO.getCouponCode());
couponDO.setCouponCode(resolveCouponCodeForCreate(couponDTO.getCouponCode()));
couponDO.setCouponName(couponDTO.getCouponName());
couponDO.setCouponType(couponDTO.getCouponType());
couponDO.setDiscountValue(couponDTO.getDiscountValue());
@@ -97,7 +99,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
return false;
}
couponDO.setCouponCode(couponDTO.getCouponCode());
couponDO.setCouponCode(resolveCouponCodeForUpdate(couponDO.getCouponCode(), couponDTO.getCouponCode()));
couponDO.setCouponName(couponDTO.getCouponName());
couponDO.setCouponType(couponDTO.getCouponType());
couponDO.setDiscountValue(couponDTO.getDiscountValue());
@@ -587,6 +589,33 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
return String.format("VIP_AUTO_SHOP_%d_%s", shopId, codeSuffix);
}
private String resolveCouponCodeForCreate(String requestedCode) {
if (StringUtils.hasText(requestedCode)) {
return requestedCode.trim();
}
return generateUniqueManualCouponCode();
}
private String resolveCouponCodeForUpdate(String existingCode, String requestedCode) {
if (StringUtils.hasText(requestedCode)) {
return requestedCode.trim();
}
if (StringUtils.hasText(existingCode)) {
return existingCode.trim();
}
return generateUniqueManualCouponCode();
}
private String generateUniqueManualCouponCode() {
for (int attempt = 0; attempt < 10; attempt++) {
String candidate = "CPN_" + UUID.randomUUID().toString().replace("-", "").substring(0, 12).toUpperCase();
if (lambdaQuery().eq(CouponDO::getCouponCode, candidate).count() == 0) {
return candidate;
}
}
throw new RuntimeException("生成优惠券编码失败,请稍后重试");
}
private CouponVO convertToCouponVO(CouponDO couponDO) {
CouponVO couponVO = new CouponVO();
couponVO.setId(couponDO.getId());
@@ -1105,11 +1105,33 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
@Override
@Transactional(rollbackFor = Exception.class)
public boolean unbindUserInShop(Long id, Long shopId) {
return unbindUserInShopWithOperator(id, shopId, null);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean unbindUserInShopWithOperator(Long id, Long shopId, Integer operatorType) {
Long normalizedShopId = normalizeShopId(shopId);
if (normalizedShopId == null) {
throw new RuntimeException("当前操作缺少店铺上下文");
}
return userShopBindingSupport.updateBindingStatus(id, normalizedShopId, 0);
UserDO userDO = userMapper.selectById(id);
if (userDO == null || Integer.valueOf(1).equals(userDO.getIsDelete())) {
return false;
}
boolean success = userShopBindingSupport.updateBindingStatus(id, normalizedShopId, 0);
if (!success) {
return false;
}
if (Integer.valueOf(2).equals(operatorType) && Integer.valueOf(3).equals(userDO.getType())) {
userDO.setType(0);
userDO.setUpdateTime(LocalDateTime.now());
userMapper.updateById(userDO);
syncDefaultPermissions(userDO.getId(), userDO.getType());
}
return true;
}
@Override