Prechádzať zdrojové kódy

优惠券查询优化

lutong 1 mesiac pred
rodič
commit
9796376809

+ 67 - 12
alien-store/src/main/java/shop/alien/store/controller/LifeDiscountCouponController.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import io.swagger.annotations.*;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.*;
 import shop.alien.entity.result.R;
 import shop.alien.entity.store.LifeDiscountCoupon;
@@ -185,8 +186,8 @@ public class LifeDiscountCouponController {
     @ApiOperation("获取该用户所有的优惠券列表。couponType=1仅满减券,couponType=2仅折扣券,不传返回全部优惠券")
     @ApiOperationSupport(order = 9)
     @GetMapping("/getUserCouponList")
-    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
-            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "String", paramType = "query", required = true),
+    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query", required = false),
+            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query", required = false),
             @ApiImplicitParam(name = "tabType", value = "分页类型(0:全部(未使用),1:即将过期,2:已使用,3:已过期)", dataType = "String", paramType = "query", required = true),
             @ApiImplicitParam(name = "type", value = "券类型(不传:优惠券+代金券都返回,1:仅优惠券查 life_discount_coupon,4:仅代金券查 life_coupon)", dataType = "Integer", paramType = "query", required = false),
             @ApiImplicitParam(name = "couponType", value = "优惠券类型:1=仅满减券,2=仅折扣券,不传=全部优惠券(可选,仅当type不为4时有效)", dataType = "Integer", paramType = "query", required = false)
@@ -197,12 +198,37 @@ public class LifeDiscountCouponController {
                                                            @RequestParam(defaultValue = "10") int size,
                                                            @RequestParam(required = false) Integer type,
                                                            @RequestParam(value = "couponType", required = false) Integer couponType) {
-        log.info("LifeDiscountCouponController.getUserCouponList?couponType={}", couponType);
         try {
-            List<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getUserCouponList(userLoginInfo, page, size, tabType, type, couponType);
+            // 参数校验
+            if (StringUtils.isEmpty(tabType)) {
+                return R.fail("分页类型不能为空");
+            }
+            if (page < 1) {
+                page = 1;
+            }
+            if (size < 1 || size > 100) {
+                size = 10;
+            }
+            if (type != null && type != 1 && type != 4) {
+                return R.fail("券类型参数错误,必须为1(优惠券)或4(代金券)");
+            }
+            if (couponType != null && couponType != 1 && couponType != 2) {
+                return R.fail("优惠券类型参数错误,必须为1(满减券)或2(折扣券)");
+            }
+            
+            log.info("LifeDiscountCouponController.getUserCouponList?userId={}, tabType={}, page={}, size={}, type={}, couponType={}", 
+                    userLoginInfo.getUserId(), tabType, page, size, type, couponType);
+            
+            List<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getUserCouponList(
+                    userLoginInfo, page, size, tabType, type, couponType);
             return R.data(storeCouponList);
+        } catch (IllegalArgumentException e) {
+            log.error("LifeDiscountCouponController.getUserCouponList 参数错误, userId={}, error={}", 
+                    userLoginInfo != null ? userLoginInfo.getUserId() : "unknown", e.getMessage(), e);
+            return R.fail(e.getMessage());
         } catch (Exception e) {
-            log.error("LifeDiscountCouponController.getUserCouponList ERROR Msg={}", e.getMessage());
+            log.error("LifeDiscountCouponController.getUserCouponList ERROR, userId={}, error={}", 
+                    userLoginInfo != null ? userLoginInfo.getUserId() : "unknown", e.getMessage(), e);
             return R.fail("查询失败");
         }
     }
@@ -210,13 +236,13 @@ public class LifeDiscountCouponController {
     @ApiOperation("获取该店铺所有优惠券(分页), 好友优惠券。couponType=1仅满减券,couponType=2仅折扣券,不传返回全部优惠券")
     @ApiOperationSupport(order = 10)
     @GetMapping("/getStoreAllCouponList")
-    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
-            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "String", paramType = "query", required = true),
+    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query", required = false),
+            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query", required = false),
             @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
             @ApiImplicitParam(name = "couponName", value = "优惠券名称", dataType = "String", paramType = "query", required = false),
             @ApiImplicitParam(name = "tab", value = "分页类型(0:全部(传其他也查全部),1:进行中,2:已结束)", dataType = "String", paramType = "query", required = true),
