This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.example.building.service;
|
||||
|
||||
import com.example.building.entity.CategoryAttribute;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 种类属性服务接口
|
||||
*/
|
||||
public interface CategoryAttributeService extends IService<CategoryAttribute> {
|
||||
|
||||
/**
|
||||
* 获取种类的所有属性
|
||||
*/
|
||||
List<CategoryAttribute> getByCategoryId(String categoryId);
|
||||
|
||||
/**
|
||||
* 保存属性
|
||||
*/
|
||||
void saveAttributes(String categoryId, List<CategoryAttribute> attrs);
|
||||
|
||||
/**
|
||||
* 删除种类的所有属性
|
||||
*/
|
||||
void deleteByCategoryId(String categoryId);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.building.service;
|
||||
|
||||
import com.example.building.entity.ProductAttribute;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 商品属性服务接口
|
||||
*/
|
||||
public interface ProductAttributeService extends IService<ProductAttribute> {
|
||||
|
||||
/**
|
||||
* 获取商品的属性值
|
||||
*/
|
||||
List<ProductAttribute> getByProductId(String productId);
|
||||
|
||||
/**
|
||||
* 保存商品属性值
|
||||
*/
|
||||
void saveProductAttributes(String productId, List<Map<String, Object>> attrs);
|
||||
|
||||
/**
|
||||
* 删除商品的所有属性
|
||||
*/
|
||||
void deleteByProductId(String productId);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.example.building.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.example.building.entity.CategoryAttribute;
|
||||
import com.example.building.mapper.CategoryAttributeMapper;
|
||||
import com.example.building.service.CategoryAttributeService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 种类属性服务实现
|
||||
*/
|
||||
@Service
|
||||
public class CategoryAttributeServiceImpl implements CategoryAttributeService {
|
||||
|
||||
@Override
|
||||
public List<CategoryAttribute> getByCategoryId(String categoryId) {
|
||||
return this.list(new LambdaQueryWrapper<CategoryAttribute>()
|
||||
.eq(CategoryAttribute::getCategoryId, categoryId)
|
||||
.orderByAsc(CategoryAttribute::getSortOrder));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveAttributes(String categoryId, List<CategoryAttribute> attrs) {
|
||||
// 删除旧属性
|
||||
this.remove(new LambdaQueryWrapper<CategoryAttribute>()
|
||||
.eq(CategoryAttribute::getCategoryId, categoryId));
|
||||
|
||||
// 保存新属性
|
||||
if (attrs != null && !attrs.isEmpty()) {
|
||||
for (int i = 0; i < attrs.size(); i++) {
|
||||
CategoryAttribute attr = attrs.get(i);
|
||||
attr.setAttrId(UUID.randomUUID().toString());
|
||||
attr.setCategoryId(categoryId);
|
||||
attr.setSortOrder(i);
|
||||
this.save(attr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteByCategoryId(String categoryId) {
|
||||
this.remove(new LambdaQueryWrapper<CategoryAttribute>()
|
||||
.eq(CategoryAttribute::getCategoryId, categoryId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.example.building.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.example.building.entity.ProductAttribute;
|
||||
import com.example.building.mapper.ProductAttributeMapper;
|
||||
import com.example.building.service.ProductAttributeService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 商品属性服务实现
|
||||
*/
|
||||
@Service
|
||||
public class ProductAttributeServiceImpl implements ProductAttributeService {
|
||||
|
||||
@Override
|
||||
public List<ProductAttribute> getByProductId(String productId) {
|
||||
return this.list(new LambdaQueryWrapper<ProductAttribute>()
|
||||
.eq(ProductAttribute::getProductId, productId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveProductAttributes(String productId, List<Map<String, Object>> attrs) {
|
||||
// 删除旧属性
|
||||
this.remove(new LambdaQueryWrapper<ProductAttribute>()
|
||||
.eq(ProductAttribute::getProductId, productId));
|
||||
|
||||
// 保存新属性
|
||||
if (attrs != null && !attrs.isEmpty()) {
|
||||
for (Map<String, Object> attr : attrs) {
|
||||
ProductAttribute pa = new ProductAttribute();
|
||||
pa.setId(UUID.randomUUID().toString());
|
||||
pa.setProductId(productId);
|
||||
pa.setAttrId((String) attr.get("attrId"));
|
||||
pa.setAttrName((String) attr.get("attrName"));
|
||||
pa.setAttrValue(String.valueOf(attr.get("attrValue")));
|
||||
this.save(pa);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteByProductId(String productId) {
|
||||
this.remove(new LambdaQueryWrapper<ProductAttribute>()
|
||||
.eq(ProductAttribute::getProductId, productId));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user