consum-base/src/main/java/com/consum/base/service/impl/BaseCategoryServiceImpl.java
@@ -1,5 +1,13 @@
package com.consum.base.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.consum.base.Constants;
import com.consum.base.core.utils.IdUtil;
import com.consum.base.pojo.BaseCategoryParam;
@@ -11,13 +19,6 @@
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.service.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Description 物品分类
@@ -27,7 +28,8 @@
@Service
public class BaseCategoryServiceImpl extends BaseServiceImpl implements BaseCategoryService {
    private static final String QUERY_TREE_ALL = "select * from base_category where states = 1  order by FATHER_CATEGORY_ID, LEVELS ASC";
    private static final String QUERY_TREE_ALL =
        "select * from base_category where states = 1  order by FATHER_CATEGORY_ID, LEVELS ASC";
    /**
     * @Description 新增分类
@@ -38,24 +40,24 @@
        BaseCategory baseCategory = new BaseCategory();
        BeanUtils.copyProperties(param, baseCategory);
        baseCategory.setId(IdUtil.generateId());
        //层级
        // 层级
        if (baseCategory.getFatherCategoryId() == null) {
            //一级分类
            // 一级分类
            baseCategory.setFatherCategoryId(0L);
            baseCategory.setLevels(Constants.LEVELS_ONE);
        } else {
            //根据父类id查询上级分类信息
            // 根据父类id查询上级分类信息
            BaseCategory category = this.get(new BaseCategory(baseCategory.getFatherCategoryId()));
            if (category.getFatherCategoryId() == 0L) {  //二级分类
            if (category.getFatherCategoryId() == 0L) { // 二级分类
                baseCategory.setLevels(Constants.LEVELS_TWO);
            } else {  //三级分类
            } else { // 三级分类
                baseCategory.setLevels(Constants.LEVELS_THREE);
            }
        }
        //创建人id和创建人姓名
        // 创建人id和创建人姓名
        baseCategory.setCreateUserId(currentUser.getId());
        baseCategory.setCreateUserName(currentUser.getUser_name());
        //创建时间
        // 创建时间
        baseCategory.setCreateTime(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
        return this.insert(baseCategory);
@@ -72,13 +74,13 @@
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
        //分类名称
        sql.append(" and category_name =:category_name ");
        paramts.put("category_name", categoryName);
        //父类id
        sql.append(" and father_category_id =:father_category_id ");
        paramts.put("father_category_id", fatherCategoryId);
        // 分类名称
        sql.append("and category_name =:categoryName ");
        paramts.put("categoryName", categoryName);
        // 父类id
        sql.append("and father_category_id =:fatherCategoryId ");
        paramts.put("fatherCategoryId", fatherCategoryId);
        sql.append("and states = 1 ");
        return this.get(sql.toString(), paramts, new BaseCategory());
    }
@@ -90,24 +92,60 @@
    public GenericPager<BaseCategory> queryBaseCategoryList(BaseCategoryParam param) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
        //分类名称
        // 分类名称
        if (param.getFatherCategoryId() != null) {
            sql.append("and father_category_id =:fatherCategoryId ");
            paramts.put("fatherCategoryId", param.getFatherCategoryId());
        } else {
            sql.append("and levels =1 ");
        }
        //分类名称
        // 分类名称
        if (!StringUtils.isEmpty(param.getCategoryName())) {
            sql.append(" and category_name like:category_name ");
            paramts.put("category_name", StringUtils.CHAR_PERCENT + param.getCategoryName() + StringUtils.CHAR_PERCENT);
        }
        //类别
        // 类别
        if (!StringUtils.isEmpty(param.getClassification())) {
            sql.append(" and classification =:classification ");
            paramts.put("classification", param.getClassification());
        }
        //状态
        // 状态
        if (param.getStates() != null) {
            sql.append(" and states =:states ");
            paramts.put("states", param.getStates());
        } else {
            sql.append(" and states !=3 ");
        }
        sql.append(" ORDER BY ORDER_NUMBER,CREATE_TIME DESC ");
        return selectSplit(sql.toString(), paramts, new BaseCategory());
    }
    /**
     * 物品分类列表查询精确查询
     * @param param
     * @return
     */
    public GenericPager<BaseCategory> queryBaseCategoryList2(BaseCategoryParam param) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
        // 分类名称
        if (param.getFatherCategoryId() != null) {
            sql.append("and father_category_id =:fatherCategoryId ");
            paramts.put("fatherCategoryId", param.getFatherCategoryId());
        } else {
            sql.append("and levels =1 ");
        }
        // 分类名称
        if (!StringUtils.isEmpty(param.getCategoryName())) {
            sql.append(" and category_name =:category_name ");
            paramts.put("category_name", param.getCategoryName());
        }
        // 类别
        if (!StringUtils.isEmpty(param.getClassification())) {
            sql.append(" and classification =:classification ");
            paramts.put("classification", param.getClassification());
        }
        // 状态
        if (param.getStates() != null) {
            sql.append(" and states =:states ");
            paramts.put("states", param.getStates());
@@ -144,9 +182,9 @@
     */
    public int updateById(BaseCategory baseCategory, S_user_core currentUser) {
        baseCategory.setStates(Constants.STATES_DELETED);
        //删除时间
        // 删除时间
        baseCategory.setDTime(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
        //删除人id和删除人姓名
        // 删除人id和删除人姓名
        baseCategory.setDUserId(currentUser.getId());
        baseCategory.setDUserName(currentUser.getUser_name());
        return this.update(baseCategory);
@@ -169,47 +207,49 @@
     */
    public List<BaseCategory> queryForTree() {
        // 展示全部节点
        return this.select(QUERY_TREE_ALL, new Object[]{}, new BaseCategory());
        return this.select(QUERY_TREE_ALL, new Object[] {}, new BaseCategory());
    }
    public List<ProjectTreeResult> tree() {
        BaseCategory categoryParam = new BaseCategory();
        categoryParam.setStates(1);
        //查出所有分类
        List<BaseCategory> all = select(categoryParam);
        //组装成父子树形结构
        //1级分类
        List<ProjectTreeResult> menus = all.stream().filter(entity -> entity.getLevels() == 1).map(entity -> {
            ProjectTreeResult projectTreeResult = new ProjectTreeResult(entity);
            if (getChildren(projectTreeResult, all).isEmpty()) {
                projectTreeResult.setChildren(null);
            } else {
                projectTreeResult.setChildren(getChildren(projectTreeResult, all));
            }
            return projectTreeResult;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
        return menus;
        // 查出所有分类
        List<BaseCategory> all = this.select(categoryParam);
        // 组装成父子树形结构
        List<ProjectTreeResult> rootNodes = new ArrayList<>();
        for (BaseCategory category : all) {
            ProjectTreeResult node = new ProjectTreeResult(category);
            Long parentId = category.getFatherCategoryId();
            if (parentId == 0) {
                // 没有父节点,将其作为根节点
                rootNodes.add(node);
            } else {
                // 有父节点,将其添加到父节点的子节点列表中
                addToParent(parentId, node, rootNodes);
            }
        }
        return rootNodes;
    }
    /**
     * 递归查找所有菜单的子菜单
     */
    private List<ProjectTreeResult> getChildren(ProjectTreeResult root, List<BaseCategory> all) {
        List<ProjectTreeResult> children = all.stream().filter(entity -> entity.getFatherCategoryId().equals(root.getId())).map(entity -> {
            ProjectTreeResult projectTreeResult = new ProjectTreeResult(entity);
            //通过递归找到子分类
            if (getChildren(projectTreeResult, all).isEmpty()) {
                projectTreeResult.setChildren(null);
    private void addToParent(Long parentId, ProjectTreeResult node, List<ProjectTreeResult> nodes) {
        for (ProjectTreeResult parent : nodes) {
            if (parent.getId().equals(parentId)) {
                // 找到了父节点,将其添加到子节点列表中
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(node);
                break;
            } else {
                projectTreeResult.setChildren(getChildren(projectTreeResult, all));
                // 继续查找父节点的子节点
                List<ProjectTreeResult> children = parent.getChildren();
                if (children != null) {
                    addToParent(parentId, node, children);
                }
            }
            return projectTreeResult;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
        return children;
        }
    }
    /**
     * @return
@@ -218,7 +258,40 @@
     * @Date 2023/10/30
     */
    public List<BaseCategory> queryForLv3Tree() {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE states = 1 and levels = 3 order by ORDER_NUMBER,CREATE_TIME desc");
        return this.select(sql.toString(), new Object[]{}, new BaseCategory());
        StringBuilder sql = new StringBuilder(
            "SELECT * FROM base_category WHERE states = 1 and levels = 3 order by ORDER_NUMBER,CREATE_TIME desc");
        return this.select(sql.toString(), new Object[] {}, new BaseCategory());
    }
    @Override
    public BaseCategory getByCategoryByName(String categoryName) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
        // 分类名称
        sql.append(" and category_name =:category_name ");
        paramts.put("category_name", categoryName);
        BaseCategory baseCategory = this.get(sql.toString(), paramts, new BaseCategory());
        return baseCategory;
    }
    /**
     * 查询分类同级别的最大排序号
     * @param id 分类
     * @return
     */
    @Override
    public int selIndexByPid(Long id) {
        StringBuilder sql = new StringBuilder("SELECT max(ORDER_NUMBER) maxNum FROM base_category WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
        // 分类名称
        sql.append(" and FATHER_CATEGORY_ID =:FATHER_CATEGORY_ID ");
        paramts.put("FATHER_CATEGORY_ID", id);
        Map getMap = this.get(sql.toString(), paramts);
        Object obj = getMap.get("maxNum");
        if(obj!=null){
            return new Integer(obj.toString());
        }
        return 0;
    }
}