|
|
@@ -29,7 +29,9 @@ import java.time.ZoneId;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -612,89 +614,160 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
private static final Integer TYPE_VOUCHER = 4;
|
|
|
|
|
|
@Override
|
|
|
- public List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponList(String storeUserId, String friendStoreUserId, String storeName, Integer type) {
|
|
|
- // 参数校验:至少需要提供一个查询条件
|
|
|
- if (StringUtils.isEmpty(storeUserId) && StringUtils.isEmpty(friendStoreUserId)) {
|
|
|
- log.warn("getReceivedSendFriendCouponList 参数错误:storeUserId 和 friendStoreUserId 不能同时为空");
|
|
|
+ public List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponList(String storeUserId, Integer queryType, String storeName, Integer type) {
|
|
|
+ // 参数校验
|
|
|
+ if (StringUtils.isEmpty(storeUserId)) {
|
|
|
+ log.warn("getReceivedSendFriendCouponList 参数错误:storeUserId 不能为空");
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ if (queryType == null || (queryType != 1 && queryType != 2)) {
|
|
|
+ log.warn("getReceivedSendFriendCouponList 参数错误:queryType 必须为 1 或 2,当前值={}", queryType);
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
// 判断是否仅查询代金券
|
|
|
boolean voucherOnly = TYPE_VOUCHER.equals(type);
|
|
|
|
|
|
- // 好友赠我场景
|
|
|
- if (StringUtils.isNotEmpty(storeUserId)) {
|
|
|
- return queryCouponList(true, storeUserId, storeName, type, voucherOnly);
|
|
|
- }
|
|
|
-
|
|
|
- // 我赠好友场景
|
|
|
- if (StringUtils.isNotEmpty(friendStoreUserId)) {
|
|
|
- return queryCouponList(false, friendStoreUserId, storeName, type, voucherOnly);
|
|
|
- }
|
|
|
+ // 确定查询类型
|
|
|
+ // queryType = 1: 我收到的(所有好友赠送给我的)-> friend_store_user_id = 我的ID(我是接收者)
|
|
|
+ // queryType = 2: 我送出的(我送给所有好友的)-> store_user_id = 我的ID(我是发放者)
|
|
|
+ boolean isReceivedByMe = (queryType == 1);
|
|
|
|
|
|
- return new ArrayList<>();
|
|
|
+ return queryCouponList(isReceivedByMe, storeUserId, storeName, type, voucherOnly);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询赠券列表(抽取公共方法,减少代码重复)
|
|
|
*
|
|
|
- * @param isReceivedByMe true=好友赠我,false=我赠好友
|
|
|
- * @param userId 店铺用户ID(根据 isReceivedByMe 决定是 store_user_id 还是 friend_store_user_id)
|
|
|
- * @param storeName 店铺名称(模糊查询)
|
|
|
- * @param type 类型参数(4=代金券,其他=优惠券,null=全部)
|
|
|
- * @param voucherOnly 是否仅查询代金券
|
|
|
+ * @param isReceivedByMe true=我收到的(所有好友赠送给我的),false=我送出的(我送给所有好友的)
|
|
|
+ * @param storeUserId 当前登录店铺用户ID
|
|
|
+ * @param storeName 店铺名称(模糊查询)
|
|
|
+ * @param type 类型参数(4=代金券,其他=优惠券,null=全部)
|
|
|
+ * @param voucherOnly 是否仅查询代金券
|
|
|
* @return 赠券列表
|
|
|
*/
|
|
|
- private List<LifeDiscountCouponFriendRuleVo> queryCouponList(boolean isReceivedByMe, String userId, String storeName, Integer type, boolean voucherOnly) {
|
|
|
+ private List<LifeDiscountCouponFriendRuleVo> queryCouponList(boolean isReceivedByMe, String storeUserId, String storeName, Integer type, boolean voucherOnly) {
|
|
|
+ List<LifeDiscountCouponFriendRuleVo> result;
|
|
|
+
|
|
|
// 仅查询代金券
|
|
|
if (voucherOnly) {
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> queryWrapper = buildBaseQueryWrapper(isReceivedByMe, userId, storeName);
|
|
|
+ QueryWrapper<LifeDiscountCouponFriendRuleVo> queryWrapper = buildBaseQueryWrapper(isReceivedByMe, storeUserId, storeName);
|
|
|
queryWrapper.isNotNull("ldcsf.voucher_id");
|
|
|
- return isReceivedByMe
|
|
|
+ result = isReceivedByMe
|
|
|
? lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListVoucher(queryWrapper)
|
|
|
: lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhyVoucher(queryWrapper);
|
|
|
+ } else {
|
|
|
+ // 查询优惠券
|
|
|
+ QueryWrapper<LifeDiscountCouponFriendRuleVo> couponQuery = buildBaseQueryWrapper(isReceivedByMe, storeUserId, storeName);
|
|
|
+ couponQuery.isNotNull("ldcsf.coupon_id");
|
|
|
+ List<LifeDiscountCouponFriendRuleVo> couponList = isReceivedByMe
|
|
|
+ ? lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponList(couponQuery)
|
|
|
+ : lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhy(couponQuery);
|
|
|
+
|
|
|
+ // 如果 type 不为 null,说明只查询优惠券,直接返回
|
|
|
+ if (type != null) {
|
|
|
+ result = couponList;
|
|
|
+ } else {
|
|
|
+ // type 为 null,需要合并优惠券和代金券
|
|
|
+ QueryWrapper<LifeDiscountCouponFriendRuleVo> voucherQuery = buildBaseQueryWrapper(isReceivedByMe, storeUserId, storeName);
|
|
|
+ voucherQuery.isNotNull("ldcsf.voucher_id");
|
|
|
+ List<LifeDiscountCouponFriendRuleVo> voucherList = isReceivedByMe
|
|
|
+ ? lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListVoucher(voucherQuery)
|
|
|
+ : lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhyVoucher(voucherQuery);
|
|
|
+
|
|
|
+ // 合并列表
|
|
|
+ result = new ArrayList<>(couponList);
|
|
|
+ result.addAll(voucherList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量查询商户头像并填充
|
|
|
+ fillStoreAvatar(result);
|
|
|
+
|
|
|
+ // 排序
|
|
|
+ return sortByEndDate(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量查询商户头像并填充到结果中
|
|
|
+ * 只从 store_user.head_img 获取商户头像,不涉及店铺信息表(store_info)
|
|
|
+ *
|
|
|
+ * @param couponList 券列表
|
|
|
+ */
|
|
|
+ private void fillStoreAvatar(List<LifeDiscountCouponFriendRuleVo> couponList) {
|
|
|
+ if (CollectionUtils.isEmpty(couponList)) {
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- // 查询优惠券
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> couponQuery = buildBaseQueryWrapper(isReceivedByMe, userId, storeName);
|
|
|
- couponQuery.isNotNull("ldcsf.coupon_id");
|
|
|
- List<LifeDiscountCouponFriendRuleVo> couponList = isReceivedByMe
|
|
|
- ? lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponList(couponQuery)
|
|
|
- : lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhy(couponQuery);
|
|
|
+ // 收集所有的店铺ID(storeId是Integer类型)
|
|
|
+ List<Integer> storeIds = couponList.stream()
|
|
|
+ .map(LifeDiscountCouponFriendRuleVo::getStoreId)
|
|
|
+ .filter(storeId -> storeId != null)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
|
- // 如果 type 不为 null,说明只查询优惠券,直接返回
|
|
|
- if (type != null) {
|
|
|
- return sortByEndDate(couponList);
|
|
|
+ if (storeIds.isEmpty()) {
|
|
|
+ log.warn("没有有效的店铺ID,无法查询商户头像");
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- // type 为 null,需要合并优惠券和代金券
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> voucherQuery = buildBaseQueryWrapper(isReceivedByMe, userId, storeName);
|
|
|
- voucherQuery.isNotNull("ldcsf.voucher_id");
|
|
|
- List<LifeDiscountCouponFriendRuleVo> voucherList = isReceivedByMe
|
|
|
- ? lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListVoucher(voucherQuery)
|
|
|
- : lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhyVoucher(voucherQuery);
|
|
|
+ // 批量查询商户头像(只从 store_user.head_img 获取,优先取主账号头像)
|
|
|
+ LambdaQueryWrapper<StoreUser> userWrapper = new LambdaQueryWrapper<>();
|
|
|
+ userWrapper.in(StoreUser::getStoreId, storeIds)
|
|
|
+ .eq(StoreUser::getDeleteFlag, 0)
|
|
|
+ .isNotNull(StoreUser::getHeadImg)
|
|
|
+ .ne(StoreUser::getHeadImg, "")
|
|
|
+ .orderByAsc(StoreUser::getAccountType) // 主账号(1)排在前面,null值排在最后
|
|
|
+ .orderByAsc(StoreUser::getId);
|
|
|
+ List<StoreUser> storeUsers = storeUserMapper.selectList(userWrapper);
|
|
|
+
|
|
|
+ // 构建店铺ID -> 头像的映射(优先主账号)
|
|
|
+ Map<Integer, String> storeAvatarMap = new HashMap<>();
|
|
|
+ for (StoreUser user : storeUsers) {
|
|
|
+ if (user.getStoreId() != null && user.getHeadImg() != null && !user.getHeadImg().trim().isEmpty()) {
|
|
|
+ Integer storeId = user.getStoreId();
|
|
|
+ // 如果该店铺还没有头像,或者当前用户是主账号(account_type = 1),则设置头像
|
|
|
+ // 主账号优先:如果已存在头像但当前是主账号,则覆盖
|
|
|
+ if (!storeAvatarMap.containsKey(storeId)) {
|
|
|
+ // 该店铺还没有头像,直接设置
|
|
|
+ storeAvatarMap.put(storeId, user.getHeadImg());
|
|
|
+ } else if (user.getAccountType() != null && user.getAccountType() == 1) {
|
|
|
+ // 该店铺已有头像,但当前是主账号,优先使用主账号头像
|
|
|
+ storeAvatarMap.put(storeId, user.getHeadImg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 合并列表并排序
|
|
|
- List<LifeDiscountCouponFriendRuleVo> result = new ArrayList<>(couponList);
|
|
|
- result.addAll(voucherList);
|
|
|
- return sortByEndDate(result);
|
|
|
+ // 填充头像到结果中
|
|
|
+ for (LifeDiscountCouponFriendRuleVo vo : couponList) {
|
|
|
+ if (vo.getStoreId() != null) {
|
|
|
+ String avatar = storeAvatarMap.get(vo.getStoreId());
|
|
|
+ if (avatar != null) {
|
|
|
+ vo.setImgUrl(avatar);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构建基础查询条件
|
|
|
*
|
|
|
- * @param isReceivedByMe true=好友赠我,false=我赠好友
|
|
|
- * @param userId 店铺用户ID
|
|
|
- * @param storeName 店铺名称(模糊查询)
|
|
|
+ * @param isReceivedByMe true=我收到的(所有好友赠送给我的),false=我送出的(我送给所有好友的)
|
|
|
+ * @param storeUserId 当前登录店铺用户ID
|
|
|
+ * @param storeName 店铺名称(模糊查询)
|
|
|
* @return QueryWrapper
|
|
|
*/
|
|
|
- private QueryWrapper<LifeDiscountCouponFriendRuleVo> buildBaseQueryWrapper(boolean isReceivedByMe, String userId, String storeName) {
|
|
|
+ private QueryWrapper<LifeDiscountCouponFriendRuleVo> buildBaseQueryWrapper(boolean isReceivedByMe, String storeUserId, String storeName) {
|
|
|
QueryWrapper<LifeDiscountCouponFriendRuleVo> queryWrapper = new QueryWrapper<>();
|
|
|
- // 根据场景设置不同的用户ID字段
|
|
|
+ // 根据表结构:
|
|
|
+ // store_user_id: 发放券的店铺用户ID
|
|
|
+ // friend_store_user_id: 接收券的店铺用户ID
|
|
|
if (isReceivedByMe) {
|
|
|
- queryWrapper.eq("ldcsf.store_user_id", userId);
|
|
|
+ // 我收到的:查询所有好友赠送给我的券(我是接收者)
|
|
|
+ queryWrapper.eq("ldcsf.friend_store_user_id", storeUserId);
|
|
|
} else {
|
|
|
- queryWrapper.eq("ldcsf.friend_store_user_id", userId);
|
|
|
+ // 我送出的:查询我送给所有好友的券(我是发放者)
|
|
|
+ queryWrapper.eq("ldcsf.store_user_id", storeUserId);
|
|
|
}
|
|
|
queryWrapper.eq("ldcsf.delete_flag", 0);
|
|
|
// 店铺名称模糊查询
|
|
|
@@ -705,7 +778,7 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 按结束时间降序排序(null 值排在最后)
|
|
|
+ * 按有效期降序排序(null 值排在最后),如果有效期为 null 则按创建时间降序排序
|
|
|
*
|
|
|
* @param list 待排序列表
|
|
|
* @return 排序后的列表
|
|
|
@@ -715,20 +788,35 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
return list;
|
|
|
}
|
|
|
list.sort((a, b) -> {
|
|
|
- Date dateA = a.getEndDate();
|
|
|
- Date dateB = b.getEndDate();
|
|
|
- // null 值排在最后
|
|
|
- if (dateA == null && dateB == null) {
|
|
|
+ // 优先按有效期(validDate)排序,如果为 null 则按创建时间(endDate)排序
|
|
|
+ Date validDateA = a.getValidDate();
|
|
|
+ Date validDateB = b.getValidDate();
|
|
|
+ Date endDateA = a.getEndDate();
|
|
|
+ Date endDateB = b.getEndDate();
|
|
|
+
|
|
|
+ // 如果两个都有有效期,按有效期排序
|
|
|
+ if (validDateA != null && validDateB != null) {
|
|
|
+ return validDateB.compareTo(validDateA); // 降序:有效期晚的在前
|
|
|
+ }
|
|
|
+ // 如果只有一个有有效期,有有效期的排在前面
|
|
|
+ if (validDateA != null) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (validDateB != null) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ // 如果都没有有效期,按创建时间排序
|
|
|
+ if (endDateA == null && endDateB == null) {
|
|
|
return 0;
|
|
|
}
|
|
|
- if (dateA == null) {
|
|
|
+ if (endDateA == null) {
|
|
|
return 1;
|
|
|
}
|
|
|
- if (dateB == null) {
|
|
|
+ if (endDateB == null) {
|
|
|
return -1;
|
|
|
}
|
|
|
// 降序排序(最新的在前)
|
|
|
- return dateB.compareTo(dateA);
|
|
|
+ return endDateB.compareTo(endDateA);
|
|
|
});
|
|
|
return list;
|
|
|
}
|