| | |
| | | package com.consum.base.service.impl; |
| | | |
| | | import java.util.Comparator; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | import java.util.Map; |
| | | |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | HashMap<String, Object> paramts = new HashMap<>(); |
| | | |
| | | // 分类名称 |
| | | sql.append(" and category_name =:category_name "); |
| | | paramts.put("category_name", categoryName); |
| | | sql.append("and category_name =:categoryName "); |
| | | paramts.put("categoryName", categoryName); |
| | | // 父类id |
| | | sql.append(" and father_category_id =:father_category_id "); |
| | | paramts.put("father_category_id", fatherCategoryId); |
| | | |
| | | sql.append("and father_category_id =:fatherCategoryId "); |
| | | paramts.put("fatherCategoryId", fatherCategoryId); |
| | | sql.append("and states = 1 "); |
| | | return this.get(sql.toString(), paramts, new BaseCategory()); |
| | | } |
| | | |
| | |
| | | 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())) { |
| | |
| | | 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); |
| | | } else { |
| | | projectTreeResult.setChildren(getChildren(projectTreeResult, all)); |
| | | 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<>()); |
| | | } |
| | | return projectTreeResult; |
| | | }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))) |
| | | .collect(Collectors.toList()); |
| | | return children; |
| | | parent.getChildren().add(node); |
| | | break; |
| | | } else { |
| | | // 继续查找父节点的子节点 |
| | | List<ProjectTreeResult> children = parent.getChildren(); |
| | | if (children != null) { |
| | | addToParent(parentId, node, children); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |