package com.ots.project.system.post.service; import com.ots.common.constant.UserConstants; import com.ots.common.exception.BusinessException; import com.ots.common.utils.StringUtils; import com.ots.common.utils.security.ShiroUtils; import com.ots.common.utils.text.Convert; import com.ots.project.system.post.domain.Post; import com.ots.project.system.post.mapper.PostMapper; import com.ots.project.system.user.mapper.UserPostMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class PostServiceImpl implements IPostService { @Autowired private PostMapper postMapper; @Autowired private UserPostMapper userPostMapper; @Override public List selectPostList(Post post) { return postMapper.selectPostList(post); } @Override public List selectPostAll() { return postMapper.selectPostAll(); } @Override public List selectPostsByUserId(Long userId) { List userPosts = postMapper.selectPostsByUserId(userId); List posts = postMapper.selectPostAll(); for (Post post : posts) { for (Post userRole : userPosts) { if (post.getPostId().longValue() == userRole.getPostId().longValue()) { post.setFlag(true); break; } } } return posts; } @Override public Post selectPostById(Long postId) { return postMapper.selectPostById(postId); } @Override public int deletePostByIds(String ids) throws BusinessException { Long[] postIds = Convert.toLongArray(ids); for (Long postId : postIds) { Post post = selectPostById(postId); if (countUserPostById(postId) > 0) { throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName())); } } return postMapper.deletePostByIds(postIds); } @Override public int insertPost(Post post) { post.setCreateBy(ShiroUtils.getLoginName()); return postMapper.insertPost(post); } @Override public int updatePost(Post post) { post.setUpdateBy(ShiroUtils.getLoginName()); return postMapper.updatePost(post); } @Override public int countUserPostById(Long postId) { return userPostMapper.countUserPostById(postId); } @Override public String checkPostNameUnique(Post post) { Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId(); Post info = postMapper.checkPostNameUnique(post.getPostName()); if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) { return UserConstants.POST_NAME_NOT_UNIQUE; } return UserConstants.POST_NAME_UNIQUE; } @Override public String checkPostCodeUnique(Post post) { Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId(); Post info = postMapper.checkPostCodeUnique(post.getPostCode()); if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) { return UserConstants.POST_CODE_NOT_UNIQUE; } return UserConstants.POST_CODE_UNIQUE; } }