CmsGoodsDonSpecServiceImpl.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. package com.cyksj.service.mange.goods;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.cyksj.common.exception.BusinessRuntimeException;
  8. import com.cyksj.common.util.Jsons;
  9. import com.cyksj.mapper.GoodsDonMapper;
  10. import com.cyksj.mapper.GoodsDonSkuMapper;
  11. import com.cyksj.mapper.GoodsDonSpecMapper;
  12. import com.cyksj.model.entity.GoodsDon;
  13. import com.cyksj.model.entity.GoodsDonSku;
  14. import com.cyksj.model.entity.GoodsDonSpec;
  15. import com.cyksj.model.manage.request.ReqGoodsSkuInsert;
  16. import com.cyksj.model.manage.request.ReqGoodsSkuSpec;
  17. import com.cyksj.model.manage.request.ReqGoodsSpecSingle;
  18. import com.cyksj.model.request.CombinationSkuReq;
  19. import com.cyksj.service.mange.CmsGoodsDonSpecService;
  20. import com.fasterxml.jackson.databind.JavaType;
  21. import lombok.RequiredArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.math.BigDecimal;
  27. import java.util.*;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import java.util.concurrent.atomic.AtomicBoolean;
  30. import java.util.stream.Collectors;
  31. /**
  32. * @author chan
  33. * @date 2022/9/27 5:19 PM
  34. */
  35. @Slf4j
  36. @Service
  37. @RequiredArgsConstructor
  38. public class CmsGoodsDonSpecServiceImpl extends ServiceImpl<GoodsDonSpecMapper, GoodsDonSpec> implements CmsGoodsDonSpecService {
  39. private final GoodsDonMapper goodsDonMapper;
  40. private final GoodsDonSkuMapper goodsDonSkuMapper;
  41. @Override
  42. @Transactional(rollbackFor = Throwable.class)
  43. public void insertOrUpdateSkuSpec(ReqGoodsSkuInsert insert, Boolean isUpdate) throws Exception {
  44. GoodsDon goods = goodsDonMapper.selectById(insert.getGoodsId());
  45. if (null == goods) {
  46. throw new RuntimeException("找不到商品spu.");
  47. }
  48. GoodsDonSpec loveSpec = this.getOne(Wrappers.lambdaQuery(GoodsDonSpec.class).eq(GoodsDonSpec::getGoodsId, insert.getGoodsId()));
  49. Map<String, List<ReqGoodsSkuSpec>> dbSpecMap = null;
  50. Map<Integer, ReqGoodsSkuSpec.Node> dbNodeMap = null;
  51. if (null != loveSpec) {
  52. insert.setId(loveSpec.getId());
  53. String specsJson = loveSpec.getSpecsJson();
  54. dbSpecMap = Jsons.parseList(specsJson, ReqGoodsSkuSpec.class).stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec::getKey));
  55. }
  56. if (isUpdate && loveSpec == null) {
  57. throw BusinessRuntimeException.getInstance("所更新的平台id:{0}属性不存在", insert.getGoodsId());
  58. }
  59. List<ReqGoodsSkuSpec> specs = insert.getSpecs();
  60. if (null == specs || specs.isEmpty()) {
  61. throw new RuntimeException("缺少商品总规格.");
  62. }
  63. // 属性最大id
  64. Integer maxId = 0;
  65. // 统计总属性数
  66. Integer total = 0;
  67. // 判断id是否有重复
  68. Map<Integer, ReqGoodsSpecSingle> map = null;
  69. // 规格数量
  70. int specNumber = specs.size();
  71. dbNodeMap = checkUpdateGoodsSpec(goods.getId(), dbSpecMap, dbNodeMap, specs);
  72. for (ReqGoodsSkuSpec spec : specs) {
  73. if (StringUtils.isBlank(spec.getKey())) {
  74. throw new RuntimeException("存在空白规格名.");
  75. }
  76. List<ReqGoodsSkuSpec.Node> values = spec.getValues();
  77. if (null == values || values.isEmpty()) {
  78. throw new RuntimeException("规格[" + spec.getKey() + "]中缺少属性.");
  79. }
  80. total += values.size();
  81. for (ReqGoodsSkuSpec.Node e : values) {
  82. if (null == e.getId() || e.getId() < 0) {
  83. e.setId(maxId + 1);
  84. }
  85. if (StringUtils.isBlank(e.getValue())) {
  86. throw new RuntimeException("规格[" + spec.getKey() + "]中存在空白属性.");
  87. }
  88. if (e.getId() > maxId) {
  89. maxId = e.getId();
  90. }
  91. //新增时 实物价格要填入
  92. if (goods.getType() == 2 && insert.getId() == null && (e.getPrice() == null || e.getPrice().compareTo(BigDecimal.ZERO) < 0)) {
  93. throw BusinessRuntimeException.getInstance("实物规格:{0}对应价格请输入", e.getValue());
  94. }
  95. if (null == map) {
  96. map = new HashMap<>(16);
  97. }
  98. String benefitsList = e.getBenefitsList();
  99. if (StrUtil.isNotBlank(benefitsList)) {
  100. Set<GoodsDonSku> benefitsSkuIds = Jsons.parseSet(benefitsList, GoodsDonSku.class);
  101. e.setBenefitsList(Jsons.toJson(benefitsSkuIds));
  102. }
  103. syncSpecNodeSku(goods.getId(), dbNodeMap, e);
  104. ReqGoodsSpecSingle single = new ReqGoodsSpecSingle();
  105. single.setId(e.getId());
  106. single.setKey(spec.getKey());
  107. single.setName(e.getValue());
  108. map.put(e.getId(), single);
  109. }
  110. }
  111. if (map.size() != total) {
  112. throw new RuntimeException("规格中存在重复id.");
  113. }
  114. GoodsDonSpec spec = new GoodsDonSpec();
  115. spec.setId(insert.getId());
  116. spec.setGoodsId(insert.getGoodsId());
  117. spec.setMaxId(maxId);
  118. spec.setSpecsJson(Jsons.toJson(specs));
  119. spec.setSpecNumber(specNumber);
  120. spec.setSpecMapping(Jsons.toJson(map));
  121. if (insert.getId() != null) {
  122. this.updateById(spec);
  123. return;
  124. }
  125. this.save(spec);
  126. }
  127. @Override
  128. public void skuAppend(ReqGoodsSkuInsert insert, GoodsDon goods) throws Exception {
  129. GoodsDonSpec loveSpec = this.getOne(Wrappers.lambdaQuery(GoodsDonSpec.class).eq(GoodsDonSpec::getGoodsId, insert.getGoodsId()));
  130. if (null == loveSpec) {
  131. throw new RuntimeException("该商品未设置总规格, 请设置总规格.");
  132. }
  133. List<ReqGoodsSkuInsert.Sku> skus = insert.getSkus();
  134. if (null == skus || skus.isEmpty()) {
  135. throw new RuntimeException("参数错误.");
  136. }
  137. int specNumber = loveSpec.getSpecNumber();
  138. JavaType type = Jsons.javaType().constructMapType(HashMap.class, Integer.class, ReqGoodsSpecSingle.class);
  139. Map<Integer, ReqGoodsSpecSingle> map = Jsons.parseObject(loveSpec.getSpecMapping(), type);
  140. StringBuilder builder = new StringBuilder();
  141. List<ReqGoodsSpecSingle> specList = null;
  142. GoodsDonSku goodsDonSku = null;
  143. // 判断属性是否属于同一个key
  144. Set<String> set = null;
  145. for (ReqGoodsSkuInsert.Sku sku : insert.getSkus()) {
  146. // 跳过空值的sku
  147. // 解析sku的规格属性
  148. final String specIds = sku.getSpecIds();
  149. if (StringUtils.isBlank(specIds)) {
  150. throw new RuntimeException("请选择商品的规格.");
  151. }
  152. int[] ids = Jsons.parseObject(specIds, int[].class);
  153. // if (ids.length != specNumber) {
  154. // throw new RuntimeException("sku规格数量 与 总规格数量 不一致.");
  155. // }
  156. // init
  157. builder.delete(0, builder.length());
  158. if (null == specList) {
  159. specList = new ArrayList<>(specNumber);
  160. } else {
  161. specList.clear();
  162. }
  163. if (null == set) {
  164. int capacity = (specNumber + 1) * 4 / 3 + 1;
  165. set = new HashSet<>(capacity);
  166. } else {
  167. set.clear();
  168. }
  169. for (int id : ids) {
  170. ReqGoodsSpecSingle single = map.get(id);
  171. if (null == single) {
  172. throw new RuntimeException("总规格属性id中 不存在 sku中的id.");
  173. }
  174. builder.append(single.getName()).append(';');
  175. specList.add(single);
  176. set.add(single.getKey());
  177. }
  178. // if (set.size() != specNumber) {
  179. // throw new RuntimeException("sku中存在多个属性属于同一个规格.");
  180. // }
  181. String specVal = builder.toString();
  182. String specJson = Jsons.toJson(specList);
  183. if (null == goodsDonSku) {
  184. goodsDonSku = new GoodsDonSku();
  185. goodsDonSku.setGoodsId(insert.getGoodsId());
  186. }
  187. BeanUtil.copyProperties(sku, goodsDonSku);
  188. String specIdsStr = Jsons.toJson(ids);
  189. //升序
  190. Arrays.sort(ids);
  191. String dupSpecIds = Jsons.toJson(ids);
  192. GoodsDonSku specIds_exists = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  193. .and(qr -> qr.eq(GoodsDonSku::getDupSpecIds, dupSpecIds).or().eq(GoodsDonSku::getSpecIds, specIdsStr))
  194. .eq(GoodsDonSku::getGoodsId, goods.getId())
  195. .last("limit 1"));
  196. if (specIds_exists != null) {
  197. log.info("库中存在相同二级分类的规格,spec_ids:{}", dupSpecIds);
  198. continue;
  199. }
  200. goodsDonSku.setSpecIds(specIdsStr);
  201. goodsDonSku.setDupSpecIds(dupSpecIds);
  202. goodsDonSku.setSpecVal(specVal);
  203. goodsDonSku.setSpecJson(specJson);
  204. String benefitsList = goodsDonSku.getBenefitsList();
  205. if (StrUtil.isNotBlank(benefitsList)) {
  206. Set<GoodsDonSku> benefitsSkuIds = Jsons.parseSet(benefitsList, GoodsDonSku.class);
  207. goodsDonSku.setIsBenefits(true);
  208. goodsDonSku.setBenefitsList(Jsons.toJson(benefitsSkuIds));
  209. }
  210. if (goods.getType() == 1) {
  211. if (sku.getMonths() != null && sku.getMonths() > 0 && sku.getDays() != null && sku.getDays() > 0)
  212. throw BusinessRuntimeException.getInstance("续费天数和续费月数不能同时存在");
  213. if (sku.getMonths() == null && sku.getDays() == null)
  214. throw BusinessRuntimeException.getInstance("请输入对应续费时间");
  215. }
  216. String specificGoodsIds = goodsDonSku.getSpecificGoodsIds();
  217. if (StrUtil.isNotBlank(specificGoodsIds)) {
  218. Set<Long> specificGids = Jsons.parseSet(specificGoodsIds, Long.class);
  219. Integer count = goodsDonMapper.selectCount(Wrappers.lambdaQuery(GoodsDon.class)
  220. .in(GoodsDon::getId, specificGids)
  221. .eq(GoodsDon::getStatus, true)
  222. .eq(GoodsDon::getDeleted, true));
  223. if (count != specificGids.size()) {
  224. throw BusinessRuntimeException.getInstance("存在被删除或已下架的平台");
  225. }
  226. BigDecimal specificPrice = goodsDonSku.getSpecificPrice();
  227. if (specificPrice == null || specificPrice.compareTo(BigDecimal.ZERO) <= 0) {
  228. throw BusinessRuntimeException.getInstance("请输入正确的特定价格");
  229. }
  230. }
  231. goodsDonSkuMapper.insert(goodsDonSku);
  232. }
  233. }
  234. @Override
  235. public void combinationSku(Long goodsId, List<CombinationSkuReq> combinations) throws Exception {
  236. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  237. if (goodsDon == null) throw BusinessRuntimeException.getInstance("平台不存在");
  238. if (goodsDon.getType() != 2) {
  239. throw BusinessRuntimeException.getInstance("暂只支持实物规格");
  240. }
  241. GoodsDonSpec loveSpec = this.getOne(Wrappers.lambdaQuery(GoodsDonSpec.class).eq(GoodsDonSpec::getGoodsId, goodsId));
  242. if (null == loveSpec) {
  243. throw new RuntimeException("该商品未设置总规格, 请设置总规格.");
  244. }
  245. String specsJson = loveSpec.getSpecsJson();
  246. List<ReqGoodsSkuSpec> specsList = Jsons.parseList(specsJson, ReqGoodsSkuSpec.class);
  247. Map<String, List<ReqGoodsSkuSpec>> specValueMap = specsList.stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec::getKey));
  248. if (combinations.isEmpty()) {
  249. throw BusinessRuntimeException.getInstance("请选择对应的规格组合");
  250. }
  251. List<ReqGoodsSkuInsert.Sku> skus = new LinkedList<>();
  252. ReqGoodsSkuInsert reqGoodsSkuInsert = new ReqGoodsSkuInsert();
  253. reqGoodsSkuInsert.setGoodsId(goodsId);
  254. List<List<Integer>> specIdLists = new LinkedList<>();
  255. CombinationSkuReq request = combinations.get(0);
  256. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = specValueMap.get(request.getKey());
  257. if (reqGoodsSkuSpecs == null) {
  258. throw BusinessRuntimeException.getInstance("传入的总规格属性:{0}不存在", request.getKey());
  259. }
  260. Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap = reqGoodsSkuSpecs.get(0).getValues().stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  261. if (CollUtil.isNotEmpty(request.getValues())) {
  262. unifiedHandleCombinationSku(combinations, request.getValues(), specValueMap, nodeIdMap, specIdLists, skus);
  263. } else {
  264. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  265. unifiedHandleCombinationSku(combinations,reqGoodsSkuSpec.getValues(),specValueMap, nodeIdMap,specIdLists,skus);
  266. }
  267. reqGoodsSkuInsert.setSkus(skus);
  268. skuAppend(reqGoodsSkuInsert, goodsDon);
  269. }
  270. public void unifiedHandleCombinationSku(List<CombinationSkuReq> combinations, List<ReqGoodsSkuSpec.Node> values, Map<String, List<ReqGoodsSkuSpec>> specValueMap, Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap, List<List<Integer>> specIdLists, List<ReqGoodsSkuInsert.Sku> skus) throws Exception {
  271. for (ReqGoodsSkuSpec.Node node_value : values) {
  272. ReqGoodsSkuInsert.Sku sku = new ReqGoodsSkuInsert.Sku();
  273. sku.setPrice(BigDecimal.ZERO);
  274. List<Integer> specIds = new LinkedList<>();
  275. unifiedHandleSecondCombinationSku(node_value, nodeIdMap, sku, specIds);
  276. recurCombinationSku(combinations, combinations.size(), 1, 1, specIds, specIdLists, sku, specValueMap, skus);
  277. }
  278. }
  279. public void unifiedHandleSecondCombinationSku(ReqGoodsSkuSpec.Node temp_node, Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap, ReqGoodsSkuInsert.Sku sku, List<Integer> specIds) {
  280. List<ReqGoodsSkuSpec.Node> nodes = nodeIdMap.get(temp_node.getId());
  281. if (CollUtil.isEmpty(nodes)) {
  282. throw BusinessRuntimeException.getInstance("{0}规格不存在", temp_node.getValue());
  283. }
  284. ReqGoodsSkuSpec.Node node = nodes.get(0);
  285. BigDecimal price = node.getPrice();
  286. if (price == null && node.getValue().contains("无")) {
  287. price = BigDecimal.ZERO;
  288. }
  289. if (price == null) {
  290. throw BusinessRuntimeException.getInstance("该实物子规格:{0}未配置价格", node.getValue());
  291. }
  292. sku.setPrice(sku.getPrice().add(price));
  293. specIds.add(temp_node.getId());
  294. if (StrUtil.isNotEmpty(node.getBenefitsList())) {
  295. sku.setIsBenefits(true);
  296. sku.setBenefitsList(node.getBenefitsList());
  297. }
  298. }
  299. /**
  300. * @param combinations 传入的组合数据
  301. * @param total 总共需要组合的大小
  302. * @param has 已有组合数大小
  303. * @param start 下一个组合数组下表
  304. * @param specIds 组合规格ids
  305. * @param sku 待插入库中的sku
  306. * @param specValueMap 总规格库中对应values
  307. */
  308. private void recurCombinationSku(List<CombinationSkuReq> combinations, Integer total, Integer has, Integer start, List<Integer> specIds, List<List<Integer>> specIdLists, ReqGoodsSkuInsert.Sku sku, Map<String, List<ReqGoodsSkuSpec>> specValueMap, List<ReqGoodsSkuInsert.Sku> skus) throws Exception {
  309. if (has == total) {
  310. if (specIdLists.contains(specIds)) {
  311. return;
  312. }
  313. sku.setSpecIds(Jsons.toJson(specIds));
  314. skus.add(sku);
  315. return;
  316. }
  317. CombinationSkuReq request = combinations.get(start);
  318. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = specValueMap.get(request.getKey());
  319. if (reqGoodsSkuSpecs == null) {
  320. throw BusinessRuntimeException.getInstance("总规格属性{0}不存在", request.getKey());
  321. }
  322. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  323. Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap = reqGoodsSkuSpec.getValues().stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  324. if (CollUtil.isNotEmpty(request.getValues())) {
  325. createNewSkuAndRecurCombination(combinations, request.getValues(), nodeIdMap, has, start, specIds, specIdLists, sku, specValueMap, skus);
  326. return;
  327. }
  328. createNewSkuAndRecurCombination(combinations, reqGoodsSkuSpec.getValues(), nodeIdMap, has, start, specIds, specIdLists, sku, specValueMap, skus);
  329. }
  330. /**
  331. * 新的规格 递归
  332. */
  333. public void createNewSkuAndRecurCombination(List<CombinationSkuReq> combinations, List<ReqGoodsSkuSpec.Node> values, Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap, Integer has, Integer start, List<Integer> specIds, List<List<Integer>> specIdLists, ReqGoodsSkuInsert.Sku sku, Map<String, List<ReqGoodsSkuSpec>> specValueMap, List<ReqGoodsSkuInsert.Sku> skus) throws Exception {
  334. for (ReqGoodsSkuSpec.Node node_value : values) {
  335. BigDecimal last_price = sku.getPrice();
  336. unifiedHandleSecondCombinationSku(node_value, nodeIdMap, sku, specIds);
  337. recurCombinationSku(combinations, combinations.size(), has + 1, start + 1, specIds, specIdLists, sku, specValueMap, skus);
  338. specIds.remove(node_value.getId());
  339. sku = new ReqGoodsSkuInsert.Sku();
  340. sku.setPrice(last_price);
  341. }
  342. }
  343. /**
  344. * 更新删除总属性 一级分类 校验
  345. */
  346. public Map<Integer, ReqGoodsSkuSpec.Node> checkUpdateGoodsSpec(Long goodsId, Map<String, List<ReqGoodsSkuSpec>> dbSpecMap, Map<Integer, ReqGoodsSkuSpec.Node> dbNodeMap, List<ReqGoodsSkuSpec> specs) {
  347. if (CollUtil.isNotEmpty(dbSpecMap)) {
  348. Map<String, List<ReqGoodsSkuSpec>> updateSpecMap = specs.stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec::getKey));
  349. dbNodeMap = new ConcurrentHashMap<>();
  350. for (String key : dbSpecMap.keySet()) {
  351. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = updateSpecMap.get(key);
  352. if (CollUtil.isNotEmpty(reqGoodsSkuSpecs)) {
  353. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  354. if (CollUtil.isEmpty(reqGoodsSkuSpec.getValues())) {
  355. //删除整个一级分类key
  356. String specFirstKey = reqGoodsSkuSpec.getKey();
  357. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  358. .eq(GoodsDonSku::getGoodsId, goodsId)
  359. .like(GoodsDonSku::getSpecJson, specFirstKey).last("limit 1"));
  360. if (sku != null) {
  361. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  362. }
  363. } else {
  364. Map<Integer, List<ReqGoodsSkuSpec.Node>> updateSecondMap = reqGoodsSkuSpec.getValues().stream().filter(node -> node.getId() != null).collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  365. ReqGoodsSkuSpec dbReqGoodsSkuSpec = dbSpecMap.get(key).get(0);
  366. for (ReqGoodsSkuSpec.Node dbSecondValue : dbReqGoodsSkuSpec.getValues()) {
  367. if (updateSecondMap.get(dbSecondValue.getId()) == null) {
  368. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  369. .eq(GoodsDonSku::getGoodsId, goodsId)
  370. .like(GoodsDonSku::getSpecVal, dbSecondValue.getValue()).last("limit 1"));
  371. if (sku != null) {
  372. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  373. }
  374. }
  375. dbNodeMap.put(dbSecondValue.getId(), dbSecondValue);
  376. }
  377. }
  378. }
  379. //去除总规格分类时
  380. //校验是否已引用该属性规格
  381. if (CollUtil.isEmpty(reqGoodsSkuSpecs)) {
  382. ReqGoodsSkuSpec dbReqGoodsSkuSpec = dbSpecMap.get(key).get(0);
  383. //删除整个一级分类key
  384. String specFirstKey = dbReqGoodsSkuSpec.getKey();
  385. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  386. .eq(GoodsDonSku::getGoodsId, goodsId)
  387. .like(GoodsDonSku::getSpecJson, specFirstKey).last("limit 1"));
  388. if (sku != null) {
  389. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  390. }
  391. }
  392. }
  393. return dbNodeMap;
  394. }
  395. return null;
  396. }
  397. /**
  398. * 更新二级分类标题价格等 同步库中sku
  399. */
  400. public void syncSpecNodeSku(Long goodsId, Map<Integer, ReqGoodsSkuSpec.Node> dbNodeMap, ReqGoodsSkuSpec.Node e) {
  401. if (CollUtil.isNotEmpty(dbNodeMap)) {
  402. ReqGoodsSkuSpec.Node dbNode = dbNodeMap.get(e.getId());
  403. if (dbNode == null) {
  404. return;
  405. }
  406. AtomicBoolean isUpdateTitle = new AtomicBoolean(false);
  407. AtomicBoolean isUpdatePrice = new AtomicBoolean(false);
  408. AtomicBoolean isUpdateBenefits = new AtomicBoolean(false);
  409. if (!StrUtil.equals(dbNode.getValue(), e.getValue())) {
  410. isUpdateTitle.set(true);
  411. }
  412. if (e.getPrice() == null) {
  413. e.setPrice(BigDecimal.ZERO);
  414. }
  415. if (dbNode.getPrice() != null && e.getPrice().compareTo(dbNode.getPrice()) != 0) {
  416. isUpdatePrice.set(true);
  417. }
  418. if (!StrUtil.equals(dbNode.getBenefitsList(), e.getBenefitsList())) {
  419. isUpdateBenefits.set(true);
  420. }
  421. if (isUpdateTitle.get() || isUpdatePrice.get() || isUpdateBenefits.get()) {
  422. List<GoodsDonSku> goodsDonSkus = goodsDonSkuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class)
  423. .eq(GoodsDonSku::getGoodsId, goodsId));
  424. goodsDonSkus.forEach(sku -> {
  425. try {
  426. List<Integer> specIdsList = Jsons.parseList(sku.getSpecIds(), Integer.class);
  427. if (specIdsList.contains(dbNode.getId())) {
  428. if (isUpdateTitle.get()) {
  429. sku.setSpecJson(StrUtil.replace(sku.getSpecJson(), dbNode.getValue(), e.getValue()));
  430. sku.setSpecVal(StrUtil.replace(sku.getSpecVal(), dbNode.getValue(), e.getValue()));
  431. }
  432. if (isUpdatePrice.get()) {
  433. sku.setPrice(sku.getPrice().subtract(dbNode.getPrice()).add(e.getPrice()));
  434. }
  435. if (isUpdateBenefits.get()) {
  436. if (StrUtil.isEmpty(e.getBenefitsList())) {
  437. sku.setBenefitsList(StrUtil.EMPTY);
  438. } else {
  439. try {
  440. sku.setBenefitsList(e.getBenefitsList());
  441. } catch (Exception exception) {
  442. }
  443. }
  444. }
  445. goodsDonSkuMapper.updateById(sku);
  446. }
  447. } catch (Exception exception) {
  448. }
  449. });
  450. }
  451. }
  452. }
  453. }