¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.modules.system.service.impl; |
| | | |
| | | 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.apache.commons.lang3.StringUtils; |
| | | import org.jeecg.common.constant.SymbolConstant; |
| | | import org.jeecg.common.exception.JeecgBootException; |
| | | import org.jeecg.common.util.oConvertUtils; |
| | | import org.jeecg.modules.system.entity.SysCategory; |
| | | import org.jeecg.modules.system.mapper.SysCategoryMapper; |
| | | import org.jeecg.modules.system.model.TreeSelectModel; |
| | | import org.jeecg.modules.system.service.ISysCategoryService; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Description: åç±»åå
¸ |
| | | * @Author: jeecg-boot |
| | | * @Date: 2019-05-29 |
| | | * @Version: V1.0 |
| | | */ |
| | | @Service |
| | | public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategory> implements ISysCategoryService { |
| | | |
| | | @Resource |
| | | private SysCategoryMapper sysCategoryMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void addSysCategory(SysCategory sysCategory) { |
| | | //ä¿®æ¹åå
¸åç±» ç¼ç 为æå¨è¾å
¥ |
| | | 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); |
| | | 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("åç±»ç¼ç å·²åå¨ï¼è¯·ä¿®æ¹åéè¯ï¼"); |
| | | } |
| | | //ç¶èç¹ä¸åå¨ é»è®¤ä¸ºæ ¹èç¹ |
| | | 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 = sysCategoryMapper.selectById(sysCategory.getPid()); |
| | | if (parent != null && !ISysCategoryService.HAS_CHILD.equals(parent.getHasChild())) { |
| | | parent.setHasChild(ISysCategoryService.HAS_CHILD); |
| | | sysCategoryMapper.updateById(parent); |
| | | } else { |
| | | sysCategory.setPid(ISysCategoryService.ROOT_PID_VALUE); |
| | | } |
| | | } |
| | | sysCategoryMapper.updateById(sysCategory); |
| | | } |
| | | |
| | | @Override |
| | | public List<TreeSelectModel> queryListByCode(String pcode) throws JeecgBootException { |
| | | String pid = ROOT_PID_VALUE; |
| | | if (oConvertUtils.isNotEmpty(pcode)) { |
| | | List<SysCategory> list = baseMapper.selectList(new LambdaQueryWrapper<SysCategory>().eq(SysCategory::getCode, pcode)); |
| | | if (list == null || list.size() == 0) { |
| | | throw new JeecgBootException("该ç¼ç ã" + pcode + "ãä¸åå¨ï¼è¯·æ ¸å®!"); |
| | | } |
| | | if (list.size() > 1) { |
| | | throw new JeecgBootException("该ç¼ç ã" + pcode + "ãåå¨å¤ä¸ªï¼è¯·æ ¸å®!"); |
| | | } |
| | | pid = list.get(0).getId(); |
| | | } |
| | | return baseMapper.queryListByPid(pid, null); |
| | | } |
| | | |
| | | @Override |
| | | public List<TreeSelectModel> queryListByPid(String pid) { |
| | | if (oConvertUtils.isEmpty(pid)) { |
| | | pid = ROOT_PID_VALUE; |
| | | } |
| | | return baseMapper.queryListByPid(pid, null); |
| | | } |
| | | |
| | | @Override |
| | | public List<TreeSelectModel> queryListByPid(String pid, Map<String, String> condition) { |
| | | if (oConvertUtils.isEmpty(pid)) { |
| | | pid = ROOT_PID_VALUE; |
| | | } |
| | | return baseMapper.queryListByPid(pid, condition); |
| | | } |
| | | |
| | | @Override |
| | | public String queryIdByCode(String code) { |
| | | return baseMapper.queryIdByCode(code); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteSysCategory(String ids) { |
| | | String allIds = this.queryTreeChildIds(ids); |
| | | String pids = this.queryTreePids(ids); |
| | | //1.å 餿¶å°èç¹ä¸ææåèç¹ä¸å¹¶å é¤ |
| | | this.baseMapper.deleteBatchIds(Arrays.asList(allIds.split(","))); |
| | | //2.å°ç¶èç¹ä¸å·²ç»æ²¡æä¸çº§çèç¹ï¼ä¿®æ¹ä¸ºæ²¡æåèç¹ |
| | | if (oConvertUtils.isNotEmpty(pids)) { |
| | | LambdaUpdateWrapper<SysCategory> updateWrapper = new UpdateWrapper<SysCategory>() |
| | | .lambda() |
| | | .in(SysCategory::getId, Arrays.asList(pids.split(","))) |
| | | .set(SysCategory::getHasChild, "0"); |
| | | this.update(updateWrapper); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢èç¹ä¸ææåèç¹ |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | private String queryTreeChildIds(String ids) { |
| | | //è·åidæ°ç» |
| | | String[] idArr = ids.split(","); |
| | | StringBuffer sb = new StringBuffer(); |
| | | for (String pidVal : idArr) { |
| | | if (pidVal != null) { |
| | | if (!sb.toString().contains(pidVal)) { |
| | | if (sb.toString().length() > 0) { |
| | | sb.append(","); |
| | | } |
| | | sb.append(pidVal); |
| | | this.getTreeChildIds(pidVal, sb); |
| | | } |
| | | } |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢éä¿®æ¹æ è¯çç¶èç¹ids |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | private String queryTreePids(String ids) { |
| | | StringBuffer sb = new StringBuffer(); |
| | | //è·åidæ°ç» |
| | | String[] idArr = ids.split(","); |
| | | for (String id : idArr) { |
| | | if (id != null) { |
| | | SysCategory category = this.baseMapper.selectById(id); |
| | | //æ ¹æ®idæ¥è¯¢pidå¼ |
| | | String metaPid = category.getPid(); |
| | | //æ¥è¯¢æ¤èç¹ä¸ä¸çº§æ¯å¦è¿æå
¶ä»åèç¹ |
| | | LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(SysCategory::getPid, metaPid); |
| | | queryWrapper.notIn(SysCategory::getId, Arrays.asList(idArr)); |
| | | List<SysCategory> dataList = this.baseMapper.selectList(queryWrapper); |
| | | boolean flag = (dataList == null || dataList.size() == 0) && !Arrays.asList(idArr).contains(metaPid) |
| | | && !sb.toString().contains(metaPid); |
| | | if (flag) { |
| | | //妿å½åèç¹åæ¬æåèç¹ ç°å¨æ¨æäºï¼æ´æ°ç¶æ |
| | | sb.append(metaPid).append(","); |
| | | } |
| | | } |
| | | } |
| | | if (sb.toString().endsWith(SymbolConstant.COMMA)) { |
| | | sb = sb.deleteCharAt(sb.length() - 1); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * éå½ æ ¹æ®ç¶idè·ååèç¹id |
| | | * |
| | | * @param pidVal |
| | | * @param sb |
| | | * @return |
| | | */ |
| | | private StringBuffer getTreeChildIds(String pidVal, StringBuffer sb) { |
| | | LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(SysCategory::getPid, pidVal); |
| | | List<SysCategory> dataList = baseMapper.selectList(queryWrapper); |
| | | if (dataList != null && dataList.size() > 0) { |
| | | for (SysCategory category : dataList) { |
| | | if (!sb.toString().contains(category.getId())) { |
| | | sb.append(",").append(category.getId()); |
| | | } |
| | | this.getTreeChildIds(category.getId(), sb); |
| | | } |
| | | } |
| | | return sb; |
| | | } |
| | | |
| | | @Override |
| | | public List<String> loadDictItem(String ids) { |
| | | return this.loadDictItem(ids, true); |
| | | } |
| | | |
| | | @Override |
| | | public List<String> loadDictItem(String ids, boolean delNotExist) { |
| | | String[] idArray = ids.split(","); |
| | | LambdaQueryWrapper<SysCategory> query = new LambdaQueryWrapper<>(); |
| | | query.in(SysCategory::getId, Arrays.asList(idArray)); |
| | | // æ¥è¯¢æ°æ® |
| | | List<SysCategory> list = super.list(query); |
| | | // ååºnameå¹¶è¿å |
| | | List<String> textList; |
| | | // update-begin--author:sunjianlei--date:20210514--forï¼æ°å¢delNotExiståæ°ï¼è®¾ä¸ºfalseä¸å 餿°æ®åºéä¸åå¨çkey ---- |
| | | if (delNotExist) { |
| | | textList = list.stream().map(SysCategory::getName).collect(Collectors.toList()); |
| | | } else { |
| | | textList = new ArrayList<>(); |
| | | for (String id : idArray) { |
| | | List<SysCategory> res = list.stream().filter(i -> id.equals(i.getId())).collect(Collectors.toList()); |
| | | textList.add(res.size() > 0 ? res.get(0).getName() : id); |
| | | } |
| | | } |
| | | // update-end--author:sunjianlei--date:20210514--forï¼æ°å¢delNotExiståæ°ï¼è®¾ä¸ºfalseä¸å 餿°æ®åºéä¸åå¨çkey ---- |
| | | 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); |
| | | } |
| | | |
| | | } |