-            @ApiImplicitParam(name = "couponsFromType", required = false, value = "查询类型(1:我的优惠券,2:好友的优惠券)"),
-            @ApiImplicitParam(name = "couponStatus", required = false, value = "优惠券状态(0:草稿,1:正式)"),
+            @ApiImplicitParam(name = "couponsFromType", required = false, value = "查询类型(1:我的优惠券,2:好友的优惠券)", dataType = "Integer", paramType = "query"),
+            @ApiImplicitParam(name = "couponStatus", required = false, value = "优惠券状态(0:草稿,1:正式)", dataType = "Integer", paramType = "query"),
             @ApiImplicitParam(name = "couponType", value = "优惠券类型:1=仅满减券,2=仅折扣券,不传=全部优惠券(可选)", dataType = "Integer", paramType = "query", required = false),
     })
     public R<IPage<LifeDiscountCouponVo>> getStoreAllCouponList(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
@@ -229,12 +255,41 @@ public class LifeDiscountCouponController {
                                                                 @RequestParam(value = "couponStatus", defaultValue = "1", required = false) int couponStatus,
                                                                 @RequestParam(value = "couponType", required = false) Integer couponType
     ) {
-        log.info("LifeDiscountCouponController.getStoreAllCouponList?storeId={}, couponName={}, tab={}, page={}, size={}, couponStatus={}, couponType={}", storeId, couponName, tab, page, size, couponStatus, couponType);
         try {
-            IPage<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getStoreAllCouponList(storeId, userLoginInfo, page, size, couponName, tab, couponsFromType, couponStatus, couponType);
+            // 参数校验
+            if (StringUtils.isEmpty(storeId)) {
+                return R.fail("商户id不能为空");
+            }
+            if (StringUtils.isEmpty(tab)) {
+                return R.fail("分页类型不能为空");
+            }
+            if (page < 1) {
+                page = 1;
+            }
+            if (size < 1 || size > 100) {
+                size = 10;
+            }
+            if (couponsFromType != 1 && couponsFromType != 2) {
+                couponsFromType = 1;
+            }
+            if (couponStatus != 0 && couponStatus != 1) {
+                couponStatus = 1;
+            }
+            if (couponType != null && couponType != 1 && couponType != 2) {
+                return R.fail("优惠券类型参数错误,必须为1(满减券)或2(折扣券)");
+            }
+            
+            log.info("LifeDiscountCouponController.getStoreAllCouponList?storeId={}, couponName={}, tab={}, page={}, size={}, couponsFromType={}, couponStatus={}, couponType={}", 
+                    storeId, couponName, tab, page, size, couponsFromType, couponStatus, couponType);
+            
+            IPage<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getStoreAllCouponList(
+                    storeId, userLoginInfo, page, size, couponName, tab, couponsFromType, couponStatus, couponType);
             return R.data(storeCouponList);
+        } catch (IllegalArgumentException e) {
+            log.error("LifeDiscountCouponController.getStoreAllCouponList 参数错误, storeId={}, error={}", storeId, e.getMessage(), e);
+            return R.fail(e.getMessage());
         } catch (Exception e) {
-            log.error("LifeDiscountCouponController.getStoreCouponList ERROR Msg={}", e.getMessage());
+            log.error("LifeDiscountCouponController.getStoreAllCouponList ERROR, storeId={}, error={}", storeId, e.getMessage(), e);
             return R.fail("查询失败");
         }
     }

+ 111 - 26
alien-store/src/main/java/shop/alien/store/controller/StoreCustomerServiceController.java

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import io.swagger.annotations.*;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.*;
 import shop.alien.entity.result.R;
 import shop.alien.entity.store.AiIntelligentAssistant;
@@ -36,11 +38,19 @@ public class StoreCustomerServiceController {
     @ApiImplicitParams({@ApiImplicitParam(name = "question", value = "问题关键字", dataType = "String", paramType = "query", required = true)})
     @GetMapping("/getByQuestion")
     public R<StoreCustomerService> getByQuestion(@RequestParam String question) {
-        StoreCustomerService result = storeCustomerServiceService.getByQuestion(question);
-        if (null == result) {
-            return R.fail("暂未搜索到相应答案");
+        try {
+            if (!StringUtils.hasText(question)) {
+                return R.fail("问题关键字不能为空");
+            }
+            StoreCustomerService result = storeCustomerServiceService.getByQuestion(question);
+            if (result == null) {
+                return R.fail("暂未搜索到相应答案");
+            }
+            return R.data(result);
+        } catch (Exception e) {
+            log.error("StoreCustomerServiceController.getByQuestion ERROR, question={}, error={}", question, e.getMessage(), e);
+            return R.fail("查询失败");
         }
-        return R.data(result);
     }
 
     @ApiOperation("获取随机问题")
@@ -49,8 +59,19 @@ public class StoreCustomerServiceController {
     , @ApiImplicitParam(name = "limit", value = "数量", dataType = "Integer", paramType = "query", required = true)})
     @GetMapping("/getRandList")
     public R<List<StoreCustomerService>> getRandList(@RequestParam String type, @RequestParam Integer limit) {
-        List<StoreCustomerService> result = storeCustomerServiceService.getRandList(type,limit);
-        return R.data(result);
+        try {
+            if (!StringUtils.hasText(type)) {
+                return R.fail("类型不能为空");
+            }
+            if (limit == null || limit <= 0) {
+                return R.fail("数量必须大于0");
+            }
+            List<StoreCustomerService> result = storeCustomerServiceService.getRandList(type, limit);
+            return R.data(result);
+        } catch (Exception e) {
+            log.error("StoreCustomerServiceController.getRandList ERROR, type={}, limit={}, error={}", type, limit, e.getMessage(), e);
+            return R.fail("查询失败");
+        }
     }
 
     @ApiOperation("删除问题")
@@ -58,9 +79,17 @@ public class StoreCustomerServiceController {
     @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
             })
     @GetMapping("/delStoreCustomerService")
-    public R delStoreCustomerService(@RequestParam String id) {
-        storeCustomerServiceService.delStoreCustomerService(id);
-        return R.data("删除成功");
+    public R<String> delStoreCustomerService(@RequestParam String id) {
+        try {
+            if (!StringUtils.hasText(id)) {
+                return R.fail("id不能为空");
+            }
+            storeCustomerServiceService.delStoreCustomerService(id);
+            return R.data("删除成功");
+        } catch (Exception e) {
+            log.error("StoreCustomerServiceController.delStoreCustomerService ERROR, id={}, error={}", id, e.getMessage(), e);
+            return R.fail("删除失败");
+        }
     }
 
     @ApiOperation("中台-问题列表")
@@ -71,21 +100,40 @@ public class StoreCustomerServiceController {
             @ApiImplicitParam(name = "type", value = "类型(1/商家/2用户)", dataType = "String", paramType = "query", required = false),
     })
     @GetMapping("/getStoreCustomerServicePage")
-    private R<IPage<StoreCustomerService>> getStoreCustomerServicePage(@RequestParam(value = "page", defaultValue = "1") int page,
+    public R<IPage<StoreCustomerService>> getStoreCustomerServicePage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                        @RequestParam(value = "size", defaultValue = "10") int size,
                                                        @RequestParam(value = "question", required = false) String question,
                                                        @RequestParam(value = "type", required = false) String type) {
-        log.info("StoreCustomerServiceController.getStoreCustomerServicePage?page={},size={},question={},type={}", page, size, question, type);
-        return R.data(storeCustomerServiceService.getStoreCustomerServicePage(page, size, question, type));
+        try {
+            if (page < 1) {
+                page = 1;
+            }
+            if (size < 1 || size > 100) {
+                size = 10;
+            }
+            log.info("StoreCustomerServiceController.getStoreCustomerServicePage?page={},size={},question={},type={}", page, size, question, type);
+            return R.data(storeCustomerServiceService.getStoreCustomerServicePage(page, size, question, type));
+        } catch (Exception e) {
+            log.error("StoreCustomerServiceController.getStoreCustomerServicePage ERROR, page={}, size={}, error={}", page, size, e.getMessage(), e);
+            return R.fail("查询失败");
+        }
     }
 
     @ApiOperation("中台-保存问题")
     @ApiOperationSupport(order = 5)
     @PostMapping("/saveStoreCustomerService")
     public R<StoreCustomerService> saveStoreCustomerService(@RequestBody StoreCustomerService storeCustomerService) {
-        log.info("StoreCustomerServiceController.saveStoreCustomerService?storeCustomerService={}", storeCustomerService.toString());
-        StoreCustomerService saved = storeCustomerServiceService.saveStoreCustomerService(storeCustomerService);
-        return R.data(saved);
+        try {
+            if (storeCustomerService == null) {
+                return R.fail("参数不能为空");
+            }
+            log.info("StoreCustomerServiceController.saveStoreCustomerService?storeCustomerService={}", storeCustomerService);
+            StoreCustomerService saved = storeCustomerServiceService.saveStoreCustomerService(storeCustomerService);
+            return R.data(saved);
+        } catch (Exception e) {
+            log.error("StoreCustomerServiceController.saveStoreCustomerService ERROR, storeCustomerService={}, error={}", storeCustomerService, e.getMessage(), e);
+            return R.fail("保存失败");
+        }
     }
 
     @ApiOperation("id查询问题")
@@ -94,28 +142,65 @@ public class StoreCustomerServiceController {
     })
     @GetMapping("/getById")
     public R<StoreCustomerService> getById(@RequestParam String id) {
-        log.info("StoreCustomerServiceController.getById?id={}", id);
-        StoreCustomerService saved = storeCustomerServiceService.getById(id);
-        return R.data(saved);
+        try {
+            if (!StringUtils.hasText(id)) {
+                return R.fail("id不能为空");
+            }
+            log.info("StoreCustomerServiceController.getById?id={}", id);
+            StoreCustomerService saved = storeCustomerServiceService.getById(id);
+            if (saved == null) {
+                return R.fail("未找到对应的问题");
+            }
+            return R.data(saved);
+        } catch (Exception e) {
+            log.error("StoreCustomerServiceController.getById ERROR, id={}, error={}", id, e.getMessage(), e);
+            return R.fail("查询失败");
+        }
     }
 
     @ApiOperation("保存聊天")
-    @ApiOperationSupport(order = 5)
+    @ApiOperationSupport(order = 7)
     @PostMapping("/saveAiIntelligentAssistant")
     public R<List<AiIntelligentAssistant>> saveAiIntelligentAssistant(@RequestBody List<AiIntelligentAssistant> aiIntelligentAssistants) {
-        log.info("StoreCustomerServiceController.saveAiIntelligentAssistant?aiIntelligentAssistants={}", aiIntelligentAssistants.toString());
-        List<AiIntelligentAssistant> saved = storeCustomerServiceService.saveAiIntelligentAssistant(aiIntelligentAssistants);
-        return R.data(saved);
+        try {
+            if (CollectionUtils.isEmpty(aiIntelligentAssistants)) {
+                return R.fail("聊天记录不能为空");
+            }
+            log.info("StoreCustomerServiceController.saveAiIntelligentAssistant?size={}", aiIntelligentAssistants.size());
+            List<AiIntelligentAssistant> saved = storeCustomerServiceService.saveAiIntelligentAssistant(aiIntelligentAssistants);
+            return R.data(saved);
+        } catch (Exception e) {
+            log.error("StoreCustomerServiceController.saveAiIntelligentAssistant ERROR, size={}, error={}", 
+                    aiIntelligentAssistants != null ? aiIntelligentAssistants.size() : 0, e.getMessage(), e);
+            return R.fail("保存失败");
+        }
     }
 
     @ApiOperation("查询聊天记录")
