zhangchen преди 2 седмици
родител
ревизия
e2b33c579d

+ 15 - 7
alien-store/src/main/java/shop/alien/store/controller/StoreMenuController.java

@@ -10,6 +10,7 @@ import shop.alien.entity.store.vo.StoreMenuVo;
 import shop.alien.store.service.StoreMenuService;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 二期-门店菜单Controller
@@ -127,13 +128,18 @@ public class StoreMenuController {
      * 获取菜单(用户端)
      * <p>
      * 根据门店ID获取菜单列表,支持按菜品类型、菜单类型筛选,并可查询用户点赞状态
+     * 返回菜单列表以及按四种组合统计的数量:
+     * 1. menu_all: dish_menu_type=1(菜单),dish_type=0(全部)
+     * 2. menu_recommend: dish_menu_type=1(菜单),dish_type=1(推荐)
+     * 3. drink_all: dish_menu_type=2(酒水),dish_type=0(全部)
+     * 4. drink_recommend: dish_menu_type=2(酒水),dish_type=1(推荐)
      * </p>
      *
      * @param storeId      门店ID,必填,必须大于0
      * @param dishType     菜品类型,可选,0:非推荐, 1:推荐
      * @param phoneId      用户手机号,可选,用于查询点赞状态
      * @param dishMenuType 菜单类型,可选,1-菜单,2-酒水
-     * @return 统一返回结果,包含菜单列表
+     * @return 统一返回结果,包含菜单列表和统计信息(list:菜单列表,count:按四种组合统计的数量)
      * @throws IllegalArgumentException 当门店ID为空或小于等于0时抛出
      */
     @ApiOperation("获取菜单(用户端)")
@@ -145,7 +151,7 @@ public class StoreMenuController {
             @ApiImplicitParam(name = "dishMenuType", value = "菜单类型:1-菜单,2-酒水", dataType = "Integer", paramType = "query")
     })
     @GetMapping("/getClientMenuByStoreId")
