CmsGoodsDonSpecServiceImpl.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. Integer count = goodsDonMapper.selectCount(Wrappers.lambdaQuery(GoodsDon.class)
  223. .in(GoodsDon::getId, specificGids)
  224. .eq(GoodsDon::getStatus, true)
  225. .eq(GoodsDon::getDeleted, true));
  226. if (count != specificGids.size()) {
  227. throw BusinessRuntimeException.getInstance("存在被删除或已下架的平台");
  228. }
  229. BigDecimal specificPrice = goodsDonSku.getSpecificPrice();
  230. if (specificPrice == null || specificPrice.compareTo(BigDecimal.ZERO) <= 0) {
  231. throw BusinessRuntimeException.getInstance("请输入正确的特定价格");
  232. }
  233. }
  234. if (StrUtil.isNotEmpty(goodsDonSku.getUserOwns())) {
  235. List<Long> userOwnsList = Jsons.parseList(goodsDonSku.getUserOwns(), Long.class);
  236. goodsDonSku.setUserOwns(userOwnsList.toString());
  237. }
  238. goodsDonSkuMapper.insert(goodsDonSku);
  239. DistributeGoodsSkuAlert distributeGoodsSkuAlert = new DistributeGoodsSkuAlert();
  240. distributeGoodsSkuAlert.setGoodsId(goods.getId());
  241. distributeGoodsSkuAlert.setSkuId(goodsDonSku.getId());
  242. distributeGoodsSkuAlertMapper.insert(distributeGoodsSkuAlert);
  243. }
  244. }
  245. @Override
  246. public void combinationSku(Long goodsId, List<CombinationSkuReq> combinations) throws Exception {
  247. GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
  248. if (goodsDon == null) throw BusinessRuntimeException.getInstance("平台不存在");
  249. if (goodsDon.getType() != 2) {
  250. throw BusinessRuntimeException.getInstance("暂只支持实物规格");
  251. }
  252. GoodsDonSpec loveSpec = this.getOne(Wrappers.lambdaQuery(GoodsDonSpec.class).eq(GoodsDonSpec::getGoodsId, goodsId));
  253. if (null == loveSpec) {
  254. throw new RuntimeException("该商品未设置总规格, 请设置总规格.");
  255. }
  256. String specsJson = loveSpec.getSpecsJson();
  257. List<ReqGoodsSkuSpec> specsList = Jsons.parseList(specsJson, ReqGoodsSkuSpec.class);
  258. Map<String, List<ReqGoodsSkuSpec>> specValueMap = specsList.stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec::getKey));
  259. if (combinations.isEmpty()) {
  260. throw BusinessRuntimeException.getInstance("请选择对应的规格组合");
  261. }
  262. List<ReqGoodsSkuInsert.Sku> skus = new LinkedList<>();
  263. ReqGoodsSkuInsert reqGoodsSkuInsert = new ReqGoodsSkuInsert();
  264. reqGoodsSkuInsert.setGoodsId(goodsId);
  265. List<List<Integer>> specIdLists = new LinkedList<>();
  266. CombinationSkuReq request = combinations.get(0);
  267. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = specValueMap.get(request.getKey());
  268. if (reqGoodsSkuSpecs == null) {
  269. throw BusinessRuntimeException.getInstance("传入的总规格属性:{0}不存在", request.getKey());
  270. }
  271. Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap = reqGoodsSkuSpecs.get(0).getValues().stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  272. if (CollUtil.isNotEmpty(request.getValues())) {
  273. unifiedHandleCombinationSku(combinations, request.getValues(), specValueMap, nodeIdMap, specIdLists, skus);
  274. } else {
  275. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  276. unifiedHandleCombinationSku(combinations,reqGoodsSkuSpec.getValues(),specValueMap, nodeIdMap,specIdLists,skus);
  277. }
  278. reqGoodsSkuInsert.setSkus(skus);
  279. skuAppend(reqGoodsSkuInsert, goodsDon);
  280. }
  281. 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 {
  282. for (ReqGoodsSkuSpec.Node node_value : values) {
  283. ReqGoodsSkuInsert.Sku sku = new ReqGoodsSkuInsert.Sku();
  284. sku.setPrice(BigDecimal.ZERO);
  285. List<Integer> specIds = new LinkedList<>();
  286. unifiedHandleSecondCombinationSku(node_value, nodeIdMap, sku, specIds);
  287. recurCombinationSku(combinations, combinations.size(), 1, 1, specIds, specIdLists, sku, specValueMap, skus);
  288. }
  289. }
  290. public void unifiedHandleSecondCombinationSku(ReqGoodsSkuSpec.Node temp_node, Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap, ReqGoodsSkuInsert.Sku sku, List<Integer> specIds) {
  291. List<ReqGoodsSkuSpec.Node> nodes = nodeIdMap.get(temp_node.getId());
  292. if (CollUtil.isEmpty(nodes)) {
  293. throw BusinessRuntimeException.getInstance("{0}规格不存在", temp_node.getValue());
  294. }
  295. ReqGoodsSkuSpec.Node node = nodes.get(0);
  296. BigDecimal price = node.getPrice();
  297. if (price == null && node.getValue().contains("无")) {
  298. price = BigDecimal.ZERO;
  299. }
  300. if (price == null) {
  301. throw BusinessRuntimeException.getInstance("该实物子规格:{0}未配置价格", node.getValue());
  302. }
  303. sku.setPrice(sku.getPrice().add(price));
  304. specIds.add(temp_node.getId());
  305. if (StrUtil.isNotEmpty(node.getBenefitsList())) {
  306. sku.setIsBenefits(true);
  307. sku.setBenefitsList(node.getBenefitsList());
  308. }
  309. }
  310. /**
  311. * @param combinations 传入的组合数据
  312. * @param total 总共需要组合的大小
  313. * @param has 已有组合数大小
  314. * @param start 下一个组合数组下表
  315. * @param specIds 组合规格ids
  316. * @param sku 待插入库中的sku
  317. * @param specValueMap 总规格库中对应values
  318. */
  319. 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 {
  320. if (has == total) {
  321. if (specIdLists.contains(specIds)) {
  322. return;
  323. }
  324. sku.setSpecIds(Jsons.toJson(specIds));
  325. skus.add(sku);
  326. return;
  327. }
  328. CombinationSkuReq request = combinations.get(start);
  329. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = specValueMap.get(request.getKey());
  330. if (reqGoodsSkuSpecs == null) {
  331. throw BusinessRuntimeException.getInstance("总规格属性{0}不存在", request.getKey());
  332. }
  333. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  334. Map<Integer, List<ReqGoodsSkuSpec.Node>> nodeIdMap = reqGoodsSkuSpec.getValues().stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  335. if (CollUtil.isNotEmpty(request.getValues())) {
  336. createNewSkuAndRecurCombination(combinations, request.getValues(), nodeIdMap, has, start, specIds, specIdLists, sku, specValueMap, skus);
  337. return;
  338. }
  339. createNewSkuAndRecurCombination(combinations, reqGoodsSkuSpec.getValues(), nodeIdMap, has, start, specIds, specIdLists, sku, specValueMap, skus);
  340. }
  341. /**
  342. * 新的规格 递归
  343. */
  344. 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 {
  345. for (ReqGoodsSkuSpec.Node node_value : values) {
  346. BigDecimal last_price = sku.getPrice();
  347. unifiedHandleSecondCombinationSku(node_value, nodeIdMap, sku, specIds);
  348. recurCombinationSku(combinations, combinations.size(), has + 1, start + 1, specIds, specIdLists, sku, specValueMap, skus);
  349. specIds.remove(node_value.getId());
  350. sku = new ReqGoodsSkuInsert.Sku();
  351. sku.setPrice(last_price);
  352. }
  353. }
  354. /**
  355. * 更新删除总属性 一级分类 校验
  356. */
  357. public Map<Integer, ReqGoodsSkuSpec.Node> checkUpdateGoodsSpec(Long goodsId, Map<String, List<ReqGoodsSkuSpec>> dbSpecMap, Map<Integer, ReqGoodsSkuSpec.Node> dbNodeMap, List<ReqGoodsSkuSpec> specs) {
  358. if (CollUtil.isNotEmpty(dbSpecMap)) {
  359. Map<String, List<ReqGoodsSkuSpec>> updateSpecMap = specs.stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec::getKey));
  360. dbNodeMap = new ConcurrentHashMap<>();
  361. for (String key : dbSpecMap.keySet()) {
  362. List<ReqGoodsSkuSpec> reqGoodsSkuSpecs = updateSpecMap.get(key);
  363. if (CollUtil.isNotEmpty(reqGoodsSkuSpecs)) {
  364. ReqGoodsSkuSpec reqGoodsSkuSpec = reqGoodsSkuSpecs.get(0);
  365. if (CollUtil.isEmpty(reqGoodsSkuSpec.getValues())) {
  366. //删除整个一级分类key
  367. String specFirstKey = reqGoodsSkuSpec.getKey();
  368. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  369. .eq(GoodsDonSku::getGoodsId, goodsId)
  370. .like(GoodsDonSku::getSpecJson, specFirstKey).last("limit 1"));
  371. if (sku != null) {
  372. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  373. }
  374. } else {
  375. Map<Integer, List<ReqGoodsSkuSpec.Node>> updateSecondMap = reqGoodsSkuSpec.getValues().stream().filter(node -> node.getId() != null).collect(Collectors.groupingBy(ReqGoodsSkuSpec.Node::getId));
  376. ReqGoodsSkuSpec dbReqGoodsSkuSpec = dbSpecMap.get(key).get(0);
  377. for (ReqGoodsSkuSpec.Node dbSecondValue : dbReqGoodsSkuSpec.getValues()) {
  378. if (updateSecondMap.get(dbSecondValue.getId()) == null) {
  379. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  380. .eq(GoodsDonSku::getGoodsId, goodsId)
  381. .like(GoodsDonSku::getSpecVal, dbSecondValue.getValue()).last("limit 1"));
  382. if (sku != null) {
  383. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  384. }
  385. }
  386. dbNodeMap.put(dbSecondValue.getId(), dbSecondValue);
  387. }
  388. }
  389. }
  390. //去除总规格分类时
  391. //校验是否已引用该属性规格
  392. if (CollUtil.isEmpty(reqGoodsSkuSpecs)) {
  393. ReqGoodsSkuSpec dbReqGoodsSkuSpec = dbSpecMap.get(key).get(0);
  394. //删除整个一级分类key
  395. String specFirstKey = dbReqGoodsSkuSpec.getKey();
  396. GoodsDonSku sku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  397. .eq(GoodsDonSku::getGoodsId, goodsId)
  398. .like(GoodsDonSku::getSpecJson, specFirstKey).last("limit 1"));
  399. if (sku != null) {
  400. throw BusinessRuntimeException.getInstance("删除总规格分类,存在使用该总规格分类的规格");
  401. }
  402. }
  403. }
  404. return dbNodeMap;
  405. }
  406. return null;
  407. }
  408. /**
  409. * 更新二级分类标题价格等 同步库中sku
  410. */
  411. public void syncSpecNodeSku(Long goodsId, Map<Integer, ReqGoodsSkuSpec.Node> dbNodeMap, ReqGoodsSkuSpec.Node e) {
  412. if (CollUtil.isNotEmpty(dbNodeMap)) {
  413. ReqGoodsSkuSpec.Node dbNode = dbNodeMap.get(e.getId());
  414. if (dbNode == null) {
  415. return;
  416. }
  417. AtomicBoolean isUpdateTitle = new AtomicBoolean(false);
  418. AtomicBoolean isUpdatePrice = new AtomicBoolean(false);
  419. AtomicBoolean isUpdateBenefits = new AtomicBoolean(false);
  420. if (!StrUtil.equals(dbNode.getValue(), e.getValue())) {
  421. isUpdateTitle.set(true);
  422. }
  423. if (e.getPrice() == null) {
  424. e.setPrice(BigDecimal.ZERO);
  425. }
  426. if (dbNode.getPrice() != null && e.getPrice().compareTo(dbNode.getPrice()) != 0) {
  427. isUpdatePrice.set(true);
  428. }
  429. if (!StrUtil.equals(dbNode.getBenefitsList(), e.getBenefitsList())) {
  430. isUpdateBenefits.set(true);
  431. }
  432. if (isUpdateTitle.get() || isUpdatePrice.get() || isUpdateBenefits.get()) {
  433. List<GoodsDonSku> goodsDonSkus = goodsDonSkuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class)
  434. .eq(GoodsDonSku::getGoodsId, goodsId));
  435. goodsDonSkus.forEach(sku -> {
  436. try {
  437. List<Integer> specIdsList = Jsons.parseList(sku.getSpecIds(), Integer.class);
  438. if (specIdsList.contains(dbNode.getId())) {
  439. if (isUpdateTitle.get()) {
  440. sku.setSpecJson(StrUtil.replace(sku.getSpecJson(), dbNode.getValue(), e.getValue()));
  441. sku.setSpecVal(StrUtil.replace(sku.getSpecVal(), dbNode.getValue(), e.getValue()));
  442. }
  443. if (isUpdatePrice.get()) {
  444. sku.setPrice(sku.getPrice().subtract(dbNode.getPrice()).add(e.getPrice()));
  445. }
  446. if (isUpdateBenefits.get()) {
  447. if (StrUtil.isEmpty(e.getBenefitsList())) {
  448. sku.setBenefitsList(StrUtil.EMPTY);
  449. } else {
  450. try {
  451. sku.setBenefitsList(e.getBenefitsList());
  452. } catch (Exception exception) {
  453. }
  454. }
  455. }
  456. goodsDonSkuMapper.updateById(sku);
  457. }
  458. } catch (Exception exception) {
  459. }
  460. });
  461. }
  462. }
  463. }
  464. }