-    @ApiOperationSupport(order = 2)
+    @ApiOperationSupport(order = 8)
     @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id)", dataType = "String", paramType = "query", required = true)
             , @ApiImplicitParam(name = "time", value = "时间", dataType = "String", paramType = "query", required = true)
             , @ApiImplicitParam(name = "talkSource", value = "1平台使用/2商户运营", dataType = "Integer", paramType = "query", required = true)})
     @GetMapping("/selectAiIntelligentAssistant")
-    public R<List<AiIntelligentAssistant>> selectAiIntelligentAssistant(@RequestParam String userId, @RequestParam String time,@RequestParam Integer talkSource) {
-        List<AiIntelligentAssistant> result = storeCustomerServiceService.selectAiIntelligentAssistant(userId,time,talkSource);
-        return R.data(result);
+    public R<List<AiIntelligentAssistant>> selectAiIntelligentAssistant(@RequestParam String userId, 
+                                                                         @RequestParam String time,
+                                                                         @RequestParam Integer talkSource) {
+        try {
+            if (!StringUtils.hasText(userId)) {
+                return R.fail("用户id不能为空");
+            }
+            if (!StringUtils.hasText(time)) {
+                return R.fail("时间参数不能为空");
+            }
+            if (talkSource == null || (talkSource != 1 && talkSource != 2)) {
+                return R.fail("talkSource参数错误,必须为1或2");
+            }
+            List<AiIntelligentAssistant> result = storeCustomerServiceService.selectAiIntelligentAssistant(userId, time, talkSource);
+            return R.data(result);
+        } catch (Exception e) {
+            log.error("StoreCustomerServiceController.selectAiIntelligentAssistant ERROR, userId={}, time={}, talkSource={}, error={}", 
+                    userId, time, talkSource, e.getMessage(), e);
+            return R.fail("查询失败");
+        }
     }
 }

+ 353 - 223
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponServiceImpl.java

@@ -659,229 +659,305 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
         return result;
     }
 
+    /**
+     * 获取用户优惠券列表
+     * 
+     * @param userLoginInfo 用户登录信息
+     * @param page 页码
+     * @param size 每页数量
+     * @param tabType 分页类型:0-全部(未使用),1-即将过期,2-已使用,3-已过期
+     * @param type 券类型:1-仅优惠券,4-仅代金券,null-全部
+     * @param couponType 优惠券类型:1-满减券,2-折扣券,null-全部(仅当type不为4时有效)
+     * @return 优惠券列表
+     */
     @Override
     public List<LifeDiscountCouponVo> getUserCouponList(UserLoginInfo userLoginInfo, int page, int size, String tabType, Integer type, Integer couponType) {
+        // 参数校验
+        if (userLoginInfo == null || StringUtils.isEmpty(userLoginInfo.getUserId())) {
+            throw new IllegalArgumentException("用户信息不能为空");
+        }
+        if (StringUtils.isEmpty(tabType)) {
+            throw new IllegalArgumentException("分页类型不能为空");
+        }
+        
         List<LifeDiscountCouponVo> lifeDiscountCouponVos = new ArrayList<>();
         IPage<LifeDiscountCouponUser> iPage = new Page<>(page, size);
         LambdaQueryWrapper<LifeDiscountCouponUser> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(LifeDiscountCouponUser::getUserId, userLoginInfo.getUserId());
-        //比较工具对象
-        //获取七日前时间
+        
+        // 计算时间边界(expirationTime是LocalDate类型,需要转换为LocalDate)
         Date now = new Date();
-        // 获取 7 天后的时间
-        Calendar calendar = Calendar.getInstance();
-        calendar.setTime(now);
-        calendar.add(Calendar.DAY_OF_YEAR, 7);
-        // 获取 Calendar 实例,默认表示当前日期和时间
-        // 将小时设置为 0
-        calendar.set(Calendar.HOUR_OF_DAY, 0);
-        // 将分钟设置为 0
-        calendar.set(Calendar.MINUTE, 0);
-        // 将秒设置为 0
-        calendar.set(Calendar.SECOND, 0);
-        // 将毫秒设置为 0
-        calendar.set(Calendar.MILLISECOND, 0);
-        Date sevenDaysLater = calendar.getTime();
-
-        // 获取当前时间
-        Calendar nowCalendar = Calendar.getInstance();
-        nowCalendar.setTime(now);
-        // 获取 Calendar 实例,默认表示当前日期和时间
-        // 将小时设置为 0
-        nowCalendar.set(Calendar.HOUR_OF_DAY, 0);
-        // 将分钟设置为 0
-        nowCalendar.set(Calendar.MINUTE, 0);
-        // 将秒设置为 0
-        nowCalendar.set(Calendar.SECOND, 0);
-        // 将毫秒设置为 0
-        nowCalendar.set(Calendar.MILLISECOND, 0);
-        // 获取设置后的 Date 对象
-        Date midnight = nowCalendar.getTime();
-        //所有状态未使用且未过期的
-        // 获取当前时间
-        LocalDateTime localNow = LocalDateTime.now();
-        // 计算七日前的日期
-        LocalDateTime sevenDaysAgo = localNow.minusDays(7);
-        // 将七日前的时间设置为凌晨 0 点 0 分 0 秒 0 纳秒
-        LocalDateTime sevenDaysAgoMidnight = sevenDaysAgo.withHour(0).withMinute(0).withSecond(0).withNano(0);
-        //判断分页类型
-        if (tabType.equals(DiscountCouponEnum.ALL.getValue())) {
-            //如果已过期 过期日期大于今天
-            queryWrapper.gt(LifeDiscountCouponUser::getExpirationTime, midnight);
-            //状态为待使用
-            queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
-        } else if (tabType.equals(DiscountCouponEnum.BE_ABOUT_TO_EXPORE.getValue())) {
-            //如果即将过期 七天之内
-            //小于等于七日后
-            queryWrapper.le(LifeDiscountCouponUser::getExpirationTime, sevenDaysLater);
-            //大于等于今天
-            queryWrapper.ge(LifeDiscountCouponUser::getExpirationTime, midnight);
-            //并且状态为待使用
-            queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
-        } else if (tabType.equals(DiscountCouponEnum.HAVE_ALREADY_APPLIED.getValue())) {
-            //如果已使用 状态为已使用
+        LocalDate today = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); // 今天
+        LocalDate sevenDaysLater = today.plusDays(7); // 7天后
+        LocalDate sevenDaysAgo = today.minusDays(7); // 7天前
+        
+        // 根据tabType添加查询条件
+        if (DiscountCouponEnum.ALL.getValue().equals(tabType)) {
+            // 全部(未使用):过期时间 >= 今天 或 过期时间为null 且 状态为待使用
+            // 注意:expirationTime为null的情况(可能是永久有效的券)也应该包含在"全部"中
+            // 使用 >= today 而不是 > today,确保包含今天过期的券(今天还没过完,券仍然有效)
+            queryWrapper.and(w -> w.ge(LifeDiscountCouponUser::getExpirationTime, today)
+                    .or().isNull(LifeDiscountCouponUser::getExpirationTime))
+                    .eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
+        } else if (DiscountCouponEnum.BE_ABOUT_TO_EXPORE.getValue().equals(tabType)) {
+            // 即将过期:今天 <= 过期时间 <= 7天后 且 状态为待使用
+            // 这是"全部(未使用)"的一个子集,只包含7天内即将过期的券
+            queryWrapper.ge(LifeDiscountCouponUser::getExpirationTime, today)
+                    .le(LifeDiscountCouponUser::getExpirationTime, sevenDaysLater)
+                    .eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
+        } else if (DiscountCouponEnum.HAVE_ALREADY_APPLIED.getValue().equals(tabType)) {
+            // 已使用:状态为已使用
             queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.HAVE_BEEN_USED.getValue());
-        } else if (tabType.equals(DiscountCouponEnum.HAVE_EXPIRED.getValue())) {
-            //如果已过期 过期日期小于今天,并且过期时间小于七天
-            queryWrapper.lt(LifeDiscountCouponUser::getExpirationTime, midnight);
-            queryWrapper.gt(LifeDiscountCouponUser::getExpirationTime, sevenDaysAgoMidnight);
-            //并且状态为待使用
-            queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
-        }
-        // type:1 仅优惠券(couponId 有值),4 仅代金券(voucherId 有值),不传则都查
+        } else if (DiscountCouponEnum.HAVE_EXPIRED.getValue().equals(tabType)) {
+            // 已过期:7天前 < 过期时间 < 今天 且 状态为待使用
+            queryWrapper.gt(LifeDiscountCouponUser::getExpirationTime, sevenDaysAgo)
+                    .lt(LifeDiscountCouponUser::getExpirationTime, today)
+                    .eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
+        }
+        // 根据type筛选券类型:1-仅优惠券(couponId有值),4-仅代金券(voucherId有值),null-全部
         if (type != null && type == 1) {
             queryWrapper.and(w -> w.isNotNull(LifeDiscountCouponUser::getCouponId));
         } else if (type != null && type == 4) {
             queryWrapper.and(w -> w.isNotNull(LifeDiscountCouponUser::getVoucherId));
         }
         queryWrapper.orderByDesc(LifeDiscountCouponUser::getCreatedTime);