-    public R<List<StoreMenuVo>> getClientMenuByStoreId(
+    public R<Map<String, Object>> getClientMenuByStoreId(
             @RequestParam(value = "storeId", required = true) Integer storeId,
             @RequestParam(value = "dishType", required = false) Integer dishType,
             @RequestParam(value = "phoneId", required = false) String phoneId,
@@ -161,15 +167,17 @@ public class StoreMenuController {
         }
         
         try {
-            // 调用服务层获取菜单列表
-            List<StoreMenuVo> menuList = storeMenuService.getClientMenuByStoreId(
+            // 调用服务层获取菜单列表和统计信息
+            Map<String, Object> result = storeMenuService.getClientMenuByStoreId(
                     storeId, dishType, phoneId, dishMenuType);
             
             // 记录返回结果
-            log.info("获取用户端菜单成功,门店ID:{},返回菜单数量:{}", 
-                    storeId, menuList != null ? menuList.size() : 0);
+            @SuppressWarnings("unchecked")
+            List<StoreMenuVo> menuList = (List<StoreMenuVo>) result.get("list");
+            log.info("获取用户端菜单成功,门店ID:{},返回菜单数量:{},统计信息:{}", 
+                    storeId, menuList != null ? menuList.size() : 0, result.get("count"));
             
-            return R.data(menuList);
+            return R.data(result);
         } catch (Exception e) {
             log.error("获取用户端菜单异常,门店ID:{},异常信息:{}", storeId, e.getMessage(), e);
             return R.fail("获取菜单失败:" + e.getMessage());

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

@@ -92,10 +92,10 @@ public interface StoreMenuService extends IService<StoreMenu> {
      * @param dishType     菜品类型,可选,0:非推荐, 1:推荐。当为0时,查询条件中不包含菜品类型限制
      * @param phoneId      用户手机号,可选,用于查询用户对推荐菜的点赞状态
      * @param dishMenuType 菜单类型,可选,1-菜单,2-酒水
-     * @return 菜单列表,按排序字段升序排列,如果查询结果为空则返回空列表
+     * @return Map包含菜单列表和统计信息:list(菜单列表)、count(按四种组合统计:menu_all、menu_recommend、drink_all、drink_recommend)
      * @throws IllegalArgumentException 当门店ID为空或小于等于0时抛出
      */
-    List<StoreMenuVo> getClientMenuByStoreId(Integer storeId, Integer dishType, String phoneId, Integer dishMenuType);
+    java.util.Map<String, Object> getClientMenuByStoreId(Integer storeId, Integer dishType, String phoneId, Integer dishMenuType);
 
     /**
      * 获取菜品详情

+ 68 - 9
alien-store/src/main/java/shop/alien/store/service/impl/StoreMenuServiceImpl.java

@@ -23,7 +23,9 @@ import shop.alien.mapper.StoreMenuMapper;
 import shop.alien.store.service.StoreImgService;
 import shop.alien.store.service.StoreMenuService;
 
+import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -339,7 +341,7 @@ public class StoreMenuServiceImpl extends ServiceImpl<StoreMenuMapper, StoreMenu
      * @return 菜单列表,按排序字段升序排列,如果查询结果为空则返回空列表
      */
     @Override
-    public List<StoreMenuVo> getClientMenuByStoreId(Integer storeId, Integer dishType, String phoneId, Integer dishMenuType) {
+    public Map<String, Object> getClientMenuByStoreId(Integer storeId, Integer dishType, String phoneId, Integer dishMenuType) {
         log.info("开始获取用户端菜单,门店ID:{},菜品类型:{},用户手机号:{},菜单类型:{}", 
                 storeId, dishType, phoneId, dishMenuType);
         
@@ -349,19 +351,68 @@ public class StoreMenuServiceImpl extends ServiceImpl<StoreMenuMapper, StoreMenu
             throw new IllegalArgumentException("门店ID不能为空且必须大于0");
         }
         
+        // 先查询该门店的所有菜单数据(不应用筛选条件),用于统计
+        List<StoreMenuVo> allMenuList = storeMenuMapper.getClientMenuByStoreId(storeId, null, null);
+        
+        // 按照四种组合进行分组统计(基于所有菜单数据,不受筛选条件影响):
+        // 1. dish_menu_type = 1(菜单),dish_type = 0(全部)
+        // 2. dish_menu_type = 1(菜单),dish_type = 1(推荐)
+        // 3. dish_menu_type = 2(酒水),dish_type = 0(全部)
+        // 4. dish_menu_type = 2(酒水),dish_type = 1(推荐)
+        Map<String, Integer> countMap = new HashMap<>();
+        
+        if (CollectionUtils.isNotEmpty(allMenuList)) {
+            // 统计:dish_menu_type = 1(菜单),dish_type = 1(推荐)
+            long menuRecommendCount = allMenuList.stream()
+                    .filter(item -> "1".equals(item.getDishMenuType()) 
+                            && DISH_TYPE_RECOMMEND.equals(item.getDishType()))
+                    .count();
+            countMap.put("menu_recommend", (int) menuRecommendCount);
+
+            // 统计:dish_menu_type = 1(菜单),dish_type = 0(全部)
+            long menuAllCount = allMenuList.stream()
+                    .filter(item -> "1".equals(item.getDishMenuType())
+                            && (item.getDishType() == null || DISH_TYPE_NON_RECOMMEND.equals(item.getDishType())))
+                    .count();
+            countMap.put("menu_all", (int) menuAllCount + (int) menuRecommendCount);
+
+            // 统计:dish_menu_type = 2(酒水),dish_type = 1(推荐)
+            long drinkRecommendCount = allMenuList.stream()
+                    .filter(item -> "2".equals(item.getDishMenuType()) 
+                            && DISH_TYPE_RECOMMEND.equals(item.getDishType()))
+                    .count();
+            countMap.put("drink_recommend", (int) drinkRecommendCount);
+
+            // 统计:dish_menu_type = 2(酒水),dish_type = 0(全部)
+            long drinkAllCount = allMenuList.stream()
+                    .filter(item -> "2".equals(item.getDishMenuType())
+                            && (item.getDishType() == null || DISH_TYPE_NON_RECOMMEND.equals(item.getDishType())))
+                    .count();
+            countMap.put("drink_all", (int) drinkAllCount + (int) drinkRecommendCount);
+        } else {
+            // 如果没有数据,返回0
+            countMap.put("menu_all", 0);
+            countMap.put("menu_recommend", 0);
+            countMap.put("drink_all", 0);
+            countMap.put("drink_recommend", 0);
+        }
+        
         // 处理菜品类型参数:当dishType为0时,转换为null以查询所有类型
-        Integer queryDishType = (DISH_TYPE_NON_RECOMMEND.equals(dishType)) ? null : dishType;
+        Integer queryDishType = (dishType != null && DISH_TYPE_NON_RECOMMEND.equals(dishType)) ? null : dishType;
         
-        // 查询菜单列表
+        // 根据筛选条件查询菜单列表(用于返回给前端)
         List<StoreMenuVo> menuList = storeMenuMapper.getClientMenuByStoreId(storeId, queryDishType, dishMenuType);
         
-        // 如果查询结果为空,直接返回空列表
+        // 如果查询结果为空,返回空列表但保留统计信息
         if (CollectionUtils.isEmpty(menuList)) {
-            log.info("获取用户端菜单完成,门店ID:{},未查询到菜单数据", storeId);
-            return menuList;
+            log.info("获取用户端菜单完成,门店ID:{},未查询到符合条件的菜单数据", storeId);
+            Map<String, Object> result = new HashMap<>();
+            result.put("list", Collections.emptyList());
+            result.put("count", countMap);
+            return result;
         }
         
-        log.info("获取用户端菜单,门店ID:{},查询到菜单数量:{}", storeId, menuList.size());
+        log.info("获取用户端菜单,门店ID:{},查询到符合条件的菜单数量:{}", storeId, menuList.size());
         
         // 如果是推荐菜且有用户标识,批量查询并设置点赞状态
         if (DISH_TYPE_RECOMMEND.equals(dishType) && StringUtils.isNotEmpty(phoneId)) {
@@ -376,9 +427,17 @@ public class StoreMenuServiceImpl extends ServiceImpl<StoreMenuMapper, StoreMenu
                 .sorted(Comparator.comparing(StoreMenuVo::getSort, Comparator.nullsLast(Integer::compareTo)))
                 .collect(Collectors.toList());
         
-        log.info("获取用户端菜单完成,门店ID:{},返回菜单数量:{}", storeId, sortedMenuList.size());
+        // 构建返回结果
+        Map<String, Object> result = new HashMap<>();
+        result.put("list", sortedMenuList);
+        result.put("count", countMap);
+        
+        log.info("获取用户端菜单完成,门店ID:{},返回菜单数量:{},统计信息:菜单-全部={},菜单-推荐={},酒水-全部={},酒水-推荐={}", 
+                storeId, sortedMenuList.size(), 
+                countMap.get("menu_all"), countMap.get("menu_recommend"), 
+                countMap.get("drink_all"), countMap.get("drink_recommend"));
         
-        return sortedMenuList;
+        return result;
     }
 
     /**