| | |
| | | package org.jeecg.modules.system.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.constant.FillRuleConstant; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.jeecg.common.constant.SymbolConstant; |
| | | import org.jeecg.common.exception.JeecgBootException; |
| | | import org.jeecg.common.util.FillRuleUtil; |
| | | import org.jeecg.common.util.oConvertUtils; |
| | | import org.jeecg.modules.system.entity.SysCategory; |
| | | import org.jeecg.modules.system.mapper.SysCategoryMapper; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @Service |
| | | public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategory> implements ISysCategoryService { |
| | | |
| | | @Resource |
| | | private SysCategoryMapper sysCategoryMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void addSysCategory(SysCategory sysCategory) { |
| | | String categoryCode = ""; |
| | | String categoryPid = ISysCategoryService.ROOT_PID_VALUE; |
| | | String parentCode = null; |
| | | if(oConvertUtils.isNotEmpty(sysCategory.getPid())){ |
| | | categoryPid = sysCategory.getPid(); |
| | | |
| | | //PID 不是根节点 说明需要设置父节点 hasChild 为1 |
| | | if(!ISysCategoryService.ROOT_PID_VALUE.equals(categoryPid)){ |
| | | SysCategory parent = baseMapper.selectById(categoryPid); |
| | | parentCode = parent.getCode(); |
| | | if(parent!=null && !ISysCategoryService.HAS_CHILD.equals(parent.getHasChild())){ |
| | | //修改字典分类 编码为手动输入 |
| | | if (StringUtils.isNotBlank(sysCategory.getPid())) { |
| | | SysCategory parent = sysCategoryMapper.selectById(sysCategory.getPid()); |
| | | if (parent == null) { |
| | | //父节点不存在 默认为根节点 |
| | | throw new JeecgBootException("父节点不存在,请重新添加!"); |
| | | } else { |
| | | //校验编码唯一 |
| | | SysCategory original = getByCodeAndRootCode(sysCategory.getCode(), parent.getRootCode()); |
| | | if (original != null) { |
| | | throw new JeecgBootException("分类编码已存在,请修改后重试!"); |
| | | } |
| | | if (ISysCategoryService.HAS_CHILD.equals(parent.getHasChild())) { |
| | | parent.setHasChild(ISysCategoryService.HAS_CHILD); |
| | | baseMapper.updateById(parent); |
| | | sysCategoryMapper.updateById(parent); |
| | | } |
| | | sysCategory.setRootCode(parent.getRootCode()); |
| | | String parentIds = parent.getParentIds() == null ? parent.getId() : (parent.getParentIds() + "," + parent.getId()); |
| | | sysCategory.setParentIds(parentIds); |
| | | } |
| | | } else { |
| | | //校验编码唯一 |
| | | SysCategory original = getByCodeAndRootCode(sysCategory.getCode(), null); |
| | | if (original != null) { |
| | | throw new JeecgBootException("分类编码已存在,请修改后重试!"); |
| | | } |
| | | //update-begin--Author:baihailong Date:20191209 for:分类字典编码规则生成器做成公用配置 |
| | | JSONObject formData = new JSONObject(); |
| | | formData.put("pid",categoryPid); |
| | | categoryCode = (String) FillRuleUtil.executeRule(FillRuleConstant.CATEGORY,formData); |
| | | //update-end--Author:baihailong Date:20191209 for:分类字典编码规则生成器做成公用配置 |
| | | sysCategory.setCode(categoryCode); |
| | | sysCategory.setPid(categoryPid); |
| | | baseMapper.insert(sysCategory); |
| | | //父节点不存在 默认为根节点 |
| | | sysCategory.setPid(ISysCategoryService.ROOT_PID_VALUE); |
| | | sysCategory.setRootCode(sysCategory.getCode()); |
| | | sysCategory.setParentIds(null); |
| | | } |
| | | sysCategoryMapper.insert(sysCategory); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateSysCategory(SysCategory sysCategory) { |
| | | SysCategory entity = sysCategoryMapper.selectById(sysCategory.getId()); |
| | | if (entity == null) { |
| | | throw new JeecgBootException("编辑的数据不存在,请刷新重试!"); |
| | | } |
| | | if(oConvertUtils.isEmpty(sysCategory.getPid())){ |
| | | sysCategory.setPid(ISysCategoryService.ROOT_PID_VALUE); |
| | | }else{ |
| | | //如果当前节点父ID不为空 则设置父节点的hasChild 为1 |
| | | SysCategory parent = baseMapper.selectById(sysCategory.getPid()); |
| | | SysCategory parent = sysCategoryMapper.selectById(sysCategory.getPid()); |
| | | if(parent!=null && !ISysCategoryService.HAS_CHILD.equals(parent.getHasChild())){ |
| | | parent.setHasChild(ISysCategoryService.HAS_CHILD); |
| | | baseMapper.updateById(parent); |
| | | sysCategoryMapper.updateById(parent); |
| | | } else { |
| | | sysCategory.setPid(ISysCategoryService.ROOT_PID_VALUE); |
| | | } |
| | | } |
| | | baseMapper.updateById(sysCategory); |
| | | sysCategoryMapper.updateById(sysCategory); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | /** |
| | | * 查询节点下所有子节点 |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 查询需修改标识的父节点ids |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 递归 根据父id获取子节点id |
| | | * |
| | | * @param pidVal |
| | | * @param sb |
| | | * @return |
| | |
| | | return textList; |
| | | } |
| | | |
| | | @Override |
| | | public List<SysCategory> listByParams(SysCategory query) { |
| | | QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<>(query); |
| | | if (query != null) { |
| | | if (StringUtils.isNotBlank(query.getName())) { |
| | | queryWrapper.like("t.name", query.getName()); |
| | | } |
| | | if (StringUtils.isNotBlank(query.getCode())) { |
| | | queryWrapper.like("t.code", query.getCode()); |
| | | } |
| | | } |
| | | queryWrapper.orderByDesc("t.id"); |
| | | return sysCategoryMapper.listByParams(queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deepAdd(SysCategory sysCategory, List<String> errorMessage) { |
| | | String parenCode = (StringUtils.isBlank(sysCategory.getParentCode()) ? "0" : sysCategory.getParentCode()); |
| | | if (ROOT_PID_VALUE.equals(parenCode)) { |
| | | sysCategory.setPid(ROOT_PID_VALUE); |
| | | sysCategory.setRootCode(sysCategory.getCode()); |
| | | sysCategory.setParentIds(null); |
| | | //根节点 |
| | | SysCategory entity = this.getByCodeAndRootCode(sysCategory.getCode(), null); |
| | | if (entity != null) { |
| | | sysCategory.setId(entity.getId()); |
| | | sysCategoryMapper.updateById(sysCategory); |
| | | } else { |
| | | sysCategoryMapper.insert(sysCategory); |
| | | } |
| | | } else { |
| | | //获取上级节点 |
| | | String pid = sysCategory.getPid(); |
| | | SysCategory parent = this.getById(pid); |
| | | if (parent == null) { |
| | | String rootCode = sysCategory.getRootCode(); |
| | | parent = this.getByCodeAndRootCode(parenCode, rootCode); |
| | | } |
| | | if (parent != null) { |
| | | sysCategory.setPid(parent.getId()); |
| | | sysCategory.setRootCode(parent.getRootCode()); |
| | | String parentIds = (parent.getParentIds() == null ? parent.getId() : (parent.getParentIds() + "," + parent.getId())); |
| | | sysCategory.setParentIds(parentIds); |
| | | if (!ISysCategoryService.HAS_CHILD.equals(parent.getHasChild())) { |
| | | //更新根节点 |
| | | parent.setHasChild(HAS_CHILD); |
| | | sysCategoryMapper.updateById(parent); |
| | | } |
| | | SysCategory entity = this.getByCodeAndRootCode(sysCategory.getCode(), parent.getRootCode()); |
| | | if (entity != null) { |
| | | sysCategory.setId(entity.getId()); |
| | | sysCategoryMapper.updateById(sysCategory); |
| | | } else { |
| | | sysCategoryMapper.insert(sysCategory); |
| | | } |
| | | } else { |
| | | errorMessage.add(String.format("编码[%s]的父节点没有找到,", sysCategory.getCode())); |
| | | return; |
| | | } |
| | | } |
| | | if (CollectionUtil.isNotEmpty(sysCategory.getChildren())) { |
| | | sysCategory.getChildren().forEach(child -> { |
| | | child.setPid(sysCategory.getId()); |
| | | this.deepAdd(child, errorMessage); |
| | | }); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 查询编码是否已存在 |
| | | * |
| | | * @param code 分类编码 |
| | | * @param rootCode 根节点编码 为空,添加根节点, 不为空,添加子节点 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public SysCategory getByCodeAndRootCode(String code, String rootCode) { |
| | | LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper<>(); |
| | | if (StringUtils.isNotBlank(rootCode)) { |
| | | queryWrapper.eq(SysCategory::getRootCode, rootCode); |
| | | } |
| | | queryWrapper.eq(SysCategory::getCode, code); |
| | | return sysCategoryMapper.selectOne(queryWrapper); |
| | | } |
| | | |
| | | } |