+        
+        // 分页查询用户优惠券记录
         IPage<LifeDiscountCouponUser> lifeDiscountCouponUserIPage = lifeDiscountCouponUserMapper.selectPage(iPage, queryWrapper);
-        //根据券id去查询该券的基本信息
-        //根据优惠券列表查询该优惠券是否领取过
-        //
-        // 判断是否在领取时间内
+        List<LifeDiscountCouponUser> records = lifeDiscountCouponUserIPage.getRecords();
+        if (records.isEmpty()) {
+            return lifeDiscountCouponVos;
+        }
+        
+        // 准备时间转换
         Instant instant = now.toInstant();
         ZoneId zoneId = ZoneId.systemDefault();
-        LocalDate localNow1 = instant.atZone(zoneId).toLocalDate();
-
-        List<LifeDiscountCouponUser> records = lifeDiscountCouponUserIPage.getRecords();
-        // type=4:仅代金券,查 life_coupon 表
+        LocalDate localNow = instant.atZone(zoneId).toLocalDate();
+        
+        // type=4:仅代金券,查询 life_coupon 表
         if (type != null && type == 4) {
-            List<String> voucherIdList = records.stream().map(LifeDiscountCouponUser::getVoucherId).filter(Objects::nonNull).collect(Collectors.toList());
-            if (voucherIdList.isEmpty()) {
-                return lifeDiscountCouponVos;
-            }
-            List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectList(new LambdaQueryWrapper<LifeCoupon>().in(LifeCoupon::getId, voucherIdList));
-            List<String> storeIdList = lifeCoupons.stream().map(LifeCoupon::getStoreId).filter(s -> !StringUtils.isEmpty(s)).collect(Collectors.toList());
-            List<StoreInfo> storeInfoList = storeInfoMapper.getList(new LambdaQueryWrapper<StoreInfo>().in(!storeIdList.isEmpty(), StoreInfo::getId, storeIdList));
-            for (LifeDiscountCouponUser lifeDiscountCouponUser : records) {
-                LifeCoupon lc = lifeCoupons.stream().filter(c -> c.getId().equals(lifeDiscountCouponUser.getVoucherId())).findFirst().orElse(null);
-                if (lc == null) {
-                    continue;
-                }
-                LifeDiscountCouponVo vo = mapLifeCouponToVo(lc);
-                vo.setUserCouponId(lifeDiscountCouponUser.getId());
-                vo.setVoucherId(lc.getId());
-                vo.setQuantityClaimed(records.size());
-                vo.setExpirationTime(lifeDiscountCouponUser.getExpirationTime());
-                vo.setReachUseTimeFlag(1);
-                if (lc.getStartDate() != null) {
-                    LocalDate startLocal = lc.getStartDate().toInstant().atZone(zoneId).toLocalDate();
-                    if (localNow1.isBefore(startLocal)) {
-                        vo.setReachUseTimeFlag(0);
-                    }
-                }
-                if (lc.getEndDate() != null) {
-                    vo.setValidDate(lc.getEndDate().toInstant().atZone(zoneId).toLocalDate());
-                }
-                StoreInfo storeInfoOne = storeInfoList.stream().filter(s -> s.getId().toString().equals(lc.getStoreId())).findFirst().orElse(null);
-                if (storeInfoOne != null) {
-                    vo.setBusinessSection(storeInfoOne.getBusinessSection());
-                    vo.setBusinessSectionName(storeInfoOne.getBusinessSectionName());
-                }
-                lifeDiscountCouponVos.add(vo);
-            }
-            return lifeDiscountCouponVos;
+            return processVoucherCoupons(records, zoneId, localNow);
         }
 
-        // type=1 或 type=null:优惠券(或混合),查 life_discount_coupon
-        List<Integer> couponIdList = records.stream().map(LifeDiscountCouponUser::getCouponId).filter(Objects::nonNull).collect(Collectors.toList());
+        // type=1 或 type=null:优惠券(或混合),查询 life_discount_coupon
+        List<Integer> couponIdList = records.stream()
+                .map(LifeDiscountCouponUser::getCouponId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList());
+        
+        // 查询优惠券信息
         LambdaQueryWrapper<LifeDiscountCoupon> couponQueryWrapper = new LambdaQueryWrapper<LifeDiscountCoupon>()
                 .in(!couponIdList.isEmpty(), LifeDiscountCoupon::getId, couponIdList);
-        //如果指定了优惠券类型(满减券或折扣券),添加筛选条件
+        // 如果指定了优惠券类型(满减券或折扣券),添加筛选条件
         if (couponType != null) {
             couponQueryWrapper.eq(LifeDiscountCoupon::getCouponType, couponType);
         }
         List<LifeDiscountCoupon> lifeDiscountCoupons = lifeDiscountCouponMapper.selectList(couponQueryWrapper);
+        
+        // 如果type=1且没有查询到优惠券,直接返回
         if (type != null && type == 1 && lifeDiscountCoupons.isEmpty()) {
             return lifeDiscountCouponVos;
         }
-        List<String> storeIdList = lifeDiscountCoupons.isEmpty() ? new ArrayList<>() : lifeDiscountCoupons.stream().map(LifeDiscountCoupon::getStoreId).collect(Collectors.toList());
-        List<StoreInfo> storeInfoList = storeInfoMapper.getList(new LambdaQueryWrapper<StoreInfo>().in(!storeIdList.isEmpty(), StoreInfo::getId, storeIdList));
-        if (type != null && type == 1 && storeInfoList.isEmpty() && records.isEmpty()) {
-            return lifeDiscountCouponVos;
-        }
+        
+        // 查询店铺信息
+        List<String> storeIdList = lifeDiscountCoupons.stream()
+                .map(LifeDiscountCoupon::getStoreId)
+                .filter(s -> !StringUtils.isEmpty(s))
+                .collect(Collectors.toList());
+        List<StoreInfo> storeInfoList = storeInfoMapper.getList(
+                new LambdaQueryWrapper<StoreInfo>().in(!storeIdList.isEmpty(), StoreInfo::getId, storeIdList));
+        
+        // type=null时,需要同时处理代金券
         List<LifeCoupon> lifeCouponsForMerge = new ArrayList<>();
