UserAppIndexController.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.ruoyi.app.user;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.ruoyi.app.utils.DateUtil;
  9. import com.ruoyi.common.annotation.Anonymous;
  10. import com.ruoyi.common.core.controller.BaseController;
  11. import com.ruoyi.common.core.domain.AjaxResult;
  12. import com.ruoyi.system.domain.*;
  13. import com.ruoyi.system.mapper.PosStoreMapper;
  14. import com.ruoyi.system.mapper.PosFoodMapper;
  15. import com.ruoyi.system.service.*;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.web.bind.annotation.GetMapping;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import org.springframework.web.bind.annotation.*;
  21. import java.math.BigDecimal;
  22. import java.util.List;
  23. import java.util.ArrayList;
  24. import java.util.Map;
  25. import java.util.stream.Collectors;
  26. import com.ruoyi.system.mapper.PosOrderMapper;
  27. import com.ruoyi.app.user.dto.StoreOutput;
  28. import org.springframework.beans.BeanUtils;
  29. /**
  30. * 用户美食首页
  31. *
  32. * @author ruoyi
  33. */
  34. @RestController
  35. @RequestMapping("/user/index")
  36. public class UserAppIndexController extends BaseController {
  37. @Autowired
  38. private IPosTypeService posTypeService;
  39. @Autowired
  40. private IPosFoodService posFoodService;
  41. @Autowired
  42. private IPosOrderService posOrderService;
  43. @Autowired
  44. private PosOrderMapper posOrderMapper;
  45. @Autowired
  46. private IFoodStatisticsService foodStatisticsService;
  47. @Autowired
  48. private PosStoreMapper posStoreMapper;
  49. @Autowired
  50. private PosFoodMapper posFoodMapper;
  51. @Autowired
  52. private IInfoUserService infoUserService;
  53. @Autowired
  54. private IOperatingHoursService operatingHoursService;
  55. /**
  56. * 获取夜市列表
  57. * @return
  58. */
  59. @Anonymous
  60. @GetMapping("/getNightMarketList")
  61. public AjaxResult getNightMarketList(){
  62. LambdaQueryWrapper<InfoUser> queryWrapper = new LambdaQueryWrapper<>();
  63. queryWrapper.eq(InfoUser::getUserType,"3");
  64. List<InfoUser> list = infoUserService.list(queryWrapper);
  65. List<Map<String,Object>> result = new ArrayList<>();
  66. // list.forEach(infoUser -> {
  67. // Map<String,Object> map = new JSONObject();
  68. // map.put("id",infoUser.getUserId());
  69. // map.put("name",infoUser.getUserName());
  70. //// map.put("phone",infoUser.getPhone());
  71. // result.add(map);
  72. // });
  73. return success(list);
  74. }
  75. /**
  76. * 获取门店分页列表
  77. */
  78. @Anonymous
  79. @GetMapping("/getStoreList")
  80. public AjaxResult getFoodStoreList(@RequestParam String language,
  81. @RequestParam BigDecimal longitude,
  82. @RequestParam BigDecimal latitude,
  83. @RequestParam Integer page,
  84. @RequestParam(defaultValue = "0") Long flId) {
  85. List<PosStore> storeList = posStoreMapper.getjxStore(longitude,latitude,(page-1)*10,flId);
  86. int count = posStoreMapper.getjxStoreCount(longitude,latitude,flId);
  87. String lang="3";
  88. if ("zh-CN".equals(language)) {
  89. lang = "2";
  90. }
  91. if ("zh-TW".equals(language)) {
  92. lang = "3";
  93. }
  94. if ("en-US".equals(language)) {
  95. lang = "1";
  96. }
  97. // 收集所有门店ID(Integer类型)
  98. List<Integer> storeIds = storeList.stream()
  99. .map(PosStore::getId)
  100. .collect(Collectors.toList());
  101. // 使用MyBatis Plus一次性查询所有门店的商品(避免N+1查询问题)
  102. Map<Integer, List<PosFood>> foodMap = new java.util.HashMap<>();
  103. List<OperatingHours> hourslist=new ArrayList<>();
  104. if (!storeIds.isEmpty()) {
  105. // 将Integer类型的门店ID转换为Long类型用于查询(因为mdid是Long类型)
  106. List<Long> storeIdsLong = storeIds.stream()
  107. .map(Integer::longValue)
  108. .collect(Collectors.toList());
  109. // 使用MyBatis Plus的Mapper方法,在数据库查询时就限制每个门店只返回6条商品
  110. List<PosFood> allFoods = posFoodMapper.selectFoodsByStoreIdsWithLimit(storeIdsLong, lang, 6);
  111. // 按门店ID分组(查询结果已经限制为每个门店最多6条)
  112. foodMap = allFoods.stream()
  113. .collect(Collectors.groupingBy(food -> food.getMdid().intValue()));
  114. QueryWrapper<OperatingHours> wrapper = new QueryWrapper<>();
  115. wrapper.in("md_id", storeIdsLong);
  116. hourslist = operatingHoursService.list(wrapper);
  117. }
  118. // 转换为StoreOutput并设置每个门店的商品列表(每个门店最多6条)
  119. List<StoreOutput> storeOutputList = new ArrayList<>();
  120. DateUtil dateUtil = new DateUtil();
  121. for (PosStore store : storeList) {
  122. StoreOutput storeOutput = new StoreOutput();
  123. // 复制PosStore的所有属性
  124. BeanUtils.copyProperties(store, storeOutput);
  125. Boolean dayang = false;
  126. List<OperatingHours> mdHours=hourslist.stream().filter(x-> store.getId().equals(x.getMdId().intValue())).collect(Collectors.toList());
  127. //在营业时间范围内返回true
  128. for (int i = 0; i < mdHours.size(); i++) {
  129. Boolean sbsj = dateUtil.isLegalTime(mdHours.get(i).getStartTime(), mdHours.get(i).getEndTime());
  130. if (sbsj == true) {
  131. dayang = true;
  132. }
  133. }
  134. //不在时间范围内标识为打烊
  135. if (dayang == false) {
  136. storeOutput.setState(1L);
  137. }
  138. // 从分组结果中获取该门店的商品列表(已限制为最多6条)
  139. List<PosFood> foodList = foodMap.getOrDefault(store.getId(), new ArrayList<>());
  140. storeOutput.setFoodList(foodList);
  141. storeOutputList.add(storeOutput);
  142. }
  143. Page<StoreOutput> result = new Page<>(page, 10);
  144. result.setTotal(count);
  145. result.setRecords(storeOutputList);
  146. return success(result);
  147. }
  148. }