|
@@ -7,6 +7,7 @@ import com.fjhx.admin.entity.product.dto.ProductClassifyDto;
|
|
|
import com.fjhx.admin.entity.product.po.ProductClassify;
|
|
|
import com.fjhx.admin.mapper.product.ProductClassifyMapper;
|
|
|
import com.fjhx.admin.service.product.ProductClassifyService;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.ruoyi.common.utils.TreeUtil;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -39,10 +40,32 @@ public class ProductClassifyServiceImpl extends ServiceImpl<ProductClassifyMappe
|
|
|
}
|
|
|
|
|
|
public void add(ProductClassifyDto productClassifyDto) {
|
|
|
+
|
|
|
+ Long parentId = productClassifyDto.getParentId();
|
|
|
+ if (parentId == null) {
|
|
|
+ productClassifyDto.setParentId(0L);
|
|
|
+ productClassifyDto.setParentIdSet(null);
|
|
|
+ } else if (parentId == 0L) {
|
|
|
+ productClassifyDto.setParentIdSet(null);
|
|
|
+ } else {
|
|
|
+ // 查询父级分类
|
|
|
+ ProductClassify parentClassify = getById(parentId);
|
|
|
+ if (parentClassify == null) {
|
|
|
+ throw new ServiceException("没有找到父级分类");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 赋值父级id集合
|
|
|
+ String parentIdSet = parentClassify.getParentIdSet();
|
|
|
+ parentClassify.setParentIdSet((ObjectUtil.isEmpty(parentIdSet) ? "" : parentIdSet + ",") + parentId);
|
|
|
+ }
|
|
|
+
|
|
|
+ peerNameOnly(productClassifyDto.getParentId(), productClassifyDto.getName(), null);
|
|
|
+
|
|
|
this.save(productClassifyDto);
|
|
|
}
|
|
|
|
|
|
public void edit(ProductClassifyDto productClassifyDto) {
|
|
|
+ peerNameOnly(productClassifyDto.getParentId(), productClassifyDto.getName(), productClassifyDto.getId());
|
|
|
this.updateById(productClassifyDto);
|
|
|
}
|
|
|
|
|
@@ -50,4 +73,21 @@ public class ProductClassifyServiceImpl extends ServiceImpl<ProductClassifyMappe
|
|
|
this.removeById(id);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 保证同级名称不重复
|
|
|
+ */
|
|
|
+ private void peerNameOnly(Long parentId, String classifyName, Long excludeId) {
|
|
|
+
|
|
|
+ Long count = lambdaQuery()
|
|
|
+ .eq(ProductClassify::getParentId, parentId)
|
|
|
+ .eq(ProductClassify::getName, classifyName)
|
|
|
+ .ne(ObjectUtil.isNotEmpty(excludeId), ProductClassify::getId, excludeId)
|
|
|
+ .count();
|
|
|
+
|
|
|
+ if (count != 0) {
|
|
|
+ throw new ServiceException("存在相同分类名称");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|