|
|
@@ -10,6 +10,7 @@ import shop.alien.entity.store.StoreCuisineCategory;
|
|
|
import shop.alien.entity.store.StoreInfo;
|
|
|
import shop.alien.entity.store.StoreTable;
|
|
|
import shop.alien.entity.store.dto.StoreInfoWithHomepageCuisinesDTO;
|
|
|
+import shop.alien.entity.store.vo.CategoryWithCuisinesVO;
|
|
|
import shop.alien.mapper.StoreCuisineCategoryMapper;
|
|
|
import shop.alien.mapper.StoreCuisineMapper;
|
|
|
import shop.alien.mapper.StoreInfoMapper;
|
|
|
@@ -19,6 +20,7 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 门店信息查询服务实现类
|
|
|
@@ -99,7 +101,101 @@ public class StoreInfoServiceImpl implements StoreInfoService {
|
|
|
return categoryIdsStr.contains(String.valueOf(categoryId));
|
|
|
}
|
|
|
})
|
|
|
- .collect(java.util.stream.Collectors.toList());
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<CategoryWithCuisinesVO> getCategoriesWithCuisinesByStoreId(Integer storeId) {
|
|
|
+ log.info("根据门店ID查询菜品种类及下属菜品, storeId={}", storeId);
|
|
|
+ List<StoreCuisineCategory> categories = getCategoriesByStoreId(storeId);
|
|
|
+ if (categories == null || categories.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<StoreCuisine> cuisineWrapper = new LambdaQueryWrapper<>();
|
|
|
+ cuisineWrapper.eq(StoreCuisine::getStoreId, storeId);
|
|
|
+ cuisineWrapper.eq(StoreCuisine::getDeleteFlag, 0);
|
|
|
+ cuisineWrapper.eq(StoreCuisine::getShelfStatus, 1);
|
|
|
+ List<StoreCuisine> allCuisines = storeCuisineMapper.selectList(cuisineWrapper);
|
|
|
+ if (allCuisines == null) {
|
|
|
+ allCuisines = new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<CategoryWithCuisinesVO> result = new ArrayList<>();
|
|
|
+ for (StoreCuisineCategory category : categories) {
|
|
|
+ Integer categoryId = category.getId();
|
|
|
+ List<StoreCuisine> cuisines = allCuisines.stream()
|
|
|
+ .filter(c -> belongsToCategory(c, categoryId))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ result.add(new CategoryWithCuisinesVO(category, cuisines));
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean belongsToCategory(StoreCuisine cuisine, Integer categoryId) {
|
|
|
+ String categoryIdsStr = cuisine.getCategoryIds();
|
|
|
+ if (StringUtils.isBlank(categoryIdsStr)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ List<Integer> categoryIds = JSON.parseArray(categoryIdsStr, Integer.class);
|
|
|
+ return categoryIds != null && categoryIds.contains(categoryId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return categoryIdsStr.contains(String.valueOf(categoryId));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean deleteCategory(Integer categoryId) {
|
|
|
+ log.info("删除菜品种类(仅解除绑定+逻辑删分类), categoryId={}", categoryId);
|
|
|
+ if (categoryId == null) {
|
|
|
+ log.warn("删除菜品种类失败:分类ID为空");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ StoreCuisineCategory category = storeCuisineCategoryMapper.selectById(categoryId);
|
|
|
+ if (category == null) {
|
|
|
+ log.warn("删除菜品种类失败:分类不存在, categoryId={}", categoryId);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Integer storeId = category.getStoreId();
|
|
|
+ // 1. 解除绑定:将该分类ID从所有关联菜品的 category_ids 中移除,价目表菜品其它字段不变
|
|
|
+ LambdaQueryWrapper<StoreCuisine> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(StoreCuisine::getStoreId, storeId);
|
|
|
+ wrapper.eq(StoreCuisine::getDeleteFlag, 0);
|
|
|
+ List<StoreCuisine> allCuisines = storeCuisineMapper.selectList(wrapper);
|
|
|
+ if (allCuisines != null) {
|
|
|
+ for (StoreCuisine cuisine : allCuisines) {
|
|
|
+ if (!belongsToCategory(cuisine, categoryId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String newCategoryIds = removeCategoryIdFromJson(cuisine.getCategoryIds(), categoryId);
|
|
|
+ cuisine.setCategoryIds(newCategoryIds);
|
|
|
+ storeCuisineMapper.updateById(cuisine);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 2. 逻辑删除菜品种类
|
|
|
+ storeCuisineCategoryMapper.deleteById(categoryId);
|
|
|
+ log.info("删除菜品种类完成, categoryId={}", categoryId);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 从 categoryIds 的 JSON 数组中移除指定 categoryId,返回新 JSON 字符串;若为空则返回 null */
|
|
|
+ private String removeCategoryIdFromJson(String categoryIdsStr, Integer categoryId) {
|
|
|
+ if (StringUtils.isBlank(categoryIdsStr)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ List<Integer> list = JSON.parseArray(categoryIdsStr, Integer.class);
|
|
|
+ if (list == null || list.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ list.remove(categoryId);
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return JSON.toJSONString(list);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("解析 categoryIds 失败, categoryIdsStr={}, 原样返回", categoryIdsStr, e);
|
|
|
+ return categoryIdsStr;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|