CmsGoodsDonSpecServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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.snowflake.Sequence;
  9. import com.cyksj.common.util.Jsons;
  10. import com.cyksj.mapper.GoodsDonMapper;
  11. import com.cyksj.mapper.GoodsDonSkuMapper;
  12. import com.cyksj.mapper.GoodsDonSpecMapper;
  13. import com.cyksj.model.entity.GoodsDon;
  14. import com.cyksj.model.entity.GoodsDonSku;
  15. import com.cyksj.model.entity.GoodsDonSpec;
  16. import com.cyksj.model.manage.request.ReqGoodsSkuInsert;
  17. import com.cyksj.model.manage.request.ReqGoodsSkuSpec;
  18. import com.cyksj.model.manage.request.ReqGoodsSpecSingle;
  19. import com.cyksj.model.request.CombinationSkuReq;
  20. import com.cyksj.service.mange.CmsGoodsDonSpecService;
  21. import com.fasterxml.jackson.databind.JavaType;
  22. import lombok.RequiredArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.springframework.stereotype.Service;
  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 static Sequence SEQUENCE = new Sequence(0);
  40. private final GoodsDonMapper goodsDonMapper;
  41. private final GoodsDonSkuMapper goodsDonSkuMapper;
  42. @Override
  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. goodsDonSkuMapper.insert(goodsDonSku);
  217. }
  218. }
  219. @Override
  220. public void combinationSku(Long goodsId, List<CombinationSkuReq> combinations) throws Exception {
  221. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  222. if (goodsDon == null) throw BusinessRuntimeException.getInstance("平台不存在");
  223. if (goodsDon.getType() != 2) {
  224. throw BusinessRuntimeException.getInstance("暂只支持实物规格");
  225. }
  226. GoodsDonSpec loveSpec = this.getOne(Wrappers.lambdaQuery(GoodsDonSpec.class).eq(GoodsDonSpec::getGoodsId, goodsId));
  227. if (null == loveSpec) {
  228. throw new RuntimeException("该商品未设置总规格, 请设置总规格.");
  229. }
  230. String specsJson = loveSpec.getSpecsJson();
  231. List<ReqGoodsSkuSpec> specsList = Jsons.parseList(specsJson, ReqGoodsSkuSpec.class);
  232. Map<String, List<ReqGoodsSkuSpec>> specValueMap = specsList.stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec::getKey));
  233. if (combinations.isEmpty()) {
  234. throw BusinessRuntimeException.getInstance("请选择对应的规格组合");
  235. }
  236. List<ReqGoodsSkuInsert.Sku> skus = new LinkedList<>();
  237. ReqGoodsSkuInsert reqGoodsSkuInsert = new ReqGoodsSkuInsert();
  238. reqGoodsSkuInsert.setGoodsId(goodsId);
  239. List<List<Integer>> specIdLists = new LinkedList<>();
  240. CombinationSkuReq request = combinations.get(0);
  241. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = specValueMap.get(request.getKey());
  242. if (reqGoodsSkuSpecs == null) {
  243. throw BusinessRuntimeException.getInstance("传入的总规格属性:{0}不存在", request.getKey());
  244. }
  245. Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap = reqGoodsSkuSpecs.get(0).getValues().stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  246. if (CollUtil.isNotEmpty(request.getValues())) {
  247. unifiedHandleCombinationSku(combinations, request.getValues(), specValueMap, nodeIdMap, specIdLists, skus);
  248. } else {
  249. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  250. unifiedHandleCombinationSku(combinations,reqGoodsSkuSpec.getValues(),specValueMap, nodeIdMap,specIdLists,skus);
  251. }
  252. reqGoodsSkuInsert.setSkus(skus);
  253. skuAppend(reqGoodsSkuInsert, goodsDon);
  254. }
  255. 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 {
  256. for (ReqGoodsSkuSpec.Node node_value : values) {
  257. ReqGoodsSkuInsert.Sku sku = new ReqGoodsSkuInsert.Sku();
  258. sku.setPrice(BigDecimal.ZERO);
  259. List<Integer> specIds = new LinkedList<>();
  260. unifiedHandleSecondCombinationSku(node_value, nodeIdMap, sku, specIds);
  261. recurCombinationSku(combinations, combinations.size(), 1, 1, specIds, specIdLists, sku, specValueMap, skus);
  262. }
  263. }
  264. public void unifiedHandleSecondCombinationSku(ReqGoodsSkuSpec.Node temp_node, Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap, ReqGoodsSkuInsert.Sku sku, List<Integer> specIds) {
  265. List<ReqGoodsSkuSpec.Node> nodes = nodeIdMap.get(temp_node.getId());
  266. if (CollUtil.isEmpty(nodes)) {
  267. throw BusinessRuntimeException.getInstance("{0}规格不存在", temp_node.getValue());
  268. }
  269. ReqGoodsSkuSpec.Node node = nodes.get(0);
  270. BigDecimal price = node.getPrice();
  271. if (price == null && node.getValue().contains("无")) {
  272. price = BigDecimal.ZERO;
  273. }
  274. if (price == null) {
  275. throw BusinessRuntimeException.getInstance("该实物子规格:{0}未配置价格", node.getValue());
  276. }
  277. sku.setPrice(sku.getPrice().add(price));
  278. specIds.add(temp_node.getId());
  279. if (StrUtil.isNotEmpty(node.getBenefitsList())) {
  280. sku.setIsBenefits(true);
  281. sku.setBenefitsList(node.getBenefitsList());
  282. }
  283. }
  284. /**
  285. * @param combinations 传入的组合数据
  286. * @param total 总共需要组合的大小
  287. * @param has 已有组合数大小
  288. * @param start 下一个组合数组下表
  289. * @param specIds 组合规格ids
  290. * @param sku 待插入库中的sku
  291. * @param specValueMap 总规格库中对应values
  292. */
  293. 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 {
  294. if (has == total) {
  295. if (specIdLists.contains(specIds)) {
  296. return;
  297. }
  298. sku.setSpecIds(Jsons.toJson(specIds));
  299. skus.add(sku);
  300. return;
  301. }
  302. CombinationSkuReq request = combinations.get(start);
  303. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = specValueMap.get(request.getKey());
  304. if (reqGoodsSkuSpecs == null) {
  305. throw BusinessRuntimeException.getInstance("总规格属性{0}不存在", request.getKey());
  306. }
  307. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  308. Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap = reqGoodsSkuSpec.getValues().stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  309. if (CollUtil.isNotEmpty(request.getValues())) {
  310. createNewSkuAndRecurCombination(combinations, request.getValues(), nodeIdMap, has, start, specIds, specIdLists, sku, specValueMap, skus);
  311. return;
  312. }
  313. createNewSkuAndRecurCombination(combinations, reqGoodsSkuSpec.getValues(), nodeIdMap, has, start, specIds, specIdLists, sku, specValueMap, skus);
  314. }
  315. /**
  316. * 新的规格 递归
  317. */
  318. 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 {
  319. for (ReqGoodsSkuSpec.Node node_value : values) {
  320. BigDecimal last_price = sku.getPrice();
  321. unifiedHandleSecondCombinationSku(node_value, nodeIdMap, sku, specIds);
  322. recurCombinationSku(combinations, combinations.size(), has + 1, start + 1, specIds, specIdLists, sku, specValueMap, skus);
  323. specIds.remove(node_value.getId());
  324. sku = new ReqGoodsSkuInsert.Sku();
  325. sku.setPrice(last_price);
  326. }
  327. }
  328. /**
  329. * 更新删除总属性 一级分类 校验
  330. */
  331. public Map<Integer, ReqGoodsSkuSpec.Node> checkUpdateGoodsSpec(Long goodsId, Map<String, List<ReqGoodsSkuSpec>> dbSpecMap, Map<Integer, ReqGoodsSkuSpec.Node> dbNodeMap, List<ReqGoodsSkuSpec> specs) {
  332. if (CollUtil.isNotEmpty(dbSpecMap)) {
  333. Map<String, List<ReqGoodsSkuSpec>> updateSpecMap = specs.stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec::getKey));
  334. dbNodeMap = new ConcurrentHashMap<>();
  335. for (String key : dbSpecMap.keySet()) {
  336. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = updateSpecMap.get(key);
  337. if (CollUtil.isNotEmpty(reqGoodsSkuSpecs)) {
  338. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  339. if (CollUtil.isEmpty(reqGoodsSkuSpec.getValues())) {
  340. //删除整个一级分类key
  341. String specFirstKey = reqGoodsSkuSpec.getKey();
  342. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  343. .eq(GoodsDonSku::getGoodsId, goodsId)
  344. .like(GoodsDonSku::getSpecJson, specFirstKey).last("limit 1"));
  345. if (sku != null) {
  346. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  347. }
  348. } else {
  349. Map<Integer, List<ReqGoodsSkuSpec.Node>> updateSecondMap = reqGoodsSkuSpec.getValues().stream().filter(node -> node.getId() != null).collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  350. ReqGoodsSkuSpec dbReqGoodsSkuSpec = dbSpecMap.get(key).get(0);
  351. for (ReqGoodsSkuSpec.Node dbSecondValue : dbReqGoodsSkuSpec.getValues()) {
  352. if (updateSecondMap.get(dbSecondValue.getId()) == null) {
  353. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  354. .eq(GoodsDonSku::getGoodsId, goodsId)
  355. .like(GoodsDonSku::getSpecVal, dbSecondValue.getValue()).last("limit 1"));
  356. if (sku != null) {
  357. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  358. }
  359. }
  360. dbNodeMap.put(dbSecondValue.getId(), dbSecondValue);
  361. }
  362. }
  363. }
  364. //去除总规格分类时
  365. //校验是否已引用该属性规格
  366. if (CollUtil.isEmpty(reqGoodsSkuSpecs)) {
  367. ReqGoodsSkuSpec dbReqGoodsSkuSpec = dbSpecMap.get(key).get(0);
  368. //删除整个一级分类key
  369. String specFirstKey = dbReqGoodsSkuSpec.getKey();
  370. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  371. .eq(GoodsDonSku::getGoodsId, goodsId)
  372. .like(GoodsDonSku::getSpecJson, specFirstKey).last("limit 1"));
  373. if (sku != null) {
  374. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  375. }
  376. }
  377. }
  378. return dbNodeMap;
  379. }
  380. return null;
  381. }
  382. /**
  383. * 更新二级分类标题价格等 同步库中sku
  384. */
  385. public void syncSpecNodeSku(Long goodsId, Map<Integer, ReqGoodsSkuSpec.Node> dbNodeMap, ReqGoodsSkuSpec.Node e) {
  386. if (CollUtil.isNotEmpty(dbNodeMap)) {
  387. ReqGoodsSkuSpec.Node dbNode = dbNodeMap.get(e.getId());
  388. if (dbNode == null) {
  389. return;
  390. }
  391. AtomicBoolean isUpdateTitle = new AtomicBoolean(false);
  392. AtomicBoolean isUpdatePrice = new AtomicBoolean(false);
  393. AtomicBoolean isUpdateBenefits = new AtomicBoolean(false);
  394. if (!StrUtil.equals(dbNode.getValue(), e.getValue())) {
  395. isUpdateTitle.set(true);
  396. }
  397. if (e.getPrice() == null) {
  398. e.setPrice(BigDecimal.ZERO);
  399. }
  400. if (dbNode.getPrice() != null && e.getPrice().compareTo(dbNode.getPrice()) != 0) {
  401. isUpdatePrice.set(true);
  402. }
  403. if (!StrUtil.equals(dbNode.getBenefitsList(), e.getBenefitsList())) {
  404. isUpdateBenefits.set(true);
  405. }
  406. if (isUpdateTitle.get() || isUpdatePrice.get() || isUpdateBenefits.get()) {
  407. List<GoodsDonSku> goodsDonSkus = goodsDonSkuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class)
  408. .eq(GoodsDonSku::getGoodsId, goodsId));
  409. goodsDonSkus.forEach(sku -> {
  410. try {
  411. List<Integer> specIdsList = Jsons.parseList(sku.getSpecIds(), Integer.class);
  412. if (specIdsList.contains(dbNode.getId())) {
  413. if (isUpdateTitle.get()) {
  414. sku.setSpecJson(sku.getSpecJson().replaceAll(dbNode.getValue(), e.getValue()));
  415. sku.setSpecVal(sku.getSpecVal().replaceAll(dbNode.getValue(), e.getValue()));
  416. }
  417. if (isUpdatePrice.get()) {
  418. sku.setPrice(sku.getPrice().subtract(dbNode.getPrice()).add(e.getPrice()));
  419. }
  420. if (isUpdateBenefits.get()) {
  421. if (StrUtil.isEmpty(e.getBenefitsList())) {
  422. sku.setBenefitsList(StrUtil.EMPTY);
  423. } else {
  424. try {
  425. sku.setBenefitsList(e.getBenefitsList());
  426. } catch (Exception exception) {
  427. }
  428. }
  429. }
  430. goodsDonSkuMapper.updateById(sku);
  431. }
  432. } catch (Exception exception) {
  433. }
  434. });
  435. }
  436. }
  437. }
  438. }