[测评系统]--测评系统核心代码库
zhijie
2023-12-11 b7988d5538046c78492d52ee2c1300169b0bbcad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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<Post> selectPostList(Post post) {
        return postMapper.selectPostList(post);
    }
    
    @Override
    public List<Post> selectPostAll() {
        return postMapper.selectPostAll();
    }
    
    @Override
    public List<Post> selectPostsByUserId(Long userId) {
        List<Post> userPosts = postMapper.selectPostsByUserId(userId);
        List<Post> 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;
    }
}