|
@@ -1,15 +1,15 @@
|
|
|
package cn.iocoder.yudao.module.product.service.property;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjUtil;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
-import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
|
-import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
|
|
-import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.*;
|
|
|
+import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.ProductPropertyCreateReqVO;
|
|
|
+import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.ProductPropertyListReqVO;
|
|
|
+import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.ProductPropertyPageReqVO;
|
|
|
+import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.ProductPropertyUpdateReqVO;
|
|
|
import cn.iocoder.yudao.module.product.convert.property.ProductPropertyConvert;
|
|
|
-import cn.iocoder.yudao.module.product.convert.propertyvalue.ProductPropertyValueConvert;
|
|
|
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyDO;
|
|
|
-import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyValueDO;
|
|
|
import cn.iocoder.yudao.module.product.dal.mysql.property.ProductPropertyMapper;
|
|
|
-import cn.iocoder.yudao.module.product.dal.mysql.property.ProductPropertyValueMapper;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
@@ -17,11 +17,9 @@ import org.springframework.validation.annotation.Validated;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
-import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PROPERTY_EXISTS;
|
|
|
-import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PROPERTY_NOT_EXISTS;
|
|
|
+import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
|
* 商品属性项 Service 实现类
|
|
@@ -36,15 +34,17 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
|
|
|
private ProductPropertyMapper productPropertyMapper;
|
|
|
|
|
|
@Resource
|
|
|
- private ProductPropertyValueMapper productPropertyValueMapper;
|
|
|
+ @Lazy // 延迟加载,解决循环依赖问题
|
|
|
+ private ProductPropertyValueService productPropertyValueService;
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Long createProperty(ProductPropertyCreateReqVO createReqVO) {
|
|
|
- // 校验存在
|
|
|
+ // 校验名字重复
|
|
|
if (productPropertyMapper.selectByName(createReqVO.getName()) != null) {
|
|
|
throw exception(PROPERTY_EXISTS);
|
|
|
}
|
|
|
+
|
|
|
// 插入
|
|
|
ProductPropertyDO property = ProductPropertyConvert.INSTANCE.convert(createReqVO);
|
|
|
productPropertyMapper.insert(property);
|
|
@@ -55,12 +55,14 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void updateProperty(ProductPropertyUpdateReqVO updateReqVO) {
|
|
|
- // 校验存在
|
|
|
validatePropertyExists(updateReqVO.getId());
|
|
|
+ // 校验名字重复
|
|
|
ProductPropertyDO productPropertyDO = productPropertyMapper.selectByName(updateReqVO.getName());
|
|
|
- if (productPropertyDO != null && !productPropertyDO.getId().equals(updateReqVO.getId())) {
|
|
|
+ if (productPropertyDO != null &&
|
|
|
+ ObjUtil.notEqual(productPropertyDO.getId(), updateReqVO.getId())) {
|
|
|
throw exception(PROPERTY_EXISTS);
|
|
|
}
|
|
|
+
|
|
|
// 更新
|
|
|
ProductPropertyDO updateObj = ProductPropertyConvert.INSTANCE.convert(updateReqVO);
|
|
|
productPropertyMapper.updateById(updateObj);
|
|
@@ -70,10 +72,15 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
|
|
|
public void deleteProperty(Long id) {
|
|
|
// 校验存在
|
|
|
validatePropertyExists(id);
|
|
|
+ // 校验其下是否有规格值
|
|
|
+ if (productPropertyValueService.getPropertyValueCountByPropertyId(id) > 0) {
|
|
|
+ throw exception(PROPERTY_DELETE_FAIL_VALUE_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
// 删除
|
|
|
productPropertyMapper.deleteById(id);
|
|
|
// 同步删除属性值
|
|
|
- productPropertyValueMapper.deletePropertyValueByPropertyId(id);
|
|
|
+ productPropertyValueService.deletePropertyValueByPropertyId(id);
|
|
|
}
|
|
|
|
|
|
private void validatePropertyExists(Long id) {
|
|
@@ -83,22 +90,18 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<ProductPropertyRespVO> getPropertyVOList(ProductPropertyListReqVO listReqVO) {
|
|
|
- return ProductPropertyConvert.INSTANCE.convertList(productPropertyMapper.selectList(new LambdaQueryWrapperX<ProductPropertyDO>()
|
|
|
- .likeIfPresent(ProductPropertyDO::getName, listReqVO.getName())
|
|
|
- .eqIfPresent(ProductPropertyDO::getStatus, listReqVO.getStatus())));
|
|
|
+ public List<ProductPropertyDO> getPropertyList(ProductPropertyListReqVO listReqVO) {
|
|
|
+ return productPropertyMapper.selectList(listReqVO);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public PageResult<ProductPropertyRespVO> getPropertyPage(ProductPropertyPageReqVO pageReqVO) {
|
|
|
- PageResult<ProductPropertyDO> pageResult = productPropertyMapper.selectPage(pageReqVO);
|
|
|
- return ProductPropertyConvert.INSTANCE.convertPage(pageResult);
|
|
|
+ public PageResult<ProductPropertyDO> getPropertyPage(ProductPropertyPageReqVO pageReqVO) {
|
|
|
+ return productPropertyMapper.selectPage(pageReqVO);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ProductPropertyRespVO getProperty(Long id) {
|
|
|
- ProductPropertyDO property = productPropertyMapper.selectById(id);
|
|
|
- return ProductPropertyConvert.INSTANCE.convert(property);
|
|
|
+ public ProductPropertyDO getProperty(Long id) {
|
|
|
+ return productPropertyMapper.selectById(id);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -106,24 +109,4 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
|
|
|
return productPropertyMapper.selectBatchIds(ids);
|
|
|
}
|
|
|
|
|
|
- // TODO @芋艿:丢到 Controller
|
|
|
- @Override
|
|
|
- public List<ProductPropertyRespVO> getPropertyVOList(Collection<Long> ids) {
|
|
|
- return ProductPropertyConvert.INSTANCE.convertList(productPropertyMapper.selectBatchIds(ids));
|
|
|
- }
|
|
|
-
|
|
|
- // TODO @芋艿:丢到 Controller
|
|
|
- @Override
|
|
|
- public List<ProductPropertyAndValueRespVO> getPropertyAndValueList(ProductPropertyListReqVO listReqVO) {
|
|
|
- List<ProductPropertyRespVO> propertyList = getPropertyVOList(listReqVO);
|
|
|
-
|
|
|
- // 查询属性值
|
|
|
- List<ProductPropertyValueDO> valueDOList = productPropertyValueMapper.selectListByPropertyId(CollectionUtils.convertList(propertyList, ProductPropertyRespVO::getId));
|
|
|
- Map<Long, List<ProductPropertyValueDO>> valueDOMap = CollectionUtils.convertMultiMap(valueDOList, ProductPropertyValueDO::getPropertyId);
|
|
|
- return CollectionUtils.convertList(propertyList, m -> {
|
|
|
- ProductPropertyAndValueRespVO productPropertyAndValueRespVO = ProductPropertyConvert.INSTANCE.convert(m);
|
|
|
- productPropertyAndValueRespVO.setValues(ProductPropertyValueConvert.INSTANCE.convertList(valueDOMap.get(m.getId())));
|
|
|
- return productPropertyAndValueRespVO;
|
|
|
- });
|
|
|
- }
|
|
|
}
|