+        List<StoreInfo> voucherStoreInfoList = new ArrayList<>();
         if (type == null) {
-            List<String> voucherIdList = records.stream().map(LifeDiscountCouponUser::getVoucherId).filter(Objects::nonNull).collect(Collectors.toList());
+            List<String> voucherIdList = records.stream()
+                    .map(LifeDiscountCouponUser::getVoucherId)
+                    .filter(Objects::nonNull)
+                    .collect(Collectors.toList());
             if (!voucherIdList.isEmpty()) {
-                lifeCouponsForMerge = lifeCouponMapper.selectList(new LambdaQueryWrapper<LifeCoupon>().in(LifeCoupon::getId, voucherIdList));
-            }
-            if (!lifeCouponsForMerge.isEmpty()) {
-                List<String> voucherStoreIds = lifeCouponsForMerge.stream().map(LifeCoupon::getStoreId).filter(s -> !StringUtils.isEmpty(s)).collect(Collectors.toList());
+                lifeCouponsForMerge = lifeCouponMapper.selectList(
+                        new LambdaQueryWrapper<LifeCoupon>().in(LifeCoupon::getId, voucherIdList));
+                List<String> voucherStoreIds = lifeCouponsForMerge.stream()
+                        .map(LifeCoupon::getStoreId)
+                        .filter(s -> !StringUtils.isEmpty(s))
+                        .collect(Collectors.toList());
                 if (!voucherStoreIds.isEmpty()) {
-                    List<StoreInfo> voucherStores = storeInfoMapper.getList(new LambdaQueryWrapper<StoreInfo>().in(StoreInfo::getId, voucherStoreIds));
-                    storeInfoList = new ArrayList<>(storeInfoList);
-                    storeInfoList.addAll(voucherStores);
+                    voucherStoreInfoList = storeInfoMapper.getList(
+                            new LambdaQueryWrapper<StoreInfo>().in(StoreInfo::getId, voucherStoreIds));
                 }
             }
         }
