|
@@ -23,6 +23,7 @@ import com.ym.mec.common.dal.BaseDAO;
|
|
|
import com.ym.mec.common.dto.PmsProductDto;
|
|
|
import com.ym.mec.common.dto.PmsProductQueryParamDto;
|
|
|
import com.ym.mec.common.entity.GoodsSubModel;
|
|
|
+import com.ym.mec.common.entity.GoodsSubStockModel;
|
|
|
import com.ym.mec.common.entity.UploadReturnBean;
|
|
|
import com.ym.mec.common.exception.BizException;
|
|
|
import com.ym.mec.common.page.PageInfo;
|
|
@@ -56,6 +57,7 @@ import java.io.InputStream;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
@@ -262,9 +264,9 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
throw new BizException("{}等商品还在销售中", goodsNames);
|
|
|
}
|
|
|
}
|
|
|
- List<GoodsSub> goodsSubs = queryGoodsSub(goodsId);
|
|
|
+ List<GoodsWrapper.GoodsSub> goodsSubs = queryGoodsSub(goodsId);
|
|
|
if (!CollectionUtils.isEmpty(goodsSubs) && status == 1) {
|
|
|
- long falseSize = goodsSubs.stream().map(GoodsSub::getGoodsStatus).filter(Boolean.FALSE::equals).count();
|
|
|
+ long falseSize = goodsSubs.stream().map(GoodsWrapper.GoodsSub::getGoodsStatus).filter(Boolean.FALSE::equals).count();
|
|
|
if (falseSize > 0) {
|
|
|
throw new BizException("存在已下架的子商品");
|
|
|
}
|
|
@@ -283,12 +285,35 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
goodsDao.update(goods);
|
|
|
}
|
|
|
|
|
|
- private List<GoodsSub> queryGoodsSub(Integer goodsId) {
|
|
|
+ private List<GoodsWrapper.GoodsSub> queryGoodsSub(Integer goodsId) {
|
|
|
// 从管乐迷商城组合的商品
|
|
|
QueryWrapper<GoodsSub> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.eq("goods_id_", goodsId);
|
|
|
List<GoodsSub> goodsSubs = goodsSubMapper.selectList(queryWrapper);
|
|
|
- return goodsSubs;
|
|
|
+ List<GoodsWrapper.GoodsSub> goodsSubList = goodsSubs.stream().map(next -> {
|
|
|
+ GoodsWrapper.GoodsSub goodsSub = new GoodsWrapper.GoodsSub();
|
|
|
+ goodsSub.setGoodsId(goodsId);
|
|
|
+ goodsSub.setMallGoodsId(next.getMallGoodsId());
|
|
|
+ goodsSub.setGoodsStatus(next.getGoodsStatus());
|
|
|
+ goodsSub.setSku(next.getSku());
|
|
|
+ goodsSub.setGoodsPrice(next.getGoodsPrice());
|
|
|
+ return goodsSub;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ if (!CollectionUtils.isEmpty(goodsSubList)) {
|
|
|
+ List<String> goodIdList = goodsSubList.stream().map(next -> next.getMallGoodsId().toString()).distinct().collect(Collectors.toList());
|
|
|
+ PmsProductQueryParamDto build = PmsProductQueryParamDto.builder()
|
|
|
+ .productIds(String.join(",", goodIdList))
|
|
|
+ .pageSize(9999)
|
|
|
+ .pageNum(1).build();
|
|
|
+ Map<String, PmsProductDto> map = mallFeignService.getProductList(build)
|
|
|
+ .getRows().stream().collect(Collectors.toMap(next -> next.getGoodsId() + "_" + next.getSkuStockId(), Function.identity()));
|
|
|
+ goodsSubList.forEach(next -> {
|
|
|
+ PmsProductDto orDefault = map.getOrDefault(next.getMallGoodsId() + "_" + next.getSku(), new PmsProductDto());
|
|
|
+ next.setPrice(orDefault.getPrice());
|
|
|
+ next.setMallGoodsName(orDefault.getName());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return goodsSubList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -781,10 +806,9 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
if(StringUtils.isNotEmpty(goods.getFreeFeeShowOrganId())){
|
|
|
goods.setFreeFeeShowOrganName(StringUtils.join(organizationDao.findByOrganIds(goods.getFreeFeeShowOrganId()),","));
|
|
|
}
|
|
|
- List<GoodsSub> goodsSubs = queryGoodsSub(goodsId);
|
|
|
GoodsWrapper.Goods detail = new GoodsWrapper.Goods();
|
|
|
BeanUtils.copyProperties(goods, detail);
|
|
|
- detail.setGoodsSubList(JSON.parseArray(JSON.toJSONString(goodsSubs), GoodsWrapper.GoodsSub.class));
|
|
|
+ detail.setGoodsSubList(queryGoodsSub(goodsId));
|
|
|
return detail;
|
|
|
}
|
|
|
|
|
@@ -849,32 +873,73 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
if (CollectionUtils.isEmpty(goodsSubModelList)) {
|
|
|
return false;
|
|
|
}
|
|
|
- log.info("开始同步商品状态:" + JSON.toJSONString(goodsSubModelList));
|
|
|
+ log.info(Thread.currentThread().getName() + "开始同步商品状态:" + JSON.toJSONString(goodsSubModelList));
|
|
|
List<Integer> goodIds = goodsSubModelList.stream().map(GoodsSubModel::getMallGoodsId).distinct().collect(Collectors.toList());
|
|
|
- List<Integer> skus = goodsSubModelList.stream().map(GoodsSubModel::getSku).distinct().collect(Collectors.toList());
|
|
|
QueryWrapper<GoodsSub> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.in("goods_id_", goodIds)
|
|
|
- .in("sku_", skus);
|
|
|
+ queryWrapper.in("mall_goods_id_", goodIds);
|
|
|
List<GoodsSub> goodsSubs = goodsSubMapper.selectList(queryWrapper);
|
|
|
if (CollectionUtils.isEmpty(goodsSubs)) {
|
|
|
return true;
|
|
|
}
|
|
|
- Map<String, Boolean> keyStatusMap = goodsSubModelList.stream().collect(Collectors.toMap(next -> next.getMallGoodsId() + "_" + next.getSku(), GoodsSubModel::getGoodsStatus));
|
|
|
- Set<String> keys = keyStatusMap.keySet();
|
|
|
- goodsSubs.removeIf(next -> !keys.contains(next.getMallGoodsId() + "_" + next.getSku()));
|
|
|
- if (CollectionUtils.isEmpty(goodsSubs)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- log.info("开始同步商品状态:0/" + goodsSubs.size());
|
|
|
- List<GoodsSub> goodsSubList = goodsSubs.stream().peek(next -> next.setGoodsStatus(keyStatusMap.get(next.getMallGoodsId() + "_" + next.getSku()))).collect(Collectors.toList());
|
|
|
+ Map<Integer, Boolean> keyStatusMap = goodsSubModelList.stream().collect(Collectors.toMap(GoodsSubModel::getMallGoodsId, GoodsSubModel::getGoodsStatus));
|
|
|
+ log.info(Thread.currentThread().getName() + "开始同步商品状态:0/" + goodsSubs.size());
|
|
|
+ List<GoodsSub> goodsSubList = goodsSubs.stream().peek(next -> next.setGoodsStatus(keyStatusMap.get(next.getMallGoodsId()))).collect(Collectors.toList());
|
|
|
// 下架的商品
|
|
|
List<GoodsSub> downGoods = goodsSubList.stream().filter(next -> Boolean.FALSE.equals(next.getGoodsStatus())).collect(Collectors.toList());
|
|
|
if(!CollectionUtils.isEmpty(downGoods)){
|
|
|
List<Integer> goodIdList = downGoods.stream().map(GoodsSub::getGoodsId).distinct().collect(Collectors.toList());
|
|
|
goodsDao.updateStatus(goodIdList, false);
|
|
|
}
|
|
|
- Map<Boolean, List<Integer>> listMap = goodsSubList.stream().collect(Collectors.groupingBy(GoodsSub::getGoodsStatus, Collectors.mapping(next -> next.getId(), Collectors.toList())));
|
|
|
+ Map<Boolean, List<Integer>> listMap = goodsSubList.stream().collect(Collectors.groupingBy(GoodsSub::getGoodsStatus, Collectors.mapping(GoodsSub::getId, Collectors.toList())));
|
|
|
listMap.forEach((key, values) -> goodsSubMapper.updateStatus(values, key));
|
|
|
+ log.info(Thread.currentThread().getName() + "同步商品状态完成");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步组合商品库存
|
|
|
+ * @param goodsSubStockModels 商品库存信息
|
|
|
+ * @return true/false
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Boolean goodsStockCountSynchronize(List<GoodsSubStockModel> goodsSubStockModels) {
|
|
|
+ if(goodsSubStockModels.isEmpty()){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ List<Integer> mallGoodIds = goodsSubStockModels.stream().map(GoodsSubStockModel::getMallGoodsId).distinct().collect(Collectors.toList());
|
|
|
+ List<Integer> skus = goodsSubStockModels.stream().map(GoodsSubStockModel::getSku).distinct().collect(Collectors.toList());
|
|
|
+
|
|
|
+ QueryWrapper<GoodsSub> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.in("mall_goods_id_", mallGoodIds).in("sku_", skus);
|
|
|
+ List<GoodsSub> goodsSubs = goodsSubMapper.selectList(queryWrapper);
|
|
|
+ if (CollectionUtils.isEmpty(goodsSubs)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ Map<String, Integer> stockMap = goodsSubStockModels.stream().collect(Collectors.toMap(next -> next.getMallGoodsId() + "_" + next.getSku(), next -> next.getStock()));
|
|
|
+ Set<String> keys = stockMap.keySet();
|
|
|
+ goodsSubs.removeIf(next -> !keys.contains(next.getMallGoodsId() + "_" + next.getSku()));
|
|
|
+ if (CollectionUtils.isEmpty(goodsSubs)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ List<Integer> goodIds = goodsSubs.stream().map(GoodsSub::getGoodsId).distinct().collect(Collectors.toList());
|
|
|
+ goodsDao.lockGoods(goodIds);
|
|
|
+ List<Goods> goodsList = goodsDao.findGoodsByIds(goodIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
|
|
|
+ Map<Integer, Integer> goodStockMap = goodsList.stream().collect(Collectors.toMap(Goods::getId, Goods::getStockCount));
|
|
|
+
|
|
|
+ List<Goods> updateGoods = new ArrayList<>();
|
|
|
+ for (GoodsSub goodsSub : goodsSubs) {
|
|
|
+ Integer goodsId = goodsSub.getGoodsId();
|
|
|
+ String key = goodsSub.getMallGoodsId() + "_" + goodsSub.getSku();
|
|
|
+ Integer stock = stockMap.get(key);
|
|
|
+ int updateStock = Math.min(goodStockMap.get(goodsId), stock);
|
|
|
+
|
|
|
+ Goods goods = new Goods();
|
|
|
+ goods.setId(goodsId);
|
|
|
+ goods.setStockCount(updateStock);
|
|
|
+ updateGoods.add(goods);
|
|
|
+ }
|
|
|
+ goodsDao.updateStock(updateGoods);
|
|
|
return true;
|
|
|
}
|
|
|
}
|