|
|
@@ -0,0 +1,228 @@
|
|
|
+package com.cyksj.service.rent.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.common.util.Jsons;
|
|
|
+import com.cyksj.mapper.rent.RentGoodsDonMapper;
|
|
|
+import com.cyksj.mapper.rent.RentGoodsDonSkuMapper;
|
|
|
+import com.cyksj.mapper.rent.RentGoodsDonSpecMapper;
|
|
|
+import com.cyksj.model.entity.RentGoodsDon;
|
|
|
+import com.cyksj.model.entity.RentGoodsDonSku;
|
|
|
+import com.cyksj.model.entity.RentGoodsDonSpec;
|
|
|
+import com.cyksj.model.manage.request.ReqGoodsSkuInsert;
|
|
|
+import com.cyksj.model.manage.request.ReqGoodsSkuSpec;
|
|
|
+import com.cyksj.model.manage.request.ReqGoodsSpecSingle;
|
|
|
+import com.cyksj.model.manage.request.rent.ReqRentGoodsSkuInert;
|
|
|
+import com.cyksj.service.rent.CmsRentGoodsDonSpecService;
|
|
|
+import com.fasterxml.jackson.databind.JavaType;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 项目名: yhlxj2
|
|
|
+ * 文件名: CmsRentGoodsDonSpecServiceImpl
|
|
|
+ * 创建者: JavaZou
|
|
|
+ * 创建时间:2024/2/22 16:24
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class CmsRentGoodsDonSpecServiceImpl extends ServiceImpl<RentGoodsDonSpecMapper, RentGoodsDonSpec> implements CmsRentGoodsDonSpecService {
|
|
|
+ private final RentGoodsDonMapper goodsDonMapper;
|
|
|
+
|
|
|
+ private final RentGoodsDonSkuMapper goodsDonSkuMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Throwable.class)
|
|
|
+ public void insertOrUpdateSkuSpec(ReqRentGoodsSkuInert insert, Boolean isUpdate) throws Exception {
|
|
|
+ RentGoodsDon goods = goodsDonMapper.selectById(insert.getGoodsId());
|
|
|
+ if (null == goods) {
|
|
|
+ throw new RuntimeException("找不到商品spu.");
|
|
|
+ }
|
|
|
+ RentGoodsDonSpec loveSpec = this.getOne(Wrappers.lambdaQuery(RentGoodsDonSpec.class).eq(RentGoodsDonSpec::getGoodsId, insert.getGoodsId()));
|
|
|
+ Map<String, List<ReqGoodsSkuSpec>> dbSpecMap = null;
|
|
|
+ if (null != loveSpec) {
|
|
|
+ insert.setId(loveSpec.getId());
|
|
|
+ String specsJson = loveSpec.getSpecsJson();
|
|
|
+ dbSpecMap = Jsons.parseList(specsJson, ReqGoodsSkuSpec.class).stream().collect(Collectors.groupingBy(ReqGoodsSkuSpec::getKey));
|
|
|
+ }
|
|
|
+ if (isUpdate && loveSpec == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("所更新的平台id:{0}属性不存在", insert.getGoodsId());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ReqGoodsSkuSpec> specs = insert.getSpecs();
|
|
|
+ if (null == specs || specs.isEmpty()) {
|
|
|
+ throw new RuntimeException("缺少商品总规格.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 属性最大id
|
|
|
+ Integer maxId = 0;
|
|
|
+ // 统计总属性数
|
|
|
+ Integer total = 0;
|
|
|
+ // 判断id是否有重复
|
|
|
+ Map<Integer, ReqGoodsSpecSingle> map = null;
|
|
|
+ // 规格数量
|
|
|
+ int specNumber = specs.size();
|
|
|
+ for (ReqGoodsSkuSpec spec : specs) {
|
|
|
+ if (StringUtils.isBlank(spec.getKey())) {
|
|
|
+ throw new RuntimeException("存在空白规格名.");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ReqGoodsSkuSpec.Node> values = spec.getValues();
|
|
|
+ if (null == values || values.isEmpty()) {
|
|
|
+ throw new RuntimeException("规格[" + spec.getKey() + "]中缺少属性.");
|
|
|
+ }
|
|
|
+ total += values.size();
|
|
|
+ for (ReqGoodsSkuSpec.Node e : values) {
|
|
|
+ if (null == e.getId() || e.getId() < 0) {
|
|
|
+ e.setId(maxId + 1);
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(e.getValue())) {
|
|
|
+ throw new RuntimeException("规格[" + spec.getKey() + "]中存在空白属性.");
|
|
|
+ }
|
|
|
+ if (e.getId() > maxId) {
|
|
|
+ maxId = e.getId();
|
|
|
+ }
|
|
|
+ //新增时 实物价格要填入
|
|
|
+ if (insert.getId() == null && (e.getDeposit() == null || e.getDeposit().compareTo(BigDecimal.ZERO) < 0)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("实物规格:{0}对应抵押价格请输入", e.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null == map) {
|
|
|
+ map = new HashMap<>(16);
|
|
|
+ }
|
|
|
+
|
|
|
+ ReqGoodsSpecSingle single = new ReqGoodsSpecSingle();
|
|
|
+ single.setId(e.getId());
|
|
|
+ single.setKey(spec.getKey());
|
|
|
+ single.setName(e.getValue());
|
|
|
+ map.put(e.getId(), single);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (map.size() != total) {
|
|
|
+ throw new RuntimeException("规格中存在重复id.");
|
|
|
+ }
|
|
|
+
|
|
|
+ RentGoodsDonSpec spec = new RentGoodsDonSpec();
|
|
|
+ spec.setId(insert.getId());
|
|
|
+ spec.setGoodsId(insert.getGoodsId());
|
|
|
+ spec.setMaxId(maxId);
|
|
|
+ spec.setSpecsJson(Jsons.toJson(specs));
|
|
|
+ spec.setSpecNumber(specNumber);
|
|
|
+ spec.setSpecMapping(Jsons.toJson(map));
|
|
|
+ if (insert.getId() != null) {
|
|
|
+ this.updateById(spec);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.save(spec);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void skuAppend(ReqGoodsSkuInsert insert, RentGoodsDon goods) throws Exception {
|
|
|
+
|
|
|
+ RentGoodsDonSpec loveSpec = this.getOne(Wrappers.lambdaQuery(RentGoodsDonSpec.class).eq(RentGoodsDonSpec::getGoodsId, insert.getGoodsId()));
|
|
|
+ if (null == loveSpec) {
|
|
|
+ throw new RuntimeException("该商品未设置总规格, 请设置总规格.");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ReqGoodsSkuInsert.Sku> skus = insert.getSkus();
|
|
|
+ if (null == skus || skus.isEmpty()) {
|
|
|
+ throw new RuntimeException("参数错误.");
|
|
|
+ }
|
|
|
+ int specNumber = loveSpec.getSpecNumber();
|
|
|
+
|
|
|
+ JavaType type = Jsons.javaType().constructMapType(HashMap.class, Integer.class, ReqGoodsSpecSingle.class);
|
|
|
+ Map<Integer, ReqGoodsSpecSingle> map = Jsons.parseObject(loveSpec.getSpecMapping(), type);
|
|
|
+
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ List<ReqGoodsSpecSingle> specList = null;
|
|
|
+ RentGoodsDonSku goodsDonSku = null;
|
|
|
+
|
|
|
+ // 判断属性是否属于同一个key
|
|
|
+ Set<String> set = null;
|
|
|
+
|
|
|
+ for (ReqGoodsSkuInsert.Sku sku : insert.getSkus()) {
|
|
|
+ // 跳过空值的sku
|
|
|
+ // 解析sku的规格属性
|
|
|
+ final String specIds = sku.getSpecIds();
|
|
|
+ if (StringUtils.isBlank(specIds)) {
|
|
|
+ throw new RuntimeException("请选择商品的规格.");
|
|
|
+ }
|
|
|
+ int[] ids = Jsons.parseObject(specIds, int[].class);
|
|
|
+// if (ids.length != specNumber) {
|
|
|
+// throw new RuntimeException("sku规格数量 与 总规格数量 不一致.");
|
|
|
+// }
|
|
|
+
|
|
|
+ // init
|
|
|
+ builder.delete(0, builder.length());
|
|
|
+ if (null == specList) {
|
|
|
+ specList = new ArrayList<>(specNumber);
|
|
|
+ } else {
|
|
|
+ specList.clear();
|
|
|
+ }
|
|
|
+ if (null == set) {
|
|
|
+ int capacity = (specNumber + 1) * 4 / 3 + 1;
|
|
|
+ set = new HashSet<>(capacity);
|
|
|
+ } else {
|
|
|
+ set.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int id : ids) {
|
|
|
+ ReqGoodsSpecSingle single = map.get(id);
|
|
|
+ if (null == single) {
|
|
|
+ throw new RuntimeException("总规格属性id中 不存在 sku中的id.");
|
|
|
+ }
|
|
|
+ builder.append(single.getName()).append(';');
|
|
|
+ specList.add(single);
|
|
|
+ set.add(single.getKey());
|
|
|
+ }
|
|
|
+// if (set.size() != specNumber) {
|
|
|
+// throw new RuntimeException("sku中存在多个属性属于同一个规格.");
|
|
|
+// }
|
|
|
+
|
|
|
+ String specVal = builder.toString();
|
|
|
+ String specJson = Jsons.toJson(specList);
|
|
|
+
|
|
|
+ if (null == goodsDonSku) {
|
|
|
+ goodsDonSku = new RentGoodsDonSku();
|
|
|
+ goodsDonSku.setGoodsId(insert.getGoodsId());
|
|
|
+ }
|
|
|
+ BeanUtil.copyProperties(sku, goodsDonSku);
|
|
|
+ String specIdsStr = Jsons.toJson(ids);
|
|
|
+ //升序
|
|
|
+ Arrays.sort(ids);
|
|
|
+ String dupSpecIds = Jsons.toJson(ids);
|
|
|
+ //是否存在相同规格
|
|
|
+ RentGoodsDonSku specIds_exists = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(RentGoodsDonSku.class)
|
|
|
+ .eq(RentGoodsDonSku::getSpecIds, specIdsStr)
|
|
|
+ .eq(RentGoodsDonSku::getGoodsId, goods.getId())
|
|
|
+ .last("limit 1"));
|
|
|
+ if (specIds_exists != null) {
|
|
|
+ log.info("库中存在相同二级分类的规格,spec_ids:{}", dupSpecIds);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ goodsDonSku.setSpecIds(specIdsStr);
|
|
|
+ goodsDonSku.setSpecVal(specVal);
|
|
|
+ goodsDonSku.setSpecJson(specJson);
|
|
|
+
|
|
|
+ if (sku.getDays() == null || sku.getDays() < 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的租赁天数");
|
|
|
+ }
|
|
|
+ if (sku.getPrice() == null || sku.getPrice().compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的价格");
|
|
|
+ }
|
|
|
+ goodsDonSkuMapper.insert(goodsDonSku);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|