From 961073ec96bfbb920f9afd952052c20b3791eb4d Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期二, 15 二月 2022 10:45:04 +0800 Subject: [PATCH] update 优化 TreeBuildUtils 工具 使用反射自动获取顶级父id --- ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java | 77 ++++++++++++++++++++++++-------------- 1 files changed, 48 insertions(+), 29 deletions(-) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java index 71035b9..7e2a56c 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java @@ -1,30 +1,45 @@ package com.ruoyi.system.service.impl; -import cn.hutool.core.lang.Validator; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import cn.hutool.core.util.ObjectUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.constant.UserConstants; -import com.ruoyi.common.exception.CustomException; +import com.ruoyi.common.core.domain.PageQuery; +import com.ruoyi.common.core.page.TableDataInfo; +import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.SysPost; +import com.ruoyi.system.domain.SysUserPost; import com.ruoyi.system.mapper.SysPostMapper; import com.ruoyi.system.mapper.SysUserPostMapper; import com.ruoyi.system.service.ISysPostService; -import org.springframework.beans.factory.annotation.Autowired; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.Arrays; import java.util.List; /** * 宀椾綅淇℃伅 鏈嶅姟灞傚鐞� * - * @author ruoyi + * @author Lion Li */ +@RequiredArgsConstructor @Service -public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService { - @Autowired - private SysPostMapper postMapper; +public class SysPostServiceImpl implements ISysPostService { - @Autowired - private SysUserPostMapper userPostMapper; + private final SysPostMapper baseMapper; + private final SysUserPostMapper userPostMapper; + + @Override + public TableDataInfo<SysPost> selectPagePostList(SysPost post, PageQuery pageQuery) { + LambdaQueryWrapper<SysPost> lqw = new LambdaQueryWrapper<SysPost>() + .like(StringUtils.isNotBlank(post.getPostCode()), SysPost::getPostCode, post.getPostCode()) + .eq(StringUtils.isNotBlank(post.getStatus()), SysPost::getStatus, post.getStatus()) + .like(StringUtils.isNotBlank(post.getPostName()), SysPost::getPostName, post.getPostName()); + Page<SysPost> page = baseMapper.selectPage(pageQuery.build(), lqw); + return TableDataInfo.build(page); + } /** * 鏌ヨ宀椾綅淇℃伅闆嗗悎 @@ -34,7 +49,10 @@ */ @Override public List<SysPost> selectPostList(SysPost post) { - return postMapper.selectPostList(post); + return baseMapper.selectList(new LambdaQueryWrapper<SysPost>() + .like(StringUtils.isNotBlank(post.getPostCode()), SysPost::getPostCode, post.getPostCode()) + .eq(StringUtils.isNotBlank(post.getStatus()), SysPost::getStatus, post.getStatus()) + .like(StringUtils.isNotBlank(post.getPostName()), SysPost::getPostName, post.getPostName())); } /** @@ -44,7 +62,7 @@ */ @Override public List<SysPost> selectPostAll() { - return postMapper.selectPostAll(); + return baseMapper.selectList(); } /** @@ -55,7 +73,7 @@ */ @Override public SysPost selectPostById(Long postId) { - return postMapper.selectPostById(postId); + return baseMapper.selectById(postId); } /** @@ -65,8 +83,8 @@ * @return 閫変腑宀椾綅ID鍒楄〃 */ @Override - public List<Integer> selectPostListByUserId(Long userId) { - return postMapper.selectPostListByUserId(userId); + public List<Long> selectPostListByUserId(Long userId) { + return baseMapper.selectPostListByUserId(userId); } /** @@ -77,9 +95,10 @@ */ @Override public String checkPostNameUnique(SysPost post) { - Long postId = Validator.isNull(post.getPostId()) ? -1L : post.getPostId(); - SysPost info = postMapper.checkPostNameUnique(post.getPostName()); - if (Validator.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) { + boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysPost>() + .eq(SysPost::getPostName, post.getPostName()) + .ne(ObjectUtil.isNotNull(post.getPostId()), SysPost::getPostId, post.getPostId())); + if (exist) { return UserConstants.NOT_UNIQUE; } return UserConstants.UNIQUE; @@ -93,9 +112,10 @@ */ @Override public String checkPostCodeUnique(SysPost post) { - Long postId = Validator.isNull(post.getPostId()) ? -1L : post.getPostId(); - SysPost info = postMapper.checkPostCodeUnique(post.getPostCode()); - if (Validator.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) { + boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysPost>() + .eq(SysPost::getPostCode, post.getPostCode()) + .ne(ObjectUtil.isNotNull(post.getPostId()), SysPost::getPostId, post.getPostId())); + if (exist) { return UserConstants.NOT_UNIQUE; } return UserConstants.UNIQUE; @@ -108,8 +128,8 @@ * @return 缁撴灉 */ @Override - public int countUserPostById(Long postId) { - return userPostMapper.countUserPostById(postId); + public long countUserPostById(Long postId) { + return userPostMapper.selectCount(new LambdaQueryWrapper<SysUserPost>().eq(SysUserPost::getPostId, postId)); } /** @@ -120,7 +140,7 @@ */ @Override public int deletePostById(Long postId) { - return postMapper.deletePostById(postId); + return baseMapper.deleteById(postId); } /** @@ -128,17 +148,16 @@ * * @param postIds 闇�瑕佸垹闄ょ殑宀椾綅ID * @return 缁撴灉 - * @throws Exception 寮傚父 */ @Override public int deletePostByIds(Long[] postIds) { for (Long postId : postIds) { SysPost post = selectPostById(postId); if (countUserPostById(postId) > 0) { - throw new CustomException(String.format("%1$s宸插垎閰�,涓嶈兘鍒犻櫎", post.getPostName())); + throw new ServiceException(String.format("%1$s宸插垎閰�,涓嶈兘鍒犻櫎", post.getPostName())); } } - return postMapper.deletePostByIds(postIds); + return baseMapper.deleteBatchIds(Arrays.asList(postIds)); } /** @@ -149,7 +168,7 @@ */ @Override public int insertPost(SysPost post) { - return postMapper.insertPost(post); + return baseMapper.insert(post); } /** @@ -160,6 +179,6 @@ */ @Override public int updatePost(SysPost post) { - return postMapper.updatePost(post); + return baseMapper.updateById(post); } } -- Gitblit v1.9.3