|
@@ -5,7 +5,9 @@ import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
import com.alibaba.fastjson.parser.Feature;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.txz.mall.constants.Constants;
|
|
@@ -31,6 +33,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import tk.mybatis.mapper.entity.Condition;
|
|
|
import tk.mybatis.mapper.entity.Example;
|
|
|
+import vo.AttrVO;
|
|
|
import vo.AttrValueVO;
|
|
|
import vo.StoreProductCountItemVO;
|
|
|
import vo.StoreProductInfoVO;
|
|
@@ -111,7 +114,15 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
}
|
|
|
|
|
|
List<ProductAttr> attrList = attrService.getListByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
|
|
|
- storeProductResponse.setAttr(attrList);
|
|
|
+ List<AttrVO> attrResponseList = attrList.stream().map(e -> {
|
|
|
+ AttrVO attrResponse = new AttrVO();
|
|
|
+ BeanUtils.copyProperties(e, attrResponse);
|
|
|
+ List<Object> stringList = JSON.parseObject(e.getAttrImgValues(), new TypeReference<List<Object>>() {
|
|
|
+ });
|
|
|
+ attrResponse.setAttrImgValues(stringList);
|
|
|
+ return attrResponse;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ storeProductResponse.setAttr(attrResponseList);
|
|
|
|
|
|
List<ProductAttrValue> attrValueList = attrValueService.getListByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
|
|
|
List<AttrValueVO> valueResponseList = attrValueList.stream().map(e -> {
|
|
@@ -185,8 +196,22 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
@Transactional
|
|
|
public void save(StoreProductAddRequest request) {
|
|
|
+ if (request.getId() != null) {
|
|
|
+ throw new ServiceException("商品已存在");
|
|
|
+ }
|
|
|
+ assignAttributes(request, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加商品属性
|
|
|
+ *
|
|
|
+ * @param request 商品参数
|
|
|
+ * @param status 商品状态 0-添加 1-修改
|
|
|
+ */
|
|
|
+ private void assignAttributes(StoreProductAddRequest request, Integer status) {
|
|
|
// 多规格需要校验规格参数
|
|
|
if (request.getSpecType().equals(0)) {
|
|
|
if (request.getAttrValue().size() > 1) {
|
|
@@ -195,9 +220,6 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
}
|
|
|
StoreProduct storeProduct = new StoreProduct();
|
|
|
BeanUtils.copyProperties(request, storeProduct);
|
|
|
- storeProduct.setId(null);
|
|
|
- storeProduct.setIsShow(0);
|
|
|
-
|
|
|
// 设置activity活动
|
|
|
storeProduct.setActivity(getProductActivityStr(request.getActivity()));
|
|
|
//主图
|
|
@@ -217,12 +239,18 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
storeProduct.setCost(minAttrValue.getCost());
|
|
|
storeProduct.setStock(attrValueAddRequestList.stream().mapToInt(StoreProductAttrValueAddRequest::getStock).sum());
|
|
|
|
|
|
+ if (StringUtils.isBlank(storeProduct.getItemNumber())) {
|
|
|
+ String itemNumber = generateItemNumber(storeProduct.getCateId());
|
|
|
+ storeProduct.setItemNumber(itemNumber);
|
|
|
+ }
|
|
|
+
|
|
|
Date date = new Date();
|
|
|
List<StoreProductAttrAddRequest> addRequestList = request.getAttr();
|
|
|
List<ProductAttr> attrList = addRequestList.stream().map(e -> {
|
|
|
ProductAttr attr = new ProductAttr();
|
|
|
BeanUtils.copyProperties(e, attr);
|
|
|
attr.setType(Constants.PRODUCT_TYPE_NORMAL);
|
|
|
+ attr.setAttrImgValues(JSONObject.toJSONString(e.getAttrImgValues()));
|
|
|
attr.setCreateTime(date);
|
|
|
attr.setIsDelete(0);
|
|
|
return attr;
|
|
@@ -232,17 +260,21 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
ProductAttrValue attrValue = new ProductAttrValue();
|
|
|
BeanUtils.copyProperties(e, attrValue);
|
|
|
attrValue.setId(null);
|
|
|
- attrValue.setStockThreshold(0);
|
|
|
+ if (attrValue.getStockThreshold() == null) {
|
|
|
+ attrValue.setStockThreshold(0);
|
|
|
+ }
|
|
|
attrValue.setSales(0);
|
|
|
+ attrValue.setQuota(0);
|
|
|
+ attrValue.setQuotaShow(0);
|
|
|
// 生成sku 根据传进来spu再加当前时分秒
|
|
|
- LocalTime now = LocalTime.now();
|
|
|
- attrValue.setSkuCode(request.getItemNumber() + now.getHour() + now.getMinute());
|
|
|
+ if (StringUtils.isBlank(e.getSkuCode())) {
|
|
|
+ LocalTime now = LocalTime.now();
|
|
|
+ attrValue.setSkuCode(request.getItemNumber() + now.getHour() + now.getMinute());
|
|
|
+ }
|
|
|
attrValue.setPrice(e.getPrice());
|
|
|
attrValue.setCost(e.getPrice());
|
|
|
- attrValue.setOtPrice(e.getPrice());
|
|
|
+ attrValue.setOtPrice(e.getOtPrice());
|
|
|
attrValue.setSuk(getSku(e.getAttrValue()));
|
|
|
- attrValue.setQuota(0);
|
|
|
- attrValue.setQuotaShow(0);
|
|
|
attrValue.setType(Constants.PRODUCT_TYPE_NORMAL);
|
|
|
attrValue.setImage(e.getImage());
|
|
|
attrValue.setIsDelete(0);
|
|
@@ -250,11 +282,26 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
return attrValue;
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
- if (StringUtils.isBlank(storeProduct.getItemNumber())) {
|
|
|
- String itemNumber = generateItemNumber(storeProduct.getCateId());
|
|
|
- storeProduct.setItemNumber(itemNumber);
|
|
|
+ if (status.equals(0)) {
|
|
|
+ storeProduct.setId(null);
|
|
|
+ storeProduct.setIsShow(0);
|
|
|
+ save(storeProduct);
|
|
|
+ } else {
|
|
|
+ update(storeProduct);
|
|
|
+ List<ProductAttr> productAttrs = attrService.getListByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
|
|
|
+ List<ProductAttrValue> productAttrValues = attrValueService.getListByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
|
|
|
+ String attrIds = productAttrs.stream()
|
|
|
+ .map(attr -> attr.getId().toString())
|
|
|
+ .collect(Collectors.joining(","));
|
|
|
+
|
|
|
+ String attrValueIds = productAttrValues.stream()
|
|
|
+ .map(attr -> attr.getId().toString())
|
|
|
+ .collect(Collectors.joining(","));
|
|
|
+
|
|
|
+ attrService.deleteByIds(attrIds);
|
|
|
+ attrValueService.deleteByIds(attrValueIds);
|
|
|
}
|
|
|
- save(storeProduct);
|
|
|
+
|
|
|
attrList.forEach(attr -> attr.setProductId(storeProduct.getId()));
|
|
|
attrValueList.forEach(value -> value.setProductId(storeProduct.getId()));
|
|
|
attrService.save(attrList);
|
|
@@ -271,6 +318,12 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void update(StoreProductAddRequest request) {
|
|
|
+ assignAttributes(request, 1);
|
|
|
+ }
|
|
|
+
|
|
|
// 生成货号的方法
|
|
|
private String generateItemNumber(String cateIdStr) {
|
|
|
// 1. 获取类目ID的字符串形式
|