| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799 |
- package com.ruoyi.app.mendian;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- 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.order.OrderInvoiceService;
- import com.ruoyi.app.utils.ImageCompressUtils;
- import com.ruoyi.common.annotation.Anonymous;
- import com.ruoyi.common.annotation.Log;
- import com.ruoyi.common.core.controller.BaseController;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.core.page.TableDataInfo;
- 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.*;
- import com.ruoyi.system.dto.menu.StoreMenuCopyExecuteRequest;
- import com.ruoyi.system.dto.menu.StoreMenuCopyPreviewRequest;
- import com.ruoyi.system.mapper.PosStoreMapper;
- import com.ruoyi.system.service.*;
- import com.ruoyi.system.utils.Auth;
- import com.ruoyi.system.utils.AuthContext;
- import com.ruoyi.system.utils.JwtUtil;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.web.bind.annotation.*;
- import jakarta.servlet.http.HttpServletResponse;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.stream.Collectors;
- /**
- * foodController
- *
- * @author ruoyi
- * @date 2023-05-21
- */
- @RestController
- @RequestMapping("/chanting/food")
- public class PosFoodController extends BaseController {
- @Autowired //商品
- private IPosFoodService posFoodService;
- @Autowired //门店
- private IPosStoreService posStoreService;
- @Autowired //商品分类
- private IPosFenleiService posFenleiService;
- @Autowired //用户
- private IInfoUserService infoUserService;
- @Autowired //店铺Mapper
- private PosStoreMapper posStoreMapper;
- @Autowired
- private IPosReviewService posReviewService;
- @Autowired
- private IOperatingHoursService operatingHoursService;
- @Autowired
- private IPosOrderService posOrderService;
- @Autowired
- private PosStoreEnrichService posStoreEnrichService;
- @Autowired //规格组
- private IFoodSpecsService foodSpecsService;
- @Autowired //规格值
- private IFoodSpecsValueService foodSpecsValueService;
- @Autowired //商品-规格关联
- private IFoodSpecRelationService foodSpecRelationService;
- @Autowired //订单电子发票(门店是否能开票)
- private OrderInvoiceService orderInvoiceService;
- @Autowired
- private MerchantStoreAccessService merchantStoreAccessService;
- @Autowired
- private StoreMenuCopyService storeMenuCopyService;
- //删除商品
- @Anonymous
- @Auth(session = true)
- @GetMapping("/delefood")
- public AjaxResult delefood(@RequestHeader String token, @RequestParam String id) {
- merchantStoreAccessService.requireFoodAccess(
- AuthContext.requireUserId(), Long.valueOf(id));
- return toAjax(posFoodService.deletePosFoodById(Long.valueOf(id)));
- }
- //推荐商品
- @Anonymous
- @Auth(session = true)
- @PutMapping("/changerecommend")
- public AjaxResult changerecommend(@RequestHeader String token, @RequestBody PosFood posFood) {
- PosFood existing = merchantStoreAccessService.requireFoodAccess(
- AuthContext.requireUserId(), posFood.getId());
- posFood.setMdid(existing.getMdid());
- return toAjax(posFoodService.saveOrUpdate(posFood));
- }
- /**
- * 添加或修改商品
- */
- @Anonymous
- @Auth(session = true)
- @PostMapping("/setposfood")
- public AjaxResult setposfood(@RequestHeader String token, @RequestBody PosFood posFood) {
- Long userId = AuthContext.requireUserId();
- if (posFood.getId() == null) {
- merchantStoreAccessService.requireStoreAccess(userId, posFood.getMdid());
- } else {
- PosFood existing = merchantStoreAccessService.requireFoodAccess(userId, posFood.getId());
- posFood.setMdid(existing.getMdid());
- }
- Boolean org = posFoodService.saveOrUpdate(posFood);
- if (!org) {
- return error();
- }
- handleFoodSpec(posFood);
- return success();
- }
- @Anonymous
- @Auth(session = true)
- @PostMapping("/menu-copy/preview")
- public AjaxResult menuCopyPreview(@RequestHeader String token,
- @RequestBody StoreMenuCopyPreviewRequest request) {
- return success(storeMenuCopyService.preview(AuthContext.requireUserId(), request));
- }
- @Anonymous
- @Auth(session = true)
- @PostMapping("/menu-copy/execute")
- public AjaxResult menuCopyExecute(@RequestHeader String token,
- @RequestBody StoreMenuCopyExecuteRequest request) {
- return success(storeMenuCopyService.execute(AuthContext.requireUserId(), request));
- }
- /**
- * (h5页面使用)通过分类查询商品列表
- */
- @Anonymous
- @GetMapping("/h5Getidlist")
- public AjaxResult h5Getidlist(@RequestParam Integer id, @RequestParam(defaultValue = "") String language, @RequestParam(required = false) String stackingUp) {
- JwtUtil jwtUtil = new JwtUtil();
- // String ids = jwtUtil.getusid(token);
- // InfoUser user = infoUserService.getById(ids);
- QueryWrapper<PosFood> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("fl_id", id);
- // if(!user.getUserType().equals("1")){
- // queryWrapper.eq("to_examine","1");
- // }
- if (stackingUp != null && !"".equals(stackingUp)) {
- queryWrapper.eq("stacking_up", stackingUp);
- }
- String lang = "3";
- if (!"".equals(language)) {
- lang = language;
- }
- queryWrapper.eq("language", lang);
- List<PosFood> list = posFoodService.list(queryWrapper);
- List<Long> __foodIds = list.stream().map(PosFood::getId).collect(Collectors.toList());
- Map<Long, List<FoodSpecs>> __specMap = loadFoodSpecsMap(__foodIds);
- JSONArray all = new JSONArray();
- for (int i = 0; i < list.size(); i++) {
- JSONObject org = new JSONObject();
- org.put("id", list.get(i).getId());
- org.put("fenlei", posFenleiService.getById(list.get(i).getFlId()));
- org.put("store", posStoreService.getById(list.get(i).getMdid()));
- org.put("name", list.get(i).getName());
- org.put("image", list.get(i).getImage());
- org.put("price", list.get(i).getPrice());
- org.put("introduce", list.get(i).getIntroduce());
- org.put("recommend", list.get(i).getRecommend());
- List<FoodSpecs> __fs = __specMap.getOrDefault(list.get(i).getId(), new ArrayList<>());
- org.put("foodSpecs", __fs);
- org.put("foodSku", __fs.isEmpty() ? JSONArray.parseArray(list.get(i).getFoodSku()) : buildFoodSkuArray(__fs));
- org.put("stackingUp", list.get(i).getStackingUp());
- org.put("toExamine", list.get(i).getToExamine());
- all.add(org);
- }
- return success(all);
- }
- /**
- * 通过分类查询商品列表
- */
- @Anonymous
- @Auth(session = true)
- @GetMapping("/getidlist")
- public AjaxResult getidlist(@RequestHeader String token, @RequestParam Integer id, @RequestParam(defaultValue = "") String language, @RequestParam(required = false) String stackingUp, @RequestParam(required = false) String name) {
- Long userId = AuthContext.requireUserId();
- InfoUser user = infoUserService.getById(userId);
- PosFenlei category = posFenleiService.getById(id);
- if (category != null && ("1".equals(user.getUserType()) || "5".equals(user.getUserType()))) {
- merchantStoreAccessService.requireStoreAccess(userId, category.getMendid());
- }
- QueryWrapper<PosFood> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("fl_id", id);
- //普通用户显示审核通过的
- if("0".equals(user.getUserType())){
- queryWrapper.eq("to_examine", "1");
- }
- queryWrapper.ne("stacking_up", "1");
- if (stackingUp != null && !"".equals(stackingUp)) {
- queryWrapper.eq("stacking_up", stackingUp);
- }
- String lang = "3";
- if (!"".equals(language)) {
- lang = language;
- }
- queryWrapper.eq("language", lang);
- if (name != null && !"".equals(name)) {
- queryWrapper.apply(" BINARY name LIKE CONCAT('%', {0}, '%')", name);
- }
- List<PosFood> list = posFoodService.list(queryWrapper);
- List<Long> __foodIds = list.stream().map(PosFood::getId).collect(Collectors.toList());
- Map<Long, List<FoodSpecs>> __specMap = loadFoodSpecsMap(__foodIds);
- JSONArray all = new JSONArray();
- for (int i = 0; i < list.size(); i++) {
- JSONObject org = new JSONObject();
- org.put("id", list.get(i).getId());
- org.put("fenlei", posFenleiService.getById(list.get(i).getFlId()));
- PosStore store = posStoreService.getById(list.get(i).getMdid());
- fillStallLocation(store);
- org.put("store", store);
- org.put("canInvoice", orderInvoiceService.canInvoice(list.get(i).getMdid()));
- org.put("name", list.get(i).getName());
- org.put("image", list.get(i).getImage());
- org.put("price", list.get(i).getPrice());
- org.put("introduce", list.get(i).getIntroduce());
- org.put("recommend", list.get(i).getRecommend());
- List<FoodSpecs> __fs = __specMap.getOrDefault(list.get(i).getId(), new ArrayList<>());
- org.put("foodSpecs", __fs);
- org.put("foodSku", __fs.isEmpty() ? JSONArray.parseArray(list.get(i).getFoodSku()) : buildFoodSkuArray(__fs));
- org.put("stackingUp", list.get(i).getStackingUp());
- org.put("toExamine", list.get(i).getToExamine());
- all.add(org);
- }
- return success(all);
- }
- /**
- * 查询商品详情
- */
- @Anonymous
- @GetMapping("/getfood")
- public AjaxResult getfood(@RequestParam Integer id) {
- JSONObject org = new JSONObject();
- PosFood posFood = posFoodService.getById(id);
- List<Long> __foodIds = new ArrayList<>();
- __foodIds.add(posFood.getId());
- Map<Long, List<FoodSpecs>> __specMap = loadFoodSpecsMap(__foodIds);
- List<FoodSpecs> __fs = __specMap.getOrDefault(posFood.getId(), new ArrayList<>());
- org.put("id", posFood.getId());
- org.put("fenlei", posFenleiService.getById(posFood.getFlId()));
- PosStore store = posStoreService.getById(posFood.getMdid());
- fillStallLocation(store);
- org.put("store", store);
- org.put("canInvoice", orderInvoiceService.canInvoice(posFood.getMdid()));
- org.put("name", posFood.getName());
- org.put("image", posFood.getImage());
- org.put("price", posFood.getPrice());
- org.put("introduce", posFood.getIntroduce());
- org.put("recommend", posFood.getRecommend());
- org.put("stackingUp", posFood.getStackingUp());
- org.put("foodSpecs", __fs);
- org.put("foodSku", __fs.isEmpty() ? JSONArray.parseArray(posFood.getFoodSku()) : buildFoodSkuArray(__fs));
- return success(org);
- }
- /**
- * 搜索
- */
- @Anonymous
- @GetMapping("/searchfor")
- public AjaxResult searchfor(@RequestParam String keyword, @RequestParam(defaultValue = "") String language) {
- if (keyword.equals("")) {
- return error(MessageUtils.message("no.tip.lock.keywork"));
- }
- JSONArray all = new JSONArray();
- JSONObject object = new JSONObject();
- QueryWrapper<PosFood> query = new QueryWrapper<>();
- String lang = "3";
- if (!"".equals(language)) {
- lang = language;
- }
- query.eq("language", lang);
- query.eq("to_examine", "1");
- // query.like("name",keyword);
- query.apply("BINARY name LIKE CONCAT('%', {0}, '%')", keyword);
- List<PosFood> folist = posFoodService.list(query);
- QueryWrapper<PosStore> wrapper = new QueryWrapper<>();
- wrapper.eq("off_shelf", "0")
- // .and(i->i.like("pos_name", keyword).or().like("brief_introduction",keyword));
- .and(i -> i.apply("BINARY pos_name LIKE CONCAT('%', {0}, '%')", keyword).or().apply("BINARY brief_introduction LIKE CONCAT('%', {0}, '%')", keyword));
- List<PosStore> stlist = posStoreService.list(wrapper);
- for (int i = 0; i < folist.size(); i++) {
- PosStore store = posStoreService.getById(folist.get(i).getMdid());
- if (store.getOffShelf().equals("0")) {
- JSONObject org = new JSONObject();
- org.put("id", folist.get(i).getId());
- org.put("fenlei", posFenleiService.getById(folist.get(i).getFlId()));
- org.put("store", posStoreService.getById(folist.get(i).getMdid()));
- org.put("name", folist.get(i).getName());
- org.put("image", folist.get(i).getImage());
- org.put("price", folist.get(i).getPrice());
- org.put("introduce", folist.get(i).getIntroduce());
- org.put("recommend", folist.get(i).getRecommend());
- org.put("stackingUp", folist.get(i).getStackingUp());
- org.put("foodSku", JSONArray.parseArray(folist.get(i).getFoodSku()));
- all.add(org);
- }
- }
- object.put("food", all);
- object.put("store", stlist);
- return success(object);
- }
- /**
- * 客户端查询美食
- *
- * @param keyword
- * @param language
- * @return
- */
- @Anonymous
- @GetMapping("/foodSearch")
- public AjaxResult foodSearch(@RequestParam String keyword, @RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(defaultValue = "") String language) {
- if (keyword.isEmpty()) {
- return error(MessageUtils.message("no.tip.lock.keywork"));
- }
- // 设置默认分页参数
- if (pageNum == null || pageNum < 1) {
- pageNum = 1;
- }
- if (pageSize == null || pageSize < 1) {
- pageSize = 10;
- }
- String lang = "3";
- if (!"".equals(language)) {
- lang = language;
- }
- // 创建分页对象
- Page<PosStore> page = new Page<>(pageNum, pageSize);
- // 1. 先通过联表查询获取分页的店铺列表(包含总数)
- IPage<PosStore> storePage = posStoreMapper.selectStoresByFoodKeyword(page, keyword, lang);
- 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
- List<Long> storeIds = stores.stream()
- .map(store -> store.getId().longValue())
- .collect(Collectors.toList());
- // 一次性查询所有店铺下匹配关键词的商品
- QueryWrapper<PosFood> foodQuery = new QueryWrapper<>();
- foodQuery.in("mdid", storeIds)
- .eq("language", lang)
- .eq("to_examine", "1")
- .apply(" CONCAT(name, ' ') LIKE CONCAT('%', {0}, '%') COLLATE utf8mb4_unicode_ci", keyword);
- List<PosFood> allFoods = posFoodService.list(foodQuery);
- {
- List<Long> __foodIds = allFoods.stream().map(PosFood::getId).collect(Collectors.toList());
- Map<Long, List<FoodSpecs>> __specMap = loadFoodSpecsMap(__foodIds);
- for (PosFood __f : allFoods) {
- __f.setFoodSpecs(__specMap.getOrDefault(__f.getId(), new ArrayList<>()));
- }
- }
- // 按店铺ID分组商品
- Map<Long, List<PosFood>> foodsByStore = allFoods.stream()
- .collect(Collectors.groupingBy(PosFood::getMdid));
- posStoreEnrichService.enrichStoreList(stores);
- // 3. 为每个店铺设置对应的商品
- for (PosStore store : stores) {
- FoodSearchOutput output = new FoodSearchOutput();
- output.setPosStore(store);
- // 从分组结果中获取该店铺的商品
- List<PosFood> storeFoods = foodsByStore.getOrDefault(store.getId().longValue(), new ArrayList<>());
- output.setPosFoodList(storeFoods);
- result.add(output);
- }
- }
- // 3. 返回分页结果
- Map<String, Object> response = new HashMap<>();
- response.put("list", result);
- response.put("total", storePage.getTotal());
- return success(response);
- }
- /**
- * 客户端查询门店
- *
- * @param keyword
- * @param language
- * @return
- */
- @Anonymous
- @GetMapping("/storeSearch")
- public AjaxResult storeSearch(@RequestParam String keyword, @RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(defaultValue = "") String language) {
- if (keyword.isEmpty()) {
- return error(MessageUtils.message("no.tip.lock.keywork"));
- }
- QueryWrapper<PosFood> query = new QueryWrapper<>();
- // 设置默认分页参数
- if (pageNum == null || pageNum < 1) {
- pageNum = 1;
- }
- if (pageSize == null || pageSize < 1) {
- pageSize = 10;
- }
- Page<PosStore> page = new Page<>(pageNum, pageSize);
- QueryWrapper<PosStore> storeWrapper = new QueryWrapper<>();
- String finalKeyword = keyword;
- 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, storeWrapper);
- Map<String, Object> response = new HashMap<>();
- List<PosStore> stores=data.getRecords();
- posStoreEnrichService.enrichStoreList(stores);
- response.put("list", stores);
- response.put("total", data.getTotal());
- return success(response);
- }
- @Anonymous
- @Auth(session = true)
- @GetMapping("/getFoodPageList")
- public AjaxResult getFoodPageList(@RequestHeader String token,
- @RequestParam Integer page,
- @RequestParam Integer size,
- @RequestParam Long flId,
- @RequestParam Long mdId,
- @RequestParam(defaultValue = "") String language,
- @RequestParam(defaultValue = "") String name) {
- Long id = AuthContext.requireUserId();
- merchantStoreAccessService.requireStoreAccess(id, mdId);
- IPage<PosFood> stlist = new Page<>(page, size);
- LambdaQueryWrapper<PosFood> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(PosFood::getFlId, flId);
- queryWrapper.eq(PosFood::getMdid, mdId);
- // language 不为 null 且不为空字符串时才加条件
- if (language != null && !"".equals(language)) {
- queryWrapper.eq(PosFood::getLanguage, language);
- }
- // name 不为 null 且不为空字符串时模糊查询
- if (name != null && !"".equals(name)) {
- queryWrapper.like(PosFood::getName, name);
- }
- IPage<PosFood> list = posFoodService.page(stlist, queryWrapper);
- return success(list);
- }
- /**
- * C端扫码:获取摊位商品列表(摊位码扫码入口)
- */
- @Anonymous
- @GetMapping("/stallFoodList")
- public AjaxResult stallFoodList(@RequestParam Long storeId,
- @RequestParam(required = false) Long flId,
- @RequestParam Integer page,
- @RequestParam Integer size,
- @RequestParam(required = false) Long tableId) {
- IPage<PosFood> foodPage = new Page<>(page, size);
- QueryWrapper<PosFood> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("mdid", storeId);
- queryWrapper.eq("to_examine", "1");
- queryWrapper.eq("stacking_up", "1");
- if (flId != null) {
- queryWrapper.eq("fl_id", flId);
- }
- IPage<PosFood> result = posFoodService.page(foodPage, queryWrapper);
- List<Long> __foodIds = result.getRecords().stream().map(PosFood::getId).collect(Collectors.toList());
- Map<Long, List<FoodSpecs>> __specMap = loadFoodSpecsMap(__foodIds);
- for (PosFood __f : result.getRecords()) {
- __f.setFoodSpecs(__specMap.getOrDefault(__f.getId(), new ArrayList<>()));
- }
- return success(result);
- }
- /**
- * 查询商品列表
- */
- @Anonymous
- @GetMapping("/getfoodlist")
- public AjaxResult getfoodlist(@RequestParam Integer page,
- @RequestParam Integer size,
- @RequestParam(defaultValue = "") String language,
- @RequestParam(defaultValue = "") String keyword) {
- IPage<PosFood> food = new Page<>(page, size);
- QueryWrapper<PosFood> queryWrapper = new QueryWrapper<>();
- String lang = "3";
- if (!"".equals(language)) {
- lang = language;
- }
- queryWrapper.eq("language", lang);
- queryWrapper.eq("to_examine", "1");
- if (!"".equals(keyword)) {
- queryWrapper.like("name", keyword);
- }
- IPage<PosFood> list = posFoodService.page(food, queryWrapper);
- List<PosFood> foodlist = list.getRecords();
- JSONArray all = new JSONArray();
- for (int i = 0; i < foodlist.size(); i++) {
- PosStore store = posStoreService.getById(foodlist.get(i).getMdid());
- if (store.getOffShelf().equals("0")) {
- JSONObject org = new JSONObject();
- org.put("id", foodlist.get(i).getId());
- org.put("fenlei", posFenleiService.getById(foodlist.get(i).getFlId()));
- org.put("store", posStoreService.getById(foodlist.get(i).getMdid()));
- org.put("name", foodlist.get(i).getName());
- org.put("image", foodlist.get(i).getImage());
- org.put("price", foodlist.get(i).getPrice());
- org.put("introduce", foodlist.get(i).getIntroduce());
- org.put("recommend", foodlist.get(i).getRecommend());
- org.put("stackingUp", foodlist.get(i).getStackingUp());
- org.put("foodSku", JSONArray.parseArray(foodlist.get(i).getFoodSku()));
- all.add(org);
- }
- }
- return success(all);
- }
- /**
- * 查询food列表
- */
- @PreAuthorize("@ss.hasPermi('chanting:food:list')")
- @GetMapping("/list")
- public TableDataInfo list(PosFood posFood) {
- startPage();
- List<PosFood> list = posFoodService.selectPosFoodList(posFood);
- return getDataTable(list);
- }
- /**
- * 导出food列表
- */
- @PreAuthorize("@ss.hasPermi('chanting:food:export')")
- @Log(title = "food", businessType = BusinessType.EXPORT)
- @PostMapping("/export")
- public void export(HttpServletResponse response, PosFood posFood) {
- List<PosFood> list = posFoodService.selectPosFoodList(posFood);
- ExcelUtil<PosFood> util = new ExcelUtil<PosFood>(PosFood.class);
- util.exportExcel(response, list, MessageUtils.message("no.export.excel.food"));
- }
- /**
- * 获取food详细信息
- */
- @Anonymous
- @GetMapping(value = "/{id}")
- public AjaxResult getInfo(@PathVariable("id") Long id) {
- return success(posFoodService.selectPosFoodById(id));
- }
- /**
- * 新增food
- */
- @PreAuthorize("@ss.hasPermi('chanting:food:add')")
- @Log(title = "food", businessType = BusinessType.INSERT)
- @PostMapping
- public AjaxResult add(@RequestBody PosFood posFood) {
- int rows = posFoodService.insertPosFood(posFood);
- if (rows > 0) {
- handleFoodSpec(posFood);
- }
- return toAjax(rows);
- }
- /**
- * 修改food
- */
- @PreAuthorize("@ss.hasPermi('chanting:food:edit')")
- @Log(title = "food", businessType = BusinessType.UPDATE)
- @PutMapping
- public AjaxResult edit(@RequestBody PosFood posFood) {
- int rows = posFoodService.updatePosFood(posFood);
- if (rows > 0) {
- handleFoodSpec(posFood);
- }
- return toAjax(rows);
- }
- /**
- * 删除food
- */
- @PreAuthorize("@ss.hasPermi('chanting:food:remove')")
- @Log(title = "food", businessType = BusinessType.DELETE)
- @DeleteMapping("/{ids}")
- public AjaxResult remove(@PathVariable Long[] ids) {
- return toAjax(posFoodService.deletePosFoodByIds(ids));
- }
- /**
- * 压缩图片店铺的商品图片
- */
- @Anonymous
- @GetMapping("/compressFoodImage")
- public AjaxResult compressFoodImage(@RequestParam Long mdId, @RequestParam String idsStr) {
- LambdaQueryWrapper<PosFood> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(PosFood::getMdid, mdId);
- List<Long> ids= new ArrayList<>();
- if(!StringUtils.isEmpty(idsStr)){
- ids = Arrays.stream(idsStr.split(","))
- .map(Long::valueOf)
- .collect(Collectors.toList());
- }
- queryWrapper.in(!ids.isEmpty(), PosFood::getId, ids);
- List<PosFood> list = posFoodService.list(queryWrapper);
- AtomicInteger count = new AtomicInteger();
- list.forEach(food -> {
- String compressImage = ImageCompressUtils.compressImageByWebPath(food.getImage());
- if (!compressImage.equals(food.getImage())) {
- food.setImage(compressImage);
- PosFood posFood = new PosFood();
- posFood.setId(food.getId());
- posFood.setImage(compressImage);
- posFoodService.saveOrUpdate(posFood);
- count.getAndIncrement();
- System.out.println("food成功压缩的图片数量:"+count.get());
- logger.info("food成功压缩的图片数量:"+count.get());
- }
- });
- return success("成功压缩的 :"+count.get());
- }
- /**
- * 压缩所有的商品图片
- */
- @Anonymous
- @GetMapping("/compressFoodImageAll")
- public AjaxResult compressFoodImage() {
- LambdaQueryWrapper<PosFood> queryWrapper = new LambdaQueryWrapper<>();
- List<PosFood> list = posFoodService.list(queryWrapper);
- AtomicInteger count = new AtomicInteger();
- list.forEach(food -> {
- String compressImage = ImageCompressUtils.compressImageByWebPath(food.getImage());
- if (!compressImage.equals(food.getImage())) {
- food.setImage(compressImage);
- PosFood posFood = new PosFood();
- posFood.setId(food.getId());
- posFood.setImage(compressImage);
- posFoodService.saveOrUpdate(posFood);
- count.getAndIncrement();
- System.out.println("food成功压缩的图片数量:"+count.get());
- logger.info("food成功压缩的图片数量:"+count.get());
- }
- });
- return success("成功压缩的图片数量:"+count.get());
- }
- /**
- * 当门店是摊位时,用夜市的经纬度覆盖摊位的经纬度
- */
- /**
- * 保存商品规格:sku JSON 写 food_sku(落库) -> 删除旧关联 -> 按 foodSpecs 建立新关联
- */
- private void handleFoodSpec(PosFood posFood) {
- if (posFood == null || posFood.getId() == null) {
- return;
- }
- JSONArray sku = posFood.getSku();
- if (sku != null) {
- PosFood upd = new PosFood();
- upd.setId(posFood.getId());
- upd.setFoodSku(sku.toString());
- posFoodService.updateById(upd);
- posFood.setFoodSku(sku.toString());
- }
- foodSpecRelationService.remove(new LambdaQueryWrapper<FoodSpecRelation>()
- .eq(FoodSpecRelation::getFoodId, posFood.getId()));
- List<FoodSpecs> foodSpecs = posFood.getFoodSpecs();
- if (foodSpecs != null && !foodSpecs.isEmpty()) {
- List<FoodSpecRelation> rels = foodSpecs.stream()
- .filter(sp -> sp.getId() != null && sp.getId() > 0)
- .map(sp -> {
- FoodSpecRelation r = new FoodSpecRelation();
- r.setFoodId(posFood.getId());
- r.setSpecsId(sp.getId());
- return r;
- }).collect(Collectors.toList());
- if (!rels.isEmpty()) {
- foodSpecRelationService.saveBatch(rels);
- }
- }
- }
- /**
- * 批量查询商品规格(避免 N+1):关联 -> 启用规格组 -> 启用规格值 -> 内存分组
- * @return foodId -> 规格组列表(含启用规格值)
- */
- private Map<Long, List<FoodSpecs>> loadFoodSpecsMap(List<Long> foodIds) {
- Map<Long, List<FoodSpecs>> result = new HashMap<>();
- if (foodIds == null || foodIds.isEmpty()) {
- return result;
- }
- List<FoodSpecRelation> rels = foodSpecRelationService.list(new LambdaQueryWrapper<FoodSpecRelation>()
- .in(FoodSpecRelation::getFoodId, foodIds));
- if (rels.isEmpty()) {
- return result;
- }
- Map<Long, List<Long>> foodToSpecIds = rels.stream()
- .collect(Collectors.groupingBy(FoodSpecRelation::getFoodId,
- Collectors.mapping(FoodSpecRelation::getSpecsId, Collectors.toList())));
- List<Long> specsIds = rels.stream().map(FoodSpecRelation::getSpecsId).distinct().collect(Collectors.toList());
- List<FoodSpecs> specsList = foodSpecsService.list(new LambdaQueryWrapper<FoodSpecs>()
- .in(FoodSpecs::getId, specsIds)
- .eq(FoodSpecs::getIsOpen, true)
- .eq(FoodSpecs::getIsDelete, false)
- .orderByAsc(FoodSpecs::getSort));
- if (specsList.isEmpty()) {
- return result;
- }
- List<Long> enabledSpecIds = specsList.stream().map(FoodSpecs::getId).collect(Collectors.toList());
- List<FoodSpecsValue> values = foodSpecsValueService.list(new LambdaQueryWrapper<FoodSpecsValue>()
- .in(FoodSpecsValue::getParentId, enabledSpecIds)
- .eq(FoodSpecsValue::getIsOpen, true)
- .orderByAsc(FoodSpecsValue::getId));
- Map<Long, List<FoodSpecsValue>> valueMap = values.stream()
- .collect(Collectors.groupingBy(FoodSpecsValue::getParentId));
- Map<Long, FoodSpecs> specsMap = specsList.stream().collect(Collectors.toMap(FoodSpecs::getId, sp -> sp));
- for (Map.Entry<Long, List<Long>> entry : foodToSpecIds.entrySet()) {
- List<FoodSpecs> fsList = new ArrayList<>();
- for (Long sid : entry.getValue()) {
- FoodSpecs src = specsMap.get(sid);
- if (src != null) {
- FoodSpecs copy = new FoodSpecs();
- copy.setId(src.getId());
- copy.setTitle(src.getTitle());
- copy.setType(src.getType());
- copy.setState(src.getState());
- copy.setLanguage(src.getLanguage());
- copy.setRemark(src.getRemark());
- copy.setMdId(src.getMdId());
- copy.setSort(src.getSort());
- copy.setIsOpen(src.getIsOpen());
- copy.setIsDelete(src.getIsDelete());
- copy.setObjects(valueMap.getOrDefault(src.getId(), new ArrayList<>()));
- fsList.add(copy);
- }
- }
- result.put(entry.getKey(), fsList);
- }
- return result;
- }
- /**
- * 由结构化规格构建兼容的 food_sku JSON 数组(明细字段名 foodSpecsItems)
- */
- private JSONArray buildFoodSkuArray(List<FoodSpecs> fsList) {
- JSONArray skuArr = new JSONArray();
- if (fsList == null || fsList.isEmpty()) {
- return skuArr;
- }
- for (FoodSpecs s : fsList) {
- skuArr.add(JSONObject.toJSON(s));
- }
- return skuArr;
- }
- private void fillStallLocation(PosStore store) {
- if (store != null && store.getIsStall() != null && store.getIsStall() == 1
- && store.getNightMarketId() != null) {
- InfoUser nightMarket = infoUserService.getById(store.getNightMarketId());
- if (nightMarket != null) {
- store.setLongitude(nightMarket.getLongitude());
- store.setLatitude(nightMarket.getLatitude());
- }
- }
- }
- }
|