+        
+        // 合并店铺信息列表
+        List<StoreInfo> allStoreInfoList = new ArrayList<>(storeInfoList);
+        allStoreInfoList.addAll(voucherStoreInfoList);
+        
+        // 构建Map提高查询效率
+        Map<Integer, LifeDiscountCoupon> couponMap = lifeDiscountCoupons.stream()
+                .collect(Collectors.toMap(LifeDiscountCoupon::getId, c -> c, (k1, k2) -> k1));
+        Map<String, LifeCoupon> voucherMap = lifeCouponsForMerge.stream()
+                .collect(Collectors.toMap(LifeCoupon::getId, v -> v, (k1, k2) -> k1));
+        Map<String, StoreInfo> storeInfoMap = allStoreInfoList.stream()
+                .collect(Collectors.toMap(s -> s.getId().toString(), s -> s, (k1, k2) -> k1));
+        
+        // 处理每条用户优惠券记录
         for (LifeDiscountCouponUser lifeDiscountCouponUser : records) {
-            if (lifeDiscountCouponUser.getVoucherId() != null && !lifeCouponsForMerge.isEmpty()) {
-                LifeCoupon lc = lifeCouponsForMerge.stream().filter(c -> c.getId().equals(lifeDiscountCouponUser.getVoucherId())).findFirst().orElse(null);
+            // 处理代金券(type=null时)
+            if (lifeDiscountCouponUser.getVoucherId() != null && !voucherMap.isEmpty()) {
+                LifeCoupon lc = voucherMap.get(lifeDiscountCouponUser.getVoucherId());
                 if (lc != null) {
-                    LifeDiscountCouponVo vo = mapLifeCouponToVo(lc);
-                    vo.setUserCouponId(lifeDiscountCouponUser.getId());
-                    vo.setVoucherId(lc.getId());
-                    vo.setQuantityClaimed(records.size());
-                    vo.setExpirationTime(lifeDiscountCouponUser.getExpirationTime());
-                    vo.setReachUseTimeFlag(1);
-                    if (lc.getStartDate() != null) {
-                        LocalDate startLocal = lc.getStartDate().toInstant().atZone(zoneId).toLocalDate();
-                        if (localNow1.isBefore(startLocal)) {
-                            vo.setReachUseTimeFlag(0);
-                        }
-                    }
-                    if (lc.getEndDate() != null) {
-                        vo.setValidDate(lc.getEndDate().toInstant().atZone(zoneId).toLocalDate());
-                    }
-                    StoreInfo storeInfoOne = storeInfoList.stream().filter(s -> s.getId().toString().equals(lc.getStoreId())).findFirst().orElse(null);
-                    if (storeInfoOne != null) {
-                        vo.setBusinessSection(storeInfoOne.getBusinessSection());
-                        vo.setBusinessSectionName(storeInfoOne.getBusinessSectionName());
-                    }
+                    LifeDiscountCouponVo vo = buildVoucherVo(lc, lifeDiscountCouponUser, zoneId, localNow, storeInfoMap);
                     lifeDiscountCouponVos.add(vo);
                 }
                 continue;
             }
-            LifeDiscountCoupon lifeDiscountCouponOne = lifeDiscountCoupons.stream().filter(lifeDiscountCoupon -> lifeDiscountCoupon.getId().equals(lifeDiscountCouponUser.getCouponId())).findFirst().orElse(null);
-            if (lifeDiscountCouponOne == null) {
+            
+            // 处理优惠券
+            if (lifeDiscountCouponUser.getCouponId() == null) {
                 continue;
             }
-            StoreInfo storeInfoOne = null;
-            if (!storeInfoList.isEmpty() && !StringUtils.isEmpty(lifeDiscountCouponOne.getStoreId())) {
-                storeInfoOne = storeInfoList.stream().filter(storeInfo -> storeInfo.getId().toString().equals(lifeDiscountCouponOne.getStoreId())).findFirst().orElse(null);
+            LifeDiscountCoupon coupon = couponMap.get(lifeDiscountCouponUser.getCouponId());
+            if (coupon == null) {
+                continue;
             }
-            LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
-            if (null != storeInfoOne) {
-                lifeDiscountCouponVo.setBusinessSection(storeInfoOne.getBusinessSection());
-                lifeDiscountCouponVo.setBusinessSectionName(storeInfoOne.getBusinessSectionName());
-            }
-            lifeDiscountCouponVo.setCouponId(lifeDiscountCouponOne.getId());
-            lifeDiscountCouponVo.setVoucherId(lifeDiscountCouponUser.getVoucherId());
-            BeanUtils.copyProperties(lifeDiscountCouponOne, lifeDiscountCouponVo);
-            lifeDiscountCouponVo.setQuantityClaimed(lifeDiscountCouponUserIPage.getRecords().size());
-            lifeDiscountCouponVo.setExpirationTime(lifeDiscountCouponUser.getExpirationTime());
-            // 设置有效期
-            String specifiedDay = lifeDiscountCouponOne.getSpecifiedDay();
-            if (!StringUtils.isEmpty(specifiedDay)) {
+            
+            LifeDiscountCouponVo vo = buildCouponVo(coupon, lifeDiscountCouponUser, zoneId, localNow, storeInfoMap);
+            lifeDiscountCouponVos.add(vo);
+        }
+        
+        return lifeDiscountCouponVos;
+    }
+    
+    /**
+     * 处理代金券列表(type=4时使用)
+     */
+    private List<LifeDiscountCouponVo> processVoucherCoupons(List<LifeDiscountCouponUser> records, 
+                                                               ZoneId zoneId, LocalDate localNow) {
+        List<LifeDiscountCouponVo> result = new ArrayList<>();
+        List<String> voucherIdList = records.stream()
+                .map(LifeDiscountCouponUser::getVoucherId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList());
+        if (voucherIdList.isEmpty()) {
+            return result;
+        }
+        
+        List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectList(
+                new LambdaQueryWrapper<LifeCoupon>().in(LifeCoupon::getId, voucherIdList));
+        List<String> storeIdList = lifeCoupons.stream()
+                .map(LifeCoupon::getStoreId)
+                .filter(s -> !StringUtils.isEmpty(s))
+                .collect(Collectors.toList());
+        List<StoreInfo> storeInfoList = storeInfoMapper.getList(
+                new LambdaQueryWrapper<StoreInfo>().in(!storeIdList.isEmpty(), StoreInfo::getId, storeIdList));
+        
+        Map<String, LifeCoupon> voucherMap = lifeCoupons.stream()
+                .collect(Collectors.toMap(LifeCoupon::getId, v -> v, (k1, k2) -> k1));
+        Map<String, StoreInfo> storeInfoMap = storeInfoList.stream()
+                .collect(Collectors.toMap(s -> s.getId().toString(), s -> s, (k1, k2) -> k1));
+        
+        for (LifeDiscountCouponUser userCoupon : records) {
+            LifeCoupon lc = voucherMap.get(userCoupon.getVoucherId());
+            if (lc != null) {
+                LifeDiscountCouponVo vo = buildVoucherVo(lc, userCoupon, zoneId, localNow, storeInfoMap);
+                result.add(vo);
+            }
+        }
+        return result;
+    }
+    
+    /**
+     * 构建代金券VO
+     */
+    private LifeDiscountCouponVo buildVoucherVo(LifeCoupon lc, LifeDiscountCouponUser userCoupon,
+                                                ZoneId zoneId, LocalDate localNow, Map<String, StoreInfo> storeInfoMap) {
+        LifeDiscountCouponVo vo = mapLifeCouponToVo(lc);
+        vo.setUserCouponId(userCoupon.getId());
+        vo.setVoucherId(lc.getId());
+        // quantityClaimed表示该券的总领取数量,在用户券列表中不设置或设置为0
+        vo.setQuantityClaimed(null);
+        vo.setExpirationTime(userCoupon.getExpirationTime());
+        vo.setReachUseTimeFlag(1);
+        
+        // 判断是否到了使用开始时间
+        if (lc.getStartDate() != null) {
+            LocalDate startLocal = lc.getStartDate().toInstant().atZone(zoneId).toLocalDate();
+            if (localNow.isBefore(startLocal)) {
+                vo.setReachUseTimeFlag(0);
+            }
+        }
+        
+        // 设置有效期
+        if (lc.getEndDate() != null) {
+            vo.setValidDate(lc.getEndDate().toInstant().atZone(zoneId).toLocalDate());
+        }
+        
+        // 设置店铺信息
+        StoreInfo storeInfo = storeInfoMap.get(lc.getStoreId());
+        if (storeInfo != null) {
+            vo.setBusinessSection(storeInfo.getBusinessSection());
+            vo.setBusinessSectionName(storeInfo.getBusinessSectionName());
+        }
+        
+        return vo;
+    }
+    
+    /**
+     * 构建优惠券VO
+     */
+    private LifeDiscountCouponVo buildCouponVo(LifeDiscountCoupon coupon, LifeDiscountCouponUser userCoupon,
+                                               ZoneId zoneId, LocalDate localNow, Map<String, StoreInfo> storeInfoMap) {
+        LifeDiscountCouponVo vo = new LifeDiscountCouponVo();
+        vo.setCouponId(coupon.getId());
+        vo.setVoucherId(userCoupon.getVoucherId());
+        vo.setUserCouponId(userCoupon.getId());
+        BeanUtils.copyProperties(coupon, vo);
+        
+        // quantityClaimed表示该券的总领取数量,在用户券列表中不设置或设置为0
+        vo.setQuantityClaimed(null);
+        vo.setExpirationTime(userCoupon.getExpirationTime());
+        
+        // 设置有效期(根据指定天数计算)
+        String specifiedDay = coupon.getSpecifiedDay();
+        if (!StringUtils.isEmpty(specifiedDay)) {
+            try {
                 int sDay = Integer.parseInt(specifiedDay);
-                if (sDay > 0) {
-                    Date receiveDay = lifeDiscountCouponUser.getReceiveTime();
-                    Date validDate = addDaysToDateJava8(receiveDay, sDay);
-                    LocalDate validDateLocalDate = validDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
-                    lifeDiscountCouponVo.setValidDate(validDateLocalDate);
+                if (sDay > 0 && userCoupon.getReceiveTime() != null) {
+                    Date validDate = addDaysToDateJava8(userCoupon.getReceiveTime(), sDay);
+                    vo.setValidDate(validDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                 }
+            } catch (NumberFormatException e) {
+                // 忽略解析错误
             }
-
-            // 判断是否到了优惠券的开始时间
-            lifeDiscountCouponVo.setReachUseTimeFlag(1);
-            if (null != lifeDiscountCouponOne.getBeginGetDate() && localNow1.isBefore(lifeDiscountCouponOne.getBeginGetDate())) {
-                lifeDiscountCouponVo.setReachUseTimeFlag(0);
+        }
+        
+        // 判断是否到了优惠券的开始时间
+        vo.setReachUseTimeFlag(1);
+        if (coupon.getBeginGetDate() != null && localNow.isBefore(coupon.getBeginGetDate())) {
+            vo.setReachUseTimeFlag(0);
+        }
+        
+        // 设置店铺信息
+        if (!StringUtils.isEmpty(coupon.getStoreId())) {
+            StoreInfo storeInfo = storeInfoMap.get(coupon.getStoreId());
+            if (storeInfo != null) {
+                vo.setBusinessSection(storeInfo.getBusinessSection());
+                vo.setBusinessSectionName(storeInfo.getBusinessSectionName());
             }
-            lifeDiscountCouponVos.add(lifeDiscountCouponVo);
         }
-        return lifeDiscountCouponVos;
+        
+        return vo;
+    }
+    
+    /**
+     * 在指定日期基础上增加天数
+     */
+    private Date addDaysToDate(Date date, int days) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        calendar.add(Calendar.DAY_OF_YEAR, days);
+        return calendar.getTime();
     }
 
     //获取纯日期
@@ -892,9 +968,31 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
         return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
     }
 
+    /**
+     * 获取店铺所有优惠券列表(分页)
+     * 支持查询我的优惠券和好友优惠券
+     * 
+     * @param storeId 店铺ID
+     * @param userLoginInfo 用户登录信息
+     * @param page 页码
+     * @param size 每页数量
+     * @param couponName 优惠券名称(模糊查询)
+     * @param tab 分页类型:0-全部,1-进行中,2-已结束
+     * @param couponsFromType 查询类型:1-我的优惠券,2-好友的优惠券
+     * @param couponStatus 优惠券状态:0-草稿,1-正式
+     * @param couponType 优惠券类型:1-满减券,2-折扣券,null-全部
+     * @return 分页优惠券列表
+     */
     @Override
     public IPage<LifeDiscountCouponVo> getStoreAllCouponList(String storeId, UserLoginInfo userLoginInfo, int page, int size, String couponName, String tab, int couponsFromType, int couponStatus, Integer couponType) {
-
+        // 参数校验
+        if (StringUtils.isEmpty(storeId)) {
+            throw new IllegalArgumentException("店铺ID不能为空");
+        }
+        if (StringUtils.isEmpty(tab)) {
+            throw new IllegalArgumentException("分页类型不能为空");
+        }
+        
         if (couponsFromType == 1) {
             IPage<LifeDiscountCoupon> iPage = new Page<>(page, size);
             List<LifeDiscountCouponVo> lifeDiscountCouponVos = new ArrayList<>();
@@ -910,23 +1008,20 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
             }
             //获取当日日期
             Date now = new Date();
-            //如果根据类型查询
-            if (!StringUtils.isEmpty(tab) && tab.equals("1")) {//进行中
-                //开始时间小于当前时间
-                lifeDiscountCouponLambdaQueryWrapper.le(LifeDiscountCoupon::getBeginGetDate, getPureDate(now));
-                //结束时间大于当前时间
-                lifeDiscountCouponLambdaQueryWrapper.ge(LifeDiscountCoupon::getEndGetDate, getPureDate(now));
-                lifeDiscountCouponLambdaQueryWrapper.gt(LifeDiscountCoupon::getSingleQty, 0);
-
-                //不要已暂停关闭领取的
-                lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getGetStatus, 1);
-            } else if (!StringUtils.isEmpty(tab) && tab.equals("2")) {//已结束
-                //结束时间小于当前时间
-                lifeDiscountCouponLambdaQueryWrapper.lt(LifeDiscountCoupon::getEndGetDate, getPureDate(now));
-
-                //不要已暂停关闭领取的
-                lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getGetStatus, 1);
-            }
+            // 根据tab类型添加查询条件
+            Date pureDate = getPureDate(now);
+            if ("1".equals(tab)) {
+                // 进行中:开始时间 <= 当前时间 且 结束时间 >= 当前时间 且 有库存 且 开启领取
+                lifeDiscountCouponLambdaQueryWrapper.le(LifeDiscountCoupon::getBeginGetDate, pureDate)
+                        .ge(LifeDiscountCoupon::getEndGetDate, pureDate)
+                        .gt(LifeDiscountCoupon::getSingleQty, 0)
+                        .eq(LifeDiscountCoupon::getGetStatus, 1);
+            } else if ("2".equals(tab)) {
+                // 已结束:结束时间 < 当前时间 且 开启领取
+                lifeDiscountCouponLambdaQueryWrapper.lt(LifeDiscountCoupon::getEndGetDate, pureDate)
+                        .eq(LifeDiscountCoupon::getGetStatus, 1);
+            }
+            // tab=0或其他值:查询全部,不添加额外条件
             lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getStoreId, storeId);
             lifeDiscountCouponLambdaQueryWrapper.orderByDesc(LifeDiscountCoupon::getCreatedTime);
             //根据店铺id查询该店铺的优惠券,状态是开启领取的券
@@ -963,17 +1058,35 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
                         continue;
                     }
                 }
