|
|
@@ -29,8 +29,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -62,39 +61,71 @@ public class UserCouponServiceImpl extends MPJBaseServiceImpl<UserCouponMapper,
|
|
|
this.couponService = couponService;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public PageBean<UserCouponVo> listUserCoupons(CouponQueryParam params) {
|
|
|
- var users = userService.lambdaQuery()
|
|
|
- .like(CommUtil.isNotEmptyAndNull(params.getMobilePhone()), User::getMobilePhone, params.getMobilePhone())
|
|
|
- .list();
|
|
|
- var userId2Mobile = users.stream().collect(Collectors.toMap(User::getId, User::getMobilePhone));
|
|
|
- var userIds = users.stream().map(User::getId).toList();
|
|
|
-
|
|
|
- PageHelper.startPage(params.getPageNum(), params.getPageSize());
|
|
|
- var list = lambdaQuery()
|
|
|
- .in(CommUtil.isNotEmptyAndNull(userIds), UserCoupon::getUserId, userIds)
|
|
|
- .eq(CommUtil.isNotEmptyAndNull(params.getActivityId()), UserCoupon::getId, params.getCouponId())
|
|
|
- .like(CommUtil.isNotEmptyAndNull(params.getActivityName()), UserCoupon::getActivityName, params.getActivityName())
|
|
|
- .eq(CommUtil.isNotEmptyAndNull(params.getCouponId()), UserCoupon::getId, params.getCouponId())
|
|
|
- .like(CommUtil.isNotEmptyAndNull(params.getCouponName()), UserCoupon::getCouponName, params.getCouponName())
|
|
|
- .eq(CommUtil.isNotEmptyAndNull(params.getCouponType()), UserCoupon::getCouponType, params.getCouponType())
|
|
|
- .eq(CommUtil.isNotEmptyAndNull(params.getUsageStatus()), UserCoupon::getUsageStatus, params.getUsageStatus())
|
|
|
- .orderByDesc(UserCoupon::getId)
|
|
|
+@Override
|
|
|
+public PageBean<UserCouponVo> listUserCoupons(CouponQueryParam params) {
|
|
|
+ // 分页设置
|
|
|
+ PageHelper.startPage(params.getPageNum(), params.getPageSize());
|
|
|
+
|
|
|
+ // 构造基础查询条件
|
|
|
+ var queryWrapper = lambdaQuery()
|
|
|
+ .eq(CommUtil.isNotEmptyAndNull(params.getActivityId()), UserCoupon::getId, params.getCouponId())
|
|
|
+ .like(CommUtil.isNotEmptyAndNull(params.getActivityName()), UserCoupon::getActivityName, params.getActivityName())
|
|
|
+ .eq(CommUtil.isNotEmptyAndNull(params.getCouponId()), UserCoupon::getId, params.getCouponId())
|
|
|
+ .like(CommUtil.isNotEmptyAndNull(params.getCouponName()), UserCoupon::getCouponName, params.getCouponName())
|
|
|
+ .eq(CommUtil.isNotEmptyAndNull(params.getCouponType()), UserCoupon::getCouponType, params.getCouponType())
|
|
|
+ .eq(CommUtil.isNotEmptyAndNull(params.getUsageStatus()), UserCoupon::getUsageStatus, params.getUsageStatus())
|
|
|
+ .orderByDesc(UserCoupon::getId);
|
|
|
+
|
|
|
+ List<Long> userIds = Collections.emptyList();
|
|
|
+ Map<Long, String> userIdToMobileMap = new HashMap<>();
|
|
|
+
|
|
|
+ // 若提供了手机号码,则查找匹配用户 ID 列表用于过滤优惠券
|
|
|
+ if (CommUtil.isNotEmptyAndNull(params.getMobilePhone())) {
|
|
|
+ List<User> matchedUsers = userService.lambdaQuery()
|
|
|
+ .like(User::getMobilePhone, params.getMobilePhone())
|
|
|
.list();
|
|
|
+ userIds = matchedUsers.stream().map(User::getId).toList();
|
|
|
+ userIdToMobileMap.putAll(matchedUsers.stream()
|
|
|
+ .collect(Collectors.toMap(User::getId, User::getMobilePhone)));
|
|
|
+ }
|
|
|
|
|
|
- var res = new PageBean<>(list);
|
|
|
- var voList = list.stream().map(userCoupon -> {
|
|
|
- var vo = new UserCouponVo();
|
|
|
- BeanUtils.copyProperties(userCoupon, vo);
|
|
|
- vo.setMobilePhone(userId2Mobile.get(userCoupon.getUserId()));
|
|
|
- return vo;
|
|
|
- }).toList();
|
|
|
- var newRes = new PageBean<UserCouponVo>();
|
|
|
- BeanUtils.copyProperties(res, newRes);
|
|
|
- newRes.setList(voList);
|
|
|
- return newRes;
|
|
|
+ // 应用用户 ID 过滤条件
|
|
|
+ queryWrapper.in(CommUtil.isNotEmptyAndNull(userIds), UserCoupon::getUserId, userIds);
|
|
|
+
|
|
|
+ // 执行主查询
|
|
|
+ List<UserCoupon> userCoupons = queryWrapper.list();
|
|
|
+
|
|
|
+ // 补充未通过手机号查询的所有相关用户信息
|
|
|
+ if (!userCoupons.isEmpty()) {
|
|
|
+ Set<Long> allUserIds = userCoupons.stream().map(UserCoupon::getUserId).collect(Collectors.toSet());
|
|
|
+ if (!allUserIds.isEmpty() && userIdToMobileMap.size() < allUserIds.size()) {
|
|
|
+ List<User> additionalUsers = userService.lambdaQuery()
|
|
|
+ .in(User::getId, allUserIds)
|
|
|
+ .list();
|
|
|
+ for (User u : additionalUsers) {
|
|
|
+ userIdToMobileMap.putIfAbsent(u.getId(), u.getMobilePhone());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ // 构建 VO 列表
|
|
|
+ List<UserCouponVo> voList = userCoupons.stream().map(coupon -> {
|
|
|
+ UserCouponVo vo = new UserCouponVo();
|
|
|
+ BeanUtils.copyProperties(coupon, vo);
|
|
|
+ vo.setMobilePhone(userIdToMobileMap.get(coupon.getUserId()));
|
|
|
+ return vo;
|
|
|
+ }).toList();
|
|
|
+
|
|
|
+ // 构造最终结果集
|
|
|
+ PageBean<UserCoupon> rawPageResult = new PageBean<>(userCoupons);
|
|
|
+ PageBean<UserCouponVo> resultPage = new PageBean<>();
|
|
|
+ BeanUtils.copyProperties(rawPageResult, resultPage);
|
|
|
+ resultPage.setList(voList);
|
|
|
+
|
|
|
+ return resultPage;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public void collectCoupon(Long couponId) {
|
|
|
// 手动切换数据源
|