功能:落地待优化建议与VIP资产联动
This commit is contained in:
+68
@@ -2,11 +2,16 @@ package com.xiaowang.yuminailcore.controller;
|
||||
|
||||
import com.xiaowang.yuminailcore.annotation.RequirePermission;
|
||||
import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.dto.AdminUserBalanceChangeDTO;
|
||||
import com.xiaowang.yuminailcore.dto.AdminUserVipOverrideDTO;
|
||||
import com.xiaowang.yuminailcore.dto.UserDTO;
|
||||
import com.xiaowang.yuminailcore.entity.UserDO;
|
||||
import com.xiaowang.yuminailcore.service.UserAssetService;
|
||||
import com.xiaowang.yuminailcore.service.UserService;
|
||||
import com.xiaowang.yuminailcore.support.AdminScopeHelper;
|
||||
import com.xiaowang.yuminailcore.vo.BalanceChangeDetailVO;
|
||||
import com.xiaowang.yuminailcore.vo.PageResultVO;
|
||||
import com.xiaowang.yuminailcore.vo.UserAssetVO;
|
||||
import com.xiaowang.yuminailcore.vo.UserVO;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -22,6 +27,7 @@ import java.util.stream.Collectors;
|
||||
public class AdminUserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final UserAssetService userAssetService;
|
||||
private final AdminScopeHelper adminScopeHelper;
|
||||
|
||||
@GetMapping("/list")
|
||||
@@ -111,6 +117,59 @@ public class AdminUserController {
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/asset")
|
||||
@RequirePermission("user:balance:view")
|
||||
public Result<UserAssetVO> getUserAsset(HttpServletRequest request, @PathVariable Long id) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
UserVO targetUser = requireAccessibleUser(currentUser, id, "无权查看其他店铺用户资产");
|
||||
UserAssetVO assetVO = userAssetService.getUserAsset(targetUser.getId());
|
||||
return Result.success(assetVO);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/balance/history")
|
||||
@RequirePermission("user:balance:view")
|
||||
public Result<List<BalanceChangeDetailVO>> getUserBalanceHistory(HttpServletRequest request, @PathVariable Long id) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
UserVO targetUser = requireAccessibleUser(currentUser, id, "无权查看其他店铺用户余额流水");
|
||||
List<BalanceChangeDetailVO> history = userAssetService.listBalanceHistory(targetUser.getId());
|
||||
return Result.success(history);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/balance/change")
|
||||
@RequirePermission("user:balance:manage")
|
||||
public Result<UserAssetVO> changeUserBalance(
|
||||
HttpServletRequest request,
|
||||
@PathVariable Long id,
|
||||
@RequestBody AdminUserBalanceChangeDTO balanceChangeDTO
|
||||
) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
UserVO targetUser = requireAccessibleUser(currentUser, id, "无权调整其他店铺用户余额");
|
||||
UserAssetVO assetVO = userAssetService.changeBalance(currentUser.getId(), targetUser.getId(), balanceChangeDTO);
|
||||
return Result.success(assetVO);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/vip/override")
|
||||
@RequirePermission("user:vip:manage")
|
||||
public Result<UserAssetVO> overrideUserVip(
|
||||
HttpServletRequest request,
|
||||
@PathVariable Long id,
|
||||
@RequestBody AdminUserVipOverrideDTO vipOverrideDTO
|
||||
) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
UserVO targetUser = requireAccessibleUser(currentUser, id, "无权调整其他店铺用户 VIP");
|
||||
UserAssetVO assetVO = userAssetService.overrideVip(currentUser.getId(), targetUser.getId(), vipOverrideDTO);
|
||||
return Result.success(assetVO);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/vip/recalculate")
|
||||
@RequirePermission("user:vip:manage")
|
||||
public Result<UserAssetVO> recalculateUserVip(HttpServletRequest request, @PathVariable Long id) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||
UserVO targetUser = requireAccessibleUser(currentUser, id, "无权重算其他店铺用户 VIP");
|
||||
UserAssetVO assetVO = userAssetService.recalculateVip(currentUser.getId(), targetUser.getId());
|
||||
return Result.success(assetVO);
|
||||
}
|
||||
|
||||
@GetMapping("/info")
|
||||
public Result<UserVO> getUserInfo(HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
@@ -154,6 +213,15 @@ public class AdminUserController {
|
||||
return Result.success(artists);
|
||||
}
|
||||
|
||||
private UserVO requireAccessibleUser(UserDO currentUser, Long userId, String message) {
|
||||
UserVO targetUser = userService.getUserById(userId);
|
||||
if (targetUser == null) {
|
||||
throw new RuntimeException("目标用户不存在");
|
||||
}
|
||||
adminScopeHelper.ensureShopAccessible(currentUser, targetUser.getShopId(), message);
|
||||
return targetUser;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class StatusRequest {
|
||||
private Integer status;
|
||||
|
||||
+7
-2
@@ -104,8 +104,13 @@ public class CouponController {
|
||||
* 获取商品可用的优惠券
|
||||
*/
|
||||
@GetMapping("/available")
|
||||
public Result<List<UserCouponVO>> getAvailableCoupons(@RequestParam String productTags, @RequestParam Double productPrice, @RequestAttribute("userId") Long userId) {
|
||||
List<UserCouponVO> availableCoupons = couponService.getAvailableCoupons(userId, productTags, productPrice);
|
||||
public Result<List<UserCouponVO>> getAvailableCoupons(
|
||||
@RequestParam(required = false, defaultValue = "") String productTags,
|
||||
@RequestParam Double productPrice,
|
||||
@RequestParam(required = false) Long shopId,
|
||||
@RequestAttribute("userId") Long userId
|
||||
) {
|
||||
List<UserCouponVO> availableCoupons = couponService.getAvailableCoupons(userId, productTags, productPrice, shopId);
|
||||
return Result.success(availableCoupons);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ public class ProductController {
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
@RequirePermission("product:update")
|
||||
@RequirePermission("product:edit")
|
||||
public Result<Boolean> updateProduct(@PathVariable Long id, @RequestBody ProductDTO productDTO) {
|
||||
boolean success = productService.updateProduct(id, productDTO);
|
||||
return Result.success(success);
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ public class ShopController {
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
@RequirePermission("shop:update")
|
||||
@RequirePermission("shop:edit")
|
||||
public Result<Boolean> updateShop(@PathVariable Long id, @RequestBody ShopDTO shopDTO) {
|
||||
boolean success = shopService.updateShop(id, shopDTO);
|
||||
return Result.success(success);
|
||||
|
||||
@@ -150,6 +150,22 @@ public class UserController {
|
||||
return Result.success(new AvailabilityVO(available, message));
|
||||
}
|
||||
|
||||
@GetMapping("/check/login-account/{account}")
|
||||
public Result<LoginAccountAvailabilityVO> checkLoginAccountAvailability(@PathVariable String account) {
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getAccount, account)
|
||||
.eq(UserDO::getIsDelete, 0)
|
||||
.last("limit 1")
|
||||
);
|
||||
boolean exists = userDO != null;
|
||||
String nickname = exists ? userDO.getNickname() : null;
|
||||
String message = exists
|
||||
? (nickname != null && !nickname.isEmpty() ? "找到账号,登录后将进入:" + nickname : "账号已注册")
|
||||
: "该账号未注册";
|
||||
return Result.success(new LoginAccountAvailabilityVO(exists, nickname, message));
|
||||
}
|
||||
|
||||
@GetMapping("/check/phone/{phone}")
|
||||
public Result<AvailabilityVO> checkPhoneAvailability(@PathVariable String phone) {
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
@@ -416,4 +432,11 @@ public class UserController {
|
||||
private final boolean available;
|
||||
private final String message;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class LoginAccountAvailabilityVO {
|
||||
private final boolean exists;
|
||||
private final String nickname;
|
||||
private final String message;
|
||||
}
|
||||
}
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Data
|
||||
public class AdminUserBalanceChangeDTO {
|
||||
|
||||
private BigDecimal amount;
|
||||
|
||||
private String changeScene;
|
||||
|
||||
private Long relatedOrderId;
|
||||
|
||||
private String externalOrderNo;
|
||||
|
||||
private String remark;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AdminUserVipOverrideDTO {
|
||||
|
||||
private String vipLevel;
|
||||
|
||||
private String remark;
|
||||
}
|
||||
@@ -34,6 +34,14 @@ public class BalanceChangeDetailDTO {
|
||||
* 变动后的当前余额
|
||||
*/
|
||||
private BigDecimal currentBalance;
|
||||
private BigDecimal beforeBalance;
|
||||
private BigDecimal afterBalance;
|
||||
private String changeScene;
|
||||
private Long relatedOrderId;
|
||||
private String relatedOrderNo;
|
||||
private String externalOrderNo;
|
||||
private Long operatorId;
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 变动时间
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.xiaowang.yuminailcore.dto;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 用户余额信息表 DTO
|
||||
@@ -23,4 +24,6 @@ public class BalanceDTO {
|
||||
* 当前余额
|
||||
*/
|
||||
private BigDecimal amount;
|
||||
private BigDecimal totalRechargeAmount;
|
||||
private LocalDateTime lastRechargeTime;
|
||||
}
|
||||
|
||||
@@ -61,6 +61,9 @@ public class CouponDTO {
|
||||
* 所属店铺ID
|
||||
*/
|
||||
private Long shopId;
|
||||
private String couponScene;
|
||||
private String requiredVipLevel;
|
||||
private Integer autoMatch;
|
||||
|
||||
/**
|
||||
* 可用标签ID列表
|
||||
|
||||
@@ -18,6 +18,7 @@ public class OrderDTO {
|
||||
* 优惠券ID
|
||||
*/
|
||||
private Long couponId;
|
||||
private String couponSourceType;
|
||||
|
||||
/**
|
||||
* 订单商品列表
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* VIP权益信息表 DTO
|
||||
* 用于VIP权益信息的传输
|
||||
@@ -21,4 +23,7 @@ public class VipBenefitDTO {
|
||||
* 所需VIP等级
|
||||
*/
|
||||
private String requiredVipLevel;
|
||||
private Integer levelRank;
|
||||
private BigDecimal rechargeThreshold;
|
||||
private Integer status;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ import java.time.LocalDateTime;
|
||||
@Data
|
||||
public class VipInfoDTO {
|
||||
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* VIP等级(如青铜、白银)
|
||||
*/
|
||||
@@ -38,4 +40,5 @@ public class VipInfoDTO {
|
||||
* 关联VIP权益信息表主键
|
||||
*/
|
||||
private Long benefitId;
|
||||
private Integer manualOverride;
|
||||
}
|
||||
|
||||
+24
@@ -47,6 +47,30 @@ public class BalanceChangeDetailDO extends BaseEntity {
|
||||
/**
|
||||
* 变动时间
|
||||
*/
|
||||
@TableField("before_balance")
|
||||
private BigDecimal beforeBalance;
|
||||
|
||||
@TableField("after_balance")
|
||||
private BigDecimal afterBalance;
|
||||
|
||||
@TableField("change_scene")
|
||||
private String changeScene;
|
||||
|
||||
@TableField("related_order_id")
|
||||
private Long relatedOrderId;
|
||||
|
||||
@TableField("related_order_no")
|
||||
private String relatedOrderNo;
|
||||
|
||||
@TableField("external_order_no")
|
||||
private String externalOrderNo;
|
||||
|
||||
@TableField("operator_id")
|
||||
private Long operatorId;
|
||||
|
||||
@TableField("remark")
|
||||
private String remark;
|
||||
|
||||
@TableField("change_time")
|
||||
private LocalDateTime changeTime;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,12 @@ public class BalanceDO extends BaseEntity {
|
||||
/**
|
||||
* 最近一次更新时间
|
||||
*/
|
||||
@TableField("total_recharge_amount")
|
||||
private BigDecimal totalRechargeAmount;
|
||||
|
||||
@TableField("last_recharge_time")
|
||||
private LocalDateTime lastRechargeTime;
|
||||
|
||||
@TableField("last_update_time")
|
||||
private LocalDateTime lastUpdateTime;
|
||||
}
|
||||
|
||||
@@ -76,6 +76,12 @@ public class CouponDO extends BaseEntity {
|
||||
* 所属店铺ID
|
||||
*/
|
||||
private Long shopId;
|
||||
|
||||
private String couponScene;
|
||||
|
||||
private String requiredVipLevel;
|
||||
|
||||
private Integer autoMatch;
|
||||
|
||||
/**
|
||||
* 逻辑删除:0-未删,1-已删
|
||||
|
||||
@@ -15,8 +15,10 @@ public class OrderDO extends BaseEntity {
|
||||
private BigDecimal totalAmount;
|
||||
private BigDecimal actualAmount;
|
||||
private Long couponId;
|
||||
private Long userCouponId;
|
||||
private String couponSourceType;
|
||||
private Integer status;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
private Integer isDelete;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,13 @@ public class VipBenefitDO extends BaseEntity {
|
||||
*/
|
||||
@TableField("required_vip_level")
|
||||
private String requiredVipLevel;
|
||||
|
||||
@TableField("level_rank")
|
||||
private Integer levelRank;
|
||||
|
||||
@TableField("recharge_threshold")
|
||||
private java.math.BigDecimal rechargeThreshold;
|
||||
|
||||
@TableField("status")
|
||||
private Integer status;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,9 @@ import java.time.LocalDateTime;
|
||||
@TableName("t_vip_info")
|
||||
public class VipInfoDO extends BaseEntity {
|
||||
|
||||
@TableField("user_id")
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* VIP等级(如青铜、白银)
|
||||
*/
|
||||
@@ -48,4 +51,7 @@ public class VipInfoDO extends BaseEntity {
|
||||
*/
|
||||
@TableField("benefit_id")
|
||||
private Long benefitId;
|
||||
|
||||
@TableField("manual_override")
|
||||
private Integer manualOverride;
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ public interface CouponService extends IService<CouponDO> {
|
||||
* @param productPrice 商品价格
|
||||
* @return 可用优惠券列表
|
||||
*/
|
||||
List<UserCouponVO> getAvailableCoupons(Long userId, String productTags, Double productPrice);
|
||||
List<UserCouponVO> getAvailableCoupons(Long userId, String productTags, Double productPrice, Long shopId);
|
||||
|
||||
/**
|
||||
* 发放优惠券
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xiaowang.yuminailcore.service;
|
||||
|
||||
import com.xiaowang.yuminailcore.dto.AdminUserBalanceChangeDTO;
|
||||
import com.xiaowang.yuminailcore.dto.AdminUserVipOverrideDTO;
|
||||
import com.xiaowang.yuminailcore.vo.BalanceChangeDetailVO;
|
||||
import com.xiaowang.yuminailcore.vo.UserAssetVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserAssetService {
|
||||
|
||||
UserAssetVO getUserAsset(Long userId);
|
||||
|
||||
List<BalanceChangeDetailVO> listBalanceHistory(Long userId);
|
||||
|
||||
UserAssetVO changeBalance(Long operatorId, Long userId, AdminUserBalanceChangeDTO request);
|
||||
|
||||
UserAssetVO overrideVip(Long operatorId, Long userId, AdminUserVipOverrideDTO request);
|
||||
|
||||
UserAssetVO recalculateVip(Long operatorId, Long userId);
|
||||
}
|
||||
+24
@@ -33,6 +33,14 @@ public class BalanceChangeDetailServiceImpl extends ServiceImpl<BalanceChangeDet
|
||||
balanceChangeDetailDO.setAmount(balanceChangeDetailDTO.getAmount());
|
||||
balanceChangeDetailDO.setType(balanceChangeDetailDTO.getType());
|
||||
balanceChangeDetailDO.setCurrentBalance(balanceChangeDetailDTO.getCurrentBalance());
|
||||
balanceChangeDetailDO.setBeforeBalance(balanceChangeDetailDTO.getBeforeBalance());
|
||||
balanceChangeDetailDO.setAfterBalance(balanceChangeDetailDTO.getAfterBalance());
|
||||
balanceChangeDetailDO.setChangeScene(balanceChangeDetailDTO.getChangeScene());
|
||||
balanceChangeDetailDO.setRelatedOrderId(balanceChangeDetailDTO.getRelatedOrderId());
|
||||
balanceChangeDetailDO.setRelatedOrderNo(balanceChangeDetailDTO.getRelatedOrderNo());
|
||||
balanceChangeDetailDO.setExternalOrderNo(balanceChangeDetailDTO.getExternalOrderNo());
|
||||
balanceChangeDetailDO.setOperatorId(balanceChangeDetailDTO.getOperatorId());
|
||||
balanceChangeDetailDO.setRemark(balanceChangeDetailDTO.getRemark());
|
||||
balanceChangeDetailDO.setChangeTime(balanceChangeDetailDTO.getChangeTime());
|
||||
balanceChangeDetailMapper.insert(balanceChangeDetailDO);
|
||||
return balanceChangeDetailDO.getId();
|
||||
@@ -48,6 +56,14 @@ public class BalanceChangeDetailServiceImpl extends ServiceImpl<BalanceChangeDet
|
||||
balanceChangeDetailDO.setAmount(balanceChangeDetailDTO.getAmount());
|
||||
balanceChangeDetailDO.setType(balanceChangeDetailDTO.getType());
|
||||
balanceChangeDetailDO.setCurrentBalance(balanceChangeDetailDTO.getCurrentBalance());
|
||||
balanceChangeDetailDO.setBeforeBalance(balanceChangeDetailDTO.getBeforeBalance());
|
||||
balanceChangeDetailDO.setAfterBalance(balanceChangeDetailDTO.getAfterBalance());
|
||||
balanceChangeDetailDO.setChangeScene(balanceChangeDetailDTO.getChangeScene());
|
||||
balanceChangeDetailDO.setRelatedOrderId(balanceChangeDetailDTO.getRelatedOrderId());
|
||||
balanceChangeDetailDO.setRelatedOrderNo(balanceChangeDetailDTO.getRelatedOrderNo());
|
||||
balanceChangeDetailDO.setExternalOrderNo(balanceChangeDetailDTO.getExternalOrderNo());
|
||||
balanceChangeDetailDO.setOperatorId(balanceChangeDetailDTO.getOperatorId());
|
||||
balanceChangeDetailDO.setRemark(balanceChangeDetailDTO.getRemark());
|
||||
balanceChangeDetailDO.setChangeTime(balanceChangeDetailDTO.getChangeTime());
|
||||
return balanceChangeDetailMapper.updateById(balanceChangeDetailDO) > 0;
|
||||
}
|
||||
@@ -98,6 +114,14 @@ public class BalanceChangeDetailServiceImpl extends ServiceImpl<BalanceChangeDet
|
||||
balanceChangeDetailVO.setAmount(balanceChangeDetailDO.getAmount());
|
||||
balanceChangeDetailVO.setType(balanceChangeDetailDO.getType());
|
||||
balanceChangeDetailVO.setCurrentBalance(balanceChangeDetailDO.getCurrentBalance());
|
||||
balanceChangeDetailVO.setBeforeBalance(balanceChangeDetailDO.getBeforeBalance());
|
||||
balanceChangeDetailVO.setAfterBalance(balanceChangeDetailDO.getAfterBalance());
|
||||
balanceChangeDetailVO.setChangeScene(balanceChangeDetailDO.getChangeScene());
|
||||
balanceChangeDetailVO.setRelatedOrderId(balanceChangeDetailDO.getRelatedOrderId());
|
||||
balanceChangeDetailVO.setRelatedOrderNo(balanceChangeDetailDO.getRelatedOrderNo());
|
||||
balanceChangeDetailVO.setExternalOrderNo(balanceChangeDetailDO.getExternalOrderNo());
|
||||
balanceChangeDetailVO.setOperatorId(balanceChangeDetailDO.getOperatorId());
|
||||
balanceChangeDetailVO.setRemark(balanceChangeDetailDO.getRemark());
|
||||
balanceChangeDetailVO.setChangeTime(balanceChangeDetailDO.getChangeTime());
|
||||
return balanceChangeDetailVO;
|
||||
}
|
||||
|
||||
+6
@@ -32,6 +32,8 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, BalanceDO> im
|
||||
BalanceDO balanceDO = new BalanceDO();
|
||||
balanceDO.setUserId(balanceDTO.getUserId());
|
||||
balanceDO.setAmount(balanceDTO.getAmount());
|
||||
balanceDO.setTotalRechargeAmount(balanceDTO.getTotalRechargeAmount());
|
||||
balanceDO.setLastRechargeTime(balanceDTO.getLastRechargeTime());
|
||||
balanceDO.setLastUpdateTime(LocalDateTime.now());
|
||||
balanceMapper.insert(balanceDO);
|
||||
return balanceDO.getId();
|
||||
@@ -45,6 +47,8 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, BalanceDO> im
|
||||
}
|
||||
balanceDO.setUserId(balanceDTO.getUserId());
|
||||
balanceDO.setAmount(balanceDTO.getAmount());
|
||||
balanceDO.setTotalRechargeAmount(balanceDTO.getTotalRechargeAmount());
|
||||
balanceDO.setLastRechargeTime(balanceDTO.getLastRechargeTime());
|
||||
balanceDO.setLastUpdateTime(LocalDateTime.now());
|
||||
return balanceMapper.updateById(balanceDO) > 0;
|
||||
}
|
||||
@@ -94,6 +98,8 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, BalanceDO> im
|
||||
balanceVO.setId(balanceDO.getId());
|
||||
balanceVO.setUserId(balanceDO.getUserId());
|
||||
balanceVO.setAmount(balanceDO.getAmount());
|
||||
balanceVO.setTotalRechargeAmount(balanceDO.getTotalRechargeAmount());
|
||||
balanceVO.setLastRechargeTime(balanceDO.getLastRechargeTime());
|
||||
balanceVO.setCreateTime(balanceDO.getCreateTime());
|
||||
balanceVO.setLastUpdateTime(balanceDO.getLastUpdateTime());
|
||||
return balanceVO;
|
||||
|
||||
+145
-52
@@ -8,11 +8,13 @@ import com.xiaowang.yuminailcore.dto.CouponDTO;
|
||||
import com.xiaowang.yuminailcore.entity.CouponDO;
|
||||
import com.xiaowang.yuminailcore.entity.CouponTagDO;
|
||||
import com.xiaowang.yuminailcore.entity.TagDO;
|
||||
import com.xiaowang.yuminailcore.entity.UserDO;
|
||||
import com.xiaowang.yuminailcore.entity.UserCouponDO;
|
||||
import com.xiaowang.yuminailcore.mapper.CouponMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.CouponTagMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.ShopMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.TagMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.UserMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.UserCouponMapper;
|
||||
import com.xiaowang.yuminailcore.service.CouponService;
|
||||
import com.xiaowang.yuminailcore.vo.CouponVO;
|
||||
@@ -26,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -35,11 +38,17 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> implements CouponService {
|
||||
|
||||
private static final String COUPON_SOURCE_USER = "USER_COUPON";
|
||||
private static final String COUPON_SOURCE_SYSTEM_VIP = "SYSTEM_VIP";
|
||||
private static final String COUPON_SCENE_VIP_AUTO = "VIP_AUTO";
|
||||
private static final String VIP_LEVEL_NORMAL = "普通";
|
||||
|
||||
private final CouponTagMapper couponTagMapper;
|
||||
private final UserCouponMapper userCouponMapper;
|
||||
private final TagMapper tagMapper;
|
||||
private final ShopMapper shopMapper;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@@ -58,6 +67,9 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
couponDO.setEndTime(couponDTO.getEndTime());
|
||||
couponDO.setStatus(1);
|
||||
couponDO.setShopId(shopId);
|
||||
couponDO.setCouponScene(couponDTO.getCouponScene());
|
||||
couponDO.setRequiredVipLevel(couponDTO.getRequiredVipLevel());
|
||||
couponDO.setAutoMatch(couponDTO.getAutoMatch());
|
||||
save(couponDO);
|
||||
|
||||
// 处理标签关联
|
||||
@@ -85,6 +97,9 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
couponDO.setStartTime(couponDTO.getStartTime());
|
||||
couponDO.setEndTime(couponDTO.getEndTime());
|
||||
couponDO.setShopId(shopId);
|
||||
couponDO.setCouponScene(couponDTO.getCouponScene());
|
||||
couponDO.setRequiredVipLevel(couponDTO.getRequiredVipLevel());
|
||||
couponDO.setAutoMatch(couponDTO.getAutoMatch());
|
||||
updateById(couponDO);
|
||||
|
||||
// 处理标签关联
|
||||
@@ -117,10 +132,13 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
@Override
|
||||
public PageResultVO<CouponVO> listCoupons(Integer page, Integer size, Long shopId) {
|
||||
Page<CouponDO> pageInfo = new Page<>(page, size);
|
||||
IPage<CouponDO> couponPage = page(pageInfo, new LambdaQueryWrapper<CouponDO>()
|
||||
LambdaQueryWrapper<CouponDO> wrapper = new LambdaQueryWrapper<CouponDO>()
|
||||
.eq(CouponDO::getIsDelete, 0)
|
||||
.orderByDesc(CouponDO::getCreateTime)
|
||||
);
|
||||
.orderByDesc(CouponDO::getCreateTime);
|
||||
if (shopId != null) {
|
||||
wrapper.eq(CouponDO::getShopId, shopId);
|
||||
}
|
||||
IPage<CouponDO> couponPage = page(pageInfo, wrapper);
|
||||
|
||||
List<CouponVO> couponVOs = couponPage.getRecords().stream()
|
||||
.map(this::convertToCouponVO)
|
||||
@@ -159,6 +177,9 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
if (couponDO.getStatus() != 1) {
|
||||
return false;
|
||||
}
|
||||
if (isVipAutoCoupon(couponDO)) {
|
||||
throw new RuntimeException("VIP 自动券为系统模板券,不支持手动发放");
|
||||
}
|
||||
|
||||
// 检查是否已经分配
|
||||
UserCouponDO existing = userCouponMapper.selectOne(new LambdaQueryWrapper<UserCouponDO>()
|
||||
@@ -193,7 +214,11 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
|
||||
List<UserCouponDO> userCouponDOs = userCouponMapper.selectList(queryWrapper);
|
||||
return userCouponDOs.stream()
|
||||
.map(this::convertToUserCouponVO)
|
||||
.map(userCouponDO -> {
|
||||
CouponDO couponDO = getById(userCouponDO.getCouponId());
|
||||
return couponDO == null ? null : convertToUserCouponVO(userCouponDO, couponDO);
|
||||
})
|
||||
.filter(java.util.Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -238,48 +263,49 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserCouponVO> getAvailableCoupons(Long userId, String productTags, Double productPrice) {
|
||||
// 获取用户未使用的优惠券
|
||||
public List<UserCouponVO> getAvailableCoupons(Long userId, String productTags, Double productPrice, Long shopId) {
|
||||
BigDecimal orderAmount = productPrice == null ? BigDecimal.ZERO : BigDecimal.valueOf(productPrice);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
List<UserCouponVO> availableCoupons = new ArrayList<>();
|
||||
|
||||
List<UserCouponDO> userCouponDOs = userCouponMapper.selectList(new LambdaQueryWrapper<UserCouponDO>()
|
||||
.eq(UserCouponDO::getUserId, userId)
|
||||
.eq(UserCouponDO::getStatus, 1)
|
||||
.eq(UserCouponDO::getIsDelete, 0)
|
||||
);
|
||||
|
||||
List<UserCouponVO> availableCoupons = new ArrayList<>();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
|
||||
for (UserCouponDO userCouponDO : userCouponDOs) {
|
||||
CouponDO couponDO = getById(userCouponDO.getCouponId());
|
||||
if (couponDO == null || couponDO.getIsDelete() == 1) {
|
||||
if (!isCouponApplicable(couponDO, now, orderAmount, productTags, shopId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查优惠券是否在有效期内
|
||||
if (now.isBefore(couponDO.getStartTime()) || now.isAfter(couponDO.getEndTime())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查优惠券是否可用
|
||||
if (couponDO.getStatus() != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查满减券的最低消费金额
|
||||
if (couponDO.getCouponType() == 2) {
|
||||
if (couponDO.getMinAmount().doubleValue() > productPrice) {
|
||||
availableCoupons.add(convertToUserCouponVO(userCouponDO, couponDO));
|
||||
}
|
||||
|
||||
UserDO userDO = userMapper.selectById(userId);
|
||||
String currentVipLevel = userDO != null ? userDO.getLevel() : null;
|
||||
if (currentVipLevel != null && !currentVipLevel.isBlank() && !VIP_LEVEL_NORMAL.equals(currentVipLevel)) {
|
||||
List<CouponDO> vipCoupons = list(new LambdaQueryWrapper<CouponDO>()
|
||||
.eq(CouponDO::getIsDelete, 0)
|
||||
.eq(CouponDO::getStatus, 1)
|
||||
.eq(CouponDO::getAutoMatch, 1)
|
||||
.eq(CouponDO::getCouponScene, COUPON_SCENE_VIP_AUTO)
|
||||
.eq(CouponDO::getRequiredVipLevel, currentVipLevel)
|
||||
.orderByDesc(CouponDO::getDiscountValue)
|
||||
.orderByAsc(CouponDO::getEndTime)
|
||||
);
|
||||
|
||||
for (CouponDO couponDO : vipCoupons) {
|
||||
if (!isCouponApplicable(couponDO, now, orderAmount, productTags, shopId)) {
|
||||
continue;
|
||||
}
|
||||
availableCoupons.add(convertToSystemVipCouponVO(couponDO));
|
||||
}
|
||||
|
||||
// 检查标签是否匹配
|
||||
if (!checkTagMatch(couponDO.getId(), productTags)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
availableCoupons.add(convertToUserCouponVO(userCouponDO));
|
||||
}
|
||||
|
||||
|
||||
availableCoupons.sort(Comparator
|
||||
.comparing((UserCouponVO item) -> calculateDiscountAmount(item, orderAmount)).reversed()
|
||||
.thenComparing(UserCouponVO::getEndTime, Comparator.nullsLast(LocalDateTime::compareTo)));
|
||||
return availableCoupons;
|
||||
}
|
||||
|
||||
@@ -296,6 +322,9 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
if (couponDO.getStatus() != 1) {
|
||||
return false;
|
||||
}
|
||||
if (isVipAutoCoupon(couponDO)) {
|
||||
throw new RuntimeException("VIP 自动券为系统模板券,不支持手动发放");
|
||||
}
|
||||
|
||||
// 检查优惠券数量是否足够
|
||||
int totalNeeded = userIds.size() * quantity;
|
||||
@@ -480,6 +509,9 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
couponVO.setEndTime(couponDO.getEndTime());
|
||||
couponVO.setStatus(couponDO.getStatus());
|
||||
couponVO.setShopId(couponDO.getShopId());
|
||||
couponVO.setCouponScene(couponDO.getCouponScene());
|
||||
couponVO.setRequiredVipLevel(couponDO.getRequiredVipLevel());
|
||||
couponVO.setAutoMatch(couponDO.getAutoMatch());
|
||||
|
||||
// 获取店铺名称
|
||||
if (couponDO.getShopId() != null) {
|
||||
@@ -535,23 +567,8 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
/**
|
||||
* 转换为UserCouponVO
|
||||
*/
|
||||
private UserCouponVO convertToUserCouponVO(UserCouponDO userCouponDO) {
|
||||
UserCouponVO userCouponVO = new UserCouponVO();
|
||||
userCouponVO.setId(userCouponDO.getId());
|
||||
userCouponVO.setCouponId(userCouponDO.getCouponId());
|
||||
|
||||
CouponDO couponDO = getById(userCouponDO.getCouponId());
|
||||
if (couponDO != null) {
|
||||
userCouponVO.setCouponName(couponDO.getCouponName());
|
||||
userCouponVO.setCouponType(couponDO.getCouponType());
|
||||
userCouponVO.setCouponTypeName(couponDO.getCouponType() == 1 ? "折扣券" : "满减券");
|
||||
userCouponVO.setDiscountValue(couponDO.getDiscountValue() != null ? couponDO.getDiscountValue().toString() : "");
|
||||
userCouponVO.setMinAmount(couponDO.getMinAmount() != null ? couponDO.getMinAmount().toString() : "");
|
||||
userCouponVO.setReduceAmount(couponDO.getReduceAmount() != null ? couponDO.getReduceAmount().toString() : "");
|
||||
userCouponVO.setStartTime(couponDO.getStartTime());
|
||||
userCouponVO.setEndTime(couponDO.getEndTime());
|
||||
}
|
||||
|
||||
private UserCouponVO convertToUserCouponVO(UserCouponDO userCouponDO, CouponDO couponDO) {
|
||||
UserCouponVO userCouponVO = buildCouponOption(userCouponDO.getId(), couponDO, COUPON_SOURCE_USER, false);
|
||||
userCouponVO.setStatus(userCouponDO.getStatus());
|
||||
switch (userCouponDO.getStatus()) {
|
||||
case 1:
|
||||
@@ -566,10 +583,86 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
default:
|
||||
userCouponVO.setStatusName("未知");
|
||||
}
|
||||
|
||||
userCouponVO.setObtainTime(userCouponDO.getObtainTime());
|
||||
userCouponVO.setUseTime(userCouponDO.getUseTime());
|
||||
|
||||
return userCouponVO;
|
||||
}
|
||||
|
||||
private UserCouponVO convertToSystemVipCouponVO(CouponDO couponDO) {
|
||||
UserCouponVO userCouponVO = buildCouponOption(couponDO.getId(), couponDO, COUPON_SOURCE_SYSTEM_VIP, true);
|
||||
userCouponVO.setStatus(1);
|
||||
userCouponVO.setStatusName("系统自动匹配");
|
||||
return userCouponVO;
|
||||
}
|
||||
|
||||
private UserCouponVO buildCouponOption(Long optionId, CouponDO couponDO, String sourceType, boolean autoMatched) {
|
||||
UserCouponVO userCouponVO = new UserCouponVO();
|
||||
userCouponVO.setId(optionId);
|
||||
userCouponVO.setCouponId(couponDO.getId());
|
||||
userCouponVO.setCouponName(couponDO.getCouponName());
|
||||
userCouponVO.setCouponType(couponDO.getCouponType());
|
||||
userCouponVO.setCouponTypeName(couponDO.getCouponType() == 1 ? "折扣券" : "满减券");
|
||||
userCouponVO.setDiscountValue(couponDO.getDiscountValue() != null ? couponDO.getDiscountValue().toString() : "");
|
||||
userCouponVO.setMinAmount(couponDO.getMinAmount() != null ? couponDO.getMinAmount().toString() : "");
|
||||
userCouponVO.setReduceAmount(couponDO.getReduceAmount() != null ? couponDO.getReduceAmount().toString() : "");
|
||||
userCouponVO.setStartTime(couponDO.getStartTime());
|
||||
userCouponVO.setEndTime(couponDO.getEndTime());
|
||||
userCouponVO.setSourceType(sourceType);
|
||||
userCouponVO.setRequiredVipLevel(couponDO.getRequiredVipLevel());
|
||||
userCouponVO.setAutoMatched(autoMatched);
|
||||
return userCouponVO;
|
||||
}
|
||||
|
||||
private boolean isCouponApplicable(CouponDO couponDO, LocalDateTime now, BigDecimal orderAmount, String productTags, Long shopId) {
|
||||
if (couponDO == null || Integer.valueOf(1).equals(couponDO.getIsDelete())) {
|
||||
return false;
|
||||
}
|
||||
if (couponDO.getStatus() == null || couponDO.getStatus() != 1) {
|
||||
return false;
|
||||
}
|
||||
if (couponDO.getStartTime() != null && now.isBefore(couponDO.getStartTime())) {
|
||||
return false;
|
||||
}
|
||||
if (couponDO.getEndTime() != null && now.isAfter(couponDO.getEndTime())) {
|
||||
return false;
|
||||
}
|
||||
if (!matchesShop(couponDO, shopId)) {
|
||||
return false;
|
||||
}
|
||||
if (couponDO.getCouponType() == 2
|
||||
&& couponDO.getMinAmount() != null
|
||||
&& couponDO.getMinAmount().compareTo(orderAmount) > 0) {
|
||||
return false;
|
||||
}
|
||||
return checkTagMatch(couponDO.getId(), productTags);
|
||||
}
|
||||
|
||||
private boolean matchesShop(CouponDO couponDO, Long shopId) {
|
||||
if (shopId == null || couponDO.getShopId() == null) {
|
||||
return true;
|
||||
}
|
||||
return shopId.equals(couponDO.getShopId());
|
||||
}
|
||||
|
||||
private boolean isVipAutoCoupon(CouponDO couponDO) {
|
||||
return couponDO != null
|
||||
&& Integer.valueOf(1).equals(couponDO.getAutoMatch())
|
||||
&& COUPON_SCENE_VIP_AUTO.equals(couponDO.getCouponScene());
|
||||
}
|
||||
|
||||
private BigDecimal calculateDiscountAmount(UserCouponVO couponVO, BigDecimal orderAmount) {
|
||||
if (couponVO == null || orderAmount == null || orderAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
if (couponVO.getCouponType() == 1 && couponVO.getDiscountValue() != null && !couponVO.getDiscountValue().isBlank()) {
|
||||
BigDecimal discountValue = new BigDecimal(couponVO.getDiscountValue());
|
||||
BigDecimal actualAmount = orderAmount.multiply(discountValue)
|
||||
.divide(BigDecimal.TEN, 2, BigDecimal.ROUND_HALF_UP);
|
||||
return orderAmount.subtract(actualAmount);
|
||||
}
|
||||
if (couponVO.getCouponType() == 2 && couponVO.getReduceAmount() != null && !couponVO.getReduceAmount().isBlank()) {
|
||||
return new BigDecimal(couponVO.getReduceAmount());
|
||||
}
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
+175
-64
@@ -52,6 +52,9 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
private static final DateTimeFormatter ORDER_NO_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
||||
private static final DateTimeFormatter APPOINTMENT_OUTPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
private static final String COUPON_SOURCE_NONE = "NONE";
|
||||
private static final String COUPON_SOURCE_USER = "USER_COUPON";
|
||||
private static final String COUPON_SOURCE_SYSTEM_VIP = "SYSTEM_VIP";
|
||||
private static final List<DateTimeFormatter> APPOINTMENT_INPUT_FORMATTERS = Arrays.asList(
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"),
|
||||
@@ -169,31 +172,26 @@ public class OrderServiceImpl implements OrderService {
|
||||
throw new RuntimeException("订单金额无效");
|
||||
}
|
||||
|
||||
String productTagNames = String.join(",", selectedTagNames);
|
||||
List<UserCouponVO> availableCouponOptions = couponService.getAvailableCoupons(
|
||||
userId,
|
||||
productTagNames,
|
||||
totalAmount.doubleValue(),
|
||||
shopId
|
||||
);
|
||||
|
||||
BigDecimal actualAmount = totalAmount;
|
||||
Long userCouponId = orderDTO.getCouponId();
|
||||
Long couponId = null;
|
||||
if (userCouponId != null && userCouponId > 0) {
|
||||
UserCouponDO userCoupon = userCouponMapper.selectOne(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("id", userCouponId)
|
||||
.eq("user_id", userId)
|
||||
.eq("status", 1)
|
||||
);
|
||||
Long userCouponId = null;
|
||||
String couponSourceType = null;
|
||||
|
||||
if (userCoupon == null) {
|
||||
throw new RuntimeException("优惠券不存在或已失效");
|
||||
}
|
||||
CouponSelection couponSelection = resolveRequestedCoupon(orderDTO, availableCouponOptions);
|
||||
if (couponSelection == null && !isExplicitNoCoupon(orderDTO)) {
|
||||
couponSelection = selectBestSystemVipCoupon(availableCouponOptions, totalAmount);
|
||||
}
|
||||
|
||||
String productTagNames = String.join(",", selectedTagNames);
|
||||
boolean couponAvailable = couponService.getAvailableCoupons(userId, productTagNames, totalAmount.doubleValue()).stream()
|
||||
.map(UserCouponVO::getId)
|
||||
.anyMatch(id -> userCouponId.equals(id));
|
||||
if (!couponAvailable) {
|
||||
throw new RuntimeException("当前订单不可使用所选优惠券");
|
||||
}
|
||||
|
||||
couponId = userCoupon.getCouponId();
|
||||
CouponDO coupon = couponMapper.selectById(couponId);
|
||||
if (couponSelection != null) {
|
||||
CouponDO coupon = couponMapper.selectById(couponSelection.getCouponId());
|
||||
if (coupon == null || Integer.valueOf(1).equals(coupon.getIsDelete()) || coupon.getStatus() != 1) {
|
||||
throw new RuntimeException("优惠券不存在或已失效");
|
||||
}
|
||||
@@ -201,19 +199,14 @@ public class OrderServiceImpl implements OrderService {
|
||||
throw new RuntimeException("该优惠券不适用于当前店铺");
|
||||
}
|
||||
|
||||
if (coupon.getCouponType() == 1) {
|
||||
actualAmount = totalAmount.multiply(coupon.getDiscountValue())
|
||||
.divide(BigDecimal.valueOf(10), 2, BigDecimal.ROUND_HALF_UP);
|
||||
} else if (coupon.getCouponType() == 2 && totalAmount.compareTo(coupon.getMinAmount()) >= 0) {
|
||||
actualAmount = totalAmount.subtract(coupon.getReduceAmount());
|
||||
if (actualAmount.compareTo(BigDecimal.ZERO) < 0) {
|
||||
actualAmount = BigDecimal.ZERO;
|
||||
}
|
||||
}
|
||||
actualAmount = applyCouponDiscount(totalAmount, coupon);
|
||||
couponId = coupon.getId();
|
||||
userCouponId = couponSelection.getUserCouponId();
|
||||
couponSourceType = couponSelection.getSourceType();
|
||||
|
||||
userCoupon.setStatus(2);
|
||||
userCoupon.setUseTime(LocalDateTime.now());
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
if (COUPON_SOURCE_USER.equals(couponSourceType)) {
|
||||
consumeUserCoupon(userCouponId, userId);
|
||||
}
|
||||
}
|
||||
|
||||
String orderNo = "ORD" + LocalDateTime.now().format(ORDER_NO_TIME_FORMATTER)
|
||||
@@ -226,6 +219,8 @@ public class OrderServiceImpl implements OrderService {
|
||||
order.setTotalAmount(totalAmount);
|
||||
order.setActualAmount(actualAmount);
|
||||
order.setCouponId(couponId);
|
||||
order.setUserCouponId(userCouponId);
|
||||
order.setCouponSourceType(couponSourceType);
|
||||
order.setStatus(1);
|
||||
order.setIsDelete(0);
|
||||
orderMapper.insert(order);
|
||||
@@ -287,22 +282,7 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
order.setStatus(3);
|
||||
orderMapper.updateById(order);
|
||||
|
||||
if (order.getCouponId() != null && order.getCouponId() > 0) {
|
||||
List<UserCouponDO> userCoupons = userCouponMapper.selectList(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("user_id", userId)
|
||||
.eq("coupon_id", order.getCouponId())
|
||||
.eq("status", 2)
|
||||
);
|
||||
if (!userCoupons.isEmpty()) {
|
||||
userCoupons.sort((a, b) -> b.getUseTime().compareTo(a.getUseTime()));
|
||||
UserCouponDO userCoupon = userCoupons.get(0);
|
||||
userCoupon.setStatus(1);
|
||||
userCoupon.setUseTime(null);
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
}
|
||||
}
|
||||
restoreUserCoupon(order, userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -321,21 +301,8 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
order.setStatus(status);
|
||||
orderMapper.updateById(order);
|
||||
|
||||
if (status == 3 && order.getCouponId() != null && order.getCouponId() > 0) {
|
||||
List<UserCouponDO> userCoupons = userCouponMapper.selectList(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("user_id", order.getUserId())
|
||||
.eq("coupon_id", order.getCouponId())
|
||||
.eq("status", 2)
|
||||
);
|
||||
if (!userCoupons.isEmpty()) {
|
||||
userCoupons.sort((a, b) -> b.getUseTime().compareTo(a.getUseTime()));
|
||||
UserCouponDO userCoupon = userCoupons.get(0);
|
||||
userCoupon.setStatus(1);
|
||||
userCoupon.setUseTime(null);
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
}
|
||||
if (status == 3) {
|
||||
restoreUserCoupon(order, order.getUserId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,6 +369,8 @@ public class OrderServiceImpl implements OrderService {
|
||||
orderVO.setTotalAmount(order.getTotalAmount());
|
||||
orderVO.setActualAmount(order.getActualAmount());
|
||||
orderVO.setCouponId(order.getCouponId());
|
||||
orderVO.setUserCouponId(order.getUserCouponId());
|
||||
orderVO.setCouponSourceType(order.getCouponSourceType());
|
||||
orderVO.setStatus(order.getStatus());
|
||||
|
||||
switch (order.getStatus()) {
|
||||
@@ -692,6 +661,148 @@ public class OrderServiceImpl implements OrderService {
|
||||
return appointmentTime.format(APPOINTMENT_OUTPUT_FORMATTER);
|
||||
}
|
||||
|
||||
private CouponSelection resolveRequestedCoupon(OrderDTO orderDTO, List<UserCouponVO> availableCoupons) {
|
||||
if (orderDTO == null || orderDTO.getCouponId() == null || orderDTO.getCouponId() <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String requestedSourceType = normalizeCouponSourceType(orderDTO.getCouponSourceType());
|
||||
List<UserCouponVO> matchedCoupons = availableCoupons.stream()
|
||||
.filter(coupon -> orderDTO.getCouponId().equals(coupon.getId()))
|
||||
.filter(coupon -> requestedSourceType == null || requestedSourceType.equals(coupon.getSourceType()))
|
||||
.toList();
|
||||
|
||||
if (matchedCoupons.isEmpty()) {
|
||||
throw new RuntimeException("当前订单不可使用所选优惠券");
|
||||
}
|
||||
|
||||
UserCouponVO selectedCoupon = matchedCoupons.stream()
|
||||
.filter(coupon -> COUPON_SOURCE_USER.equals(coupon.getSourceType()))
|
||||
.findFirst()
|
||||
.orElse(matchedCoupons.get(0));
|
||||
|
||||
return new CouponSelection(
|
||||
selectedCoupon.getSourceType(),
|
||||
selectedCoupon.getCouponId(),
|
||||
COUPON_SOURCE_USER.equals(selectedCoupon.getSourceType()) ? selectedCoupon.getId() : null
|
||||
);
|
||||
}
|
||||
|
||||
private CouponSelection selectBestSystemVipCoupon(List<UserCouponVO> availableCoupons, BigDecimal totalAmount) {
|
||||
return availableCoupons.stream()
|
||||
.filter(coupon -> COUPON_SOURCE_SYSTEM_VIP.equals(coupon.getSourceType()))
|
||||
.max(Comparator.comparing(coupon -> calculateCouponDiscountAmount(coupon, totalAmount)))
|
||||
.map(coupon -> new CouponSelection(coupon.getSourceType(), coupon.getCouponId(), null))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private String normalizeCouponSourceType(String couponSourceType) {
|
||||
if (couponSourceType == null || couponSourceType.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return couponSourceType.trim().toUpperCase();
|
||||
}
|
||||
|
||||
private boolean isExplicitNoCoupon(OrderDTO orderDTO) {
|
||||
return orderDTO != null
|
||||
&& orderDTO.getCouponId() == null
|
||||
&& COUPON_SOURCE_NONE.equalsIgnoreCase(orderDTO.getCouponSourceType());
|
||||
}
|
||||
|
||||
private BigDecimal calculateCouponDiscountAmount(UserCouponVO coupon, BigDecimal totalAmount) {
|
||||
if (coupon == null || totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
if (coupon.getCouponType() == 1 && coupon.getDiscountValue() != null && !coupon.getDiscountValue().isBlank()) {
|
||||
BigDecimal discountValue = new BigDecimal(coupon.getDiscountValue());
|
||||
BigDecimal discountedAmount = totalAmount.multiply(discountValue)
|
||||
.divide(BigDecimal.TEN, 2, BigDecimal.ROUND_HALF_UP);
|
||||
return totalAmount.subtract(discountedAmount);
|
||||
}
|
||||
|
||||
if (coupon.getCouponType() == 2 && coupon.getReduceAmount() != null && !coupon.getReduceAmount().isBlank()) {
|
||||
return new BigDecimal(coupon.getReduceAmount());
|
||||
}
|
||||
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
private BigDecimal applyCouponDiscount(BigDecimal totalAmount, CouponDO coupon) {
|
||||
if (coupon.getCouponType() == 1 && coupon.getDiscountValue() != null) {
|
||||
return totalAmount.multiply(coupon.getDiscountValue())
|
||||
.divide(BigDecimal.TEN, 2, BigDecimal.ROUND_HALF_UP);
|
||||
}
|
||||
if (coupon.getCouponType() == 2 && coupon.getReduceAmount() != null) {
|
||||
BigDecimal actualAmount = totalAmount.subtract(coupon.getReduceAmount());
|
||||
return actualAmount.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : actualAmount;
|
||||
}
|
||||
return totalAmount;
|
||||
}
|
||||
|
||||
private void consumeUserCoupon(Long userCouponId, Long userId) {
|
||||
if (userCouponId == null) {
|
||||
throw new RuntimeException("用户优惠券信息缺失");
|
||||
}
|
||||
|
||||
UserCouponDO userCoupon = userCouponMapper.selectById(userCouponId);
|
||||
if (userCoupon == null || Integer.valueOf(1).equals(userCoupon.getIsDelete())) {
|
||||
throw new RuntimeException("优惠券不存在或已失效");
|
||||
}
|
||||
if (!userId.equals(userCoupon.getUserId())) {
|
||||
throw new RuntimeException("优惠券不属于当前用户");
|
||||
}
|
||||
if (userCoupon.getStatus() == null || userCoupon.getStatus() != 1) {
|
||||
throw new RuntimeException("优惠券已被使用或不可用");
|
||||
}
|
||||
|
||||
userCoupon.setStatus(2);
|
||||
userCoupon.setUseTime(LocalDateTime.now());
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
}
|
||||
|
||||
private void restoreUserCoupon(OrderDO order, Long userId) {
|
||||
if (order == null
|
||||
|| !COUPON_SOURCE_USER.equals(order.getCouponSourceType())
|
||||
|| order.getUserCouponId() == null
|
||||
|| userId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UserCouponDO userCoupon = userCouponMapper.selectById(order.getUserCouponId());
|
||||
if (userCoupon == null || !userId.equals(userCoupon.getUserId()) || userCoupon.getStatus() == null || userCoupon.getStatus() != 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
userCoupon.setStatus(1);
|
||||
userCoupon.setUseTime(null);
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
}
|
||||
|
||||
private static final class CouponSelection {
|
||||
private final String sourceType;
|
||||
private final Long couponId;
|
||||
private final Long userCouponId;
|
||||
|
||||
private CouponSelection(String sourceType, Long couponId, Long userCouponId) {
|
||||
this.sourceType = sourceType;
|
||||
this.couponId = couponId;
|
||||
this.userCouponId = userCouponId;
|
||||
}
|
||||
|
||||
private String getSourceType() {
|
||||
return sourceType;
|
||||
}
|
||||
|
||||
private Long getCouponId() {
|
||||
return couponId;
|
||||
}
|
||||
|
||||
private Long getUserCouponId() {
|
||||
return userCouponId;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AppointmentSlot {
|
||||
private final String productTitle;
|
||||
private final LocalDateTime startTime;
|
||||
|
||||
+19
@@ -113,6 +113,16 @@ public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permiss
|
||||
|
||||
@Override
|
||||
public List<PermissionVO> listUserPermissions(Long userId) {
|
||||
UserDO userDO = userMapper.selectById(userId);
|
||||
if (userDO != null && Integer.valueOf(4).equals(userDO.getType())) {
|
||||
return permissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(PermissionDO.class)
|
||||
.eq(PermissionDO::getIsDelete, 0))
|
||||
.stream()
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
List<UserPermissionDO> userPermissions = userPermissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(UserPermissionDO.class)
|
||||
.eq(UserPermissionDO::getUserId, userId)
|
||||
@@ -190,6 +200,15 @@ public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permiss
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(Long userId, String permissionCode) {
|
||||
UserDO userDO = userMapper.selectById(userId);
|
||||
if (userDO != null && Integer.valueOf(4).equals(userDO.getType())) {
|
||||
return permissionMapper.selectCount(
|
||||
Wrappers.lambdaQuery(PermissionDO.class)
|
||||
.eq(PermissionDO::getPermissionCode, permissionCode)
|
||||
.eq(PermissionDO::getIsDelete, 0)
|
||||
) > 0;
|
||||
}
|
||||
|
||||
List<UserPermissionDO> userPermissions = userPermissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(UserPermissionDO.class)
|
||||
.eq(UserPermissionDO::getUserId, userId)
|
||||
|
||||
+339
@@ -0,0 +1,339 @@
|
||||
package com.xiaowang.yuminailcore.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.xiaowang.yuminailcore.dto.AdminUserBalanceChangeDTO;
|
||||
import com.xiaowang.yuminailcore.dto.AdminUserVipOverrideDTO;
|
||||
import com.xiaowang.yuminailcore.entity.BalanceChangeDetailDO;
|
||||
import com.xiaowang.yuminailcore.entity.BalanceDO;
|
||||
import com.xiaowang.yuminailcore.entity.OrderDO;
|
||||
import com.xiaowang.yuminailcore.entity.UserDO;
|
||||
import com.xiaowang.yuminailcore.entity.VipBenefitDO;
|
||||
import com.xiaowang.yuminailcore.entity.VipInfoDO;
|
||||
import com.xiaowang.yuminailcore.mapper.BalanceChangeDetailMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.BalanceMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.OrderMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.UserMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.VipBenefitMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.VipInfoMapper;
|
||||
import com.xiaowang.yuminailcore.service.UserAssetService;
|
||||
import com.xiaowang.yuminailcore.vo.BalanceChangeDetailVO;
|
||||
import com.xiaowang.yuminailcore.vo.UserAssetVO;
|
||||
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;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserAssetServiceImpl implements UserAssetService {
|
||||
|
||||
private static final String VIP_LEVEL_NORMAL = "普通";
|
||||
private static final String SCENE_RECHARGE = "RECHARGE";
|
||||
private static final String SCENE_MANUAL_ADD = "MANUAL_ADD";
|
||||
private static final String SCENE_MANUAL_DEDUCT = "MANUAL_DEDUCT";
|
||||
private static final String SCENE_ORDER_CONSUME = "ORDER_CONSUME";
|
||||
private static final String SCENE_ORDER_REFUND = "ORDER_REFUND";
|
||||
|
||||
private final BalanceMapper balanceMapper;
|
||||
private final BalanceChangeDetailMapper balanceChangeDetailMapper;
|
||||
private final VipInfoMapper vipInfoMapper;
|
||||
private final VipBenefitMapper vipBenefitMapper;
|
||||
private final UserMapper userMapper;
|
||||
private final OrderMapper orderMapper;
|
||||
|
||||
@Override
|
||||
public UserAssetVO getUserAsset(Long userId) {
|
||||
UserDO user = getAvailableUser(userId);
|
||||
BalanceDO balance = ensureBalance(userId);
|
||||
VipInfoDO vipInfo = getVipInfo(userId);
|
||||
VipBenefitDO benefit = vipInfo != null && vipInfo.getBenefitId() != null
|
||||
? vipBenefitMapper.selectById(vipInfo.getBenefitId())
|
||||
: null;
|
||||
return toAssetVO(user, balance, vipInfo, benefit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BalanceChangeDetailVO> listBalanceHistory(Long userId) {
|
||||
UserDO user = getAvailableUser(userId);
|
||||
return balanceChangeDetailMapper.selectList(
|
||||
Wrappers.lambdaQuery(BalanceChangeDetailDO.class)
|
||||
.eq(BalanceChangeDetailDO::getUserId, userId)
|
||||
.orderByDesc(BalanceChangeDetailDO::getChangeTime)
|
||||
.orderByDesc(BalanceChangeDetailDO::getId))
|
||||
.stream()
|
||||
.map(detail -> toBalanceHistoryVO(detail, user.getNickname()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public UserAssetVO changeBalance(Long operatorId, Long userId, AdminUserBalanceChangeDTO request) {
|
||||
if (request == null || request.getAmount() == null || request.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
throw new RuntimeException("变动金额必须大于 0");
|
||||
}
|
||||
if (!StringUtils.hasText(request.getChangeScene())) {
|
||||
throw new RuntimeException("变动场景不能为空");
|
||||
}
|
||||
|
||||
UserDO user = getAvailableUser(userId);
|
||||
BalanceDO balance = ensureBalance(userId);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
BigDecimal beforeBalance = defaultDecimal(balance.getAmount());
|
||||
BigDecimal delta = normalizeDelta(request.getAmount(), request.getChangeScene());
|
||||
BigDecimal afterBalance = beforeBalance.add(delta);
|
||||
if (afterBalance.compareTo(BigDecimal.ZERO) < 0) {
|
||||
throw new RuntimeException("余额不足,无法执行扣减");
|
||||
}
|
||||
|
||||
OrderDO relatedOrder = null;
|
||||
String relatedOrderNo = null;
|
||||
if (request.getRelatedOrderId() != null) {
|
||||
relatedOrder = orderMapper.selectById(request.getRelatedOrderId());
|
||||
if (relatedOrder == null) {
|
||||
throw new RuntimeException("关联系统订单不存在");
|
||||
}
|
||||
if (!userId.equals(relatedOrder.getUserId())) {
|
||||
throw new RuntimeException("所选订单不属于当前用户");
|
||||
}
|
||||
relatedOrderNo = relatedOrder.getOrderNo();
|
||||
}
|
||||
|
||||
if (isDeductScene(request.getChangeScene())
|
||||
&& relatedOrder == null
|
||||
&& !StringUtils.hasText(request.getExternalOrderNo())) {
|
||||
throw new RuntimeException("扣减余额时必须关联系统订单或填写外部订单号");
|
||||
}
|
||||
|
||||
balance.setAmount(afterBalance);
|
||||
balance.setLastUpdateTime(now);
|
||||
if (SCENE_RECHARGE.equals(request.getChangeScene())) {
|
||||
balance.setTotalRechargeAmount(defaultDecimal(balance.getTotalRechargeAmount()).add(request.getAmount()));
|
||||
balance.setLastRechargeTime(now);
|
||||
}
|
||||
balanceMapper.updateById(balance);
|
||||
|
||||
BalanceChangeDetailDO detail = new BalanceChangeDetailDO();
|
||||
detail.setUserId(userId);
|
||||
detail.setAmount(delta);
|
||||
detail.setType(request.getChangeScene());
|
||||
detail.setCurrentBalance(afterBalance);
|
||||
detail.setBeforeBalance(beforeBalance);
|
||||
detail.setAfterBalance(afterBalance);
|
||||
detail.setChangeScene(request.getChangeScene());
|
||||
detail.setRelatedOrderId(relatedOrder != null ? relatedOrder.getId() : null);
|
||||
detail.setRelatedOrderNo(relatedOrderNo);
|
||||
detail.setExternalOrderNo(StringUtils.hasText(request.getExternalOrderNo()) ? request.getExternalOrderNo().trim() : null);
|
||||
detail.setOperatorId(operatorId);
|
||||
detail.setRemark(StringUtils.hasText(request.getRemark()) ? request.getRemark().trim() : null);
|
||||
detail.setChangeTime(now);
|
||||
balanceChangeDetailMapper.insert(detail);
|
||||
|
||||
if (SCENE_RECHARGE.equals(request.getChangeScene())) {
|
||||
recalculateVipInternal(user, false);
|
||||
}
|
||||
|
||||
return getUserAsset(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public UserAssetVO overrideVip(Long operatorId, Long userId, AdminUserVipOverrideDTO request) {
|
||||
if (request == null || !StringUtils.hasText(request.getVipLevel())) {
|
||||
throw new RuntimeException("VIP 等级不能为空");
|
||||
}
|
||||
|
||||
UserDO user = getAvailableUser(userId);
|
||||
String targetLevel = request.getVipLevel().trim();
|
||||
VipInfoDO vipInfo = getOrCreateVipInfo(userId);
|
||||
vipInfo.setActivationMethod("MANUAL_OVERRIDE");
|
||||
vipInfo.setManualOverride(1);
|
||||
vipInfo.setStartTime(LocalDateTime.now());
|
||||
|
||||
VipBenefitDO benefit = null;
|
||||
if (!VIP_LEVEL_NORMAL.equals(targetLevel)) {
|
||||
benefit = vipBenefitMapper.selectOne(
|
||||
Wrappers.lambdaQuery(VipBenefitDO.class)
|
||||
.eq(VipBenefitDO::getRequiredVipLevel, targetLevel)
|
||||
.eq(VipBenefitDO::getStatus, 1)
|
||||
.last("limit 1"));
|
||||
if (benefit == null) {
|
||||
throw new RuntimeException("目标 VIP 等级未配置有效规则");
|
||||
}
|
||||
vipInfo.setBenefitId(benefit.getId());
|
||||
} else {
|
||||
vipInfo.setBenefitId(null);
|
||||
}
|
||||
vipInfo.setVipLevel(targetLevel);
|
||||
vipInfoMapper.updateById(vipInfo);
|
||||
|
||||
updateUserVipFields(user, vipInfo, targetLevel);
|
||||
return toAssetVO(userMapper.selectById(userId), ensureBalance(userId), vipInfo, benefit);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public UserAssetVO recalculateVip(Long operatorId, Long userId) {
|
||||
UserDO user = getAvailableUser(userId);
|
||||
recalculateVipInternal(user, true);
|
||||
return getUserAsset(userId);
|
||||
}
|
||||
|
||||
private void recalculateVipInternal(UserDO user, boolean clearManualOverride) {
|
||||
BalanceDO balance = ensureBalance(user.getId());
|
||||
VipBenefitDO matchedBenefit = vipBenefitMapper.selectList(
|
||||
Wrappers.lambdaQuery(VipBenefitDO.class)
|
||||
.eq(VipBenefitDO::getStatus, 1)
|
||||
.orderByAsc(VipBenefitDO::getLevelRank)
|
||||
.orderByAsc(VipBenefitDO::getRechargeThreshold))
|
||||
.stream()
|
||||
.filter(benefit -> defaultDecimal(balance.getTotalRechargeAmount())
|
||||
.compareTo(defaultDecimal(benefit.getRechargeThreshold())) >= 0)
|
||||
.max(Comparator.comparing(VipBenefitDO::getLevelRank, Comparator.nullsLast(Integer::compareTo))
|
||||
.thenComparing(VipBenefitDO::getRechargeThreshold, Comparator.nullsLast(BigDecimal::compareTo)))
|
||||
.orElse(null);
|
||||
|
||||
VipInfoDO vipInfo = getVipInfo(user.getId());
|
||||
if (vipInfo != null && Integer.valueOf(1).equals(vipInfo.getManualOverride()) && !clearManualOverride) {
|
||||
return;
|
||||
}
|
||||
if (vipInfo == null && matchedBenefit == null) {
|
||||
updateUserVipFields(user, null, VIP_LEVEL_NORMAL);
|
||||
return;
|
||||
}
|
||||
|
||||
vipInfo = vipInfo == null ? getOrCreateVipInfo(user.getId()) : vipInfo;
|
||||
vipInfo.setActivationMethod("AUTO_RECALCULATE");
|
||||
vipInfo.setManualOverride(clearManualOverride ? 0 : vipInfo.getManualOverride());
|
||||
vipInfo.setStartTime(LocalDateTime.now());
|
||||
|
||||
String targetLevel = matchedBenefit != null ? matchedBenefit.getRequiredVipLevel() : VIP_LEVEL_NORMAL;
|
||||
vipInfo.setVipLevel(targetLevel);
|
||||
vipInfo.setBenefitId(matchedBenefit != null ? matchedBenefit.getId() : null);
|
||||
vipInfoMapper.updateById(vipInfo);
|
||||
|
||||
updateUserVipFields(user, vipInfo, targetLevel);
|
||||
}
|
||||
|
||||
private void updateUserVipFields(UserDO user, VipInfoDO vipInfo, String vipLevel) {
|
||||
user.setLevel(StringUtils.hasText(vipLevel) ? vipLevel : VIP_LEVEL_NORMAL);
|
||||
user.setVipId(VIP_LEVEL_NORMAL.equals(user.getLevel()) || vipInfo == null ? 0L : vipInfo.getId());
|
||||
user.setUpdateTime(LocalDateTime.now());
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
|
||||
private BigDecimal normalizeDelta(BigDecimal amount, String changeScene) {
|
||||
if (isDeductScene(changeScene)) {
|
||||
return amount.negate();
|
||||
}
|
||||
if (SCENE_RECHARGE.equals(changeScene) || SCENE_MANUAL_ADD.equals(changeScene) || SCENE_ORDER_REFUND.equals(changeScene)) {
|
||||
return amount;
|
||||
}
|
||||
throw new RuntimeException("不支持的余额变动场景");
|
||||
}
|
||||
|
||||
private boolean isDeductScene(String changeScene) {
|
||||
return SCENE_MANUAL_DEDUCT.equals(changeScene) || SCENE_ORDER_CONSUME.equals(changeScene);
|
||||
}
|
||||
|
||||
private BalanceDO ensureBalance(Long userId) {
|
||||
BalanceDO balance = balanceMapper.selectOne(
|
||||
Wrappers.lambdaQuery(BalanceDO.class)
|
||||
.eq(BalanceDO::getUserId, userId)
|
||||
.last("limit 1"));
|
||||
if (balance != null) {
|
||||
if (balance.getAmount() == null) {
|
||||
balance.setAmount(BigDecimal.ZERO);
|
||||
}
|
||||
if (balance.getTotalRechargeAmount() == null) {
|
||||
balance.setTotalRechargeAmount(BigDecimal.ZERO);
|
||||
}
|
||||
return balance;
|
||||
}
|
||||
|
||||
balance = new BalanceDO();
|
||||
balance.setUserId(userId);
|
||||
balance.setAmount(BigDecimal.ZERO);
|
||||
balance.setTotalRechargeAmount(BigDecimal.ZERO);
|
||||
balance.setLastUpdateTime(LocalDateTime.now());
|
||||
balanceMapper.insert(balance);
|
||||
return balance;
|
||||
}
|
||||
|
||||
private VipInfoDO getVipInfo(Long userId) {
|
||||
return vipInfoMapper.selectOne(
|
||||
Wrappers.lambdaQuery(VipInfoDO.class)
|
||||
.eq(VipInfoDO::getUserId, userId)
|
||||
.last("limit 1"));
|
||||
}
|
||||
|
||||
private VipInfoDO getOrCreateVipInfo(Long userId) {
|
||||
VipInfoDO vipInfo = getVipInfo(userId);
|
||||
if (vipInfo != null) {
|
||||
return vipInfo;
|
||||
}
|
||||
vipInfo = new VipInfoDO();
|
||||
vipInfo.setUserId(userId);
|
||||
vipInfo.setVipLevel(VIP_LEVEL_NORMAL);
|
||||
vipInfo.setManualOverride(0);
|
||||
vipInfo.setActivationMethod("INIT");
|
||||
vipInfo.setStartTime(LocalDateTime.now());
|
||||
vipInfoMapper.insert(vipInfo);
|
||||
return vipInfo;
|
||||
}
|
||||
|
||||
private UserDO getAvailableUser(Long userId) {
|
||||
UserDO user = userMapper.selectById(userId);
|
||||
if (user == null || Integer.valueOf(1).equals(user.getIsDelete())) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
private BigDecimal defaultDecimal(BigDecimal value) {
|
||||
return value == null ? BigDecimal.ZERO : value;
|
||||
}
|
||||
|
||||
private UserAssetVO toAssetVO(UserDO user, BalanceDO balance, VipInfoDO vipInfo, VipBenefitDO benefit) {
|
||||
UserAssetVO vo = new UserAssetVO();
|
||||
vo.setUserId(user.getId());
|
||||
vo.setNickname(user.getNickname());
|
||||
vo.setBalance(defaultDecimal(balance != null ? balance.getAmount() : null));
|
||||
vo.setTotalRechargeAmount(defaultDecimal(balance != null ? balance.getTotalRechargeAmount() : null));
|
||||
vo.setLastRechargeTime(balance != null ? balance.getLastRechargeTime() : null);
|
||||
vo.setVipInfoId(vipInfo != null ? vipInfo.getId() : null);
|
||||
vo.setVipLevel(StringUtils.hasText(user.getLevel()) ? user.getLevel() : VIP_LEVEL_NORMAL);
|
||||
vo.setBenefitId(vipInfo != null ? vipInfo.getBenefitId() : null);
|
||||
vo.setBenefitInfo(benefit != null ? benefit.getBenefitInfo() : null);
|
||||
vo.setManualOverride(vipInfo != null && Integer.valueOf(1).equals(vipInfo.getManualOverride()));
|
||||
vo.setVipStartTime(vipInfo != null ? vipInfo.getStartTime() : null);
|
||||
vo.setVipExpireTime(vipInfo != null ? vipInfo.getExpireTime() : null);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private BalanceChangeDetailVO toBalanceHistoryVO(BalanceChangeDetailDO detail, String nickname) {
|
||||
BalanceChangeDetailVO vo = new BalanceChangeDetailVO();
|
||||
vo.setId(detail.getId());
|
||||
vo.setUserId(detail.getUserId());
|
||||
vo.setUserNickname(nickname);
|
||||
vo.setAmount(detail.getAmount());
|
||||
vo.setType(detail.getType());
|
||||
vo.setCurrentBalance(detail.getCurrentBalance());
|
||||
vo.setBeforeBalance(detail.getBeforeBalance());
|
||||
vo.setAfterBalance(detail.getAfterBalance());
|
||||
vo.setChangeScene(detail.getChangeScene());
|
||||
vo.setRelatedOrderId(detail.getRelatedOrderId());
|
||||
vo.setRelatedOrderNo(detail.getRelatedOrderNo());
|
||||
vo.setExternalOrderNo(detail.getExternalOrderNo());
|
||||
vo.setOperatorId(detail.getOperatorId());
|
||||
vo.setRemark(detail.getRemark());
|
||||
vo.setChangeTime(detail.getChangeTime());
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
+20
-4
@@ -43,6 +43,8 @@ import java.util.stream.Collectors;
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {
|
||||
|
||||
private static final String VIP_LEVEL_NORMAL = "普通";
|
||||
|
||||
// 注入用户数据访问层、JWT工具类和店铺服务
|
||||
private final UserMapper userMapper;
|
||||
private final JwtUtil jwtUtil;
|
||||
@@ -87,6 +89,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
// 设置默认头像
|
||||
String defaultAvatar = "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png";
|
||||
userDO.setAvatar(defaultAvatar);
|
||||
|
||||
if (Integer.valueOf(0).equals(userDO.getType())) {
|
||||
if (!StringUtils.hasText(userDO.getLevel())) {
|
||||
userDO.setLevel(VIP_LEVEL_NORMAL);
|
||||
}
|
||||
if (userDO.getVipId() == null) {
|
||||
userDO.setVipId(0L);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 插入用户数据
|
||||
@@ -146,8 +157,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
permissions = permissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(PermissionDO.class)
|
||||
.eq(PermissionDO::getIsDelete, 0)
|
||||
.in(PermissionDO::getPermissionCode, "home:view", "dashboard:view", "user:view",
|
||||
"user:create", "user:edit", "user:reset_password", "user:delete",
|
||||
.in(PermissionDO::getPermissionCode, "home:view", "dashboard:view", "user:view",
|
||||
"user:create", "user:edit", "user:disable", "user:reset_password", "user:delete",
|
||||
"user:balance:view", "user:balance:manage", "user:vip:manage",
|
||||
"product:view", "product:create", "product:edit", "product:delete",
|
||||
"product:category:manage", "shop:view", "shop:create", "shop:edit",
|
||||
"shop:delete", "shop:staff:manage", "permission:view", "permission:create",
|
||||
@@ -496,7 +508,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO.setPhoneVerified(1);
|
||||
userDO.setNickname("用户" + phone.substring(7));
|
||||
userDO.setType(0);
|
||||
userDO.setLevel("普通");
|
||||
userDO.setLevel(VIP_LEVEL_NORMAL);
|
||||
userDO.setVipId(0L);
|
||||
userDO.setShopId(shopId);
|
||||
userDO.setIsDelete(0);
|
||||
userDO.setStatus(1);
|
||||
@@ -505,6 +518,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO.setPassword(ENCODED_UNSET_PASSWORD);
|
||||
|
||||
userMapper.insert(userDO);
|
||||
assignDefaultPermissions(userDO.getId(), userDO.getType());
|
||||
return userDO;
|
||||
}
|
||||
|
||||
@@ -582,12 +596,14 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO.setPhone(registerRequestDTO.getPhone());
|
||||
userDO.setPhoneVerified(1);
|
||||
userDO.setType(Integer.valueOf(0));
|
||||
userDO.setLevel("普通");
|
||||
userDO.setLevel(VIP_LEVEL_NORMAL);
|
||||
userDO.setVipId(0L);
|
||||
userDO.setIsDelete(Integer.valueOf(0));
|
||||
userDO.setCreateTime(LocalDateTime.now());
|
||||
userDO.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
userMapper.insert(userDO);
|
||||
assignDefaultPermissions(userDO.getId(), userDO.getType());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+6
@@ -29,11 +29,13 @@ public class VipInfoServiceImpl extends ServiceImpl<VipInfoMapper, VipInfoDO> im
|
||||
@Override
|
||||
public Long createVipInfo(VipInfoDTO vipInfoDTO) {
|
||||
VipInfoDO vipInfoDO = new VipInfoDO();
|
||||
vipInfoDO.setUserId(vipInfoDTO.getUserId());
|
||||
vipInfoDO.setVipLevel(vipInfoDTO.getVipLevel());
|
||||
vipInfoDO.setStartTime(vipInfoDTO.getStartTime());
|
||||
vipInfoDO.setExpireTime(vipInfoDTO.getExpireTime());
|
||||
vipInfoDO.setActivationMethod(vipInfoDTO.getActivationMethod());
|
||||
vipInfoDO.setBenefitId(vipInfoDTO.getBenefitId());
|
||||
vipInfoDO.setManualOverride(vipInfoDTO.getManualOverride());
|
||||
vipInfoMapper.insert(vipInfoDO);
|
||||
return vipInfoDO.getId();
|
||||
}
|
||||
@@ -44,11 +46,13 @@ public class VipInfoServiceImpl extends ServiceImpl<VipInfoMapper, VipInfoDO> im
|
||||
if (vipInfoDO == null) {
|
||||
return false;
|
||||
}
|
||||
vipInfoDO.setUserId(vipInfoDTO.getUserId());
|
||||
vipInfoDO.setVipLevel(vipInfoDTO.getVipLevel());
|
||||
vipInfoDO.setStartTime(vipInfoDTO.getStartTime());
|
||||
vipInfoDO.setExpireTime(vipInfoDTO.getExpireTime());
|
||||
vipInfoDO.setActivationMethod(vipInfoDTO.getActivationMethod());
|
||||
vipInfoDO.setBenefitId(vipInfoDTO.getBenefitId());
|
||||
vipInfoDO.setManualOverride(vipInfoDTO.getManualOverride());
|
||||
return vipInfoMapper.updateById(vipInfoDO) > 0;
|
||||
}
|
||||
|
||||
@@ -94,11 +98,13 @@ public class VipInfoServiceImpl extends ServiceImpl<VipInfoMapper, VipInfoDO> im
|
||||
private VipInfoVO convertToVO(VipInfoDO vipInfoDO) {
|
||||
VipInfoVO vipInfoVO = new VipInfoVO();
|
||||
vipInfoVO.setId(vipInfoDO.getId());
|
||||
vipInfoVO.setUserId(vipInfoDO.getUserId());
|
||||
vipInfoVO.setVipLevel(vipInfoDO.getVipLevel());
|
||||
vipInfoVO.setStartTime(vipInfoDO.getStartTime());
|
||||
vipInfoVO.setExpireTime(vipInfoDO.getExpireTime());
|
||||
vipInfoVO.setActivationMethod(vipInfoDO.getActivationMethod());
|
||||
vipInfoVO.setBenefitId(vipInfoDO.getBenefitId());
|
||||
vipInfoVO.setManualOverride(vipInfoDO.getManualOverride());
|
||||
vipInfoVO.setCreateTime(vipInfoDO.getCreateTime());
|
||||
vipInfoVO.setUpdateTime(vipInfoDO.getUpdateTime());
|
||||
return vipInfoVO;
|
||||
|
||||
@@ -44,6 +44,14 @@ public class BalanceChangeDetailVO {
|
||||
* 变动后的当前余额
|
||||
*/
|
||||
private BigDecimal currentBalance;
|
||||
private BigDecimal beforeBalance;
|
||||
private BigDecimal afterBalance;
|
||||
private String changeScene;
|
||||
private Long relatedOrderId;
|
||||
private String relatedOrderNo;
|
||||
private String externalOrderNo;
|
||||
private Long operatorId;
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 变动时间
|
||||
|
||||
@@ -34,6 +34,8 @@ public class BalanceVO {
|
||||
* 当前余额
|
||||
*/
|
||||
private BigDecimal amount;
|
||||
private BigDecimal totalRechargeAmount;
|
||||
private LocalDateTime lastRechargeTime;
|
||||
|
||||
/**
|
||||
* 余额账户创建时间
|
||||
|
||||
@@ -96,6 +96,9 @@ public class CouponVO {
|
||||
* 所属店铺名称
|
||||
*/
|
||||
private String shopName;
|
||||
private String couponScene;
|
||||
private String requiredVipLevel;
|
||||
private Integer autoMatch;
|
||||
|
||||
/**
|
||||
* 可用标签列表
|
||||
|
||||
@@ -15,9 +15,11 @@ public class OrderVO {
|
||||
private BigDecimal totalAmount;
|
||||
private BigDecimal actualAmount;
|
||||
private Long couponId;
|
||||
private Long userCouponId;
|
||||
private String couponSourceType;
|
||||
private String couponName;
|
||||
private Integer status;
|
||||
private String statusText;
|
||||
private LocalDateTime createTime;
|
||||
private List<OrderItemVO> items;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.xiaowang.yuminailcore.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class UserAssetVO {
|
||||
|
||||
private Long userId;
|
||||
|
||||
private String nickname;
|
||||
|
||||
private BigDecimal balance;
|
||||
|
||||
private BigDecimal totalRechargeAmount;
|
||||
|
||||
private LocalDateTime lastRechargeTime;
|
||||
|
||||
private Long vipInfoId;
|
||||
|
||||
private String vipLevel;
|
||||
|
||||
private Long benefitId;
|
||||
|
||||
private String benefitInfo;
|
||||
|
||||
private Boolean manualOverride;
|
||||
|
||||
private LocalDateTime vipStartTime;
|
||||
|
||||
private LocalDateTime vipExpireTime;
|
||||
}
|
||||
@@ -79,4 +79,7 @@ public class UserCouponVO {
|
||||
* 使用时间
|
||||
*/
|
||||
private LocalDateTime useTime;
|
||||
private String sourceType;
|
||||
private String requiredVipLevel;
|
||||
private Boolean autoMatched;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.xiaowang.yuminailcore.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
@@ -28,6 +29,9 @@ public class VipBenefitVO {
|
||||
* 所需VIP等级
|
||||
*/
|
||||
private String requiredVipLevel;
|
||||
private Integer levelRank;
|
||||
private BigDecimal rechargeThreshold;
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
|
||||
@@ -14,6 +14,8 @@ import java.time.LocalDateTime;
|
||||
@Data
|
||||
public class VipInfoVO {
|
||||
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* VIP信息唯一主键ID(雪花ID)
|
||||
*/
|
||||
@@ -48,6 +50,7 @@ public class VipInfoVO {
|
||||
* 权益详情
|
||||
*/
|
||||
private String benefitInfo;
|
||||
private Integer manualOverride;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
|
||||
+501
@@ -0,0 +1,501 @@
|
||||
-- 2026-03-20 用户资产 / VIP / 优惠券 / 权限增量补丁
|
||||
-- 适用前提:已先执行 YumiNail.sql 完成基础建表
|
||||
-- 执行说明:
|
||||
-- 1. 本文件只做增量修正,不覆盖原始 YumiNail.sql
|
||||
-- 2. 可用于空库初始化后的补丁,也可用于已有库的结构与初始化对齐
|
||||
-- 3. 历史累计充值金额无法从旧表精确反推,本补丁仅把缺失值归零;如需保留历史累计充值,请按业务数据手工回填 total_recharge_amount
|
||||
|
||||
SET NAMES utf8mb4;
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_add_column_if_not_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_add_index_if_not_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_add_unique_index_if_not_exists;
|
||||
|
||||
DELIMITER $$
|
||||
|
||||
CREATE PROCEDURE sp_add_column_if_not_exists(
|
||||
IN p_table_name VARCHAR(64),
|
||||
IN p_column_name VARCHAR(64),
|
||||
IN p_definition TEXT
|
||||
)
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = p_table_name
|
||||
AND COLUMN_NAME = p_column_name
|
||||
) THEN
|
||||
SET @sql = CONCAT(
|
||||
'ALTER TABLE `', p_table_name, '` ADD COLUMN `', p_column_name, '` ',
|
||||
p_definition
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END$$
|
||||
|
||||
CREATE PROCEDURE sp_add_index_if_not_exists(
|
||||
IN p_table_name VARCHAR(64),
|
||||
IN p_index_name VARCHAR(64),
|
||||
IN p_columns TEXT
|
||||
)
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.STATISTICS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = p_table_name
|
||||
AND INDEX_NAME = p_index_name
|
||||
) THEN
|
||||
SET @sql = CONCAT(
|
||||
'ALTER TABLE `', p_table_name, '` ADD INDEX `', p_index_name, '` ',
|
||||
p_columns
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END$$
|
||||
|
||||
CREATE PROCEDURE sp_add_unique_index_if_not_exists(
|
||||
IN p_table_name VARCHAR(64),
|
||||
IN p_index_name VARCHAR(64),
|
||||
IN p_columns TEXT
|
||||
)
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.STATISTICS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = p_table_name
|
||||
AND INDEX_NAME = p_index_name
|
||||
) THEN
|
||||
SET @sql = CONCAT(
|
||||
'ALTER TABLE `', p_table_name, '` ADD UNIQUE INDEX `', p_index_name, '` ',
|
||||
p_columns
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
END IF;
|
||||
END$$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
CALL sp_add_column_if_not_exists('t_balance', 'total_recharge_amount', 'decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT ''累计充值金额''');
|
||||
CALL sp_add_column_if_not_exists('t_balance', 'last_recharge_time', 'datetime NULL COMMENT ''最近一次充值时间''');
|
||||
|
||||
UPDATE t_balance
|
||||
SET total_recharge_amount = COALESCE(total_recharge_amount, 0.00)
|
||||
WHERE total_recharge_amount IS NULL;
|
||||
|
||||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'before_balance', 'decimal(10,2) NULL COMMENT ''变动前余额''');
|
||||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'after_balance', 'decimal(10,2) NULL COMMENT ''变动后余额''');
|
||||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'change_scene', 'varchar(32) NULL COMMENT ''余额变动场景''');
|
||||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'related_order_id', 'bigint unsigned NULL COMMENT ''关联系统订单ID''');
|
||||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'related_order_no', 'varchar(64) NULL COMMENT ''关联系统订单号''');
|
||||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'external_order_no', 'varchar(64) NULL COMMENT ''手工录入的外部订单号''');
|
||||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'operator_id', 'bigint unsigned NULL COMMENT ''操作人ID''');
|
||||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'remark', 'varchar(255) NULL COMMENT ''备注''');
|
||||
|
||||
UPDATE t_balance_change_detail
|
||||
SET before_balance = COALESCE(before_balance, current_balance - amount),
|
||||
after_balance = COALESCE(after_balance, current_balance),
|
||||
change_scene = COALESCE(NULLIF(change_scene, ''), type)
|
||||
WHERE before_balance IS NULL
|
||||
OR after_balance IS NULL
|
||||
OR change_scene IS NULL
|
||||
OR change_scene = '';
|
||||
|
||||
CALL sp_add_index_if_not_exists('t_balance_change_detail', 'idx_balance_detail_related_order_id', '(`related_order_id`)');
|
||||
CALL sp_add_index_if_not_exists('t_balance_change_detail', 'idx_balance_detail_operator_id', '(`operator_id`)');
|
||||
|
||||
CALL sp_add_column_if_not_exists('t_order', 'shop_id', 'bigint unsigned NULL COMMENT ''所属店铺ID''');
|
||||
CALL sp_add_column_if_not_exists('t_order', 'user_coupon_id', 'bigint unsigned NULL COMMENT ''用户券ID''');
|
||||
CALL sp_add_column_if_not_exists('t_order', 'coupon_source_type', 'varchar(32) NULL COMMENT ''优惠来源类型''');
|
||||
|
||||
UPDATE t_order
|
||||
SET coupon_id = NULL
|
||||
WHERE coupon_id = 0;
|
||||
|
||||
UPDATE t_order o
|
||||
INNER JOIN (
|
||||
SELECT oi.order_id, MIN(p.shop_id) AS resolved_shop_id
|
||||
FROM t_order_item oi
|
||||
INNER JOIN t_product p ON p.id = oi.product_id
|
||||
GROUP BY oi.order_id
|
||||
) detail_shop ON detail_shop.order_id = o.id
|
||||
SET o.shop_id = detail_shop.resolved_shop_id
|
||||
WHERE o.shop_id IS NULL;
|
||||
|
||||
UPDATE t_order
|
||||
SET coupon_source_type = 'USER_COUPON'
|
||||
WHERE coupon_source_type IS NULL
|
||||
AND coupon_id IS NOT NULL;
|
||||
|
||||
CALL sp_add_index_if_not_exists('t_order', 'idx_t_order_shop_id', '(`shop_id`)');
|
||||
CALL sp_add_index_if_not_exists('t_order', 'idx_t_order_coupon_source_type', '(`coupon_source_type`)');
|
||||
|
||||
CALL sp_add_column_if_not_exists('t_order_item', 'appointment_time', 'datetime NULL COMMENT ''预约开始时间''');
|
||||
CALL sp_add_index_if_not_exists('t_order_item', 'idx_t_order_item_artist_appointment_time', '(`artist_id`, `appointment_time`)');
|
||||
|
||||
CALL sp_add_column_if_not_exists('t_coupon', 'coupon_scene', 'varchar(32) NOT NULL DEFAULT ''NORMAL'' COMMENT ''优惠券场景''');
|
||||
CALL sp_add_column_if_not_exists('t_coupon', 'required_vip_level', 'varchar(20) NULL COMMENT ''要求的VIP等级''');
|
||||
CALL sp_add_column_if_not_exists('t_coupon', 'auto_match', 'tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''是否自动匹配''');
|
||||
|
||||
UPDATE t_coupon
|
||||
SET coupon_scene = COALESCE(NULLIF(coupon_scene, ''), 'NORMAL'),
|
||||
auto_match = COALESCE(auto_match, 0)
|
||||
WHERE coupon_scene IS NULL
|
||||
OR coupon_scene = ''
|
||||
OR auto_match IS NULL;
|
||||
|
||||
CALL sp_add_index_if_not_exists('t_coupon', 'idx_t_coupon_scene_auto_match', '(`coupon_scene`, `auto_match`)');
|
||||
CALL sp_add_index_if_not_exists('t_coupon', 'idx_t_coupon_required_vip_level', '(`required_vip_level`)');
|
||||
|
||||
CALL sp_add_column_if_not_exists('t_vip_benefit', 'level_rank', 'int NOT NULL DEFAULT 0 COMMENT ''等级序号''');
|
||||
CALL sp_add_column_if_not_exists('t_vip_benefit', 'recharge_threshold', 'decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT ''累计充值门槛''');
|
||||
CALL sp_add_column_if_not_exists('t_vip_benefit', 'status', 'tinyint unsigned NOT NULL DEFAULT 1 COMMENT ''状态''');
|
||||
|
||||
UPDATE t_vip_benefit
|
||||
SET level_rank = CASE required_vip_level
|
||||
WHEN '普通' THEN 0
|
||||
WHEN '青铜' THEN 1
|
||||
WHEN '白银' THEN 2
|
||||
WHEN '黄金' THEN 3
|
||||
ELSE level_rank
|
||||
END,
|
||||
recharge_threshold = CASE required_vip_level
|
||||
WHEN '普通' THEN 0.00
|
||||
WHEN '青铜' THEN 500.00
|
||||
WHEN '白银' THEN 1000.00
|
||||
WHEN '黄金' THEN 2000.00
|
||||
ELSE recharge_threshold
|
||||
END,
|
||||
status = COALESCE(status, 1)
|
||||
WHERE required_vip_level IN ('普通', '青铜', '白银', '黄金');
|
||||
|
||||
CALL sp_add_index_if_not_exists('t_vip_benefit', 'idx_t_vip_benefit_status_rank', '(`status`, `level_rank`)');
|
||||
|
||||
CALL sp_add_column_if_not_exists('t_vip_info', 'user_id', 'bigint unsigned NULL COMMENT ''所属用户ID''');
|
||||
CALL sp_add_column_if_not_exists('t_vip_info', 'manual_override', 'tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''是否手工覆盖VIP等级''');
|
||||
|
||||
ALTER TABLE t_vip_info
|
||||
MODIFY COLUMN expire_time datetime NULL COMMENT 'VIP到期时间',
|
||||
MODIFY COLUMN benefit_id bigint unsigned NULL COMMENT '关联VIP权益信息表主键';
|
||||
|
||||
UPDATE t_vip_info vi
|
||||
INNER JOIN t_user u ON u.vip_id = vi.id
|
||||
SET vi.user_id = u.id
|
||||
WHERE (vi.user_id IS NULL OR vi.user_id = 0)
|
||||
AND u.vip_id IS NOT NULL
|
||||
AND u.vip_id <> 0;
|
||||
|
||||
UPDATE t_vip_info
|
||||
SET start_time = COALESCE(start_time, create_time, NOW()),
|
||||
activation_method = COALESCE(NULLIF(activation_method, ''), 'INIT'),
|
||||
manual_override = COALESCE(manual_override, 0);
|
||||
|
||||
CALL sp_add_unique_index_if_not_exists('t_vip_info', 'uk_t_vip_info_user_id', '(`user_id`)');
|
||||
CALL sp_add_index_if_not_exists('t_vip_info', 'idx_t_vip_info_manual_override', '(`manual_override`)');
|
||||
|
||||
UPDATE t_user u
|
||||
LEFT JOIN t_vip_info vi ON vi.id = u.vip_id
|
||||
SET u.level = CASE
|
||||
WHEN u.type = 0 AND u.vip_id IS NOT NULL AND u.vip_id <> 0 AND vi.vip_level IS NOT NULL THEN vi.vip_level
|
||||
WHEN u.type = 0 THEN '普通'
|
||||
ELSE u.level
|
||||
END,
|
||||
u.vip_id = CASE
|
||||
WHEN u.type = 0 AND u.vip_id IS NULL THEN 0
|
||||
ELSE u.vip_id
|
||||
END,
|
||||
u.update_time = NOW()
|
||||
WHERE u.is_delete = 0
|
||||
AND u.type = 0
|
||||
AND (u.level IS NULL OR u.level = '' OR u.vip_id IS NULL);
|
||||
|
||||
INSERT INTO t_permission (permission_code, permission_name, permission_group, description, create_time, update_time, is_delete)
|
||||
VALUES
|
||||
('home:view', '查看首页', '首页', '查看系统首页入口与概览', NOW(), NOW(), 0),
|
||||
('dashboard:view', '查看仪表盘', '仪表盘', '查看后台仪表盘统计', NOW(), NOW(), 0),
|
||||
('user:view', '查看用户', '用户管理', '查看用户列表与详情', NOW(), NOW(), 0),
|
||||
('user:create', '新增用户', '用户管理', '创建用户账号', NOW(), NOW(), 0),
|
||||
('user:edit', '编辑用户', '用户管理', '修改用户基础信息', NOW(), NOW(), 0),
|
||||
('user:disable', '启停用户', '用户管理', '启用或禁用用户账号', NOW(), NOW(), 0),
|
||||
('user:reset_password', '重置密码', '用户管理', '重置用户密码', NOW(), NOW(), 0),
|
||||
('user:delete', '删除用户', '用户管理', '删除用户账号', NOW(), NOW(), 0),
|
||||
('user:balance:view', '查看余额资产', '用户资产', '查看用户余额、累计充值与流水', NOW(), NOW(), 0),
|
||||
('user:balance:manage', '调整余额', '用户资产', '充值、手工加减余额与关联订单', NOW(), NOW(), 0),
|
||||
('user:vip:manage', '管理VIP', '用户资产', '手工设置VIP等级或恢复自动判级', NOW(), NOW(), 0),
|
||||
('product:view', '查看商品', '商品管理', '查看商品列表与详情', NOW(), NOW(), 0),
|
||||
('product:create', '新增商品', '商品管理', '创建商品', NOW(), NOW(), 0),
|
||||
('product:edit', '编辑商品', '商品管理', '编辑商品信息', NOW(), NOW(), 0),
|
||||
('product:delete', '删除商品', '商品管理', '删除商品', NOW(), NOW(), 0),
|
||||
('product:category:manage', '管理商品分类', '商品管理', '维护商品分类与归类信息', NOW(), NOW(), 0),
|
||||
('shop:view', '查看店铺', '店铺管理', '查看店铺列表与详情', NOW(), NOW(), 0),
|
||||
('shop:create', '新增店铺', '店铺管理', '创建店铺', NOW(), NOW(), 0),
|
||||
('shop:edit', '编辑店铺', '店铺管理', '编辑店铺信息', NOW(), NOW(), 0),
|
||||
('shop:delete', '删除店铺', '店铺管理', '删除店铺', NOW(), NOW(), 0),
|
||||
('shop:staff:manage', '管理员工', '店铺管理', '管理店铺员工与归属', NOW(), NOW(), 0),
|
||||
('permission:view', '查看权限', '权限管理', '查看权限清单', NOW(), NOW(), 0),
|
||||
('permission:create', '新增权限', '权限管理', '新增系统权限定义', NOW(), NOW(), 0),
|
||||
('permission:edit', '编辑权限', '权限管理', '编辑系统权限定义', NOW(), NOW(), 0),
|
||||
('permission:delete', '删除权限', '权限管理', '删除系统权限定义', NOW(), NOW(), 0),
|
||||
('permission:assign', '分配权限', '权限管理', '给用户分配后台权限', NOW(), NOW(), 0),
|
||||
('tag:view', '查看标签', '标签管理', '查看标签列表与详情', NOW(), NOW(), 0),
|
||||
('tag:create', '新增标签', '标签管理', '创建标签', NOW(), NOW(), 0),
|
||||
('tag:edit', '编辑标签', '标签管理', '编辑标签', NOW(), NOW(), 0),
|
||||
('tag:delete', '删除标签', '标签管理', '删除标签', NOW(), NOW(), 0),
|
||||
('tag:assign', '分配标签', '标签管理', '给商品或优惠券分配标签', NOW(), NOW(), 0),
|
||||
('coupon:view', '查看优惠券', '优惠券管理', '查看优惠券与发放情况', NOW(), NOW(), 0),
|
||||
('coupon:create', '新增优惠券', '优惠券管理', '创建优惠券模板', NOW(), NOW(), 0),
|
||||
('coupon:edit', '编辑优惠券', '优惠券管理', '编辑优惠券模板', NOW(), NOW(), 0),
|
||||
('coupon:delete', '删除优惠券', '优惠券管理', '删除优惠券模板', NOW(), NOW(), 0),
|
||||
('coupon:assign', '发放优惠券', '优惠券管理', '向用户发放普通优惠券', NOW(), NOW(), 0),
|
||||
('order:view', '查看订单', '订单管理', '查看订单列表与详情', NOW(), NOW(), 0),
|
||||
('order:process', '处理订单', '订单管理', '处理订单状态或由美甲师完结订单', NOW(), NOW(), 0),
|
||||
('order:edit', '编辑订单', '订单管理', '编辑订单信息的预留权限', NOW(), NOW(), 0),
|
||||
('order:delete', '删除订单', '订单管理', '删除订单的预留权限', NOW(), NOW(), 0),
|
||||
('nail_artist:view', '查看美甲师', '美甲师管理', '查看美甲师信息与排班入口', NOW(), NOW(), 0),
|
||||
('nail_artist:edit', '编辑美甲师', '美甲师管理', '编辑美甲师信息', NOW(), NOW(), 0)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
permission_name = VALUES(permission_name),
|
||||
permission_group = VALUES(permission_group),
|
||||
description = VALUES(description),
|
||||
update_time = NOW(),
|
||||
is_delete = 0;
|
||||
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time, create_by, update_by, is_delete)
|
||||
SELECT u.id, p.id, NOW(), NOW(), u.id, u.id, 0
|
||||
FROM t_user u
|
||||
INNER JOIN t_permission p ON p.is_delete = 0
|
||||
WHERE u.is_delete = 0
|
||||
AND u.type = 4
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM t_user_permission up
|
||||
WHERE up.user_id = u.id
|
||||
AND up.permission_id = p.id
|
||||
);
|
||||
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time, create_by, update_by, is_delete)
|
||||
SELECT u.id, p.id, NOW(), NOW(), u.id, u.id, 0
|
||||
FROM t_user u
|
||||
INNER JOIN t_permission p
|
||||
ON p.is_delete = 0
|
||||
AND p.permission_code IN (
|
||||
'home:view',
|
||||
'dashboard:view',
|
||||
'user:view',
|
||||
'user:create',
|
||||
'user:edit',
|
||||
'user:disable',
|
||||
'user:reset_password',
|
||||
'user:delete',
|
||||
'user:balance:view',
|
||||
'user:balance:manage',
|
||||
'user:vip:manage',
|
||||
'product:view',
|
||||
'product:create',
|
||||
'product:edit',
|
||||
'product:delete',
|
||||
'product:category:manage',
|
||||
'shop:view',
|
||||
'shop:create',
|
||||
'shop:edit',
|
||||
'shop:delete',
|
||||
'shop:staff:manage',
|
||||
'permission:view',
|
||||
'permission:create',
|
||||
'permission:edit',
|
||||
'permission:delete',
|
||||
'permission:assign',
|
||||
'tag:view',
|
||||
'tag:create',
|
||||
'tag:edit',
|
||||
'tag:delete',
|
||||
'tag:assign',
|
||||
'coupon:view',
|
||||
'coupon:create',
|
||||
'coupon:edit',
|
||||
'coupon:delete',
|
||||
'coupon:assign',
|
||||
'order:view',
|
||||
'order:edit',
|
||||
'order:delete',
|
||||
'order:process'
|
||||
)
|
||||
WHERE u.is_delete = 0
|
||||
AND u.type = 2
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM t_user_permission up
|
||||
WHERE up.user_id = u.id
|
||||
AND up.permission_id = p.id
|
||||
);
|
||||
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time, create_by, update_by, is_delete)
|
||||
SELECT u.id, p.id, NOW(), NOW(), u.id, u.id, 0
|
||||
FROM t_user u
|
||||
INNER JOIN t_permission p
|
||||
ON p.is_delete = 0
|
||||
AND p.permission_code IN (
|
||||
'home:view',
|
||||
'dashboard:view',
|
||||
'user:view',
|
||||
'user:create',
|
||||
'user:reset_password',
|
||||
'product:view',
|
||||
'product:create',
|
||||
'product:edit',
|
||||
'product:category:manage',
|
||||
'shop:view',
|
||||
'tag:view',
|
||||
'tag:create',
|
||||
'tag:edit',
|
||||
'tag:assign',
|
||||
'coupon:view',
|
||||
'coupon:assign',
|
||||
'order:view',
|
||||
'order:process'
|
||||
)
|
||||
WHERE u.is_delete = 0
|
||||
AND u.type = 3
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM t_user_permission up
|
||||
WHERE up.user_id = u.id
|
||||
AND up.permission_id = p.id
|
||||
);
|
||||
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time, create_by, update_by, is_delete)
|
||||
SELECT u.id, p.id, NOW(), NOW(), u.id, u.id, 0
|
||||
FROM t_user u
|
||||
INNER JOIN t_permission p
|
||||
ON p.is_delete = 0
|
||||
AND p.permission_code IN (
|
||||
'home:view',
|
||||
'dashboard:view',
|
||||
'product:view',
|
||||
'tag:view',
|
||||
'shop:view',
|
||||
'order:view'
|
||||
)
|
||||
WHERE u.is_delete = 0
|
||||
AND u.type = 0
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM t_user_permission up
|
||||
WHERE up.user_id = u.id
|
||||
AND up.permission_id = p.id
|
||||
);
|
||||
|
||||
UPDATE t_vip_benefit
|
||||
SET benefit_info = '普通用户,无额外VIP折扣',
|
||||
level_rank = 0,
|
||||
recharge_threshold = 0.00,
|
||||
status = 1,
|
||||
update_time = NOW()
|
||||
WHERE required_vip_level = '普通';
|
||||
|
||||
INSERT INTO t_vip_benefit (benefit_info, required_vip_level, level_rank, recharge_threshold, status, create_time, update_time)
|
||||
SELECT '普通用户,无额外VIP折扣', '普通', 0, 0.00, 1, NOW(), NOW()
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM t_vip_benefit
|
||||
WHERE required_vip_level = '普通'
|
||||
);
|
||||
|
||||
UPDATE t_vip_benefit
|
||||
SET benefit_info = '青铜VIP,系统自动匹配9.5折券',
|
||||
level_rank = 1,
|
||||
recharge_threshold = 500.00,
|
||||
status = 1,
|
||||
update_time = NOW()
|
||||
WHERE required_vip_level = '青铜';
|
||||
|
||||
INSERT INTO t_vip_benefit (benefit_info, required_vip_level, level_rank, recharge_threshold, status, create_time, update_time)
|
||||
SELECT '青铜VIP,系统自动匹配9.5折券', '青铜', 1, 500.00, 1, NOW(), NOW()
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM t_vip_benefit
|
||||
WHERE required_vip_level = '青铜'
|
||||
);
|
||||
|
||||
UPDATE t_vip_benefit
|
||||
SET benefit_info = '白银VIP,系统自动匹配9.2折券',
|
||||
level_rank = 2,
|
||||
recharge_threshold = 1000.00,
|
||||
status = 1,
|
||||
update_time = NOW()
|
||||
WHERE required_vip_level = '白银';
|
||||
|
||||
INSERT INTO t_vip_benefit (benefit_info, required_vip_level, level_rank, recharge_threshold, status, create_time, update_time)
|
||||
SELECT '白银VIP,系统自动匹配9.2折券', '白银', 2, 1000.00, 1, NOW(), NOW()
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM t_vip_benefit
|
||||
WHERE required_vip_level = '白银'
|
||||
);
|
||||
|
||||
UPDATE t_vip_benefit
|
||||
SET benefit_info = '黄金VIP,系统自动匹配9.0折券',
|
||||
level_rank = 3,
|
||||
recharge_threshold = 2000.00,
|
||||
status = 1,
|
||||
update_time = NOW()
|
||||
WHERE required_vip_level = '黄金';
|
||||
|
||||
INSERT INTO t_vip_benefit (benefit_info, required_vip_level, level_rank, recharge_threshold, status, create_time, update_time)
|
||||
SELECT '黄金VIP,系统自动匹配9.0折券', '黄金', 3, 2000.00, 1, NOW(), NOW()
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM t_vip_benefit
|
||||
WHERE required_vip_level = '黄金'
|
||||
);
|
||||
|
||||
INSERT INTO t_coupon (
|
||||
coupon_code,
|
||||
coupon_name,
|
||||
coupon_type,
|
||||
discount_value,
|
||||
min_amount,
|
||||
reduce_amount,
|
||||
total_count,
|
||||
used_count,
|
||||
start_time,
|
||||
end_time,
|
||||
status,
|
||||
create_time,
|
||||
update_time,
|
||||
is_delete,
|
||||
shop_id,
|
||||
coupon_scene,
|
||||
required_vip_level,
|
||||
auto_match
|
||||
)
|
||||
VALUES
|
||||
('VIP_AUTO_BRONZE_95', '青铜VIP自动95折券', 1, 9.50, 0.00, 0.00, 999999, 0, NOW(), '2099-12-31 23:59:59', 1, NOW(), NOW(), 0, NULL, 'VIP_AUTO', '青铜', 1),
|
||||
('VIP_AUTO_SILVER_92', '白银VIP自动92折券', 1, 9.20, 0.00, 0.00, 999999, 0, NOW(), '2099-12-31 23:59:59', 1, NOW(), NOW(), 0, NULL, 'VIP_AUTO', '白银', 1),
|
||||
('VIP_AUTO_GOLD_90', '黄金VIP自动90折券', 1, 9.00, 0.00, 0.00, 999999, 0, NOW(), '2099-12-31 23:59:59', 1, NOW(), NOW(), 0, NULL, 'VIP_AUTO', '黄金', 1)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
coupon_name = VALUES(coupon_name),
|
||||
coupon_type = VALUES(coupon_type),
|
||||
discount_value = VALUES(discount_value),
|
||||
min_amount = VALUES(min_amount),
|
||||
reduce_amount = VALUES(reduce_amount),
|
||||
total_count = VALUES(total_count),
|
||||
status = VALUES(status),
|
||||
is_delete = VALUES(is_delete),
|
||||
shop_id = VALUES(shop_id),
|
||||
coupon_scene = VALUES(coupon_scene),
|
||||
required_vip_level = VALUES(required_vip_level),
|
||||
auto_match = VALUES(auto_match),
|
||||
update_time = NOW();
|
||||
|
||||
DROP PROCEDURE IF EXISTS sp_add_column_if_not_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_add_index_if_not_exists;
|
||||
DROP PROCEDURE IF EXISTS sp_add_unique_index_if_not_exists;
|
||||
Reference in New Issue
Block a user