dev798
wxy 2024-04-09 14:26:37 +08:00
parent 25ada3932d
commit 88de15ced1
6 changed files with 58 additions and 45 deletions

View File

@ -19,7 +19,7 @@ public class ProductSkuAttr extends BaseEntity {
private Integer id;
@ApiModelProperty(value = "SKU ID")
private BigInteger skuId;
private long skuId;
@ApiModelProperty(value = "属性ID")
private Integer attrId;

View File

@ -20,7 +20,7 @@ public class ProductTypeAttrValue extends BaseEntity {
private Integer typeAttrId;
@ApiModelProperty(value = "属性值详情")
private Integer typeAttrValue;
private String typeAttrValue;
@ApiModelProperty(value = "录入方式0-自动1-手动")
private Integer inputMethod;

View File

@ -3,6 +3,7 @@ package com.muyu.product.mapper;
import com.muyu.product.domain.DTO.ProductSku;
import com.muyu.product.domain.DTO.ProductSkuAttr;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -12,10 +13,11 @@ import java.util.List;
*/
@Mapper
public interface ProductSkuMapper {
void insertProductSku(List<ProductSku> productSkuList);
void insertProductSkuAttr(List<ProductSkuAttr> productSkuAttrs);
void insertProductSkuAttr(@Param("productSkuAttrs") List<ProductSkuAttr> productSkuAttrs);
Integer findSkuId(List<ProductSku> productSkus);
void insertProductSku(@Param("productSkus") List<ProductSku> productSkus, @Param("id") Integer id, @Param("createBy") String createBy);
Integer findSkuId(@Param("productSkus") List<ProductSku> productSkus);
}

View File

@ -18,23 +18,24 @@ import java.util.List;
public interface TypeMapper {
List<ProductTypeResp> queryTypeAll();
List<ProductTypeAttr> queryType(@Param("flag") Integer flag, @Param("id") Integer id);
List<ProductTypeResp> queryTypeAll();
Integer deleteType(@Param("typeId") Integer typeId, @Param("name") String name);
void deleteTypeAttrValue(Integer typeId);
Integer deleteTypeAttr(Integer typeId);
Integer insertType(ProductType productType);
Integer updateType(ProductType productType);
Integer insertTypeAttr(ProductTypeAttrReq productTypeAttrReq);
void insertTypeValue(@Param("productTypeAttrValueList") List<ProductTypeAttrValue> productTypeAttrValueList);
Integer deleteTypeAttr(Integer id);
Integer updateAttr(ProductTypeAttrReq productTypeAttrReq);
void deleteTypeAttrValue(Integer typeId);
void insertTypeValue(ProductTypeAttrValue productTypeAttrValue);
}

View File

@ -1,5 +1,5 @@
package com.muyu.product.service.impl;
import com.alibaba.fastjson.JSONObject; // 导入 FastJSON 的 JSONObject 类
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper; // 导入 PageHelper 类
import com.github.pagehelper.PageInfo; // 导入 PageInfo 类
import com.muyu.common.security.utils.SecurityUtils;
@ -10,6 +10,7 @@ import com.muyu.product.domain.DTO.*;
import com.muyu.product.domain.DTO.Number;
import com.muyu.product.domain.Resp.ProductResp;
import com.muyu.product.domain.req.ProductReq;
import com.muyu.product.domain.req.ProductSkuNew;
import com.muyu.product.domain.req.QueryProductReq;
import com.muyu.product.mapper.ProductMapper;
import com.muyu.product.mapper.ProductSkuMapper;
@ -136,34 +137,34 @@ public class ProductServiceImpl implements ProductService {
private void insertProductSku(ProductReq productReq) {
List<ProductSku> productSkuList = productReq.getProductSkus();
int productId = productReq.getProduct().getId();
// 设置商品ID
productSkuList.forEach(productSku -> productSku.setProductId(productId));
// 批量添加 productSku
productSkuMapper.insertProductSku(productSkuList);
// 添加 skuAttr
List<ProductSkuAttr> productSkuAttrs = new ArrayList<>();
productSkuList.forEach(productSku -> {
// 获取 sku 的属性值
Map<String, Integer> attributes = productSku.getAttributes();
// 构造 ProductSkuAttr 对象
attributes.forEach((key, value) -> {
String createBy = SecurityUtils.getUsername();
//批量添加
productSkuMapper.insertProductSku(productSkuList,productReq.getProduct().getId(),createBy);
//添加skuAttr除实体类以外的字段
ArrayList<ProductSkuAttr> productSkuAttrs = new ArrayList<>();
productSkuList.forEach(productSkusReq -> {
//获取id
//productSkusReq.setProductId(productReq.getProduct().getId());
//获取map的属性值
ProductSkuNew productSkuNew = com.alibaba.fastjson2.JSONObject.parseObject(com.alibaba.fastjson2.JSONObject.toJSONString(productSkusReq), ProductSkuNew.class);
HashMap hashMap = com.alibaba.fastjson2.JSONObject.parseObject(JSONObject.toJSONString(productSkuNew), HashMap.class);
Set skuReq = productSkusReq.keySet();
Set sku = hashMap.keySet();
//添加一个新的set
Set set = skuReq;
//去重
set.removeAll(sku);
//格属性赋值
set.forEach(key ->{
ProductSkuAttr productSkuAttr = new ProductSkuAttr();
productSkuAttr.setSkuId(productSku.getId());
productSkuAttr.setAttrId(Integer.valueOf(key));
productSkuAttr.setAttrValueId(value);
productSkuAttr.setSkuId(productSkuNew.getId());
productSkuAttr.setAttrId(Integer.valueOf(key.toString()));
productSkuAttr.setAttrValueId(Integer.valueOf(productSkusReq.get(key).toString()));
productSkuAttr.setFlag(Integer.valueOf(Number.zero.getValue()));
productSkuAttrs.add(productSkuAttr);
});
});
log.info("productSkuAttrs 中的数据为:{}", JSONObject.toJSONString(productSkuAttrs));
// 批量添加 productSkuAttr
//批量添加productSkuAttr
productSkuMapper.insertProductSkuAttr(productSkuAttrs);
}

View File

@ -31,13 +31,13 @@ public class TypeServiceImpl implements TypeService {
@Override
public List<ProductTypeAttr> queryType(Integer flag, Integer id) {
return typeMapper.queryType(flag,id);
return typeMapper.queryType(flag, id);
}
@Override
public Integer deleteType(Integer typeId) {
String name = SecurityUtils.getUsername();
return typeMapper.deleteType(typeId,name);
return typeMapper.deleteType(typeId, name);
}
@Override
@ -62,13 +62,18 @@ public class TypeServiceImpl implements TypeService {
public Integer insertTypeAttr(ProductTypeAttrReq productTypeAttrReq) {
productTypeAttrReq.setCreateBy(SecurityUtils.getUsername());
Integer res = typeMapper.insertTypeAttr(productTypeAttrReq);
if(res >0 && productTypeAttrReq.getProductTypeAttrValueList().size()>0){
String[] split = productTypeAttrReq.getProductTypeAttrValueList().split(",");
if (res > 0) {
Integer typeAttrId = productTypeAttrReq.getId();
for (ProductTypeAttrValue productTypeAttrValue : productTypeAttrReq.getProductTypeAttrValueList()) {
ProductTypeAttrValue productTypeAttrValue = new ProductTypeAttrValue();
for (String s : split) {
productTypeAttrValue.setCreateBy(SecurityUtils.getUsername());
productTypeAttrValue.setTypeAttrId(typeAttrId);
productTypeAttrValue.setTypeAttrValue(s);
typeMapper.insertTypeValue(productTypeAttrValue);
}
typeMapper.insertTypeValue(productTypeAttrReq.getProductTypeAttrValueList());
}
return res;
}
@ -78,12 +83,16 @@ public class TypeServiceImpl implements TypeService {
productTypeAttrReq.setCreateBy(SecurityUtils.getUsername());
typeMapper.deleteTypeAttrValue(productTypeAttrReq.getId());
Integer res = typeMapper.updateAttr(productTypeAttrReq);
if(res >0 && productTypeAttrReq.getProductTypeAttrValueList().size()>0){
for (ProductTypeAttrValue productTypeAttrValue : productTypeAttrReq.getProductTypeAttrValueList()) {
String[] split = productTypeAttrReq.getProductTypeAttrValueList().split(",");
if (res > 0) {
Integer typeAttrId = productTypeAttrReq.getId();
ProductTypeAttrValue productTypeAttrValue = new ProductTypeAttrValue();
for (String s : split) {
productTypeAttrValue.setCreateBy(SecurityUtils.getUsername());
productTypeAttrValue.setTypeAttrId(productTypeAttrReq.getId());
productTypeAttrValue.setTypeAttrId(typeAttrId);
productTypeAttrValue.setTypeAttrValue(s);
typeMapper.insertTypeValue(productTypeAttrValue);
}
typeMapper.insertTypeValue(productTypeAttrReq.getProductTypeAttrValueList());
}
return res;
}