lutong преди 2 месеца
родител
ревизия
6bd28bec3e

+ 17 - 16
alien-entity/src/main/java/shop/alien/mapper/LifeDiscountCouponStoreFriendMapper.java

@@ -36,8 +36,9 @@ public interface LifeDiscountCouponStoreFriendMapper extends BaseMapper<LifeDisc
     IPage<LifeDiscountCouponVo> selectPage(IPage<LifeDiscountCouponStoreFriendVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponStoreFriendVo> friendLifeDiscountCouponQueryWrapper);
 
     /** 好友赠我 - 优惠券 */
-    @Select("select ldcsf.created_time endDate,ldc.valid_date validDate,ldc.nominal_value nominalValue,ldc.minimum_spending_amount minimumSpendingAmount,img.img_url imgUrl,\n" +
+    @Select("select ldcsf.created_time endDate,ldc.valid_date validDate,ldc.nominal_value nominalValue,ldc.minimum_spending_amount minimumSpendingAmount,\n" +
             "si.store_name storeName,\n" +
+            "si.id storeId,\n" +
             "ldc.name couponName,\n" +
             "ldc.id couponId,\n" +
             "ldcsf.single_qty couponNum,\n" +
@@ -47,15 +48,14 @@ public interface LifeDiscountCouponStoreFriendMapper extends BaseMapper<LifeDisc
             "left join life_discount_coupon ldc\n" +
             "on ldc.id = ldcsf.coupon_id and ldc.delete_flag = 0\n" +
             "left join store_info si\n" +
-            "on si.id = ldc.store_id and si.delete_flag = 0\n" +
-            "left join store_user su on si.id = su.store_id\n" +
-            "left join store_img img on si.id = img.store_id and img.img_type = 1 and img.delete_flag = 0\n" +
+            "on CAST(si.id AS CHAR) COLLATE utf8mb4_unicode_ci = ldc.store_id and si.delete_flag = 0\n" +
             "${ew.customSqlSegment}")
     List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponList(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleVo> lifeDiscountCouponFriendRuleVoQueryWrapper);
 
     /** 好友赠我 - 代金券(type=4) */
-    @Select("select ldcsf.created_time endDate,lc.end_date validDate,CAST(lc.price AS DECIMAL(10,2)) nominalValue,null minimumSpendingAmount,img.img_url imgUrl,\n" +
+    @Select("select ldcsf.created_time endDate,lc.end_date validDate,CAST(lc.price AS DECIMAL(10,2)) nominalValue,null minimumSpendingAmount,\n" +
             "si.store_name storeName,\n" +
+            "si.id storeId,\n" +
             "lc.name couponName,\n" +
             "null couponId,\n" +
             "ldcsf.single_qty couponNum,\n" +
@@ -63,15 +63,14 @@ public interface LifeDiscountCouponStoreFriendMapper extends BaseMapper<LifeDisc
             "lc.id voucherId\n" +
             "from life_discount_coupon_store_friend ldcsf\n" +
             "left join life_coupon lc on lc.id = ldcsf.voucher_id and lc.delete_flag = 0\n" +
-            "left join store_info si on si.id = lc.store_id and si.delete_flag = 0\n" +
-            "left join store_user su on si.id = su.store_id\n" +
-            "left join store_img img on si.id = img.store_id and img.img_type = 1 and img.delete_flag = 0\n" +
+            "left join store_info si on CAST(si.id AS CHAR) COLLATE utf8mb4_unicode_ci = lc.store_id and si.delete_flag = 0\n" +
             "${ew.customSqlSegment}")
     List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponListVoucher(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleVo> lifeDiscountCouponFriendRuleVoQueryWrapper);
 
     /** 我赠好友 - 优惠券 */
-    @Select("select ldcsf.created_time endDate,ldc.valid_date validDate,ldc.nominal_value nominalValue,ldc.minimum_spending_amount minimumSpendingAmount,img.img_url imgUrl,\n" +
+    @Select("select ldcsf.created_time endDate,ldc.valid_date validDate,ldc.nominal_value nominalValue,ldc.minimum_spending_amount minimumSpendingAmount,\n" +
             "si.store_name storeName,\n" +
+            "si.id storeId,\n" +
             "ldc.name couponName,\n" +
             "ldc.id couponId,\n" +
             "ldcsf.single_qty couponNum,\n" +
@@ -80,16 +79,17 @@ public interface LifeDiscountCouponStoreFriendMapper extends BaseMapper<LifeDisc
             "from  life_discount_coupon_store_friend ldcsf\n" +
             "left join life_discount_coupon ldc\n" +
             "on ldc.id = ldcsf.coupon_id and ldc.delete_flag = 0\n" +
+            "left join store_user su\n" +
+            "on su.id = ldcsf.friend_store_user_id and su.delete_flag = 0\n" +
             "left join store_info si\n" +
-            "on si.id = ldc.store_id and si.delete_flag = 0\n" +
-            "left join store_user su on ldcsf.store_user_id = su.id\n" +
-            "left join store_img img on si.id = img.store_id and img.img_type = 1 and img.delete_flag = 0\n" +
+            "on si.id = su.store_id and si.delete_flag = 0\n" +
             "${ew.customSqlSegment}")
     List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponListwzhy(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleVo> lifeDiscountCouponFriendRuleVoQueryWrapper);
 
     /** 我赠好友 - 代金券(type=4) */
-    @Select("select ldcsf.created_time endDate,lc.end_date validDate,CAST(lc.price AS DECIMAL(10,2)) nominalValue,null minimumSpendingAmount,img.img_url imgUrl,\n" +
+    @Select("select ldcsf.created_time endDate,lc.end_date validDate,CAST(lc.price AS DECIMAL(10,2)) nominalValue,null minimumSpendingAmount,\n" +
             "si.store_name storeName,\n" +
+            "si.id storeId,\n" +
             "lc.name couponName,\n" +
             "null couponId,\n" +
             "ldcsf.single_qty couponNum,\n" +
@@ -97,9 +97,10 @@ public interface LifeDiscountCouponStoreFriendMapper extends BaseMapper<LifeDisc
             "lc.id voucherId\n" +
             "from life_discount_coupon_store_friend ldcsf\n" +
             "left join life_coupon lc on lc.id = ldcsf.voucher_id and lc.delete_flag = 0\n" +
-            "left join store_info si on si.id = lc.store_id and si.delete_flag = 0\n" +
-            "left join store_user su on ldcsf.store_user_id = su.id\n" +
-            "left join store_img img on si.id = img.store_id and img.img_type = 1 and img.delete_flag = 0\n" +
+            "left join store_user su\n" +
+            "on su.id = ldcsf.friend_store_user_id and su.delete_flag = 0\n" +
+            "left join store_info si\n" +
+            "on si.id = su.store_id and si.delete_flag = 0\n" +
             "${ew.customSqlSegment}")
     List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponListwzhyVoucher(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleVo> lifeDiscountCouponFriendRuleVoQueryWrapper);
 }

+ 9 - 12
alien-store/src/main/java/shop/alien/store/controller/LifeDiscountCouponStoreFriendController.java

@@ -7,14 +7,11 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.*;
 import shop.alien.entity.result.R;
 import shop.alien.entity.store.LifeDiscountCouponFriendRule;
-import shop.alien.entity.store.LifeGroupBuyThali;
 import shop.alien.entity.store.UserLoginInfo;
 import shop.alien.entity.store.dto.LifeDiscountCouponStoreFriendDto;
-import shop.alien.entity.store.dto.LifeGroupBuyDto;
 import shop.alien.entity.store.vo.LifeDiscountCouponFriendRuleDetailVo;
 import shop.alien.entity.store.vo.LifeDiscountCouponFriendRuleVo;
 import shop.alien.entity.store.vo.LifeDiscountCouponStoreFriendVo;
-import shop.alien.entity.store.vo.LifeGroupBuyCountDateVo;
 import shop.alien.store.annotation.TrackEvent;
 import shop.alien.store.service.LifeDiscountCouponStoreFriendService;
 import shop.alien.util.common.TokenInfo;
@@ -186,20 +183,20 @@ public class LifeDiscountCouponStoreFriendController {
         return R.data(lifeDiscountCouponStoreFriendService.getRuleList(storeId, acName, status));
     }
 
-    @ApiOperation("查询赠券记录。type=4 仅代金券,不传返回全部(优惠券+代金券)")
+    @ApiOperation("查询赠券记录(商户送给商户券)。queryType=1查询我收到的,queryType=2查询我送出的。type=4仅代金券,不传返回全部(优惠券+代金券)")
     @ApiImplicitParams({
-            @ApiImplicitParam(name = "storeUserId", value = "好友赠我-当前登录店铺id", dataType = "String", paramType = "query", required = false),
-            @ApiImplicitParam(name = "friendStoreUserId", value = "我赠好友-选中好友店铺用户id", dataType = "String", paramType = "query", required = false),
-            @ApiImplicitParam(name = "storeName", value = "店铺名称模糊", dataType = "String", paramType = "query", required = false),
-            @ApiImplicitParam(name = "type", value = "4=仅代金券,不传=全部", dataType = "Integer", paramType = "query", required = false)
+            @ApiImplicitParam(name = "storeUserId", value = "当前登录店铺用户id(必填)", dataType = "String", paramType = "query", required = true),
+            @ApiImplicitParam(name = "queryType", value = "查询类型:1=我收到的(所有好友赠送给我的),2=我送出的(我送给所有好友的)(必填)", dataType = "Integer", paramType = "query", required = true),
+            @ApiImplicitParam(name = "storeName", value = "店铺名称模糊查询(可选)", dataType = "String", paramType = "query", required = false),
+            @ApiImplicitParam(name = "type", value = "4=仅代金券,不传=全部(优惠券+代金券)(可选)", dataType = "Integer", paramType = "query", required = false)
     })
     @GetMapping("/getReceivedSendFriendCouponList")
-    public R<List<LifeDiscountCouponFriendRuleVo>> getReceivedSendFriendCouponList(@RequestParam(value = "storeUserId", required = false) String storeUserId,
-                                                                                   @RequestParam(value = "friendStoreUserId", required = false) String friendStoreUserId,
+    public R<List<LifeDiscountCouponFriendRuleVo>> getReceivedSendFriendCouponList(@RequestParam(value = "storeUserId") String storeUserId,
+                                                                                   @RequestParam(value = "queryType") Integer queryType,
                                                                                    @RequestParam(value = "storeName", required = false) String storeName,
                                                                                    @RequestParam(value = "type", required = false) Integer type) {
-        log.info("LifeDiscountCouponStoreFriendController.getReceivedSendFriendCouponList?storeUserId={},friendStoreUserId={},storeName={},type={}", storeUserId, friendStoreUserId, storeName, type);
-        return R.data(lifeDiscountCouponStoreFriendService.getReceivedSendFriendCouponList(storeUserId, friendStoreUserId, storeName, type));
+        log.info("LifeDiscountCouponStoreFriendController.getReceivedSendFriendCouponList?storeUserId={},queryType={},storeName={},type={}", storeUserId, queryType, storeName, type);
+        return R.data(lifeDiscountCouponStoreFriendService.getReceivedSendFriendCouponList(storeUserId, queryType, storeName, type));
 
 
 

+ 9 - 2
alien-store/src/main/java/shop/alien/store/service/LifeDiscountCouponStoreFriendService.java

@@ -62,8 +62,15 @@ public interface LifeDiscountCouponStoreFriendService extends IService<LifeDisco
 
     LifeDiscountCouponFriendRuleVo getRuleById(String id);
 
-    /** type=4 仅代金券,不传则返回全部(优惠券+代金券) */
-    List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponList(String storeUserId, String friendStoreUserId, String storeName, Integer type);
+    /**
+     * 查询赠券记录(商户送给商户券)
+     * @param storeUserId 当前登录店铺用户id(必填)
+     * @param queryType 查询类型:1=我收到的(所有好友赠送给我的),2=我送出的(我送给所有好友的)(必填)
+     * @param storeName 店铺名称模糊查询(可选)
+     * @param type 4=仅代金券,不传=全部(优惠券+代金券)(可选)
+     * @return 赠券列表
+     */
+    List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponList(String storeUserId, Integer queryType, String storeName, Integer type);
 
     /**
      * 好评送券:用户好评且AI审核通过后,按运营活动配置的优惠券/代金券ID发放到用户券包,发放成功后扣减库存;按券类型发送对应通知。

+ 144 - 56
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponStoreFriendServiceImpl.java

@@ -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;
     }