-                // 查询三个规则
-                List<LifeDiscountCouponUnavailableRules> discountCouponId = lifeDiscountCouponUnavailableRulesMapper.selectList(new QueryWrapper<LifeDiscountCouponUnavailableRules>().eq("discount_coupon_id", lifeDiscountCoupon.getId()));
-                Map<String, List<LifeDiscountCouponUnavailableRules>> collect = discountCouponId.stream().collect(Collectors.groupingBy(x -> x.getUnavailableRuleType()));
-                if (collect.containsKey(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue())) {
-                    lifeDiscountCouponVo.setWeeklyDisabledList(collect.get(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue()).stream().map(x -> x.getUnavailableRuleValue()).collect(Collectors.toList()));
+                // 查询优惠券不可用规则(工作日、节假日、领取规则)
+                List<LifeDiscountCouponUnavailableRules> unavailableRules = lifeDiscountCouponUnavailableRulesMapper.selectList(
+                        new LambdaQueryWrapper<LifeDiscountCouponUnavailableRules>()
+                                .eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, lifeDiscountCoupon.getId()));
+                
+                // 按规则类型分组
+                Map<String, List<LifeDiscountCouponUnavailableRules>> rulesMap = unavailableRules.stream()
+                        .collect(Collectors.groupingBy(LifeDiscountCouponUnavailableRules::getUnavailableRuleType));
+                
+                // 设置工作日不可用列表
+                String weekdayKey = DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue();
+                if (rulesMap.containsKey(weekdayKey)) {
+                    lifeDiscountCouponVo.setWeeklyDisabledList(rulesMap.get(weekdayKey).stream()
+                            .map(LifeDiscountCouponUnavailableRules::getUnavailableRuleValue)
+                            .collect(Collectors.toList()));
                 }
-                if (collect.containsKey(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue())) {
-                    lifeDiscountCouponVo.setHolidayDisabledList(collect.get(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue()).stream().map(x -> x.getUnavailableRuleValue()).collect(Collectors.toList()));
+                
+                // 设置节假日不可用列表
+                String holidayKey = DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue();
+                if (rulesMap.containsKey(holidayKey)) {
+                    lifeDiscountCouponVo.setHolidayDisabledList(rulesMap.get(holidayKey).stream()
+                            .map(LifeDiscountCouponUnavailableRules::getUnavailableRuleValue)
+                            .collect(Collectors.toList()));
                 }
-                if (collect.containsKey(DiscountCouponEnum.CLAIM_RULE.getValue())) {
-                    lifeDiscountCouponVo.setClaimRule(collect.get(DiscountCouponEnum.CLAIM_RULE.getValue()).get(0).getUnavailableRuleValue());
+                
+                // 设置领取规则
+                String claimRuleKey = DiscountCouponEnum.CLAIM_RULE.getValue();
+                if (rulesMap.containsKey(claimRuleKey) && !rulesMap.get(claimRuleKey).isEmpty()) {
+                    lifeDiscountCouponVo.setClaimRule(rulesMap.get(claimRuleKey).get(0).getUnavailableRuleValue());
                 }
                 BeanUtils.copyProperties(lifeDiscountCoupon, lifeDiscountCouponVo);
                 lifeDiscountCouponVos.add(lifeDiscountCouponVo);
@@ -997,21 +1110,19 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
             }
             //获取当日日期
             Date now = new Date();
-            //如果根据类型查询
-            if (!StringUtils.isEmpty(tab) && tab.equals("1")) {//进行中
-                //开始时间小于当前时间
-                friendLifeDiscountCouponQueryWrapper.le("ldc.start_date", getPureDate(now));
-                //结束时间大于当前时间
-                friendLifeDiscountCouponQueryWrapper.ge("ldc.end_date", getPureDate(now));
-                //不要已暂停关闭领取的
-                friendLifeDiscountCouponQueryWrapper.eq("ldc.get_status", 1);
-            } else if (!StringUtils.isEmpty(tab) && tab.equals("2")) {//已结束
-                //结束时间小于当前时间
-                friendLifeDiscountCouponQueryWrapper.lt("ldc.end_date", getPureDate(now));
-
-                //不要已暂停关闭领取的
-                friendLifeDiscountCouponQueryWrapper.eq("ldc.get_status", 1);
-            }
+            // 根据tab类型添加查询条件(好友优惠券)
+            Date pureDate = getPureDate(now);
+            if ("1".equals(tab)) {
+                // 进行中:开始时间 <= 当前时间 且 结束时间 >= 当前时间 且 开启领取
+                friendLifeDiscountCouponQueryWrapper.le("ldc.start_date", pureDate)
+                        .ge("ldc.end_date", pureDate)
+                        .eq("ldc.get_status", 1);
+            } else if ("2".equals(tab)) {
+                // 已结束:结束时间 < 当前时间 且 开启领取
+                friendLifeDiscountCouponQueryWrapper.lt("ldc.end_date", pureDate)
+                        .eq("ldc.get_status", 1);
+            }
+            // tab=0或其他值:查询全部,不添加额外条件
             friendLifeDiscountCouponQueryWrapper.orderByDesc("ldc.created_time");
             friendLifeDiscountCouponQueryWrapper.eq("ldcsf.store_user_id", storeId);
             friendLifeDiscountCouponQueryWrapper.eq("ldcsf.delete_flag", 0);
@@ -1047,16 +1158,35 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
                         continue;
                     }
                 }
-                List<LifeDiscountCouponUnavailableRules> discountCouponId = lifeDiscountCouponUnavailableRulesMapper.selectList(new QueryWrapper<LifeDiscountCouponUnavailableRules>().eq("discount_coupon_id", record.getCouponId()));
-                Map<String, List<LifeDiscountCouponUnavailableRules>> collect = discountCouponId.stream().collect(Collectors.groupingBy(x -> x.getUnavailableRuleType()));
-                if (collect.containsKey(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue())) {
-                    record.setWeeklyDisabledList(collect.get(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue()).stream().map(x -> x.getUnavailableRuleValue()).collect(Collectors.toList()));
+                // 查询优惠券不可用规则(好友优惠券)
+                List<LifeDiscountCouponUnavailableRules> unavailableRules = lifeDiscountCouponUnavailableRulesMapper.selectList(
+                        new LambdaQueryWrapper<LifeDiscountCouponUnavailableRules>()
+                                .eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, record.getCouponId()));
+                
+                // 按规则类型分组
+                Map<String, List<LifeDiscountCouponUnavailableRules>> rulesMap = unavailableRules.stream()
+                        .collect(Collectors.groupingBy(LifeDiscountCouponUnavailableRules::getUnavailableRuleType));
+                
+                // 设置工作日不可用列表
+                String weekdayKey = DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue();
+                if (rulesMap.containsKey(weekdayKey)) {
+                    record.setWeeklyDisabledList(rulesMap.get(weekdayKey).stream()
+                            .map(LifeDiscountCouponUnavailableRules::getUnavailableRuleValue)
+                            .collect(Collectors.toList()));
                 }
