|
@@ -24,6 +24,7 @@ import dto.StoreProductAddRequest;
|
|
|
import dto.StoreProductAttrAddRequest;
|
|
|
import dto.StoreProductAttrValueAddRequest;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -38,6 +39,7 @@ import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.time.LocalTime;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -94,7 +96,7 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public StoreProductInfoVO getInfo(Integer id) {
|
|
|
+ public StoreProductInfoVO getInfo(Long id) {
|
|
|
StoreProduct storeProduct = findById(id);
|
|
|
if (ObjectUtil.isNull(storeProduct)) {
|
|
|
throw new ServiceException("未找到对应商品信息");
|
|
@@ -103,8 +105,10 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
BeanUtils.copyProperties(storeProduct, storeProductResponse);
|
|
|
|
|
|
// 设置商品所参与的活动
|
|
|
- List<String> activityList = getProductActivityList(storeProduct.getActivity());
|
|
|
- storeProductResponse.setActivity(activityList);
|
|
|
+ if (StrUtil.isNotBlank(storeProduct.getActivity())) {
|
|
|
+ List<String> activityList = getProductActivityList(storeProduct.getActivity());
|
|
|
+ storeProductResponse.setActivity(activityList);
|
|
|
+ }
|
|
|
|
|
|
List<ProductAttr> attrList = attrService.getListByProductIdAndType(storeProduct.getId(), Constants.PRODUCT_TYPE_NORMAL);
|
|
|
storeProductResponse.setAttr(attrList);
|
|
@@ -181,9 +185,10 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
public void save(StoreProductAddRequest request) {
|
|
|
// 多规格需要校验规格参数
|
|
|
- if (!request.getSpecType()) {
|
|
|
+ if (request.getSpecType().equals(0)) {
|
|
|
if (request.getAttrValue().size() > 1) {
|
|
|
throw new ServiceException("单规格商品属性值不能大于1");
|
|
|
}
|
|
@@ -195,10 +200,8 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
|
|
|
// 设置activity活动
|
|
|
storeProduct.setActivity(getProductActivityStr(request.getActivity()));
|
|
|
-
|
|
|
//主图
|
|
|
storeProduct.setImage(storeProduct.getImage());
|
|
|
-
|
|
|
//轮播图
|
|
|
storeProduct.setSliderImage(storeProduct.getSliderImage());
|
|
|
// 展示图
|
|
@@ -214,12 +217,14 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
storeProduct.setCost(minAttrValue.getCost());
|
|
|
storeProduct.setStock(attrValueAddRequestList.stream().mapToInt(StoreProductAttrValueAddRequest::getStock).sum());
|
|
|
|
|
|
-
|
|
|
+ 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.setCreateTime(date);
|
|
|
+ attr.setIsDelete(0);
|
|
|
return attr;
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
@@ -227,14 +232,28 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
ProductAttrValue attrValue = new ProductAttrValue();
|
|
|
BeanUtils.copyProperties(e, attrValue);
|
|
|
attrValue.setId(null);
|
|
|
+ attrValue.setStockThreshold(0);
|
|
|
+ attrValue.setSales(0);
|
|
|
+ // 生成sku 根据传进来spu再加当前时分秒
|
|
|
+ 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.setSuk(getSku(e.getAttrValue()));
|
|
|
attrValue.setQuota(0);
|
|
|
attrValue.setQuotaShow(0);
|
|
|
attrValue.setType(Constants.PRODUCT_TYPE_NORMAL);
|
|
|
attrValue.setImage(e.getImage());
|
|
|
+ attrValue.setIsDelete(0);
|
|
|
+ attrValue.setCreateTime(date);
|
|
|
return attrValue;
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
+ if (StringUtils.isBlank(storeProduct.getItemNumber())) {
|
|
|
+ String itemNumber = generateItemNumber(storeProduct.getCateId());
|
|
|
+ storeProduct.setItemNumber(itemNumber);
|
|
|
+ }
|
|
|
save(storeProduct);
|
|
|
attrList.forEach(attr -> attr.setProductId(storeProduct.getId()));
|
|
|
attrValueList.forEach(value -> value.setProductId(storeProduct.getId()));
|
|
@@ -252,6 +271,29 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
+ // 生成货号的方法
|
|
|
+ private String generateItemNumber(String cateIdStr) {
|
|
|
+ // 1. 获取类目ID的字符串形式
|
|
|
+ int cateIdLength = cateIdStr.length();
|
|
|
+ // 2. 确定随机部分的长度
|
|
|
+ int randomPartLength = 8 - cateIdLength;
|
|
|
+ // 3. 生成随机部分
|
|
|
+ String randomPart = generateRandomString(randomPartLength);
|
|
|
+ // 4. 拼接类目ID和随机部分
|
|
|
+ return cateIdStr + randomPart;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成指定长度的随机字符串(数字和字母)
|
|
|
+ private String generateRandomString(int length) {
|
|
|
+ String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ java.util.Random random = new java.util.Random();
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
+ sb.append(chars.charAt(random.nextInt(chars.length())));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 商品sku
|
|
|
*
|