|
|
@@ -16,9 +16,11 @@ import shop.alien.entity.store.dto.CategoryGroupDto;
|
|
|
import shop.alien.entity.store.dto.CuisineComboDto;
|
|
|
import shop.alien.entity.store.dto.CuisineItemDto;
|
|
|
import shop.alien.entity.store.dto.CuisineTypeResponseDto;
|
|
|
+import shop.alien.entity.store.StoreCuisineCategory;
|
|
|
import shop.alien.mapper.StoreCuisineMapper;
|
|
|
import shop.alien.mapper.StoreInfoMapper;
|
|
|
import shop.alien.store.service.StoreCuisineComboService;
|
|
|
+import shop.alien.store.service.StoreCuisineCategoryService;
|
|
|
import shop.alien.store.service.StoreCuisineService;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
@@ -44,6 +46,8 @@ public class StoreCuisineServiceImpl extends ServiceImpl<StoreCuisineMapper, Sto
|
|
|
|
|
|
private final StoreInfoMapper storeInfoMapper;
|
|
|
|
|
|
+ private final StoreCuisineCategoryService storeCuisineCategoryService;
|
|
|
+
|
|
|
/**
|
|
|
* 新增美食(单品或套餐)
|
|
|
*/
|
|
|
@@ -59,6 +63,8 @@ public class StoreCuisineServiceImpl extends ServiceImpl<StoreCuisineMapper, Sto
|
|
|
// 1. 保存主表信息到 store_cuisine
|
|
|
StoreCuisine cuisine = new StoreCuisine();
|
|
|
BeanUtils.copyProperties(cuisineComboDto, cuisine);
|
|
|
+ // 处理分类ID:将分类ID数组转换为JSON字符串格式存储
|
|
|
+ cuisine.setCategoryIds(formatCategoryIds(cuisineComboDto.getCategoryIds()));
|
|
|
save(cuisine);
|
|
|
|
|
|
// 单品:只写主表,不写中间表
|
|
|
@@ -90,6 +96,8 @@ public class StoreCuisineServiceImpl extends ServiceImpl<StoreCuisineMapper, Sto
|
|
|
StoreCuisine cuisine = new StoreCuisine();
|
|
|
BeanUtils.copyProperties(cuisineComboDto, cuisine);
|
|
|
cuisine.setId(comboId);
|
|
|
+ // 处理分类ID:将分类ID数组转换为JSON字符串格式存储
|
|
|
+ cuisine.setCategoryIds(formatCategoryIds(cuisineComboDto.getCategoryIds()));
|
|
|
updateById(cuisine);
|
|
|
|
|
|
// 单品:只更新主表即可
|
|
|
@@ -150,9 +158,16 @@ public class StoreCuisineServiceImpl extends ServiceImpl<StoreCuisineMapper, Sto
|
|
|
data.setStoreId(base.getStoreId());
|
|
|
data.setName(base.getName());
|
|
|
data.setTotalPrice(base.getTotalPrice());
|
|
|
+ data.setCategoryIds(base.getCategoryIds());
|
|
|
+ // 根据分类ID查询分类名称
|
|
|
+ data.setCategoryNames(getCategoryNames(base.getCategoryIds()));
|
|
|
+ data.setIsHomepageDisplay(base.getIsHomepageDisplay());
|
|
|
+ data.setTags(base.getTags());
|
|
|
+ data.setDishReview(base.getDishReview());
|
|
|
data.setImages(base.getImages());
|
|
|
data.setImageContent(base.getImageContent());
|
|
|
data.setDetailContent(base.getDetailContent());
|
|
|
+ data.setDescription(base.getDescription());
|
|
|
data.setExtraNote(base.getExtraNote());
|
|
|
data.setNeedReserve(base.getNeedReserve());
|
|
|
data.setReserveRule(base.getReserveRule());
|
|
|
@@ -278,6 +293,99 @@ public class StoreCuisineServiceImpl extends ServiceImpl<StoreCuisineMapper, Sto
|
|
|
}
|
|
|
return comboList;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化分类ID为JSON数组字符串
|
|
|
+ * 如果传入的是有效的JSON数组字符串,直接返回;如果是空或null,返回null
|
|
|
+ *
|
|
|
+ * @param categoryIds 分类ID字符串(JSON数组格式,如:"[1,2,3]")
|
|
|
+ * @return 格式化后的JSON数组字符串,如果为空则返回null
|
|
|
+ */
|
|
|
+ private String formatCategoryIds(String categoryIds) {
|
|
|
+ if (StringUtils.isBlank(categoryIds)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 去除首尾空格
|
|
|
+ String trimmed = categoryIds.trim();
|
|
|
+
|
|
|
+ // 如果已经是有效的JSON数组格式(以[开头,]结尾),直接返回
|
|
|
+ if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
|
+ try {
|
|
|
+ // 验证是否为有效的JSON数组
|
|
|
+ objectMapper.readValue(trimmed, new TypeReference<List<Integer>>() {});
|
|
|
+ return trimmed;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new IllegalArgumentException("分类ID格式不正确,应为JSON数组格式,如:[1,2,3]", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果不是JSON格式,尝试解析为逗号分隔的字符串并转换为JSON数组
|
|
|
+ try {
|
|
|
+ String[] ids = trimmed.split(",");
|
|
|
+ List<Integer> idList = new ArrayList<>();
|
|
|
+ for (String id : ids) {
|
|
|
+ String idStr = id.trim();
|
|
|
+ if (StringUtils.isNotBlank(idStr)) {
|
|
|
+ idList.add(Integer.parseInt(idStr));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (idList.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return objectMapper.writeValueAsString(idList);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new IllegalArgumentException("分类ID格式不正确,应为数字数组", e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException("转换分类ID为JSON格式失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据分类ID字符串(JSON数组格式)查询分类名称
|
|
|
+ *
|
|
|
+ * @param categoryIds 分类ID字符串(JSON数组格式,如:"[1,2,3]")
|
|
|
+ * @return 分类名称JSON数组字符串(如:"[\"热菜\",\"凉菜\"]"),如果categoryIds为空则返回null
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getCategoryNames(String categoryIds) {
|
|
|
+ if (StringUtils.isBlank(categoryIds)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 解析分类ID数组
|
|
|
+ List<Integer> idList = objectMapper.readValue(categoryIds, new TypeReference<List<Integer>>() {});
|
|
|
+ if (idList == null || idList.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量查询分类信息
|
|
|
+ List<StoreCuisineCategory> categories = new ArrayList<>(storeCuisineCategoryService.listByIds(idList));
|
|
|
+ if (categories == null || categories.isEmpty()) {
|
|
|
+ return "[]";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建ID到名称的映射
|
|
|
+ Map<Integer, String> idToNameMap = categories.stream()
|
|
|
+ .collect(Collectors.toMap(StoreCuisineCategory::getId, StoreCuisineCategory::getCategoryName));
|
|
|
+
|
|
|
+ // 按照原始ID顺序构建名称列表
|
|
|
+ List<String> nameList = new ArrayList<>();
|
|
|
+ for (Integer id : idList) {
|
|
|
+ String name = idToNameMap.get(id);
|
|
|
+ if (name != null) {
|
|
|
+ nameList.add(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换为JSON数组字符串
|
|
|
+ return objectMapper.writeValueAsString(nameList);
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 解析分类ID或构建分类名称失败时,返回null,不抛出异常
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|