-                if (collect.containsKey(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue())) {
-                    record.setHolidayDisabledList(collect.get(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue()).stream().map(x -> x.getUnavailableRuleValue()).collect(Collectors.toList()));
+                
+                // 设置节假日不可用列表
+                String holidayKey = DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue();
+                if (rulesMap.containsKey(holidayKey)) {
+                    record.setHolidayDisabledList(rulesMap.get(holidayKey).stream()
+                            .map(LifeDiscountCouponUnavailableRules::getUnavailableRuleValue)
+                            .collect(Collectors.toList()));
                 }
-                if (collect.containsKey(DiscountCouponEnum.CLAIM_RULE.getValue())) {
-                    record.setClaimRule(collect.get(DiscountCouponEnum.CLAIM_RULE.getValue()).get(0).getUnavailableRuleValue());
+                
+                // 设置领取规则
+                String claimRuleKey = DiscountCouponEnum.CLAIM_RULE.getValue();
+                if (rulesMap.containsKey(claimRuleKey) && !rulesMap.get(claimRuleKey).isEmpty()) {
+                    record.setClaimRule(rulesMap.get(claimRuleKey).get(0).getUnavailableRuleValue());
                 }
                 BeanUtils.copyProperties(record, lifeDiscountcouponVo);
                 friendLifeDiscountCouponVos.add(lifeDiscountcouponVo);

+ 6 - 1
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponStoreFriendServiceImpl.java

@@ -922,7 +922,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);

+ 51 - 10
alien-store/src/main/java/shop/alien/store/service/impl/StoreCustomerServiceServiceImpl.java

@@ -15,9 +15,11 @@ import shop.alien.mapper.StoreCustomerServiceMapper;
 import shop.alien.store.service.StoreCustomerServiceService;
 
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -37,9 +39,15 @@ public class StoreCustomerServiceServiceImpl extends ServiceImpl<StoreCustomerSe
 
     @Override
     public StoreCustomerService getByQuestion(String question) {
+        if (StringUtils.isEmpty(question)) {
+            return null;
+        }
         LambdaQueryWrapper<StoreCustomerService> lambdaQueryWrapper = new LambdaQueryWrapper<>();
-        List<StoreCustomerService> list = storeCustomerServiceMapper.selectList(lambdaQueryWrapper);
-        return list.stream().filter(item -> item.getQuestion().contains(question)).findFirst().orElse(null);
+        lambdaQueryWrapper.like(StoreCustomerService::getQuestion, question)
+                .eq(StoreCustomerService::getDeleteFlag, 0)
+                .orderByDesc(StoreCustomerService::getCreatedTime)
+                .last("limit 1");
+        return storeCustomerServiceMapper.selectOne(lambdaQueryWrapper);
     }
 
     @Override
@@ -78,6 +86,13 @@ public class StoreCustomerServiceServiceImpl extends ServiceImpl<StoreCustomerSe
 
     @Override
     public List<StoreCustomerService> getRandList(String type, Integer limit) {
+        if (StringUtils.isEmpty(type) || limit == null || limit <= 0) {
+            return new ArrayList<>();
+        }
+        // 限制最大查询数量,防止SQL注入和性能问题
+        if (limit > 100) {
+            limit = 100;
+        }
         LambdaQueryWrapper<StoreCustomerService> lambdaQueryWrapper = new LambdaQueryWrapper<>();
         lambdaQueryWrapper.eq(StoreCustomerService::getType, type)
                 .eq(StoreCustomerService::getDeleteFlag, 0)
@@ -87,22 +102,40 @@ public class StoreCustomerServiceServiceImpl extends ServiceImpl<StoreCustomerSe
 
     @Override
     public List<AiIntelligentAssistant> saveAiIntelligentAssistant(List<AiIntelligentAssistant> aiIntelligentAssistants) {
+        if (aiIntelligentAssistants == null || aiIntelligentAssistants.isEmpty()) {
+            return new ArrayList<>();
+        }
+        Date now = new Date();
         for (AiIntelligentAssistant aiIntelligentAssistant : aiIntelligentAssistants) {
+            if (aiIntelligentAssistant == null) {
+                continue;
+            }
             aiIntelligentAssistant.setDeleteFlag(0);
-            if (aiIntelligentAssistant.getType() != 0) {
-                aiIntelligentAssistant.setCreatedTime(new Date());
-                aiIntelligentAssistant.setUpdatedTime(new Date());
+            if (aiIntelligentAssistant.getType() != null && aiIntelligentAssistant.getType() != 0) {
+                aiIntelligentAssistant.setCreatedTime(now);
+                aiIntelligentAssistant.setUpdatedTime(now);
             }
         }
-        aiIntelligentAssistantMapper.insertList(aiIntelligentAssistants);
-        return aiIntelligentAssistants;
+        // 过滤掉null值
+        List<AiIntelligentAssistant> validList = aiIntelligentAssistants.stream()
+                .filter(item -> item != null)
+                .collect(Collectors.toList());
+        if (validList.isEmpty()) {
+            return new ArrayList<>();
+        }
+        aiIntelligentAssistantMapper.insertList(validList);
+        return validList;
     }
 
+    /**
+     * 查询聊天记录
+     * 只返回最近7天内的记录
+     */
     @Override
-    public List<AiIntelligentAssistant> selectAiIntelligentAssistant(String userId,String time,Integer talkSource) {
+    public List<AiIntelligentAssistant> selectAiIntelligentAssistant(String userId, String time, Integer talkSource) {
         // 参数校验
         if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(time) || talkSource == null) {
-            return new java.util.ArrayList<>();
+            return new ArrayList<>();
         }
         
         // 解析time字符串为Date类型
@@ -122,6 +155,11 @@ public class StoreCustomerServiceServiceImpl extends ServiceImpl<StoreCustomerSe
         calendar.add(Calendar.DAY_OF_YEAR, -7);
         Date sevenDaysAgo = calendar.getTime();
         
+        // 确保查询时间不早于7天前
+        if (queryTime.before(sevenDaysAgo)) {
+            queryTime = sevenDaysAgo;
+        }
+        
         // 查找最后一个type=0的记录作为分页标记点(限制在7天内)
         LambdaQueryWrapper<AiIntelligentAssistant> last = new LambdaQueryWrapper<AiIntelligentAssistant>()
                 .lt(AiIntelligentAssistant::getCreatedTime, queryTime)
@@ -136,8 +174,11 @@ public class StoreCustomerServiceServiceImpl extends ServiceImpl<StoreCustomerSe
         
         if (ObjectUtils.isNotEmpty(aiIntelligentAssistant)) {
             // 找到了标记点,从标记点开始查询到time之间的记录
-            // 由于标记点查询已经限制了7天内,所以标记点时间一定不早于7天前
             Date markPointTime = aiIntelligentAssistant.getCreatedTime();
+            // 确保标记点时间不早于7天前
+            if (markPointTime.before(sevenDaysAgo)) {
+                markPointTime = sevenDaysAgo;
+            }
             
             LambdaQueryWrapper<AiIntelligentAssistant> queryWrapper = new LambdaQueryWrapper<AiIntelligentAssistant>()
                     .ge(AiIntelligentAssistant::getCreatedTime, markPointTime)