|
@@ -1,17 +1,26 @@
|
|
|
package com.fjhx.classify.service.impl;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
-import com.fjhx.utils.WrapperUtil;
|
|
|
-import com.fjhx.entity.classify.Classify;
|
|
|
-import com.fjhx.params.classify.ClassifyVo;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.base.BaseEntity;
|
|
|
import com.fjhx.classify.mapper.ClassifyMapper;
|
|
|
import com.fjhx.classify.service.ClassifyService;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.entity.classify.Classify;
|
|
|
+import com.fjhx.entity.material.Material;
|
|
|
+import com.fjhx.entity.product.Product;
|
|
|
+import com.fjhx.enums.classify.ClassifyTypeEnum;
|
|
|
+import com.fjhx.material.service.MaterialService;
|
|
|
+import com.fjhx.params.classify.ClassifyVo;
|
|
|
+import com.fjhx.product.service.ProductService;
|
|
|
+import com.fjhx.utils.Assert;
|
|
|
+import com.fjhx.utils.TreeUtil;
|
|
|
+import org.springblade.core.secure.utils.AuthUtil;
|
|
|
+import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -24,37 +33,96 @@ import java.util.Map;
|
|
|
@Service
|
|
|
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ProductService productService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MaterialService materialService;
|
|
|
+
|
|
|
@Override
|
|
|
- public Page<Classify> getPage(Map<String, String> condition) {
|
|
|
+ public List<Map<String, Object>> getTree(Map<String, String> condition) {
|
|
|
|
|
|
- QueryWrapper<Classify> wrapper = Wrappers.query();
|
|
|
+ String type = condition.get("type");
|
|
|
+ Assert.notEmpty(type, "分类类型不能为空");
|
|
|
|
|
|
- WrapperUtil.init(condition, wrapper)
|
|
|
- .eqTenantId()
|
|
|
- .createTimeDesc();
|
|
|
+ List<Classify> list = lambdaQuery()
|
|
|
+ .eq(Classify::getType, type)
|
|
|
+ .select(Classify::getId, Classify::getName, Classify::getParentId)
|
|
|
+ .list();
|
|
|
|
|
|
- Page<Classify> page = page(condition, wrapper);
|
|
|
- return page;
|
|
|
+ // 构建树形
|
|
|
+ return TreeUtil.buildTree("name", list);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void add(ClassifyVo classifyVo) {
|
|
|
+
|
|
|
+ Assert.notEmpty(classifyVo.getType(), "分类类型不能为空");
|
|
|
+ Assert.notEmpty(classifyVo.getType(), "分类名称不能为空");
|
|
|
+
|
|
|
+ Long parentId = classifyVo.getParentId();
|
|
|
+ if (parentId == null) {
|
|
|
+ classifyVo.setParentId(0L);
|
|
|
+ classifyVo.setParentIdSet(null);
|
|
|
+ } else if (parentId == 0L) {
|
|
|
+ classifyVo.setParentIdSet(null);
|
|
|
+ } else {
|
|
|
+ // 查询父级分类
|
|
|
+ Classify parentClassify = getById(parentId);
|
|
|
+ Assert.notEmpty(parentClassify, "没有找到父级分类");
|
|
|
+
|
|
|
+ // 赋值父级id集合
|
|
|
+ String parentIdSet = parentClassify.getParentIdSet();
|
|
|
+ classifyVo.setParentIdSet(
|
|
|
+ (ObjectUtil.isEmpty(parentIdSet) ? "" : parentIdSet + ",") + parentId);
|
|
|
+ }
|
|
|
+
|
|
|
save(classifyVo);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void edit(ClassifyVo classifyVo) {
|
|
|
+ Assert.notEmpty(classifyVo.getId(), "分类Id不能为空");
|
|
|
+ classifyVo.setParentId(null);
|
|
|
+ classifyVo.setParentIdSet(null);
|
|
|
updateById(classifyVo);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void delete(ClassifyVo classifyVo) {
|
|
|
+ Long id = classifyVo.getId();
|
|
|
+ Integer type = classifyVo.getType();
|
|
|
+
|
|
|
+ Assert.notEmpty(id, "分类id不能为空");
|
|
|
+ Assert.notEmpty(type, "分类类型不能为空");
|
|
|
+
|
|
|
+ Integer childrenCount = lambdaQuery().eq(Classify::getParentId, id).count();
|
|
|
+ Assert.eqZero(childrenCount, "该分类下存在子级分类,无法删除");
|
|
|
+
|
|
|
+ if (type.equals(ClassifyTypeEnum.PRODUCT_TYPE.getCode())) {
|
|
|
+ Integer relationCount = productService.lambdaQuery().eq(Product::getClassifyId, id).count();
|
|
|
+ Assert.eqZero(relationCount, "该分类下存在产品,无法删除");
|
|
|
+ } else {
|
|
|
+ Integer relationCount = materialService.lambdaQuery().eq(Material::getClassifyId, id).count();
|
|
|
+ Assert.eqZero(relationCount, "该分类下存在物料,无法删除");
|
|
|
+ }
|
|
|
+
|
|
|
removeById(classifyVo.getId());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Classify detail(ClassifyVo classifyVo) {
|
|
|
- Classify result = getById(classifyVo.getId());
|
|
|
+ public List<Long> getChildrenIdList(Long classifyId) {
|
|
|
+
|
|
|
+ List<Classify> list = lambdaQuery()
|
|
|
+ .select(BaseEntity::getId)
|
|
|
+ .eq(BaseEntity::getTenantId, AuthUtil.getTenantId())
|
|
|
+ .like(Classify::getParentIdSet, classifyId)
|
|
|
+ .list();
|
|
|
+
|
|
|
+ List<Long> result = list.stream().map(BaseEntity::getId).collect(Collectors.toList());
|
|
|
+
|
|
|
+ result.add(0, classifyId);
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
|