CmsGoodsDonSpecServiceImpl.java 21 KB

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