|
@@ -1,5 +1,6 @@
|
|
|
package shop.alien.store.service.impl;
|
|
package shop.alien.store.service.impl;
|
|
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -8,19 +9,26 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import shop.alien.entity.result.CommonEnum;
|
|
import shop.alien.entity.result.CommonEnum;
|
|
|
import shop.alien.entity.result.R;
|
|
import shop.alien.entity.result.R;
|
|
|
import shop.alien.entity.store.StoreProductBar;
|
|
import shop.alien.entity.store.StoreProductBar;
|
|
|
|
|
+import shop.alien.entity.store.StoreProductDelicacies;
|
|
|
|
|
+import shop.alien.entity.store.StoreProductGym;
|
|
|
import shop.alien.entity.store.StoreProductItem;
|
|
import shop.alien.entity.store.StoreProductItem;
|
|
|
import shop.alien.entity.store.vo.StoreProductItemGymVo;
|
|
import shop.alien.entity.store.vo.StoreProductItemGymVo;
|
|
|
import shop.alien.entity.store.dto.StoreProductItemDto;
|
|
import shop.alien.entity.store.dto.StoreProductItemDto;
|
|
|
import shop.alien.mapper.StoreProductItemMapper;
|
|
import shop.alien.mapper.StoreProductItemMapper;
|
|
|
import shop.alien.store.service.StoreProductBarService;
|
|
import shop.alien.store.service.StoreProductBarService;
|
|
|
|
|
+import shop.alien.store.service.StoreProductDelicaciesService;
|
|
|
|
|
+import shop.alien.store.service.StoreProductGymService;
|
|
|
import shop.alien.store.service.StoreProductItemService;
|
|
import shop.alien.store.service.StoreProductItemService;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.function.BiConsumer;
|
|
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -39,29 +47,19 @@ public class StoreProductItemServiceImpl extends ServiceImpl<StoreProductItemMap
|
|
|
|
|
|
|
|
private final StoreProductBarService storeProductBarService;
|
|
private final StoreProductBarService storeProductBarService;
|
|
|
|
|
|
|
|
|
|
+ private final StoreProductDelicaciesService storeProductDelicaciesService;
|
|
|
|
|
+
|
|
|
|
|
+ private final StoreProductGymService storeProductGymService;
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public R<StoreProductItem> addStoreProductItemDto(StoreProductItemDto storeProductItemDto) {
|
|
public R<StoreProductItem> addStoreProductItemDto(StoreProductItemDto storeProductItemDto) {
|
|
|
log.info("StoreProductItemServiceImpl.addStoreProductItem?storeProductItem={}", storeProductItemDto);
|
|
log.info("StoreProductItemServiceImpl.addStoreProductItem?storeProductItem={}", storeProductItemDto);
|
|
|
StoreProductItem storeProductItem = new StoreProductItem();
|
|
StoreProductItem storeProductItem = new StoreProductItem();
|
|
|
- BeanUtils.copyProperties(storeProductItemDto,storeProductItem);
|
|
|
|
|
- // 设置pid默认值为0
|
|
|
|
|
- storeProductItem.setPid(0);
|
|
|
|
|
- // MySQL 驱动开启 useGeneratedKeys(Spring Boot 默认开启,通常不用改)。
|
|
|
|
|
|
|
+ BeanUtils.copyProperties(storeProductItemDto, storeProductItem);
|
|
|
|
|
+ // 先保存主商品,确保可以拿到自增的 id 作为子表 extId。
|
|
|
this.save(storeProductItem);
|
|
this.save(storeProductItem);
|
|
|
- // 添加套餐时,需要前端在子集合内元素分别设置type
|
|
|
|
|
- // 处理子项(判空)
|
|
|
|
|
- List<?> rawSubList = storeProductItemDto.getSubList();
|
|
|
|
|
- if (rawSubList != null && !rawSubList.isEmpty()) {
|
|
|
|
|
- List<StoreProductBar> subList = rawSubList.stream()
|
|
|
|
|
- .map(v -> objectMapper.convertValue(v, StoreProductBar.class))
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
- Integer parentId = storeProductItem.getId();
|
|
|
|
|
- subList.forEach(v -> v.setExtId(parentId));
|
|
|
|
|
- boolean subSaved = storeProductBarService.saveBatch(subList);
|
|
|
|
|
- if (!subSaved) {
|
|
|
|
|
- throw new RuntimeException("子项保存失败");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 根据商品类型分发子表保存逻辑。
|
|
|
|
|
+ processSubItems(storeProductItemDto, storeProductItem, false);
|
|
|
return R.data(storeProductItem);
|
|
return R.data(storeProductItem);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -69,29 +67,95 @@ public class StoreProductItemServiceImpl extends ServiceImpl<StoreProductItemMap
|
|
|
public R<StoreProductItem> editStoreProductItem(StoreProductItemDto storeProductItemDto) {
|
|
public R<StoreProductItem> editStoreProductItem(StoreProductItemDto storeProductItemDto) {
|
|
|
log.info("StoreProductItemServiceImpl.editStoreProductItem?storeProductItem={}", storeProductItemDto);
|
|
log.info("StoreProductItemServiceImpl.editStoreProductItem?storeProductItem={}", storeProductItemDto);
|
|
|
StoreProductItem storeProductItem = new StoreProductItem();
|
|
StoreProductItem storeProductItem = new StoreProductItem();
|
|
|
- BeanUtils.copyProperties(storeProductItemDto,storeProductItem);
|
|
|
|
|
|
|
+ BeanUtils.copyProperties(storeProductItemDto, storeProductItem);
|
|
|
this.updateById(storeProductItem);
|
|
this.updateById(storeProductItem);
|
|
|
- // 添加套餐时,需要前端在子集合内元素分别设置type
|
|
|
|
|
- // 处理子项(判空)
|
|
|
|
|
|
|
+ // 统一的子表处理:先清理旧数据,再按商品类型重建。
|
|
|
|
|
+ processSubItems(storeProductItemDto, storeProductItem, true);
|
|
|
|
|
+ return R.data(storeProductItem);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 新增/编辑场景下的子表处理。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param storeProductItemDto 前端传入的数据,包含主表和子表信息
|
|
|
|
|
+ * @param storeProductItem 已保存/更新的主表实体,需从中读取 id 作为子表 extId
|
|
|
|
|
+ * @param replaceExisting 是否需要先删除同 extId 的旧数据(编辑场景)
|
|
|
|
|
+ */
|
|
|
|
|
+ private void processSubItems(StoreProductItemDto storeProductItemDto,
|
|
|
|
|
+ StoreProductItem storeProductItem,
|
|
|
|
|
+ boolean replaceExisting) {
|
|
|
List<?> rawSubList = storeProductItemDto.getSubList();
|
|
List<?> rawSubList = storeProductItemDto.getSubList();
|
|
|
- if (rawSubList != null && !rawSubList.isEmpty()) {
|
|
|
|
|
- List<StoreProductBar> subList = rawSubList.stream()
|
|
|
|
|
- .map(v -> objectMapper.convertValue(v, StoreProductBar.class))
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
- Integer parentId = storeProductItem.getId();
|
|
|
|
|
- subList.forEach(v -> v.setExtId(parentId));
|
|
|
|
|
- boolean subSaved = storeProductBarService.saveBatch(subList);
|
|
|
|
|
- if (!subSaved) {
|
|
|
|
|
- throw new RuntimeException("子项保存失败");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (rawSubList == null || rawSubList.isEmpty()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer modelType = storeProductItemDto.getModelType();
|
|
|
|
|
+ if (modelType == null) {
|
|
|
|
|
+ log.warn("商品缺少 modelType,跳过子项处理");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer extId = storeProductItem.getId();
|
|
|
|
|
+
|
|
|
|
|
+ // 编辑场景:删除当前商品 extId 对应的旧子表数据,避免脏数据。
|
|
|
|
|
+ if (replaceExisting) {
|
|
|
|
|
+ clearSubItemsByModelType(modelType, extId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 按商品类型把原始子项转换为目标实体并批量入库。
|
|
|
|
|
+ if (CommonEnum.ModelType.BAR.getCode() == modelType) {
|
|
|
|
|
+ convertAndSave(rawSubList, extId, StoreProductBar.class,
|
|
|
|
|
+ (item, id) -> item.setExtId(id), storeProductBarService::saveBatch);
|
|
|
|
|
+ } else if (CommonEnum.ModelType.DELICACY.getCode() == modelType) {
|
|
|
|
|
+ convertAndSave(rawSubList, extId, StoreProductDelicacies.class,
|
|
|
|
|
+ (item, id) -> item.setExtId(id), storeProductDelicaciesService::saveBatch);
|
|
|
|
|
+ } else if (CommonEnum.ModelType.GYM.getCode() == modelType) {
|
|
|
|
|
+ convertAndSave(rawSubList, extId, StoreProductGym.class,
|
|
|
|
|
+ (item, id) -> {
|
|
|
|
|
+ item.setExtId(id);
|
|
|
|
|
+ if (item.getUsageCount() == null) {
|
|
|
|
|
+ item.setUsageCount(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, storeProductGymService::saveBatch);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("未知的商品 modelType:{},子项未处理", modelType);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据商品模块清理同一个 extId 的旧子表数据,防止编辑后出现重复/脏数据。
|
|
|
|
|
+ */
|
|
|
|
|
+ private void clearSubItemsByModelType(Integer modelType, Integer extId) {
|
|
|
|
|
+ if (CommonEnum.ModelType.BAR.getCode() == modelType) {
|
|
|
|
|
+ storeProductBarService.lambdaUpdate().eq(StoreProductBar::getExtId, extId).remove();
|
|
|
|
|
+ } else if (CommonEnum.ModelType.DELICACY.getCode() == modelType) {
|
|
|
|
|
+ storeProductDelicaciesService.lambdaUpdate().eq(StoreProductDelicacies::getExtId, extId).remove();
|
|
|
|
|
+ } else if (CommonEnum.ModelType.GYM.getCode() == modelType) {
|
|
|
|
|
+ storeProductGymService.lambdaUpdate().eq(StoreProductGym::getExtId, extId).remove();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通用转换 + 批量保存,保持原有异常行为。
|
|
|
|
|
+ */
|
|
|
|
|
+ private <T> void convertAndSave(List<?> rawSubList, Integer extId, Class<T> clazz,
|
|
|
|
|
+ BiConsumer<T, Integer> extSetter,
|
|
|
|
|
+ Function<List<T>, Boolean> saveBatchFunc) {
|
|
|
|
|
+ List<T> subList = rawSubList.stream()
|
|
|
|
|
+ .map(v -> objectMapper.convertValue(v, clazz))
|
|
|
|
|
+ .peek(v -> extSetter.accept(v, extId))
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ boolean subSaved = saveBatchFunc.apply(subList);
|
|
|
|
|
+ if (!subSaved) {
|
|
|
|
|
+ throw new RuntimeException("子项保存失败");
|
|
|
}
|
|
}
|
|
|
- return R.data(storeProductItem);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public R<Boolean> deleteStoreProductItem(Integer id) {
|
|
public R<Boolean> deleteStoreProductItem(Integer id) {
|
|
|
log.info("StoreProductItemServiceImpl.deleteStoreProductItem?id={}", id);
|
|
log.info("StoreProductItemServiceImpl.deleteStoreProductItem?id={}", id);
|
|
|
boolean result = this.removeById(id);
|
|
boolean result = this.removeById(id);
|
|
|
|
|
+ storeProductBarService.lambdaUpdate().eq(StoreProductBar::getExtId, id).remove();
|
|
|
|
|
+ storeProductDelicaciesService.lambdaUpdate().eq(StoreProductDelicacies::getExtId, id).remove();
|
|
|
|
|
+ storeProductGymService.lambdaUpdate().eq(StoreProductGym::getExtId, id).remove();
|
|
|
if (result) {
|
|
if (result) {
|
|
|
return R.success("删除成功");
|
|
return R.success("删除成功");
|
|
|
}
|
|
}
|
|
@@ -102,17 +166,44 @@ public class StoreProductItemServiceImpl extends ServiceImpl<StoreProductItemMap
|
|
|
public R<List<StoreProductItemDto>> getStoreProductItemById(Integer id) {
|
|
public R<List<StoreProductItemDto>> getStoreProductItemById(Integer id) {
|
|
|
log.info("StoreProductItemServiceImpl.getStoreProductItemById?id={}", id);
|
|
log.info("StoreProductItemServiceImpl.getStoreProductItemById?id={}", id);
|
|
|
StoreProductItem storeProductItem = this.getById(id);
|
|
StoreProductItem storeProductItem = this.getById(id);
|
|
|
- ArrayList<StoreProductItemDto> list = new ArrayList<>();
|
|
|
|
|
- StoreProductItemDto storeProductItemDto = new StoreProductItemDto();
|
|
|
|
|
- list.add(storeProductItemDto);
|
|
|
|
|
- BeanUtils.copyProperties(storeProductItem,storeProductItemDto);
|
|
|
|
|
- if (CommonEnum.StoreProductItemProdType.DELICACY_FOOD.getCode() == storeProductItem.getProdType()) {
|
|
|
|
|
- list = this.getBaseMapper().getByPId(id);
|
|
|
|
|
- }else {
|
|
|
|
|
|
|
+ if (storeProductItem == null) {
|
|
|
|
|
+ ArrayList<StoreProductItemDto> list = new ArrayList<>();
|
|
|
|
|
+ StoreProductItemDto storeProductItemDto = new StoreProductItemDto();
|
|
|
|
|
+ list.add(storeProductItemDto);
|
|
|
|
|
+ BeanUtils.copyProperties(storeProductItem,storeProductItemDto);
|
|
|
List<StoreProductBar> barList = storeProductBarService.getByExtId(id);
|
|
List<StoreProductBar> barList = storeProductBarService.getByExtId(id);
|
|
|
storeProductItemDto.setSubList(barList);
|
|
storeProductItemDto.setSubList(barList);
|
|
|
|
|
+ return R.data(list);
|
|
|
}
|
|
}
|
|
|
- return R.data(list);
|
|
|
|
|
|
|
+ return R.fail("未找到数据");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public R<List<StoreProductItem>> listByProdType(Integer prodType) {
|
|
|
|
|
+ log.info("StoreProductItemServiceImpl.listByProdType?prodType={}", prodType);
|
|
|
|
|
+ if (prodType == null) {
|
|
|
|
|
+ return R.fail("prodType不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ List<StoreProductItem> result = lambdaQuery().eq(StoreProductItem::getProdType, prodType)
|
|
|
|
|
+ .orderByDesc(StoreProductItem::getCreatedTime).list();
|
|
|
|
|
+ return R.data(result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public IPage<StoreProductItem> pageStoreProductItems(int pageNum, int pageSize,
|
|
|
|
|
+ Integer storeId, String prodName, Integer prodType,
|
|
|
|
|
+ Integer status, Integer needReserve) {
|
|
|
|
|
+ log.info("StoreProductItemServiceImpl.pageStoreProductItems?pageNum={}, pageSize={}, storeId={}, prodName={}, prodType={}, status={}, needReserve={}",
|
|
|
|
|
+ pageNum, pageSize, storeId, prodName, prodType, status, needReserve);
|
|
|
|
|
+ Page<StoreProductItem> page = new Page<>(pageNum, pageSize);
|
|
|
|
|
+ LambdaQueryWrapper<StoreProductItem> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(storeId != null, StoreProductItem::getStoreId, storeId)
|
|
|
|
|
+ .eq(prodType != null, StoreProductItem::getProdType, prodType)
|
|
|
|
|
+ .eq(status != null, StoreProductItem::getStatus, status)
|
|
|
|
|
+ .eq(needReserve != null, StoreProductItem::getNeedReserve, needReserve)
|
|
|
|
|
+ .like(StringUtils.hasText(prodName), StoreProductItem::getProdName, prodName)
|
|
|
|
|
+ .orderByDesc(StoreProductItem::getCreatedTime);
|
|
|
|
|
+ return this.page(page, wrapper);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -139,5 +230,6 @@ public class StoreProductItemServiceImpl extends ServiceImpl<StoreProductItemMap
|
|
|
}
|
|
}
|
|
|
return R.fail("查询失败,数据不存在");
|
|
return R.fail("查询失败,数据不存在");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|