IT-KIMI_SHI\SINOIT.KIMI
2018-12-07 50eb1d766c470dc6ff927199eaee934f972a8b70
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package service.myPanel.impl;
 
import common.util.TemplateUtil;
import dao.BaseMapper;
import dao.mapper.authority.GroupMapper;
import dao.mapper.authority.UserRealmMapper;
import model.authority.Authority;
import model.authority.Group;
import model.authority.User;
import model.myPanel.MyCharts;
import org.apache.shiro.SecurityUtils;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.stereotype.Service;
import service.authority.UserService;
import service.myPanel.MyChartsService;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * Created by ct on 2016/8/30.
 */
@Service
public class MyChartsServiceImpl implements MyChartsService{
 
    private static final String NAMESPACE = MyCharts.class.getName();
    @Resource
    private BaseMapper<MyCharts> baseMapper;
 
    @Resource
    private UserRealmMapper userRealmMapper;
 
    @Resource
    private GroupMapper groupMapper;
 
    @Override
    public int insert(MyCharts myCharts) throws Exception {
        User currentUser = (User)SecurityUtils.getSubject().getPrincipal();
        myCharts.setStatmentId(NAMESPACE + ".insert");
        baseMapper.insert(myCharts);
        int reporterId = Integer.parseInt(myCharts.getId()) - 1;
 
        Authority authority = TemplateUtil.genObjFormJson(myCharts.getAuthority(), Authority.class);
        if(!"".equals(authority) && authority != null){
            //有读权限的用户组
            List<String> groupsForRead = authority.getGroupRead();
            //有写权限的用户组
            List<String> groupsForWrite = authority.getGroupWrite();
            //当前用户id
            String currentUserId = userRealmMapper.queryAsObject(currentUser.getUserName()).getUserId();
            //所有普通用户
            List<User> consumers = userRealmMapper.queryByUserType(0);
            //所有超级用户
            List<User> admins = userRealmMapper.queryByUserType(1);
 
            List<Map<String, Object>> paramList = new ArrayList<>();
 
            //添加普通用户对于该图表的权限
            for(User user : consumers){
                Map<String, Object> map = new HashMap<>();
                map.put("reporterId", reporterId);
                map.put("privsResourceId", "US"+user.getUserId());
                if(user.getUserId().equals(currentUserId)){
                    map.put("read", 1);
                    map.put("write", 1);
                }else {
                    map.put("read", authority.getConsumerRead());
                    map.put("write", authority.getConsumerWrite());
                }
                paramList.add(map);
            }
            //添加超级用户对于该表的权限
            userRealmMapper.insertAuthorityForBatch(paramList);
            paramList.clear();
            for(User user : admins){
                Map<String, Object> map = new HashMap<>();
                map.put("reporterId", reporterId);
                map.put("privsResourceId", "US"+user.getUserId());
                map.put("read", 1);
                map.put("write", 1);
                paramList.add(map);
            }
            userRealmMapper.insertAuthorityForBatch(paramList);
            //添加有读权限分组
            for(String groupId : groupsForRead){
                userRealmMapper.insertAuthority(Integer.toString(reporterId),"GR"+groupId,1,0);
            }
            //添加有写权限的分组
            for(String groupId : groupsForWrite){
                if(groupsForRead.contains(groupId)){
                    userRealmMapper.updateAuthority("GR"+groupId);
                }else {
                    userRealmMapper.insertAuthority(Integer.toString(reporterId),"GR"+groupId,1,1);
                }
            }
        }
        return reporterId;
    }
 
    @Override
    public int update(MyCharts myCharts) throws Exception {
        myCharts.setStatmentId(NAMESPACE + ".update");
        return baseMapper.update(myCharts);
    }
 
    @Override
    public int delete(MyCharts myCharts) throws Exception {
        myCharts.setStatmentId(NAMESPACE + ".delete");
        return baseMapper.delete(myCharts);
    }
 
    @Override
    public MyCharts selectOneChartInfo(MyCharts myCharts) throws Exception {
        myCharts.setStatmentId(NAMESPACE + ".selectOne");
        return baseMapper.selectOne(myCharts);
    }
 
    @Override
    public List<MyCharts> selectChartInfo(MyCharts myCharts) throws Exception {
        HashSet<String> reporterIdSet = new HashSet<>();
        User currentUser = (User)SecurityUtils.getSubject().getPrincipal();
 
        User paramUser = userRealmMapper.queryAsObject(currentUser.getUserName());
        paramUser.setUserId("US"+currentUser.getUserId());
        //当前用户所在的用户组
        List<String> groupIds = groupMapper.queryGroupIdFromRelation(paramUser.getUserId());
 
        List<String> reporterIds1 = userRealmMapper.queryReporterIdByUser(paramUser);
        reporterIdSet.addAll(reporterIds1);
        for(String groupId : groupIds){
            List<String> reporterIds2 = userRealmMapper.queryReporterIdByGroupId(groupId);
            reporterIdSet.addAll(reporterIds2);
        }
 
        List<MyCharts> result = new ArrayList<>();
        myCharts.setStatmentId(NAMESPACE + ".selectOne");
 
        Object[] reporterIds = reporterIdSet.toArray();
        int pageSize = myCharts.getPageSize();
        int page = myCharts.getPage();
        int totalPage = (reporterIdSet.size() % pageSize) == 0 ? reporterIdSet.size() / pageSize : reporterIdSet.size() / pageSize + 1;
        if(!(page < 0) && !(page > totalPage)){
            int endSize = page * pageSize > reporterIds.length ? reporterIds.length : page * pageSize;
            for(int i=(page - 1) * pageSize; i< endSize;i++){
                myCharts.setId(reporterIds[i].toString());
                result.add(baseMapper.selectOne(myCharts));
            }
        }
 
//        for(String reporterId : reporterIdSet){
//            myCharts.setId(reporterId);
//            result.add(baseMapper.selectOne(myCharts));
//        }
        return result;
    }
}