PosStoreController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package com.ruoyi.app.mendian;
  2. import cn.hutool.json.JSONObject;
  3. import cn.hutool.json.JSONUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.ruoyi.app.order.dto.OrderCreatItem;
  10. import com.ruoyi.app.order.dto.OrderCreateInput;
  11. import com.ruoyi.app.order.dto.OrderPushBodyDto;
  12. import com.ruoyi.app.utils.DateUtil;
  13. import com.ruoyi.app.utils.ImageCompressUtils;
  14. import com.ruoyi.app.user.dto.StoreOutput;
  15. import com.ruoyi.app.utils.PayPush;
  16. import com.ruoyi.common.annotation.Anonymous;
  17. import com.ruoyi.common.annotation.Log;
  18. import com.ruoyi.common.core.controller.BaseController;
  19. import com.ruoyi.common.core.domain.AjaxResult;
  20. import com.ruoyi.common.core.page.TableDataInfo;
  21. import com.ruoyi.common.enums.BusinessType;
  22. import com.ruoyi.common.exception.ServiceException;
  23. import com.ruoyi.common.utils.MessageUtils;
  24. import com.ruoyi.common.utils.StringUtils;
  25. import com.ruoyi.common.utils.poi.ExcelUtil;
  26. import com.ruoyi.framework.manager.AsyncManager;
  27. import com.ruoyi.system.domain.*;
  28. import com.ruoyi.system.domain.vo.StoreOperatOutput;
  29. import com.ruoyi.system.mapper.PosStoreMapper;
  30. import com.ruoyi.system.mapper.SysConfigMapper;
  31. import com.ruoyi.system.service.*;
  32. import com.ruoyi.system.utils.Auth;
  33. import com.ruoyi.system.utils.JwtUtil;
  34. import io.swagger.models.auth.In;
  35. import org.springframework.beans.BeanUtils;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.security.access.prepost.PreAuthorize;
  38. import org.springframework.transaction.annotation.Transactional;
  39. import org.springframework.web.bind.annotation.*;
  40. import jakarta.servlet.http.HttpServletResponse;
  41. import java.math.BigDecimal;
  42. import java.text.SimpleDateFormat;
  43. import java.util.*;
  44. import java.util.concurrent.atomic.AtomicInteger;
  45. import java.util.stream.Collectors;
  46. /**
  47. * posstoreController
  48. *
  49. * @author ruoyi
  50. * @date 2023-05-14
  51. */
  52. @RestController
  53. @RequestMapping("/chanting/store")
  54. public class PosStoreController extends BaseController {
  55. @Autowired
  56. private IPosStoreService posStoreService;
  57. @Autowired
  58. private PosStoreMapper posStoreMapper;
  59. @Autowired
  60. private IPosCollectService posCollectService;
  61. @Autowired
  62. private IPosFenleiService posFenleiService;
  63. @Autowired
  64. private IInfoUserService infoUserService;
  65. @Autowired
  66. private IOperatingHoursService operatingHoursService;
  67. @Autowired
  68. private PosStoreEnrichService posStoreEnrichService;
  69. @Autowired
  70. private SysConfigMapper sysConfigMapper;
  71. @Autowired
  72. private IOrderParentService orderParentService;
  73. @Autowired
  74. private IPosOrderService posOrderService;
  75. /**
  76. * 根据评分查旬门店
  77. *
  78. * @param longitude
  79. * @param latitude
  80. * @param page
  81. * @param juli
  82. * @param type
  83. * @return
  84. */
  85. @Anonymous
  86. @GetMapping("/getPfStore")
  87. public AjaxResult getPfStore(@RequestParam BigDecimal longitude,
  88. @RequestParam BigDecimal latitude,
  89. @RequestParam Integer page,
  90. @RequestParam Integer juli,
  91. @RequestParam Integer type) {
  92. if (type == 0) {
  93. List<PosStore> list = posStoreMapper.getPingfStore(longitude, latitude, (page - 1) * 10, juli);
  94. return success(list);
  95. } else if (type == 1) {
  96. List<PosStore> list = posStoreMapper.getReduStore(longitude, latitude, (page - 1) * 10, juli);
  97. return success(list);
  98. } else {
  99. List<PosStore> list = posStoreMapper.getNewStore(longitude, latitude, (page - 1) * 10, juli);
  100. return success(list);
  101. }
  102. }
  103. //删除我的门店
  104. @Anonymous
  105. @Auth
  106. @GetMapping("/delemendian")
  107. public AjaxResult delemendian(@RequestParam String id) {
  108. QueryWrapper<PosFenlei> queryWrapper = new QueryWrapper<>();
  109. queryWrapper.eq("mendid", id);
  110. List<PosFenlei> list = posFenleiService.list(queryWrapper);
  111. if (list.size() > 0) {
  112. return error(MessageUtils.message("no.mendian.exist.classify.not.del"));
  113. } else {
  114. return toAjax(posStoreService.deletePosStoreById(Long.valueOf(id)));
  115. }
  116. }
  117. /**
  118. * H5端门店列表
  119. */
  120. @Anonymous
  121. @Auth
  122. @GetMapping("/storelistlist")
  123. public AjaxResult storelistlist(@RequestHeader String token,
  124. @RequestParam Integer page,
  125. @RequestParam Integer size) {
  126. JwtUtil jwtUtil = new JwtUtil();
  127. String id = jwtUtil.getusid(token);
  128. IPage<PosStore> stlist = new Page<>(page, size);
  129. QueryWrapper<PosStore> queryWrapper = new QueryWrapper<>();
  130. // 判断是否为摊位主(type=4),摊位主通过 storeId 关联摊位
  131. InfoUser loginUser = infoUserService.selectInfoUserByUserId(Long.valueOf(id));
  132. if ("4".equals(loginUser.getUserType())) {
  133. if (loginUser.getStoreId() != null) {
  134. queryWrapper.eq("id", loginUser.getStoreId());
  135. } else {
  136. queryWrapper.eq("id", -1); // 无关联摊位,返回空列表
  137. }
  138. } else {
  139. queryWrapper.eq("user_id", id);
  140. }
  141. IPage<PosStore> list = posStoreService.page(stlist, queryWrapper);
  142. if (!list.getRecords().isEmpty()) {
  143. QueryWrapper<OperatingHours> wrapper = new QueryWrapper<>();
  144. wrapper.in("md_id", list.getRecords().stream().map(PosStore::getId).collect(Collectors.toList()));
  145. List<OperatingHours> hourslist = operatingHoursService.list(wrapper);
  146. list.getRecords().forEach(posStore -> {
  147. posStore.setBusinessHours(getStoreOperatOutput(hourslist, Long.valueOf(posStore.getId())));
  148. });
  149. }
  150. return success(list);
  151. }
  152. //查询我的门店列表
  153. @Anonymous
  154. @Auth
  155. @GetMapping("/getmystorelist")
  156. public AjaxResult getmystorelist(@RequestHeader String token) {
  157. JwtUtil jwtUtil = new JwtUtil();
  158. String id = jwtUtil.getusid(token);
  159. QueryWrapper<PosStore> queryWrapper = new QueryWrapper<>();
  160. queryWrapper.eq("user_id", id);
  161. List<PosStore> list = posStoreService.list(queryWrapper);
  162. return success(list);
  163. }
  164. //查询门店列表
  165. @Anonymous
  166. @GetMapping("/getstore")
  167. public AjaxResult getstore(@RequestHeader(defaultValue = "") String token, @RequestParam Integer id) {
  168. List<PosCollect> list = new ArrayList<>();
  169. if (!"".equals(token)) {
  170. JwtUtil jwtUtil = new JwtUtil();
  171. String usid = jwtUtil.getusid(token);
  172. QueryWrapper<PosCollect> queryWrapper = new QueryWrapper<>();
  173. queryWrapper.eq("user_id", usid);
  174. queryWrapper.eq("md_id", id);
  175. list = posCollectService.list(queryWrapper);
  176. }
  177. PosStore posStore = posStoreService.getById(id);
  178. List<PosStore> stores=new ArrayList<>();
  179. stores.add(posStore);
  180. posStoreEnrichService.enrichStoreList(stores);
  181. StoreOutput storeOutput = new StoreOutput();
  182. BeanUtils.copyProperties(stores.getFirst(), storeOutput);
  183. JSONObject json = JSONUtil.parseObj(storeOutput, false);
  184. json.set("collect", !list.isEmpty() ? 1 : 0);
  185. return success(storeOutput);
  186. }
  187. //根据行业分类查询门店列表
  188. @Anonymous
  189. @GetMapping("/getstorelist")
  190. public AjaxResult getstorelist(@RequestParam BigDecimal longitude,
  191. @RequestParam BigDecimal latitude,
  192. @RequestParam Integer page,
  193. @RequestParam String area,
  194. @RequestParam Integer juli,
  195. @RequestParam(defaultValue = "") Integer sort,
  196. @RequestParam(defaultValue = "zh-TW", required = false) String language) {
  197. List<PosStore> list;
  198. if (sort == null) {
  199. list = posStoreMapper.getmdlist(longitude, latitude, (page - 1) * 20, "%" + area + "%", juli);
  200. } else {
  201. list = posStoreMapper.getdaidiq(longitude, latitude, (page - 1) * 20, "%" + area + "%", juli, sort);
  202. }
  203. posStoreEnrichService.enrichStoreList(list);
  204. Page<StoreOutput> result = posStoreEnrichService.buildStoreOutputPage(list, language, page, list.size());
  205. return success(result);
  206. }
  207. //根据服务分类查询门店列表
  208. @Anonymous
  209. @GetMapping("/getserverlist")
  210. public AjaxResult getserverlist(@RequestParam BigDecimal longitude,
  211. @RequestParam BigDecimal latitude,
  212. @RequestParam Integer page,
  213. @RequestParam String area,
  214. @RequestParam Integer juli,
  215. @RequestParam(defaultValue = "") String serverType) {
  216. if (!"".equals(serverType)) {
  217. List<PosStore> list = posStoreMapper.getserverlist(longitude, latitude, (page - 1) * 20, "%" + area + "%", juli, "%" + serverType + "%");
  218. return success(list);
  219. } else {
  220. List<PosStore> list = posStoreMapper.getmdlist(longitude, latitude, (page - 1) * 20, "%" + area + "%", juli);
  221. return success(list);
  222. }
  223. }
  224. //添加门店
  225. @Anonymous
  226. @PostMapping("/addmendian")
  227. @Transactional
  228. public AjaxResult addmendian(@RequestBody PosStore posStore) {
  229. try {
  230. // 1. 先保存门店信息
  231. Boolean org = posStoreService.saveOrUpdate(posStore);
  232. if (!org) {
  233. return error();
  234. }
  235. // 2. 查询保存后的门店信息(使用ID查询,避免锁等待)
  236. PosStore store;
  237. if (posStore.getId() != null) {
  238. store = posStoreService.getById(posStore.getId());
  239. } else {
  240. // 如果是新增,通过名称和用户ID查询
  241. QueryWrapper<PosStore> wrapper = new QueryWrapper<>();
  242. wrapper.eq("pos_name", posStore.getPosName());
  243. wrapper.eq("user_id", posStore.getUserId());
  244. store = posStoreService.getOne(wrapper);
  245. }
  246. if (store == null) {
  247. return error("门店信息保存失败");
  248. }
  249. // 3. 处理营业时间信息
  250. handlerBusinessHours(posStore, store);
  251. return success(MessageUtils.message("no.success"), store);
  252. } catch (Exception e) {
  253. return error("添加门店失败: " + e.getMessage());
  254. }
  255. }
  256. /**
  257. * 更新门店信息处理营业时间
  258. * @param input
  259. * @param store
  260. */
  261. private void handlerBusinessHours(PosStore input, PosStore store) {
  262. if (input.getBusinessHours() != null && !input.getBusinessHours().isEmpty()) {
  263. // 先删除旧的营业时间记录
  264. LambdaQueryWrapper<OperatingHours> query = new LambdaQueryWrapper<>();
  265. query.eq(OperatingHours::getMdId, store.getId());
  266. operatingHoursService.remove(query);
  267. // 批量插入新的营业时间记录
  268. List<OperatingHours> operatingHours = new ArrayList<>();
  269. input.getBusinessHours().forEach(o -> {
  270. o.getTimeSlots().forEach(time -> {
  271. OperatingHours op = new OperatingHours();
  272. op.setStartTime(time.getStartTime());
  273. op.setEndTime(time.getEndTime());
  274. op.setDayOfWeek(time.getDayOfWeek());
  275. op.setIsOpen(o.getIsOpen());
  276. op.setMdId(Long.valueOf(store.getId()));
  277. operatingHours.add(op);
  278. });
  279. });
  280. if (!operatingHours.isEmpty()) {
  281. operatingHoursService.saveBatch(operatingHours);
  282. }
  283. }
  284. }
  285. /**
  286. * (商家端app使用)获取门店营业时间
  287. * @param mdId 门店id
  288. * @return
  289. */
  290. @Anonymous
  291. @Auth
  292. @GetMapping("/getMdBusinessHours")
  293. public AjaxResult getMdBusinessHours(@RequestParam Long mdId){
  294. LambdaQueryWrapper<OperatingHours> query=new LambdaQueryWrapper<>();
  295. query.eq(OperatingHours::getMdId,mdId);
  296. List<OperatingHours> list = operatingHoursService.list(query);
  297. List<StoreOperatOutput> data = new ArrayList<>();
  298. if(!list.isEmpty()){
  299. data = getStoreOperatOutput(list,mdId);
  300. }
  301. return success(data);
  302. }
  303. /**
  304. * 商家端app更新门店营业时间
  305. * @param businessHours
  306. * @return
  307. */
  308. @Anonymous
  309. @Auth
  310. @PostMapping("/saveMdBusinessHours")
  311. public AjaxResult saveMdBusinessHours(@RequestBody List<StoreOperatOutput> businessHours) {
  312. if(businessHours == null || businessHours.isEmpty()) {
  313. throw new ServiceException(MessageUtils.message("no.mendian.not.set.business.hours"));
  314. }
  315. Long mdId = businessHours.get(0).getMdId();
  316. PosStore store = posStoreService.getById(businessHours.get(0).getMdId());
  317. // 先删除旧的营业时间记录
  318. LambdaQueryWrapper<OperatingHours> query = new LambdaQueryWrapper<>();
  319. query.eq(OperatingHours::getMdId, store.getId());
  320. operatingHoursService.remove(query);
  321. // 批量插入新的营业时间记录
  322. List<OperatingHours> operatingHours = new ArrayList<>();
  323. businessHours.forEach(o -> {
  324. o.getTimeSlots().forEach(time -> {
  325. OperatingHours op = new OperatingHours();
  326. op.setStartTime(time.getStartTime());
  327. op.setEndTime(time.getEndTime());
  328. op.setDayOfWeek(time.getDayOfWeek());
  329. op.setIsOpen(o.getIsOpen());
  330. op.setMdId(Long.valueOf(store.getId()));
  331. operatingHours.add(op);
  332. });
  333. });
  334. if(!operatingHours.isEmpty()) {
  335. operatingHoursService.saveBatch(operatingHours);
  336. }
  337. return success();
  338. }
  339. //返回营业时间(按星期分组)
  340. private List<StoreOperatOutput> getStoreOperatOutput(List<OperatingHours> list, Long mdId){
  341. List<StoreOperatOutput> data= new ArrayList<>();
  342. list=list.stream().filter(x-> x.getMdId().equals(mdId)).collect(Collectors.toList());
  343. if(!list.isEmpty()){
  344. Map<Integer,List<OperatingHours>> map = list.stream().collect(Collectors.groupingBy(OperatingHours::getDayOfWeek));
  345. map.forEach((k,v)->{
  346. data.add(new StoreOperatOutput(){
  347. {
  348. setMdId(mdId);
  349. setDayOfWeek(k);
  350. setIsOpen(v.get(0).getIsOpen());
  351. setTimeSlots(v);
  352. }
  353. });
  354. });
  355. }
  356. return data;
  357. }
  358. /**
  359. * 查询posstore列表
  360. */
  361. @PreAuthorize("@ss.hasPermi('chanting:store:list')")
  362. // @Anonymous
  363. @GetMapping("/list")
  364. public TableDataInfo list(PosStore posStore) {
  365. startPage();
  366. List<PosStore> list = posStoreService.selectPosStoreList(posStore);
  367. for (PosStore store : list) {
  368. InfoUser user = infoUserService.getById(store.getUserId());
  369. store.setUserName(user.getUserName());
  370. }
  371. return getDataTable(list);
  372. }
  373. /**
  374. * 导出posstore列表
  375. */
  376. @PreAuthorize("@ss.hasPermi('chanting:store:export')")
  377. @Log(title = "posstore", businessType = BusinessType.EXPORT)
  378. @PostMapping("/export")
  379. public void export(HttpServletResponse response, PosStore posStore) {
  380. List<PosStore> list = posStoreService.selectPosStoreList(posStore);
  381. ExcelUtil<PosStore> util = new ExcelUtil<PosStore>(PosStore.class);
  382. util.exportExcel(response, list, MessageUtils.message("no.export.excel.posstore"));
  383. }
  384. /**
  385. * 获取posstore详细信息
  386. */
  387. @PreAuthorize("@ss.hasPermi('chanting:store:query')")
  388. @GetMapping(value = "/{id}")
  389. public AjaxResult getInfo(@PathVariable("id") Long id) {
  390. return success(posStoreService.selectPosStoreById(id));
  391. }
  392. /**
  393. * 新增posstore
  394. */
  395. @PreAuthorize("@ss.hasPermi('chanting:store:add')")
  396. @Log(title = "posstore", businessType = BusinessType.INSERT)
  397. @PostMapping
  398. public AjaxResult add(@RequestBody PosStore posStore) {
  399. return toAjax(posStoreService.insertPosStore(posStore));
  400. }
  401. /**
  402. * 修改posstore
  403. */
  404. @PreAuthorize("@ss.hasPermi('chanting:store:edit')")
  405. @Log(title = "posstore", businessType = BusinessType.UPDATE)
  406. @PutMapping
  407. public AjaxResult edit(@RequestBody PosStore posStore) {
  408. return toAjax(posStoreService.updatePosStore(posStore));
  409. }
  410. /**
  411. * 修改posstore下架状态
  412. */
  413. @Anonymous
  414. @PutMapping("/changeOffShelf")
  415. public AjaxResult changeOffShelf(@RequestBody PosStore posStore) {
  416. Boolean res = posStoreService.saveOrUpdate(posStore);
  417. if (res) {
  418. return success();
  419. } else {
  420. return error();
  421. }
  422. }
  423. /**
  424. * 删除posstore
  425. */
  426. @PreAuthorize("@ss.hasPermi('chanting:store:remove')")
  427. @Log(title = "posstore", businessType = BusinessType.DELETE)
  428. @DeleteMapping("/{ids}")
  429. public AjaxResult remove(@PathVariable Long[] ids) {
  430. return toAjax(posStoreService.deletePosStoreByIds(ids));
  431. }
  432. /**
  433. * 压缩图片店铺的商品图片
  434. */
  435. @Anonymous
  436. @GetMapping("/compressStoreImage")
  437. public AjaxResult compressStoreImage(@RequestParam String idsStr) {
  438. LambdaQueryWrapper<PosStore> queryWrapper = new LambdaQueryWrapper<>();
  439. List<Integer> ids = new ArrayList<>();
  440. if (!StringUtils.isEmpty(idsStr)) {
  441. ids = Arrays.stream(idsStr.split(","))
  442. .map(Integer::valueOf)
  443. .collect(Collectors.toList());
  444. }
  445. queryWrapper.in(!ids.isEmpty(), PosStore::getId, ids);
  446. List<PosStore> list = posStoreService.list(queryWrapper);
  447. AtomicInteger count = new AtomicInteger();
  448. list.forEach(food -> {
  449. String compressImage = ImageCompressUtils.compressImageByWebPath(food.getImage());
  450. if (!compressImage.equals(food.getImage())) {
  451. food.setImage(compressImage);
  452. PosStore posStore = new PosStore();
  453. posStore.setId(food.getId());
  454. posStore.setImage(compressImage);
  455. posStoreService.saveOrUpdate(posStore);
  456. count.getAndIncrement();
  457. System.out.println("store成功压缩的图片数量:" + count.get());
  458. logger.info("store成功压缩的图片数量:" + count.get());
  459. }
  460. });
  461. return success("成功压缩的图片数量:" + count.get());
  462. }
  463. }