diff --git a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java index fabb55dbb..709723993 100644 --- a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java +++ b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java @@ -124,6 +124,13 @@ public class RedisKeys { * @date 2019/11/28 09:56 */ public static String getAdminUserDeptOptionKey(Long userId) { - return rootPrefix.concat("sys:user:dept:options:").concat(String.valueOf(userId)); + return rootPrefix.concat("options:dept:user:").concat(String.valueOf(userId)); + } + + /** + * 事件分类 + */ + public static String getEventsCategoryKey() { + return rootPrefix.concat("options:category:events"); } } diff --git a/esua-epdc/epdc-module/epdc-events/epdc-events-client/src/main/java/com/elink/esua/epdc/dto/category/CategoryOption.java b/esua-epdc/epdc-module/epdc-events/epdc-events-client/src/main/java/com/elink/esua/epdc/dto/category/CategoryOption.java new file mode 100644 index 000000000..6a8bc7923 --- /dev/null +++ b/esua-epdc/epdc-module/epdc-events/epdc-events-client/src/main/java/com/elink/esua/epdc/dto/category/CategoryOption.java @@ -0,0 +1,15 @@ +package com.elink.esua.epdc.dto.category; + +import lombok.Data; + +import java.util.List; + +/** + * @Description 事件类别树 + * @Author yinzuomei + * @Date 2019/12/3 18:03 + */ +@Data +public class CategoryOption { + private List options; +} diff --git a/esua-epdc/epdc-module/epdc-events/epdc-events-client/src/main/java/com/elink/esua/epdc/dto/category/CategoryTreeDTO.java b/esua-epdc/epdc-module/epdc-events/epdc-events-client/src/main/java/com/elink/esua/epdc/dto/category/CategoryTreeDTO.java new file mode 100644 index 000000000..07370fb80 --- /dev/null +++ b/esua-epdc/epdc-module/epdc-events/epdc-events-client/src/main/java/com/elink/esua/epdc/dto/category/CategoryTreeDTO.java @@ -0,0 +1,37 @@ +package com.elink.esua.epdc.dto.category; + + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * @Description + * @Author yinzuomei + * @Date 2019/12/3 14:34 + */ +@Data +public class CategoryTreeDTO implements Serializable { + + private static final long serialVersionUID = 8751086975269527708L; + /** + * id + */ + private Long id; + /** + * 上级分类ID + */ + private Long pid; + + /** + * 分类名称 + */ + private String categoryName; + + private String value; + + private String label; + + private List children; +} diff --git a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/controller/CategoryController.java b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/controller/CategoryController.java index 9ddd04bab..a4827fb56 100644 --- a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/controller/CategoryController.java +++ b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/controller/CategoryController.java @@ -26,6 +26,7 @@ import com.elink.esua.epdc.commons.tools.validator.group.AddGroup; import com.elink.esua.epdc.commons.tools.validator.group.UpdateGroup; import com.elink.esua.epdc.commons.tools.validator.group.DefaultGroup; import com.elink.esua.epdc.dto.category.CategoryDTO; +import com.elink.esua.epdc.dto.category.CategoryOption; import com.elink.esua.epdc.modules.category.excel.CategoryExcel; import com.elink.esua.epdc.modules.category.service.CategoryService; import io.swagger.annotations.ApiOperation; @@ -102,4 +103,28 @@ public class CategoryController { return categoryService.insertPartyList(file); } + /** + * @param + * @return com.elink.esua.epdc.commons.tools.utils.Result + * @Author yinzuomei + * @Description 组装事件类别 + * @Date 2019/12/3 18:19 + **/ + @GetMapping("packageCategoryOption") + public Result packageCategoryOption() { + categoryService.packageCategoryOption(); + return new Result(); + } + + /** + * @param + * @return com.elink.esua.epdc.commons.tools.utils.Result + * @Author yinzuomei + * @Description 查询事件类别树, 供下拉框调用 + * @Date 2019/12/3 18:07 + **/ + @GetMapping("getCategoryTree") + public Result getCategoryTree() { + return categoryService.getCategoryTree(); + } } diff --git a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/dao/CategoryDao.java b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/dao/CategoryDao.java index 246e82610..d41e913ee 100644 --- a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/dao/CategoryDao.java +++ b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/dao/CategoryDao.java @@ -18,6 +18,7 @@ package com.elink.esua.epdc.modules.category.dao; import com.elink.esua.epdc.commons.mybatis.dao.BaseDao; +import com.elink.esua.epdc.dto.category.CategoryTreeDTO; import com.elink.esua.epdc.modules.category.entity.CategoryEntity; import org.apache.ibatis.annotations.Mapper; @@ -44,4 +45,5 @@ public interface CategoryDao extends BaseDao { */ List getSubCategoryIdList(String id); + List selectListCategoryTreeDTO(); } diff --git a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/service/CategoryService.java b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/service/CategoryService.java index b855a241a..7a01e5fb0 100644 --- a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/service/CategoryService.java +++ b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/service/CategoryService.java @@ -20,6 +20,7 @@ package com.elink.esua.epdc.modules.category.service; import com.elink.esua.epdc.commons.mybatis.service.BaseService; import com.elink.esua.epdc.commons.tools.utils.Result; import com.elink.esua.epdc.dto.category.CategoryDTO; +import com.elink.esua.epdc.dto.category.CategoryOption; import com.elink.esua.epdc.modules.category.entity.CategoryEntity; import org.springframework.web.multipart.MultipartFile; @@ -101,4 +102,22 @@ public interface CategoryService extends BaseService { * 导入分类表格 */ Result insertPartyList(MultipartFile file); + + /** + * @param + * @return void + * @Author yinzuomei + * @Description 组装事件类别 + * @Date 2019/12/3 18:20 + **/ + void packageCategoryOption(); + + /** + * @param + * @return com.elink.esua.epdc.commons.tools.utils.Result + * @Author yinzuomei + * @Description 查询事件类别树, 供下拉框调用 + * @Date 2019/12/3 18:04 + **/ + Result getCategoryTree(); } diff --git a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/service/impl/CategoryServiceImpl.java b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/service/impl/CategoryServiceImpl.java index f771bcb9a..8c5841e6f 100644 --- a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/service/impl/CategoryServiceImpl.java +++ b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/category/service/impl/CategoryServiceImpl.java @@ -17,32 +17,35 @@ package com.elink.esua.epdc.modules.category.service.impl; -import cn.afterturn.easypoi.excel.ExcelImportUtil; -import cn.afterturn.easypoi.excel.entity.ImportParams; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.core.metadata.IPage; import com.elink.esua.epdc.commons.mybatis.service.impl.BaseServiceImpl; import com.elink.esua.epdc.commons.tools.constant.Constant; +import com.elink.esua.epdc.commons.tools.constant.NumConstant; import com.elink.esua.epdc.commons.tools.exception.ErrorCode; import com.elink.esua.epdc.commons.tools.exception.RenException; -import com.elink.esua.epdc.commons.tools.page.PageData; +import com.elink.esua.epdc.commons.tools.redis.RedisKeys; +import com.elink.esua.epdc.commons.tools.redis.RedisUtils; import com.elink.esua.epdc.commons.tools.utils.ConvertUtils; -import com.elink.esua.epdc.commons.tools.constant.FieldConstant; import com.elink.esua.epdc.commons.tools.utils.Result; import com.elink.esua.epdc.commons.tools.utils.TreeUtils; import com.elink.esua.epdc.dto.category.CategoryDTO; +import com.elink.esua.epdc.dto.category.CategoryOption; +import com.elink.esua.epdc.dto.category.CategoryTreeDTO; import com.elink.esua.epdc.modules.category.dao.CategoryDao; import com.elink.esua.epdc.modules.category.entity.CategoryEntity; import com.elink.esua.epdc.modules.category.redis.CategoryRedis; import com.elink.esua.epdc.modules.category.service.CategoryService; +import com.google.common.collect.Lists; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.io.File; import java.util.*; /** @@ -54,173 +57,291 @@ import java.util.*; @Service public class CategoryServiceImpl extends BaseServiceImpl implements CategoryService { - @Autowired - private CategoryRedis categoryRedis; - - @Override - public List list(Map params) { - List entityList = baseDao.getList(params); - - List dtoList = ConvertUtils.sourceToTarget(entityList, CategoryDTO.class); - - return TreeUtils.build(dtoList); - } - - @Override - public CategoryDTO get(Long id) { - if (id == null) { - return null; - } - CategoryEntity entity = baseDao.getById(id); - - return ConvertUtils.sourceToTarget(entity, CategoryDTO.class); - } - - @Override - @Transactional(rollbackFor = Exception.class) - public void save(CategoryDTO dto) { - String categoryCode = dto.getCategoryCode(); - if (StringUtils.isNotBlank(categoryCode)) { - if (getCodeCount(dto) > 0) { - throw new RenException("您输入的编码已存在"); - } - } - CategoryEntity entity = ConvertUtils.sourceToTarget(dto, CategoryEntity.class); - List list = getPidListByPid(entity.getPid()); - if (list.size() == 0){ - entity.setPids("0"); - }else { - String pids = ""; - for (int i = 0; i < list.size(); i++){ - if (i == list.size() -1){ - pids = pids + list.get(i).getId().toString(); - }else { - pids = pids + list.get(i).getId().toString() + ","; - } - } - entity.setPids(pids); - } - insert(entity); - } - - @Override - public List getPidListById(Long id) { - //所有的分类列表 - CategoryEntity categoryEntity = baseDao.selectById(id); - return getPidListByPid(categoryEntity.getPid()); - } - - /** - * 获取所有上级分类 - * - * @param pid 上级ID - */ - private List getPidListByPid(Long pid) { - //顶级部门,无上级部门 - if (Constant.CATEGORY_ROOT.equals(pid)) { - return new ArrayList<>(); - } - - //所有的分类列表 - QueryWrapper wrapper = new QueryWrapper<>(); - List categoryEntityList = baseDao.selectList(wrapper); - - //list转map - Map map = new HashMap<>(categoryEntityList.size()); - for (CategoryEntity entity : categoryEntityList) { - map.put(entity.getId(), entity); - } - - //递归查询所有上级分类列表 - List pCategoryEntityList = new ArrayList<>(); - - getPidTree(pid, map, pCategoryEntityList); - - return pCategoryEntityList; - } - - private void getPidTree(Long pid, Map map, List pCategoryEntityList) { - //顶级,无上级 - if (Constant.CATEGORY_ROOT.equals(pid)) { - return; - } - //上级存在 - CategoryEntity parent = map.get(pid); - if (parent != null) { - getPidTree(parent.getPid(), map, pCategoryEntityList); - } - - pCategoryEntityList.add(parent); - } - - @Override - public Integer getCodeCount(CategoryDTO dto) { - QueryWrapper wrapper = new QueryWrapper<>(); - wrapper.eq("category_code", dto.getCategoryCode()); - Long id = dto.getId(); - wrapper.ne(id!=null, "id", dto.getId()); - wrapper.eq("del_flag","0"); - return baseDao.selectCount(wrapper); - } - - - - @Override - @Transactional(rollbackFor = Exception.class) - public void update(CategoryDTO dto) { - String categoryCode = dto.getCategoryCode(); - if (StringUtils.isNotBlank(categoryCode)) { - if (getCodeCount(dto) > 0) { - throw new RenException("您输入的编码已存在"); - } - } - CategoryEntity entity = ConvertUtils.sourceToTarget(dto, CategoryEntity.class); - //上级部门不能为自身 - if (entity.getId().equals(entity.getPid())) { - throw new RenException(ErrorCode.SUPERIOR_DEPT_ERROR); - } - List list = getPidListByPid(entity.getPid()); - if (list.size() == 0){ - entity.setPids("0"); - }else { - String pids = ""; - for (int i = 0; i < list.size(); i++){ - if (i == list.size() -1){ - pids = pids + list.get(i).getId().toString(); - }else { - pids = pids + list.get(i).getId().toString() + ","; - } - } - entity.setPids(pids); - } - updateById(entity); - } - - @Override - @Transactional(rollbackFor = Exception.class) - public void delete(Long id) { - //判断是否有子部门 - List subList = getSubCategoryIdList(id); - if (subList.size() > 1) { - throw new RenException(ErrorCode.DEPT_SUB_DELETE_ERROR); - } - baseDao.deleteBatchIds(Arrays.asList(new Long[]{id})); - - // 逻辑删除(@TableLogic 注解) - //logicDelete(new Long[]{id}, CategoryEntity.class); - - } - - @Override - public List getSubCategoryIdList(Long id) { - List subCategoryIdList = baseDao.getSubCategoryIdList("%" + id + "%"); - subCategoryIdList.add(id); - return subCategoryIdList; - } - - @Override - public Result insertPartyList(MultipartFile file) { - - return new Result(); - } + @Autowired + private CategoryRedis categoryRedis; + @Autowired + private RedisUtils redisUtils; + + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Override + public List list(Map params) { + List entityList = baseDao.getList(params); + + List dtoList = ConvertUtils.sourceToTarget(entityList, CategoryDTO.class); + + return TreeUtils.build(dtoList); + } + + @Override + public CategoryDTO get(Long id) { + if (id == null) { + return null; + } + CategoryEntity entity = baseDao.getById(id); + + return ConvertUtils.sourceToTarget(entity, CategoryDTO.class); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void save(CategoryDTO dto) { + String categoryCode = dto.getCategoryCode(); + if (StringUtils.isNotBlank(categoryCode)) { + if (getCodeCount(dto) > 0) { + throw new RenException("您输入的编码已存在"); + } + } + CategoryEntity entity = ConvertUtils.sourceToTarget(dto, CategoryEntity.class); + List list = getPidListByPid(entity.getPid()); + if (list.size() == 0) { + entity.setPids("0"); + } else { + String pids = ""; + for (int i = 0; i < list.size(); i++) { + if (i == list.size() - 1) { + pids = pids + list.get(i).getId().toString(); + } else { + pids = pids + list.get(i).getId().toString() + ","; + } + } + entity.setPids(pids); + } + insert(entity); + this.packageCategoryOption(); + } + + @Override + public List getPidListById(Long id) { + //所有的分类列表 + CategoryEntity categoryEntity = baseDao.selectById(id); + return getPidListByPid(categoryEntity.getPid()); + } + + /** + * 获取所有上级分类 + * + * @param pid 上级ID + */ + private List getPidListByPid(Long pid) { + //顶级部门,无上级部门 + if (Constant.CATEGORY_ROOT.equals(pid)) { + return new ArrayList<>(); + } + + //所有的分类列表 + QueryWrapper wrapper = new QueryWrapper<>(); + List categoryEntityList = baseDao.selectList(wrapper); + + //list转map + Map map = new HashMap<>(categoryEntityList.size()); + for (CategoryEntity entity : categoryEntityList) { + map.put(entity.getId(), entity); + } + + //递归查询所有上级分类列表 + List pCategoryEntityList = new ArrayList<>(); + + getPidTree(pid, map, pCategoryEntityList); + + return pCategoryEntityList; + } + + private void getPidTree(Long pid, Map map, List pCategoryEntityList) { + //顶级,无上级 + if (Constant.CATEGORY_ROOT.equals(pid)) { + return; + } + //上级存在 + CategoryEntity parent = map.get(pid); + if (parent != null) { + getPidTree(parent.getPid(), map, pCategoryEntityList); + } + + pCategoryEntityList.add(parent); + } + + @Override + public Integer getCodeCount(CategoryDTO dto) { + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("category_code", dto.getCategoryCode()); + Long id = dto.getId(); + wrapper.ne(id != null, "id", dto.getId()); + wrapper.eq("del_flag", "0"); + return baseDao.selectCount(wrapper); + } + + + @Override + @Transactional(rollbackFor = Exception.class) + public void update(CategoryDTO dto) { + String categoryCode = dto.getCategoryCode(); + if (StringUtils.isNotBlank(categoryCode)) { + if (getCodeCount(dto) > 0) { + throw new RenException("您输入的编码已存在"); + } + } + CategoryEntity entity = ConvertUtils.sourceToTarget(dto, CategoryEntity.class); + //上级部门不能为自身 + if (entity.getId().equals(entity.getPid())) { + throw new RenException(ErrorCode.SUPERIOR_DEPT_ERROR); + } + List list = getPidListByPid(entity.getPid()); + if (list.size() == 0) { + entity.setPids("0"); + } else { + String pids = ""; + for (int i = 0; i < list.size(); i++) { + if (i == list.size() - 1) { + pids = pids + list.get(i).getId().toString(); + } else { + pids = pids + list.get(i).getId().toString() + ","; + } + } + entity.setPids(pids); + } + updateById(entity); + this.packageCategoryOption(); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void delete(Long id) { + //判断是否有子部门 + List subList = getSubCategoryIdList(id); + if (subList.size() > 1) { + throw new RenException(ErrorCode.DEPT_SUB_DELETE_ERROR); + } + baseDao.deleteBatchIds(Arrays.asList(new Long[]{id})); + + // 逻辑删除(@TableLogic 注解) + //logicDelete(new Long[]{id}, CategoryEntity.class); + this.packageCategoryOption(); + } + + @Override + public List getSubCategoryIdList(Long id) { + List subCategoryIdList = baseDao.getSubCategoryIdList("%" + id + "%"); + subCategoryIdList.add(id); + return subCategoryIdList; + } + + @Override + public Result insertPartyList(MultipartFile file) { + + return new Result(); + } + + /** + * @param + * @return com.elink.esua.epdc.commons.tools.utils.Result + * @Author yinzuomei + * @Description 查询事件类别树, 供下拉框调用 + * @Date 2019/12/3 18:07 + **/ + @Override + public Result getCategoryTree() { + String eventsCategoryKey = RedisKeys.getEventsCategoryKey(); + Object obj = redisUtils.get(eventsCategoryKey); + if (null == obj) { + this.packageCategoryOption(); + obj = redisUtils.get(eventsCategoryKey); + } + return new Result().ok((CategoryOption) obj); + } + + /** + * @param + * @return void + * @Author yinzuomei + * @Description 组装事件类别 + * @Date 2019/12/3 18:18 + **/ + @Override + public void packageCategoryOption() { + List dtoList = baseDao.selectListCategoryTreeDTO(); + JSONObject node; + JSONArray headNodes = new JSONArray(); + for (CategoryTreeDTO categoryTreeDto : dtoList) { + + if (categoryTreeDto.getPid().longValue() == NumConstant.ZERO_L) { + node = new JSONObject(); + node.put("value", categoryTreeDto.getId()); + node.put("label", categoryTreeDto.getCategoryName()); + headNodes.add(node); + } + } + + // 用于存放所有父级节点 + JSONArray parent; + // 用于存放所有子级节点 + JSONArray allChildren = new JSONArray(); + JSONArray children; + // 用于存放单个子级节点 + JSONObject childNode; + + parent = headNodes; + // 存放其余未处理的类别(节点) + List others = this.getOtherCategory(dtoList, parent); + + while (!others.isEmpty()) { + for (int i = 0; i < parent.size(); i++) { + node = parent.getJSONObject(i); + children = new JSONArray(); + for (CategoryTreeDTO categoryTreeDto : others) { + if (categoryTreeDto.getPid().equals(node.get("value"))) { + childNode = new JSONObject(); + childNode.put("value", categoryTreeDto.getId()); + childNode.put("label", categoryTreeDto.getCategoryName()); + + children.add(childNode); + allChildren.add(childNode); + } + } + if (!children.isEmpty()) { + node.put("children", children); + } + } + parent = allChildren; + + others = this.getOtherCategory(others, parent); + + } +// JSONObject treeNodes = new JSONObject(); +// treeNodes.put("options", headNodes); +// logger.info(treeNodes.toJSONString()); + //存放到redis中 + List cache = Lists.newArrayList(); + cache.add(headNodes); + CategoryOption option = new CategoryOption(); + option.setOptions(cache.get(0)); + redisUtils.set(RedisKeys.getEventsCategoryKey(), option); + } + + /* + * @param dtoList 上次进行筛选的分类集合 + * @param parent 已处理过的上级节点 + * @return java.util.List + * @Author yinzuomei + * @Description 组装其余未处理过的分类集合 + * @Date 2019/12/3 17:27 + **/ + private List getOtherCategory(List dtoList, JSONArray parent) { + List already = Lists.newArrayList(); + for (int i = 0; i < parent.size(); i++) { + already.add((Long) parent.getJSONObject(i).get("value")); + } + + List others = Lists.newArrayList(); + + for (CategoryTreeDTO categoryTreeDto : dtoList) { + Long categoryTreeDtoId = categoryTreeDto.getId(); + if (!already.contains(categoryTreeDtoId)) { + others.add(categoryTreeDto); + } + } + return others; + } + + } diff --git a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/resources/mapper/category/CategoryDao.xml b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/resources/mapper/category/CategoryDao.xml index 01a4f0fbd..35d08f827 100644 --- a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/resources/mapper/category/CategoryDao.xml +++ b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/resources/mapper/category/CategoryDao.xml @@ -34,4 +34,18 @@ select id from epdc_category where pids like #{id} and del_flag = 0 +