|
|
@@ -29,7 +29,10 @@ import java.time.ZoneId;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -534,7 +537,7 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
* @return 领取的优惠券列表
|
|
|
*/
|
|
|
@Override
|
|
|
- public List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(String storeId, String friendStoreUserId, Integer type) {
|
|
|
+ public List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(String storeId, String friendStoreUserId, Integer type, Integer couponType) {
|
|
|
QueryWrapper<LifeDiscountCouponFriendRuleDetailVo> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.eq("ldcsf.store_user_id", storeId);
|
|
|
queryWrapper.eq("ldcsf.delete_flag", 0);
|
|
|
@@ -552,6 +555,10 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
// 优惠券:保持原逻辑,只查 release_type=1
|
|
|
queryWrapper.eq("ldcsf.release_type", 1);
|
|
|
queryWrapper.isNotNull("ldcsf.coupon_id");
|
|
|
+ //如果指定了优惠券类型(满减券或折扣券),添加筛选条件
|
|
|
+ if (couponType != null) {
|
|
|
+ queryWrapper.eq("ldc.coupon_type", couponType);
|
|
|
+ }
|
|
|
if (StringUtils.isEmpty(friendStoreUserId)) {
|
|
|
queryWrapper.groupBy("ldcsf.friend_store_user_id").orderByDesc("couponNum");
|
|
|
} else {
|
|
|
@@ -562,35 +569,59 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
|
|
|
@Override
|
|
|
public List<LifeDiscountCouponFriendRuleVo> getRuleList(String storeId, String acName, String status) {
|
|
|
+ // 参数校验
|
|
|
if (StringUtils.isEmpty(storeId)) {
|
|
|
+ log.warn("getRuleList 参数错误:storeId 不能为空");
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
+
|
|
|
+ // 查询规则列表(一个规则可能关联多个优惠券,会返回多条记录)
|
|
|
List<LifeDiscountCouponFriendRuleVo> ruleList = lifeDiscountCouponFriendRuleDetailMapper.getRuleList(storeId);
|
|
|
if (ruleList == null) {
|
|
|
ruleList = new ArrayList<>();
|
|
|
}
|
|
|
+
|
|
|
+ // 计算状态:根据结束日期判断活动是否已结束
|
|
|
+ // 状态:0-启用(活动进行中),1-禁用(活动已结束)
|
|
|
if (ObjectUtils.isNotEmpty(ruleList)) {
|
|
|
Date now = new Date();
|
|
|
ruleList.forEach(i -> {
|
|
|
if (i.getEndDate() != null) {
|
|
|
+ // 如果结束日期在当前时间之后,活动进行中(启用)
|
|
|
+ // 如果结束日期在当前时间之前或等于,活动已结束(禁用)
|
|
|
i.setStatus(i.getEndDate().after(now) ? "0" : "1");
|
|
|
} else {
|
|
|
- i.setStatus(null);
|
|
|
+ // 如果结束日期为null,视为永久有效,状态为启用
|
|
|
+ i.setStatus("0");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ // 按规则ID分组去重:同一个规则ID只保留第一条记录(避免一个规则关联多个优惠券时重复展示)
|
|
|
+ Map<Integer, LifeDiscountCouponFriendRuleVo> ruleMap = new LinkedHashMap<>();
|
|
|
+ for (LifeDiscountCouponFriendRuleVo rule : ruleList) {
|
|
|
+ if (rule.getId() != null && !ruleMap.containsKey(rule.getId())) {
|
|
|
+ ruleMap.put(rule.getId(), rule);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ruleList = new ArrayList<>(ruleMap.values());
|
|
|
+
|
|
|
+ // 根据活动名称过滤(模糊查询)
|
|
|
if (StringUtils.isNotEmpty(acName)) {
|
|
|
- String name = acName;
|
|
|
+ String name = acName.trim();
|
|
|
ruleList = ruleList.stream()
|
|
|
.filter(i -> i.getAcName() != null && i.getAcName().contains(name))
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
+
|
|
|
+ // 根据状态过滤
|
|
|
if (StringUtils.isNotEmpty(status)) {
|
|
|
- String s = status;
|
|
|
+ String s = status.trim();
|
|
|
ruleList = ruleList.stream()
|
|
|
.filter(i -> i.getStatus() != null && i.getStatus().equals(s))
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
+
|
|
|
return ruleList;
|
|
|
}
|
|
|
|
|
|
@@ -602,58 +633,256 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
if (ObjectUtils.isNotEmpty(lifeDiscountCouponFriendRule)) {
|
|
|
List<LifeDiscountCouponFriendRuleDetailVo> lifeDiscountCouponFriendRuleDetails = lifeDiscountCouponFriendRuleDetailMapper.getDetailList(lifeDiscountCouponFriendRule.getId().toString());
|
|
|
ruleVo.setLifeDiscountCouponFriendRuleDetailVos(lifeDiscountCouponFriendRuleDetails);
|
|
|
+
|
|
|
+ // 如果规则表中没有 couponType,尝试从关联的优惠券中获取
|
|
|
+ if (ruleVo.getCouponType() == null && ObjectUtils.isNotEmpty(lifeDiscountCouponFriendRuleDetails)) {
|
|
|
+ // 查找第一个优惠券(非代金券)的 couponType
|
|
|
+ for (LifeDiscountCouponFriendRuleDetailVo detail : lifeDiscountCouponFriendRuleDetails) {
|
|
|
+ if (detail.getCouponId() != null && detail.getCouponType() != null) {
|
|
|
+ ruleVo.setCouponType(detail.getCouponType());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return ruleVo;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 代金券类型常量
|
|
|
+ */
|
|
|
+ private static final Integer TYPE_VOUCHER = 4;
|
|
|
+
|
|
|
@Override
|
|
|
- public List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponList(String storeUserId, String friendStoreUserId, String storeName, Integer type) {
|
|
|
- boolean voucherOnly = Integer.valueOf(4).equals(type);
|
|
|
- if (StringUtils.isNotEmpty(storeUserId)) {
|
|
|
- // 好友赠我
|
|
|
- if (voucherOnly) {
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> q = new QueryWrapper<>();
|
|
|
- q.eq("ldcsf.store_user_id", storeUserId).eq("ldcsf.delete_flag", 0).isNotNull("ldcsf.voucher_id");
|
|
|
- if (StringUtils.isNotEmpty(storeName)) q.like("si.store_name", storeName);
|
|
|
- return lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListVoucher(q);
|
|
|
+ public List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponList(String storeUserId, Integer queryType, String storeName, Integer type, Integer couponType) {
|
|
|
+ // 参数校验
|
|
|
+ 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<>();
|
|
|
+ }
|
|
|
+ // 校验couponType参数(1=满减券,2=折扣券)
|
|
|
+ if (couponType != null && couponType != 1 && couponType != 2) {
|
|
|
+ log.warn("getReceivedSendFriendCouponList 参数错误:couponType 必须为 1(满减券)或 2(折扣券),当前值={}", couponType);
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断是否仅查询代金券
|
|
|
+ boolean voucherOnly = TYPE_VOUCHER.equals(type);
|
|
|
+
|
|
|
+ // 确定查询类型
|
|
|
+ // 根据表结构(实体类注释):
|
|
|
+ // store_user_id: store_info的ID(店铺ID),收到券的店铺
|
|
|
+ // friend_store_user_id: store_user的ID(店铺用户ID),送券的用户
|
|
|
+ // 前端传入的 storeUserId 是当前登录账号的 store_user.id(店铺用户ID)
|
|
|
+ // queryType = 1: 我收到的(所有好友赠送给我的)-> store_user_id = 我的店铺ID(我是收到券的店铺,需要从店铺用户ID转换为店铺ID)
|
|
|
+ // queryType = 2: 我送出的(我送给所有好友的)-> friend_store_user_id = 我的店铺用户ID(我是送券的用户)
|
|
|
+ boolean isReceivedByMe = (queryType == 1);
|
|
|
+
|
|
|
+ return queryCouponList(isReceivedByMe, storeUserId, storeName, type, voucherOnly, couponType);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询赠券列表(抽取公共方法,减少代码重复)
|
|
|
+ *
|
|
|
+ * @param isReceivedByMe true=我收到的(所有好友赠送给我的),false=我送出的(我送给所有好友的)
|
|
|
+ * @param storeUserId 当前登录店铺用户ID
|
|
|
+ * @param storeName 店铺名称(模糊查询)
|
|
|
+ * @param type 类型参数(4=代金券,其他=优惠券,null=全部)
|
|
|
+ * @param voucherOnly 是否仅查询代金券
|
|
|
+ * @param couponType 优惠券类型(1=满减券,2=折扣券,null=全部优惠券)
|
|
|
+ * @return 赠券列表
|
|
|
+ */
|
|
|
+ private List<LifeDiscountCouponFriendRuleVo> queryCouponList(boolean isReceivedByMe, String storeUserId, String storeName, Integer type, boolean voucherOnly, Integer couponType) {
|
|
|
+ List<LifeDiscountCouponFriendRuleVo> result;
|
|
|
+
|
|
|
+ // 仅查询代金券
|
|
|
+ if (voucherOnly) {
|
|
|
+ QueryWrapper<LifeDiscountCouponFriendRuleVo> queryWrapper = buildBaseQueryWrapper(isReceivedByMe, storeUserId, storeName);
|
|
|
+ queryWrapper.isNotNull("ldcsf.voucher_id");
|
|
|
+ result = isReceivedByMe
|
|
|
+ ? lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListVoucher(queryWrapper)
|
|
|
+ : lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhyVoucher(queryWrapper);
|
|
|
+ } else {
|
|
|
+ // 查询优惠券
|
|
|
+ QueryWrapper<LifeDiscountCouponFriendRuleVo> couponQuery = buildBaseQueryWrapper(isReceivedByMe, storeUserId, storeName);
|
|
|
+ couponQuery.isNotNull("ldcsf.coupon_id");
|
|
|
+ // 如果指定了优惠券类型(满减券或折扣券),添加筛选条件
|
|
|
+ if (couponType != null) {
|
|
|
+ couponQuery.eq("ldc.coupon_type", couponType);
|
|
|
+ }
|
|
|
+ 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);
|
|
|
}
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> qCoupon = new QueryWrapper<>();
|
|
|
- qCoupon.eq("ldcsf.store_user_id", storeUserId).eq("ldcsf.delete_flag", 0).isNotNull("ldcsf.coupon_id");
|
|
|
- if (StringUtils.isNotEmpty(storeName)) qCoupon.like("si.store_name", storeName);
|
|
|
- List<LifeDiscountCouponFriendRuleVo> list = lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponList(qCoupon);
|
|
|
- if (type != null) return list;
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> qVoucher = new QueryWrapper<>();
|
|
|
- qVoucher.eq("ldcsf.store_user_id", storeUserId).eq("ldcsf.delete_flag", 0).isNotNull("ldcsf.voucher_id");
|
|
|
- if (StringUtils.isNotEmpty(storeName)) qVoucher.like("si.store_name", storeName);
|
|
|
- List<LifeDiscountCouponFriendRuleVo> voucherList = lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListVoucher(qVoucher);
|
|
|
- list = new ArrayList<>(list);
|
|
|
- list.addAll(voucherList);
|
|
|
- list.sort((a, b) -> (b.getEndDate() != null && a.getEndDate() != null) ? b.getEndDate().compareTo(a.getEndDate()) : 0);
|
|
|
- return list;
|
|
|
}
|
|
|
- if (StringUtils.isNotEmpty(friendStoreUserId)) {
|
|
|
- // 我赠好友
|
|
|
- if (voucherOnly) {
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> q = new QueryWrapper<>();
|
|
|
- q.eq("ldcsf.friend_store_user_id", friendStoreUserId).eq("ldcsf.delete_flag", 0).isNotNull("ldcsf.voucher_id");
|
|
|
- if (StringUtils.isNotEmpty(storeName)) q.like("si.store_name", storeName);
|
|
|
- return lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhyVoucher(q);
|
|
|
+
|
|
|
+ // 批量查询商户头像并填充
|
|
|
+ fillStoreAvatar(result);
|
|
|
+
|
|
|
+ // 排序
|
|
|
+ return sortByEndDate(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量查询商户头像并填充到结果中
|
|
|
+ * 只从 store_user.head_img 获取商户头像,不涉及店铺信息表(store_info)
|
|
|
+ *
|
|
|
+ * @param couponList 券列表
|
|
|
+ */
|
|
|
+ private void fillStoreAvatar(List<LifeDiscountCouponFriendRuleVo> couponList) {
|
|
|
+ if (CollectionUtils.isEmpty(couponList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 收集所有的店铺ID(storeId是Integer类型)
|
|
|
+ List<Integer> storeIds = couponList.stream()
|
|
|
+ .map(LifeDiscountCouponFriendRuleVo::getStoreId)
|
|
|
+ .filter(storeId -> storeId != null)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (storeIds.isEmpty()) {
|
|
|
+ log.warn("没有有效的店铺ID,无法查询商户头像");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量查询商户头像(只从 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 填充头像到结果中
|
|
|
+ for (LifeDiscountCouponFriendRuleVo vo : couponList) {
|
|
|
+ if (vo.getStoreId() != null) {
|
|
|
+ String avatar = storeAvatarMap.get(vo.getStoreId());
|
|
|
+ if (avatar != null) {
|
|
|
+ vo.setImgUrl(avatar);
|
|
|
+ }
|
|
|
}
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> qCoupon = new QueryWrapper<>();
|
|
|
- qCoupon.eq("ldcsf.friend_store_user_id", friendStoreUserId).eq("ldcsf.delete_flag", 0).isNotNull("ldcsf.coupon_id");
|
|
|
- if (StringUtils.isNotEmpty(storeName)) qCoupon.like("si.store_name", storeName);
|
|
|
- List<LifeDiscountCouponFriendRuleVo> list = lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhy(qCoupon);
|
|
|
- if (type != null) return list;
|
|
|
- QueryWrapper<LifeDiscountCouponFriendRuleVo> qVoucher = new QueryWrapper<>();
|
|
|
- qVoucher.eq("ldcsf.friend_store_user_id", friendStoreUserId).eq("ldcsf.delete_flag", 0).isNotNull("ldcsf.voucher_id");
|
|
|
- if (StringUtils.isNotEmpty(storeName)) qVoucher.like("si.store_name", storeName);
|
|
|
- List<LifeDiscountCouponFriendRuleVo> voucherList = lifeDiscountCouponStoreFriendMapper.getReceivedSendFriendCouponListwzhyVoucher(qVoucher);
|
|
|
- list = new ArrayList<>(list);
|
|
|
- list.addAll(voucherList);
|
|
|
- list.sort((a, b) -> (b.getEndDate() != null && a.getEndDate() != null) ? b.getEndDate().compareTo(a.getEndDate()) : 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建基础查询条件
|
|
|
+ *
|
|
|
+ * @param isReceivedByMe true=我收到的(所有好友赠送给我的),false=我送出的(我送给所有好友的)
|
|
|
+ * @param storeUserId 当前登录店铺用户ID
|
|
|
+ * @param storeName 店铺名称(模糊查询)
|
|
|
+ * @return QueryWrapper
|
|
|
+ */
|
|
|
+ private QueryWrapper<LifeDiscountCouponFriendRuleVo> buildBaseQueryWrapper(boolean isReceivedByMe, String storeUserId, String storeName) {
|
|
|
+ QueryWrapper<LifeDiscountCouponFriendRuleVo> queryWrapper = new QueryWrapper<>();
|
|
|
+ // 根据表结构(实体类注释):
|
|
|
+ // store_user_id: store_info的ID(店铺ID),收到券的店铺
|
|
|
+ // friend_store_user_id: store_user的ID(店铺用户ID),送券的用户
|
|
|
+ // 前端传入的 storeUserId 是当前登录账号的 store_user.id(店铺用户ID)
|
|
|
+ if (isReceivedByMe) {
|
|
|
+ // 我收到的:查询所有好友赠送给我的券(我是收到券的店铺)
|
|
|
+ // store_user_id = 我的店铺ID(store_info.id),需要从店铺用户ID转换为店铺ID
|
|
|
+ StoreUser storeUser = storeUserMapper.selectById(storeUserId);
|
|
|
+ if (storeUser != null && storeUser.getStoreId() != null) {
|
|
|
+ queryWrapper.eq("ldcsf.store_user_id", storeUser.getStoreId());
|
|
|
+ } else {
|
|
|
+ // 如果查询不到店铺用户,返回空结果
|
|
|
+ queryWrapper.eq("1", "0"); // 永远不匹配的条件
|
|
|
+ log.warn("buildBaseQueryWrapper 查询不到店铺用户,storeUserId={}", storeUserId);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 我送出的:查询我送给所有好友的券(我是送券的用户)
|
|
|
+ // friend_store_user_id = 我的店铺用户ID(store_user.id)
|
|
|
+ queryWrapper.eq("ldcsf.friend_store_user_id", storeUserId);
|
|
|
+ }
|
|
|
+ queryWrapper.eq("ldcsf.delete_flag", 0);
|
|
|
+ // 店铺名称模糊查询
|
|
|
+ if (StringUtils.isNotEmpty(storeName)) {
|
|
|
+ queryWrapper.like("si.store_name", storeName);
|
|
|
+ }
|
|
|
+ return queryWrapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按有效期降序排序(null 值排在最后),如果有效期为 null 则按创建时间降序排序
|
|
|
+ *
|
|
|
+ * @param list 待排序列表
|
|
|
+ * @return 排序后的列表
|
|
|
+ */
|
|
|
+ private List<LifeDiscountCouponFriendRuleVo> sortByEndDate(List<LifeDiscountCouponFriendRuleVo> list) {
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
return list;
|
|
|
}
|
|
|
- return new ArrayList<>();
|
|
|
+ list.sort((a, b) -> {
|
|
|
+ // 优先按有效期(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 (endDateA == null) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ if (endDateB == null) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ // 降序排序(最新的在前)
|
|
|
+ return endDateB.compareTo(endDateA);
|
|
|
+ });
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -717,6 +946,7 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
int grantedVoucher = 0;
|
|
|
String couponName = null; // 优惠券名称
|
|
|
String voucherName = null; // 代金券名称
|
|
|
+ LifeDiscountCoupon lifeDiscountCoupon = null; // 保存优惠券对象,用于通知消息
|
|
|
|
|
|
// 按优惠券ID发放:发放1张,扣减库存,发优惠券通知
|
|
|
if (couponId != null && couponId > 0) {
|
|
|
@@ -728,7 +958,12 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
lifeDiscountCouponUser.setCouponId(coupon.getId());
|
|
|
lifeDiscountCouponUser.setUserId(commenterUserId);
|
|
|
lifeDiscountCouponUser.setReceiveTime(new Date());
|
|
|
- lifeDiscountCouponUser.setExpirationTime(coupon.getValidDate());
|
|
|
+ // 设置过期时间:优先使用validDate,如果为null则使用endDate
|
|
|
+ LocalDate expirationTime = coupon.getValidDate();
|
|
|
+ if (expirationTime == null && coupon.getEndDate() != null) {
|
|
|
+ expirationTime = coupon.getEndDate();
|
|
|
+ }
|
|
|
+ lifeDiscountCouponUser.setExpirationTime(expirationTime);
|
|
|
lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
|
|
|
lifeDiscountCouponUser.setDeleteFlag(0);
|
|
|
lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
|
|
|
@@ -736,6 +971,7 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
lifeDiscountCouponMapper.updateById(coupon);
|
|
|
grantedCoupon = 1;
|
|
|
couponName = coupon.getName(); // 保存优惠券名称
|
|
|
+ lifeDiscountCoupon = coupon; // 保存优惠券对象,用于通知消息
|
|
|
} catch (Exception e) {
|
|
|
log.error("发放优惠券失败,userId={}, storeId={}, couponId={}, error={}", userId, storeId, couponId, e.getMessage(), e);
|
|
|
}
|
|
|
@@ -774,12 +1010,42 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
|
|
|
LifeNotice couponNotice = new LifeNotice();
|
|
|
couponNotice.setSenderId("system");
|
|
|
couponNotice.setReceiverId("user_" + lifeUser.getUserPhone());
|
|
|
- // 明确标注是优惠券,并包含券的具体名称
|
|
|
- String couponText = "您对店铺「" + storeInfo.getStoreName() + "」的好评已通过审核,已为您发放优惠券「" + couponName + "」,快去我的券包查看吧~";
|
|
|
+
|
|
|
+ // 构建优惠券详情信息
|
|
|
+ String couponDetailText = "";
|
|
|
+ if (lifeDiscountCoupon != null) {
|
|
|
+ Integer couponType = lifeDiscountCoupon.getCouponType();
|
|
|
+ if (couponType != null && couponType == 2) {
|
|
|
+ // 折扣券
|
|
|
+ BigDecimal discountRate = lifeDiscountCoupon.getDiscountRate();
|
|
|
+ if (discountRate != null) {
|
|
|
+ couponDetailText = "(" + discountRate + "折";
|
|
|
+ if (lifeDiscountCoupon.getMinimumSpendingAmount() != null) {
|
|
|
+ couponDetailText += ",满" + lifeDiscountCoupon.getMinimumSpendingAmount() + "可用";
|
|
|
+ }
|
|
|
+ couponDetailText += ")";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 满减券(默认)
|
|
|
+ if (lifeDiscountCoupon.getNominalValue() != null) {
|
|
|
+ couponDetailText = "(满减" + lifeDiscountCoupon.getNominalValue() + "元";
|
|
|
+ if (lifeDiscountCoupon.getMinimumSpendingAmount() != null) {
|
|
|
+ couponDetailText += ",满" + lifeDiscountCoupon.getMinimumSpendingAmount() + "可用";
|
|
|
+ }
|
|
|
+ couponDetailText += ")";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 明确标注是优惠券,并包含券的具体名称和详情
|
|
|
+ String couponText = "您对店铺「" + storeInfo.getStoreName() + "」的好评已通过审核,已为您发放优惠券「" + couponName + "」" + couponDetailText + ",快去我的券包查看吧~";
|
|
|
JSONObject couponJson = new JSONObject();
|
|
|
couponJson.put("message", couponText);
|
|
|
couponJson.put("couponType", "优惠券"); // 明确标识券类型
|
|
|
couponJson.put("couponName", couponName); // 券名称
|
|
|
+ if (lifeDiscountCoupon != null) {
|
|
|
+ couponJson.put("couponTypeValue", lifeDiscountCoupon.getCouponType()); // 优惠券类型值(1-满减券,2-折扣券)
|
|
|
+ }
|
|
|
couponNotice.setContext(couponJson.toJSONString());
|
|
|
couponNotice.setNoticeType(1);
|
|
|
couponNotice.setTitle("好评送优惠券"); // 标题明确标注是优惠券
|