PosStoreController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package com.ruoyi.app.mendian;
  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.ruoyi.app.utils.DateUtil;
  8. import com.ruoyi.app.utils.ImageCompressUtils;
  9. import com.ruoyi.common.annotation.Anonymous;
  10. import com.ruoyi.common.annotation.Log;
  11. import com.ruoyi.common.core.controller.BaseController;
  12. import com.ruoyi.common.core.domain.AjaxResult;
  13. import com.ruoyi.common.core.page.TableDataInfo;
  14. import com.ruoyi.common.enums.BusinessType;
  15. import com.ruoyi.common.utils.MessageUtils;
  16. import com.ruoyi.common.utils.StringUtils;
  17. import com.ruoyi.common.utils.poi.ExcelUtil;
  18. import com.ruoyi.system.domain.*;
  19. import com.ruoyi.system.mapper.PosStoreMapper;
  20. import com.ruoyi.system.mapper.SysConfigMapper;
  21. import com.ruoyi.system.service.*;
  22. import com.ruoyi.system.utils.Auth;
  23. import com.ruoyi.system.utils.JwtUtil;
  24. import io.swagger.models.auth.In;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.security.access.prepost.PreAuthorize;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import org.springframework.web.bind.annotation.*;
  29. import jakarta.servlet.http.HttpServletResponse;
  30. import java.math.BigDecimal;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Collections;
  36. import java.util.concurrent.atomic.AtomicInteger;
  37. import java.util.stream.Collectors;
  38. /**
  39. * posstoreController
  40. *
  41. * @author ruoyi
  42. * @date 2023-05-14
  43. */
  44. @RestController
  45. @RequestMapping("/chanting/store")
  46. public class PosStoreController extends BaseController {
  47. @Autowired
  48. private IPosStoreService posStoreService;
  49. @Autowired
  50. private PosStoreMapper posStoreMapper;
  51. @Autowired
  52. private IPosCollectService posCollectService;
  53. @Autowired
  54. private IPosFenleiService posFenleiService;
  55. @Autowired
  56. private IInfoUserService infoUserService;
  57. @Autowired
  58. private IPosReviewService posReviewService;
  59. @Autowired
  60. private IOperatingHoursService operatingHoursService;
  61. @Autowired
  62. private IPosOrderService posOrderService;
  63. @Autowired
  64. private SysConfigMapper sysConfigMapper;
  65. /**
  66. * 根据评分查旬门店
  67. *
  68. * @param longitude
  69. * @param latitude
  70. * @param page
  71. * @param juli
  72. * @param type
  73. * @return
  74. */
  75. @Anonymous
  76. @GetMapping("/getPfStore")
  77. public AjaxResult getPfStore(@RequestParam BigDecimal longitude,
  78. @RequestParam BigDecimal latitude,
  79. @RequestParam Integer page,
  80. @RequestParam Integer juli,
  81. @RequestParam Integer type) {
  82. if (type == 0) {
  83. List<PosStore> list = posStoreMapper.getPingfStore(longitude, latitude, (page - 1) * 10, juli);
  84. return success(list);
  85. } else if (type == 1) {
  86. List<PosStore> list = posStoreMapper.getReduStore(longitude, latitude, (page - 1) * 10, juli);
  87. return success(list);
  88. } else {
  89. List<PosStore> list = posStoreMapper.getNewStore(longitude, latitude, (page - 1) * 10, juli);
  90. return success(list);
  91. }
  92. }
  93. //删除我的门店
  94. @Anonymous
  95. @Auth
  96. @GetMapping("/delemendian")
  97. public AjaxResult delemendian(@RequestParam String id) {
  98. QueryWrapper<PosFenlei> queryWrapper = new QueryWrapper<>();
  99. queryWrapper.eq("mendid", id);
  100. List<PosFenlei> list = posFenleiService.list(queryWrapper);
  101. if (list.size() > 0) {
  102. return error(MessageUtils.message("no.mendian.exist.classify.not.del"));
  103. } else {
  104. return toAjax(posStoreService.deletePosStoreById(Long.valueOf(id)));
  105. }
  106. }
  107. /**
  108. * H5端门店列表
  109. */
  110. @Anonymous
  111. @Auth
  112. @GetMapping("/storelistlist")
  113. public AjaxResult storelistlist(@RequestHeader String token,
  114. @RequestParam Integer page,
  115. @RequestParam Integer size) {
  116. JwtUtil jwtUtil = new JwtUtil();
  117. String id = jwtUtil.getusid(token);
  118. IPage<PosStore> stlist = new Page<>(page, size);
  119. QueryWrapper<PosStore> queryWrapper = new QueryWrapper<>();
  120. queryWrapper.eq("user_id", id);
  121. IPage<PosStore> list = posStoreService.page(stlist, queryWrapper);
  122. if (!list.getRecords().isEmpty()) {
  123. QueryWrapper<OperatingHours> wrapper = new QueryWrapper<>();
  124. wrapper.in("md_id", list.getRecords().stream().map(PosStore::getId).collect(Collectors.toList()));
  125. List<OperatingHours> hourslist = operatingHoursService.list(wrapper);
  126. list.getRecords().forEach(posStore -> {
  127. posStore.setBusinessHours(hourslist);
  128. });
  129. }
  130. return success(list);
  131. }
  132. //查询我的门店列表
  133. @Anonymous
  134. @Auth
  135. @GetMapping("/getmystorelist")
  136. public AjaxResult getmystorelist(@RequestHeader String token) {
  137. JwtUtil jwtUtil = new JwtUtil();
  138. String id = jwtUtil.getusid(token);
  139. QueryWrapper<PosStore> queryWrapper = new QueryWrapper<>();
  140. queryWrapper.eq("user_id", id);
  141. List<PosStore> list = posStoreService.list(queryWrapper);
  142. return success(list);
  143. }
  144. //查询门店列表
  145. @Anonymous
  146. @GetMapping("/getstore")
  147. public AjaxResult getstore(@RequestHeader(defaultValue = "") String token, @RequestParam Integer id) {
  148. if (!"".equals(token)) {
  149. JwtUtil jwtUtil = new JwtUtil();
  150. String usid = jwtUtil.getusid(token);
  151. QueryWrapper<PosCollect> queryWrapper = new QueryWrapper<>();
  152. queryWrapper.eq("user_id", usid);
  153. queryWrapper.eq("md_id", id);
  154. List<PosCollect> list = posCollectService.list(queryWrapper);
  155. PosStore posStore = posStoreService.getById(id);
  156. JSONObject org = new JSONObject();
  157. org.put("id", posStore.getId());
  158. org.put("posName", posStore.getPosName());
  159. org.put("image", posStore.getImage());
  160. org.put("posPrice", posStore.getPosPrice());
  161. org.put("area", posStore.getArea());
  162. org.put("address", posStore.getAddress());
  163. org.put("longitude", posStore.getLongitude());
  164. org.put("latitude", posStore.getLatitude());
  165. org.put("briefIntroduction", posStore.getBriefIntroduction());
  166. org.put("type", posStore.getType());
  167. org.put("hygienePermit", posStore.getHygienePermit());
  168. org.put("hygieneLicence", posStore.getHygieneLicence());
  169. org.put("openBusiness", posStore.getOpenBusiness());
  170. org.put("windingUp", posStore.getWindingUp());
  171. org.put("sort", posStore.getSort());
  172. org.put("userId", posStore.getUserId());
  173. org.put("juli", posStore.getJuli());
  174. org.put("cretim", posStore.getCretim());
  175. org.put("zhitsj", posStore.getJuli());
  176. org.put("tgpaixv", posStore.getCretim());
  177. org.put("state", posStore.getState());
  178. org.put("serverType", posStore.getServerType());
  179. org.put("telephone", posStore.getTelephone());
  180. org.put("Collect", !list.isEmpty() ? 1 : 0);
  181. org.put("logo", posStore.getLogo());
  182. org.put("isNightMarket", posStore.getIsNightMarket());
  183. org.put("nightMarketId", posStore.getNightMarketId());
  184. QueryWrapper<OperatingHours> wrapper = new QueryWrapper<>();
  185. wrapper.eq("md_id", posStore.getId());
  186. List<OperatingHours> hourslist = operatingHoursService.list(wrapper);
  187. DateUtil dateUtil = new DateUtil();
  188. Boolean dayang = false;
  189. //在营业时间范围内返回true
  190. for (int i = 0; i < hourslist.size(); i++) {
  191. Boolean sbsj = dateUtil.isLegalTime(hourslist.get(i).getStartTime(), hourslist.get(i).getEndTime());
  192. if (sbsj == true) {
  193. dayang = true;
  194. }
  195. }
  196. //不在时间范围内标识为打烊
  197. if (dayang == false) {
  198. org.put("state", 1);
  199. }
  200. org.put("businessHours", hourslist);
  201. return success(org);
  202. } else {
  203. PosStore posStore = posStoreService.getById(id);
  204. return success(posStore);
  205. }
  206. }
  207. //根据行业分类查询门店列表
  208. @Anonymous
  209. @GetMapping("/getstorelist")
  210. public AjaxResult getstorelist(@RequestParam BigDecimal longitude,
  211. @RequestParam BigDecimal latitude,
  212. @RequestParam Integer page,
  213. @RequestParam String area,
  214. @RequestParam Integer juli,
  215. @RequestParam(defaultValue = "") Integer sort) {
  216. List<PosStore> list;
  217. if (sort == null) {
  218. list = posStoreMapper.getmdlist(longitude, latitude, (page - 1) * 20, "%" + area + "%", juli);
  219. } else {
  220. list = posStoreMapper.getdaidiq(longitude, latitude, (page - 1) * 20, "%" + area + "%", juli, sort);
  221. }
  222. // 为门店列表设置评分与月售订单数量
  223. if (!list.isEmpty()) {
  224. List<Long> mdIds = list.stream()
  225. .map(store -> store.getId() == null ? null : store.getId().longValue())
  226. .filter(idVal -> idVal != null)
  227. .collect(Collectors.toList());
  228. Map<Long, List<PosReview>> reviewMap = Collections.emptyMap();
  229. Map<Long, Long> orderCountMap = Collections.emptyMap();
  230. List<OperatingHours> hourslist=new ArrayList<>();
  231. if (!mdIds.isEmpty()) {
  232. // 1. 计算各门店评分
  233. QueryWrapper<PosReview> reviewWrapper = new QueryWrapper<>();
  234. reviewWrapper.in("md_id", mdIds);
  235. List<PosReview> reviews = posReviewService.list(reviewWrapper);
  236. reviewMap = reviews.stream()
  237. .collect(Collectors.groupingBy(PosReview::getMdId));
  238. // 2. 统计各门店本月订单数(按 cretim 当前月份)
  239. QueryWrapper<PosOrder> orderWrapper = new QueryWrapper<>();
  240. orderWrapper.in("md_id", mdIds);
  241. orderWrapper.apply("DATE_FORMAT(cretim,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m')");
  242. List<PosOrder> orders = posOrderService.list(orderWrapper);
  243. orderCountMap = orders.stream()
  244. .collect(Collectors.groupingBy(PosOrder::getMdId, Collectors.counting()));
  245. QueryWrapper<OperatingHours> wrapper = new QueryWrapper<>();
  246. wrapper.in("md_id", mdIds);
  247. hourslist = operatingHoursService.list(wrapper);
  248. }
  249. DateUtil dateUtil = new DateUtil();
  250. // 统一循环设置评分与月售(mdIds 为空时使用默认值)
  251. for (PosStore store : list) {
  252. Long mdId = store.getId() == null ? null : store.getId().longValue();
  253. List<PosReview> storeReviews = (mdId == null || reviewMap.isEmpty()) ? null : reviewMap.get(mdId);
  254. double score;
  255. if (storeReviews == null || storeReviews.isEmpty()) {
  256. score = 4.5d;
  257. } else {
  258. double avg = storeReviews.stream()
  259. .mapToLong(PosReview::getScore)
  260. .average()
  261. .orElse(4.5d);
  262. // 平均分按 0.5 分粒度四舍五入,例如 3, 3.5, 4, 4.5
  263. score = Math.round(avg * 2.0d) / 2.0d;
  264. }
  265. store.setPingf(score);
  266. // 设置本月售出订单数量(ddsl)
  267. long monthCount = (mdId == null || orderCountMap.isEmpty()) ? 0L : orderCountMap.getOrDefault(mdId, 0L);
  268. store.setDdsl(monthCount);
  269. Boolean dayang = false;
  270. List<OperatingHours> mdHours=hourslist.stream().filter(x-> store.getId().equals(x.getMdId().intValue())).collect(Collectors.toList());
  271. //在营业时间范围内返回true
  272. for (int i = 0; i < mdHours.size(); i++) {
  273. Boolean sbsj = dateUtil.isLegalTime(mdHours.get(i).getStartTime(), mdHours.get(i).getEndTime());
  274. if (sbsj == true) {
  275. dayang = true;
  276. }
  277. }
  278. //不在时间范围内标识为打烊
  279. if (dayang == false) {
  280. store.setState(1L);
  281. }
  282. }
  283. }
  284. return success(list);
  285. }
  286. //根据服务分类查询门店列表
  287. @Anonymous
  288. @GetMapping("/getserverlist")
  289. public AjaxResult getserverlist(@RequestParam BigDecimal longitude,
  290. @RequestParam BigDecimal latitude,
  291. @RequestParam Integer page,
  292. @RequestParam String area,
  293. @RequestParam Integer juli,
  294. @RequestParam(defaultValue = "") String serverType) {
  295. if (!"".equals(serverType)) {
  296. List<PosStore> list = posStoreMapper.getserverlist(longitude, latitude, (page - 1) * 20, "%" + area + "%", juli, "%" + serverType + "%");
  297. return success(list);
  298. } else {
  299. List<PosStore> list = posStoreMapper.getmdlist(longitude, latitude, (page - 1) * 20, "%" + area + "%", juli);
  300. return success(list);
  301. }
  302. }
  303. //添加门店
  304. @Anonymous
  305. @PostMapping("/addmendian")
  306. @Transactional
  307. public AjaxResult addmendian(@RequestBody PosStore posStore) {
  308. Boolean org = posStoreService.saveOrUpdate(posStore);
  309. if (org) {
  310. QueryWrapper<PosStore> wrapper = new QueryWrapper<>();
  311. wrapper.eq("pos_name", posStore.getPosName());
  312. wrapper.eq("user_id", posStore.getUserId());
  313. PosStore store = posStoreService.getOne(wrapper);
  314. if (posStore.getBusinessHours() != null && posStore.getBusinessHours().size() > 0) {
  315. LambdaQueryWrapper<OperatingHours> query = new LambdaQueryWrapper<>();
  316. query.eq(OperatingHours::getMdId, Long.valueOf(store.getId()));
  317. operatingHoursService.remove(query);
  318. posStore.getBusinessHours().forEach(operatingHours -> {
  319. operatingHours.setMdId(Long.valueOf(store.getId()));
  320. });
  321. operatingHoursService.saveBatch(posStore.getBusinessHours());
  322. }
  323. return success(MessageUtils.message("no.success"), store);
  324. } else {
  325. return error();
  326. }
  327. }
  328. /**
  329. * 查询posstore列表
  330. */
  331. @PreAuthorize("@ss.hasPermi('chanting:store:list')")
  332. // @Anonymous
  333. @GetMapping("/list")
  334. public TableDataInfo list(PosStore posStore) {
  335. startPage();
  336. List<PosStore> list = posStoreService.selectPosStoreList(posStore);
  337. for (PosStore store : list) {
  338. InfoUser user = infoUserService.getById(store.getUserId());
  339. store.setUserName(user.getUserName());
  340. }
  341. return getDataTable(list);
  342. }
  343. /**
  344. * 导出posstore列表
  345. */
  346. @PreAuthorize("@ss.hasPermi('chanting:store:export')")
  347. @Log(title = "posstore", businessType = BusinessType.EXPORT)
  348. @PostMapping("/export")
  349. public void export(HttpServletResponse response, PosStore posStore) {
  350. List<PosStore> list = posStoreService.selectPosStoreList(posStore);
  351. ExcelUtil<PosStore> util = new ExcelUtil<PosStore>(PosStore.class);
  352. util.exportExcel(response, list, MessageUtils.message("no.export.excel.posstore"));
  353. }
  354. /**
  355. * 获取posstore详细信息
  356. */
  357. @PreAuthorize("@ss.hasPermi('chanting:store:query')")
  358. @GetMapping(value = "/{id}")
  359. public AjaxResult getInfo(@PathVariable("id") Long id) {
  360. return success(posStoreService.selectPosStoreById(id));
  361. }
  362. /**
  363. * 新增posstore
  364. */
  365. @PreAuthorize("@ss.hasPermi('chanting:store:add')")
  366. @Log(title = "posstore", businessType = BusinessType.INSERT)
  367. @PostMapping
  368. public AjaxResult add(@RequestBody PosStore posStore) {
  369. return toAjax(posStoreService.insertPosStore(posStore));
  370. }
  371. /**
  372. * 修改posstore
  373. */
  374. @PreAuthorize("@ss.hasPermi('chanting:store:edit')")
  375. @Log(title = "posstore", businessType = BusinessType.UPDATE)
  376. @PutMapping
  377. public AjaxResult edit(@RequestBody PosStore posStore) {
  378. return toAjax(posStoreService.updatePosStore(posStore));
  379. }
  380. /**
  381. * 修改posstore下架状态
  382. */
  383. @Anonymous
  384. @PutMapping("/changeOffShelf")
  385. public AjaxResult changeOffShelf(@RequestBody PosStore posStore) {
  386. Boolean res = posStoreService.saveOrUpdate(posStore);
  387. if (res) {
  388. return success();
  389. } else {
  390. return error();
  391. }
  392. }
  393. /**
  394. * 删除posstore
  395. */
  396. @PreAuthorize("@ss.hasPermi('chanting:store:remove')")
  397. @Log(title = "posstore", businessType = BusinessType.DELETE)
  398. @DeleteMapping("/{ids}")
  399. public AjaxResult remove(@PathVariable Long[] ids) {
  400. return toAjax(posStoreService.deletePosStoreByIds(ids));
  401. }
  402. /**
  403. * 压缩图片店铺的商品图片
  404. */
  405. @Anonymous
  406. @GetMapping("/compressStoreImage")
  407. public AjaxResult compressStoreImage(@RequestParam String idsStr) {
  408. LambdaQueryWrapper<PosStore> queryWrapper = new LambdaQueryWrapper<>();
  409. List<Integer> ids = new ArrayList<>();
  410. if (!StringUtils.isEmpty(idsStr)) {
  411. ids = Arrays.stream(idsStr.split(","))
  412. .map(Integer::valueOf)
  413. .collect(Collectors.toList());
  414. }
  415. queryWrapper.in(!ids.isEmpty(), PosStore::getId, ids);
  416. List<PosStore> list = posStoreService.list(queryWrapper);
  417. AtomicInteger count = new AtomicInteger();
  418. list.forEach(food -> {
  419. String compressImage = ImageCompressUtils.compressImageByWebPath(food.getImage());
  420. if (!compressImage.equals(food.getImage())) {
  421. food.setImage(compressImage);
  422. PosStore posStore = new PosStore();
  423. posStore.setId(food.getId());
  424. posStore.setImage(compressImage);
  425. posStoreService.saveOrUpdate(posStore);
  426. count.getAndIncrement();
  427. System.out.println("store成功压缩的图片数量:" + count.get());
  428. logger.info("store成功压缩的图片数量:" + count.get());
  429. }
  430. });
  431. return success("成功压缩的图片数量:" + count.get());
  432. }
  433. }