|
@@ -0,0 +1,98 @@
|
|
|
+package com.fjhx.item.service.product.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.item.entity.product.dto.ProductClassifyDto;
|
|
|
+import com.fjhx.item.entity.product.po.ProductClassify;
|
|
|
+import com.fjhx.item.mapper.product.ProductClassifyMapper;
|
|
|
+import com.fjhx.item.service.product.ProductClassifyService;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.TreeUtil;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 产品分类 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author zlj
|
|
|
+ * @since 2023-03-15
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ProductClassifyServiceImpl extends ServiceImpl<ProductClassifyMapper, ProductClassify> implements ProductClassifyService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<JSONObject> tree(ProductClassify productClassify) {
|
|
|
+ // 分类名称
|
|
|
+ String name = productClassify.getName();
|
|
|
+
|
|
|
+ List<ProductClassify> list = lambdaQuery()
|
|
|
+ .select(ProductClassify::getId, ProductClassify::getName, ProductClassify::getParentId)
|
|
|
+ .eq(ProductClassify::getDefinition, productClassify.getDefinition())
|
|
|
+ .like(ObjectUtil.isNotEmpty(name), ProductClassify::getName, name)
|
|
|
+ .list();
|
|
|
+
|
|
|
+ // 构建树形
|
|
|
+ return TreeUtil.buildTree("name", list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ 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();
|
|
|
+ productClassifyDto.setParentIdSet((ObjectUtil.isEmpty(parentIdSet) ? "" : parentIdSet + ",") + parentId);
|
|
|
+ }
|
|
|
+
|
|
|
+ peerNameOnly(productClassifyDto.getDefinition(), productClassifyDto.getParentId(), productClassifyDto.getName(), null);
|
|
|
+
|
|
|
+ this.save(productClassifyDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void edit(ProductClassifyDto productClassifyDto) {
|
|
|
+ peerNameOnly(productClassifyDto.getDefinition(), productClassifyDto.getParentId(), productClassifyDto.getName(), productClassifyDto.getId());
|
|
|
+ this.updateById(productClassifyDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(Long id) {
|
|
|
+ this.removeById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保证同级名称不重复
|
|
|
+ */
|
|
|
+ private void peerNameOnly(Integer type, Long parentId, String classifyName, Long excludeId) {
|
|
|
+
|
|
|
+ Long count = lambdaQuery()
|
|
|
+ .eq(ProductClassify::getParentId, parentId)
|
|
|
+ .eq(ProductClassify::getName, classifyName)
|
|
|
+ .eq(ProductClassify::getDefinition, type)
|
|
|
+ .ne(ObjectUtil.isNotEmpty(excludeId), ProductClassify::getId, excludeId)
|
|
|
+ .count();
|
|
|
+
|
|
|
+ if (count != 0) {
|
|
|
+ throw new ServiceException("存在相同分类名称");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|