Browse Source

1。商品搜索、门店搜索返回评分等信息

qmj 3 ngày trước cách đây
mục cha
commit
8f77a09f9a

+ 136 - 28
ruoyi-admin/src/main/java/com/ruoyi/app/mendian/PosFoodController.java

@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ruoyi.app.mendian.dto.FoodSearchOutput;
+import com.ruoyi.app.utils.DateUtil;
 import com.ruoyi.app.utils.ImageCompressUtils;
 import com.ruoyi.common.annotation.Anonymous;
 import com.ruoyi.common.annotation.Log;
@@ -17,15 +18,9 @@ import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.common.utils.MessageUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
-import com.ruoyi.system.domain.InfoUser;
-import com.ruoyi.system.domain.OperatingHours;
-import com.ruoyi.system.domain.PosFood;
-import com.ruoyi.system.domain.PosStore;
+import com.ruoyi.system.domain.*;
 import com.ruoyi.system.mapper.PosStoreMapper;
-import com.ruoyi.system.service.IInfoUserService;
-import com.ruoyi.system.service.IPosFenleiService;
-import com.ruoyi.system.service.IPosFoodService;
-import com.ruoyi.system.service.IPosStoreService;
+import com.ruoyi.system.service.*;
 import com.ruoyi.system.utils.Auth;
 import com.ruoyi.system.utils.JwtUtil;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -61,6 +56,12 @@ public class PosFoodController extends BaseController {
     private IInfoUserService infoUserService;
     @Autowired //店铺Mapper
     private PosStoreMapper posStoreMapper;
+    @Autowired
+    private IPosReviewService posReviewService;
+    @Autowired
+    private IOperatingHoursService operatingHoursService;
+    @Autowired
+    private IPosOrderService posOrderService;
 
     //删除商品
     @Anonymous
@@ -288,7 +289,9 @@ public class PosFoodController extends BaseController {
         List<PosStore> stores = storePage.getRecords();
 
         List<FoodSearchOutput> result = new ArrayList<>();
-
+        Map<Long, List<PosReview>> reviewMap = Collections.emptyMap();
+        Map<Long, Long> orderCountMap = Collections.emptyMap();
+        List<OperatingHours> hourslist=new ArrayList<>();
         // 2. 一次性查询所有相关商品(避免循环查询数据库)
         if (!stores.isEmpty()) {
             // 提取所有店铺ID
@@ -308,30 +311,67 @@ public class PosFoodController extends BaseController {
             // 按店铺ID分组商品
             Map<Long, List<PosFood>> foodsByStore = allFoods.stream()
                     .collect(Collectors.groupingBy(PosFood::getMdid));
-            List<OperatingHours> storeHours = new ArrayList<>();
-//            List<FoodOperatingHours> foodHours = new ArrayList<>();
-//            if (!stores.isEmpty()) {
-//                storeHours = operatingHoursService.list(new LambdaQueryWrapper<OperatingHours>().in(OperatingHours::getMdId, stores.stream().map(store -> store.getId().longValue()).collect(Collectors.toList())));
-//                foodHours = foodOperatingHoursService.list(new LambdaQueryWrapper<FoodOperatingHours>().in(FoodOperatingHours::getFoodId, allFoods.stream().map(PosFood::getId).collect(Collectors.toList())));
-//            }
-
+            // 1. 计算各门店评分
+            QueryWrapper<PosReview> reviewWrapper = new QueryWrapper<>();
+            reviewWrapper.in("md_id", storeIds);
+            List<PosReview> reviews = posReviewService.list(reviewWrapper);
+
+            reviewMap = reviews.stream()
+                    .collect(Collectors.groupingBy(PosReview::getMdId));
+
+            // 2. 统计各门店本月订单数(按 cretim 当前月份)
+            QueryWrapper<PosOrder> orderWrapper = new QueryWrapper<>();
+            orderWrapper.in("md_id", storeIds);
+            orderWrapper.apply("DATE_FORMAT(cretim,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m')");
+            List<PosOrder> orders = posOrderService.list(orderWrapper);
+            orderCountMap = orders.stream()
+                    .collect(Collectors.groupingBy(PosOrder::getMdId, Collectors.counting()));
+            QueryWrapper<OperatingHours> wrapper = new QueryWrapper<>();
+            wrapper.in("md_id", storeIds);
+            hourslist = operatingHoursService.list(wrapper);
+            DateUtil dateUtil = new DateUtil();
 
             // 3. 为每个店铺设置对应的商品
             for (PosStore store : stores) {
                 FoodSearchOutput output = new FoodSearchOutput();
-                List<OperatingHours> hours = storeHours.stream().filter(hour -> hour.getMdId().equals(store.getId().longValue())).collect(Collectors.toList());
-//                boolean isOperating = OperatingUtil.storeIsOperatingOfDay(hours);
-//                store.setIsOperating(isOperating);
+                Long mdId = store.getId() == null ? null : store.getId().longValue();
+                List<PosReview> storeReviews = (mdId == null || reviewMap.isEmpty()) ? null : reviewMap.get(mdId);
+
+                double score;
+                if (storeReviews == null || storeReviews.isEmpty()) {
+                    score = 4.5d;
+                } else {
+                    double avg = storeReviews.stream()
+                            .mapToLong(PosReview::getScore)
+                            .average()
+                            .orElse(4.5d);
+                    // 平均分按 0.5 分粒度四舍五入,例如 3, 3.5, 4, 4.5
+                    score = Math.round(avg * 2.0d) / 2.0d;
+                }
+                store.setPingf(score);
+
+                // 设置本月售出订单数量(ddsl)
+                long monthCount = (mdId == null || orderCountMap.isEmpty()) ? 0L : orderCountMap.getOrDefault(mdId, 0L);
+                store.setDdsl(monthCount);
+
+                Boolean dayang = false;
+                List<OperatingHours> mdHours=hourslist.stream().filter(x-> store.getId().equals(x.getMdId().intValue())).collect(Collectors.toList());
+                //在营业时间范围内返回true
+                for (int i = 0; i < mdHours.size(); i++) {
+                    Boolean sbsj = dateUtil.isLegalTime(mdHours.get(i).getStartTime(), mdHours.get(i).getEndTime());
+                    if (sbsj == true) {
+                        dayang = true;
+                    }
+                }
+                //不在时间范围内标识为打烊
+                if (dayang == false) {
+                    store.setState(1L);
+                }
                 output.setPosStore(store);
                 // 从分组结果中获取该店铺的商品
                 List<PosFood> storeFoods = foodsByStore.getOrDefault(store.getId().longValue(), new ArrayList<>());
-//                for (PosFood food : storeFoods) {
-//                    List<FoodOperatingHours> foodHour = foodHours.stream().filter(hour -> hour.getFoodId().equals(food.getId())).collect(Collectors.toList());
-//                    boolean isOperatingFood = getFoodIsOparetig(food, foodHour, storeHours, new HashMap<Long, Boolean>());
-//                    food.setIsOperating(isOperatingFood);
-//                }
-                output.setPosFoodList(storeFoods);
 
+                output.setPosFoodList(storeFoods);
                 result.add(output);
             }
         }
@@ -380,13 +420,81 @@ public class PosFoodController extends BaseController {
             pageSize = 10;
         }
         Page<PosStore> page = new Page<>(pageNum, pageSize);
-        QueryWrapper<PosStore> wrapper = new QueryWrapper<>();
+        QueryWrapper<PosStore> storeWrapper = new QueryWrapper<>();
         String finalKeyword = keyword;
-        wrapper.eq("off_shelf", "0")
+        storeWrapper.eq("off_shelf", "0")
 //                .and(i->i.like("pos_name", keyword).or().like("brief_introduction",keyword));
                 .and(i -> i.apply(" CONCAT(pos_name,' ') LIKE CONCAT('%', {0}, '%') COLLATE utf8mb4_unicode_ci", finalKeyword).or().apply(" CONCAT(brief_introduction,' ') LIKE CONCAT('%', {0}, '%') COLLATE utf8mb4_unicode_ci", finalKeyword));
-        IPage<PosStore> data = posStoreService.page(page, wrapper);
+        IPage<PosStore> data = posStoreService.page(page, storeWrapper);
         Map<String, Object> response = new HashMap<>();
+        List<PosStore> stores=data.getRecords();
+        Map<Long, List<PosReview>> reviewMap = Collections.emptyMap();
+        Map<Long, Long> orderCountMap = Collections.emptyMap();
+        List<OperatingHours> hourslist=new ArrayList<>();
+        if(!stores.isEmpty()){
+            List<Long> mdIds = stores.stream()
+                    .map(store -> store.getId() == null ? null : store.getId().longValue())
+                    .filter(idVal -> idVal != null)
+                    .collect(Collectors.toList());
+
+            if (!mdIds.isEmpty()) {
+                // 1. 计算各门店评分
+                QueryWrapper<PosReview> reviewWrapper = new QueryWrapper<>();
+                reviewWrapper.in("md_id", mdIds);
+                List<PosReview> reviews = posReviewService.list(reviewWrapper);
+
+                reviewMap = reviews.stream()
+                        .collect(Collectors.groupingBy(PosReview::getMdId));
+
+                // 2. 统计各门店本月订单数(按 cretim 当前月份)
+                QueryWrapper<PosOrder> orderWrapper = new QueryWrapper<>();
+                orderWrapper.in("md_id", mdIds);
+                orderWrapper.apply("DATE_FORMAT(cretim,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m')");
+                List<PosOrder> orders = posOrderService.list(orderWrapper);
+                orderCountMap = orders.stream()
+                        .collect(Collectors.groupingBy(PosOrder::getMdId, Collectors.counting()));
+                QueryWrapper<OperatingHours> wrapper = new QueryWrapper<>();
+                wrapper.in("md_id", mdIds);
+                hourslist = operatingHoursService.list(wrapper);
+
+            }
+            DateUtil dateUtil = new DateUtil();
+            for(PosStore store:stores){
+                Long mdId = store.getId() == null ? null : store.getId().longValue();
+                List<PosReview> storeReviews = (mdId == null || reviewMap.isEmpty()) ? null : reviewMap.get(mdId);
+
+                double score;
+                if (storeReviews == null || storeReviews.isEmpty()) {
+                    score = 4.5d;
+                } else {
+                    double avg = storeReviews.stream()
+                            .mapToLong(PosReview::getScore)
+                            .average()
+                            .orElse(4.5d);
+                    // 平均分按 0.5 分粒度四舍五入,例如 3, 3.5, 4, 4.5
+                    score = Math.round(avg * 2.0d) / 2.0d;
+                }
+                store.setPingf(score);
+
+                // 设置本月售出订单数量(ddsl)
+                long monthCount = (mdId == null || orderCountMap.isEmpty()) ? 0L : orderCountMap.getOrDefault(mdId, 0L);
+                store.setDdsl(monthCount);
+
+                Boolean dayang = false;
+                List<OperatingHours> mdHours=hourslist.stream().filter(x-> store.getId().equals(x.getMdId().intValue())).collect(Collectors.toList());
+                //在营业时间范围内返回true
+                for (int i = 0; i < mdHours.size(); i++) {
+                    Boolean sbsj = dateUtil.isLegalTime(mdHours.get(i).getStartTime(), mdHours.get(i).getEndTime());
+                    if (sbsj == true) {
+                        dayang = true;
+                    }
+                }
+                //不在时间范围内标识为打烊
+                if (dayang == false) {
+                    store.setState(1L);
+                }
+            }
+        }
 
         response.put("list", data.getRecords());
         response.put("total", data.getTotal());