cy
2023-11-20 717ff115ee5c57c8df0fd491b40b848090d2c68e
consum-base/src/main/java/com/consum/base/service/BaseCategoryServiceImpl.java
@@ -2,17 +2,20 @@
import com.consum.base.Constants;
import com.consum.base.pojo.BaseCategoryParam;
import com.consum.base.util.IdUtil;
import com.consum.base.pojo.ProjectTreeResult;
import com.consum.base.core.utils.IdUtil;
import com.consum.model.po.BaseCategory;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.service.BaseServiceImpl;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.HashMap;
/**
 * @Description 物品分类
@@ -21,6 +24,8 @@
 */
@Service
public class BaseCategoryServiceImpl extends BaseServiceImpl {
    private static final String QUERY_TREE_ALL = "select * from base_category where states = 1  order by FATHER_CATEGORY_ID, LEVELS ASC";
    /**
     * @Description 新增分类
@@ -32,7 +37,9 @@
        BeanUtils.copyProperties(param, baseCategory);
        baseCategory.setId(IdUtil.generateId());
        //层级
        if (baseCategory.getFatherCategoryId() == 0L) {  //一级分类
        if (baseCategory.getFatherCategoryId() == null) {
            //一级分类
            baseCategory.setFatherCategoryId(0L);
            baseCategory.setLevels(Constants.LEVELS_ONE);
        } else {
            //根据父类id查询上级分类信息
@@ -53,11 +60,11 @@
    }
    /**
     * @param categoryName
     * @param fatherCategoryId
     * @Description 根据分类名称和父类id查询分类
     * @Author 卢庆阳
     * @Date 2023/10/23
     * @param categoryName
     * @param fatherCategoryId
     */
    public BaseCategory getByCategoryNameAndFatherCategoryId(String categoryName, Long fatherCategoryId) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_category WHERE 1 = 1 ");
@@ -81,7 +88,11 @@
    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());
        }
        //分类名称
        if (!StringUtils.isEmpty(param.getCategoryName())) {
            sql.append(" and category_name like:category_name ");
@@ -94,15 +105,17 @@
        }
        //状态
        if (param.getStates() != null) {
            sql.append(" and status =:status ");
            paramts.put("status", param.getStates());
            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());
    }
    /**
     * @Description  编辑
     * @Description 编辑
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
@@ -112,6 +125,7 @@
    /**
     * 修改状态
     *
     * @author 卢庆阳
     * @date 2023/9/27
     */
@@ -120,12 +134,12 @@
    }
    /**
     * @Description 根据节点id删除节点(逻辑删除)
     * @Description 根据id删除物品分类
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    public int updateById(BaseCategory baseCategory, S_user_core currentUser) {
        baseCategory.setStates(Constants.CATEGORY_DELETED);
        baseCategory.setStates(Constants.STATES_DELETED);
        //删除时间
        baseCategory.setDTime(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
        //删除人id和删除人姓名
@@ -136,10 +150,63 @@
    /**
     * 根据节点id查询节点详情
     *
     * @author 卢庆阳
     * @date 2023/9/26
     * @Date 2023/10/23
     */
    public BaseCategory getById(Long id) {
        return this.get(new BaseCategory(id));
    }
    /**
     * @Description
     * @Author 卢庆阳
     * @Date 2023/10/30
     */
    public List<BaseCategory> queryForTree() {
        // 展示全部节点
        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);
            projectTreeResult.setChildren(getChildren(projectTreeResult, all));
            return projectTreeResult;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
        return menus;
    }
    /**
     * 递归查找所有菜单的子菜单
     */
    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);
            //通过递归找到子分类
            projectTreeResult.setChildren(getChildren(projectTreeResult, all));
            return projectTreeResult;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
        return children;
    }
    /**
     * @return
     * @Description 三级分类列表查询
     * @Author 卢庆阳
     * @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());
    }
}