田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.vci.server.framework.right.roleRight;
 
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
 
import com.vci.common.objects.UserEntity;
import com.vci.corba.common.VCIError;
import com.vci.corba.common.data.VCIInvocationInfo;
import com.vci.server.base.persistence.dao.BaseService;
import com.vci.server.base.persistence.dao.HibernateCallback;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.base.persistence.dao.HibernateTemplate;
 
/**
 * 角色功能模块权限配置
 * 
 * @author xf 2012-5-20
 */
public class RoleRightService extends BaseService {
 
    public RoleRightService() {
 
    }
 
    public RoleRightService(UserEntity userEntity) {
        super(userEntity);
    }
 
    /**
     * 清除权限
     * 
     * @author weidy@2018-10-12
     * @param roleId    角色的主键
     * @param rightType 权限类型
     * @return
     */
    public boolean clearRoleRight(final String roleId, final int rightType) {
        return (Boolean) new HibernateTemplate().run(new HibernateCallback() {
 
            @Override
            public Object execute() throws HibernateException, SQLException, VCIError {
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                if (userEntity == null) {
                    userEntity = new UserEntity();
                    VCIInvocationInfo vcii = HibernateSessionFactory.getVciSessionInfo();
                    userEntity.setIp(vcii.clientIPInfo);
                    userEntity.setModule("功能模块授权");
                    userEntity.setUserName(vcii.userName);
                }
                String hql = "delete RoleRight where roleId = ? and rightType=?";
 
                impl.createQuery(hql, new Object[] { roleId.trim(), rightType });
                return true;
            }
        });
    }
 
    /**
     * 保存角色功能模块权限
     * 
     * @return
     */
    public boolean saveRoleRight(final RoleRight[] roleRights, final String roleId, final int rightType) {
        return (Boolean) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException, SQLException {
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                /** 先删除后保存 **/
                // 注释根据rightType删除角色权限,避免管理员角色和普通用户角色相互影响
                String hql = "delete RoleRight where rightType = ? and roleId = ? ";
                Object[] values = new Object[2];
                values[0] = rightType;
                values[1] = roleId;
                impl.createQuery(hql, values);
 
//                long s = System.currentTimeMillis();
                batchSaveRoleRight(roleRights, roleId, rightType);
//                long t = System.currentTimeMillis();
//                System.out.println(roleRights.length + " " + (t - s));
                return true;
            }
        });
    }
 
    public boolean removeRoleRight(final RoleRight[] roleRights, final String roleId) {
        return (Boolean) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException, SQLException {
                String hql = "delete RoleRight where funcId = ? and roleId = ?";
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                for (int i = 0; i < roleRights.length; i++) {
                    // 根据角色和功能funcId删除授权
                    RoleRight roleRight = roleRights[i];
                    impl.createQuery(hql, new Object[] { roleRight.getFuncId(), roleRight.getRoleId() });
                }
                return true;
            }
        });
    }
 
    private void batchSaveRoleRight(RoleRight[] roleRights, String roleId, int rightType)
            throws HibernateException, SQLException {
        String sql = "insert into PLROLERIGHT (PLROLEOID, PLFUNCOID, PLRIGHTTYPE, PLRIGHTVALUE, PLCREATEUSER, "
                + "PLCREATETIME, PLUPDATEUSER, PLUPDATETIME, PLLICENSORS, PLOID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement pst = null;
        try {
            pst = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);// HibernateSessionFactory.getSession().connection().prepareStatement(sql);
            int batchSize = 200;
//            if (StringUtils.isBlank(roleId)) {
//                if (roleRights != null && roleRights.length > 0) {
//                    roleId = roleRights[0].getRoleId();
//                }
//            }
//            RoleRightDaoImpl impl = new RoleRightDaoImpl();
//            String hql = "delete RoleRight where roleId = ? and rightType=?";
//
//            if (StringUtils.isNotBlank(roleId) && StringUtils.isNotBlank(hql)) {// 有传递角色才删除,因为有时候追加的情况
//                impl.createQuery(hql, new Object[] { roleId, rightType });
//            }
 
            Set<String> roleOidHasFunctionOid = new HashSet<String>();
            for (int i = 0; i < roleRights.length; i++) {
                // 根据角色和功能funcId删除授权
                RoleRight roleRight = roleRights[i];
 
                // 增加权限
                String roleOidFuncOid = roleRight.getFuncId();
                if (roleOidHasFunctionOid.contains(roleOidFuncOid)) {
                    continue;
                } else {
                    roleOidHasFunctionOid.add(roleOidFuncOid);
                    prepareCreateBOPsmt(pst, roleRight);
                    pst.addBatch();
                    // 是否达到批量执行阀值
                    if ((i + 1) % batchSize == 0) {
                        // 达到则执行
                        pst.executeBatch();
                    }
                }
 
            }
            pst.executeBatch();
        } finally {
            if (pst != null) {
                pst.close();
            }
        }
    }
 
//    private boolean judgeHasExitRight(RoleRight roleRight) throws VCIError {
//        boolean flag = false;
//        String oid = roleRight.getId();
//        String funcId = roleRight.getFuncId();
//        
//        String judgeSql = "select count(ploid) from PLROLERIGHT r " +
//                "where r.plroleoid = '" + oid + "' " +
//                "and r.plfuncoid = '" + funcId + "'";
//        
//        String[][] result = new QTServiceImpl().queryBySqlWithoutKey(judgeSql);
//        if (result != null && result.length > 0) {
//            int sum = Integer.parseInt(result[0][0]);
//            if (sum == 0) {
//                
//            } else {
//                
//            }
//        }
//    }
 
    private void prepareCreateBOPsmt(PreparedStatement pst, RoleRight roleRight) throws SQLException {
        pst.setString(1, roleRight.getRoleId());
        pst.setString(2, roleRight.getFuncId());
        pst.setLong(3, roleRight.getRightType());
        pst.setLong(4, roleRight.getRightValue());
        pst.setString(5, roleRight.getCreateUser());
        pst.setTimestamp(6, new Timestamp(roleRight.getCreateTime().getTime()));
        pst.setString(7, roleRight.getModifyUser());
        pst.setTimestamp(8, new Timestamp(roleRight.getModifyTime().getTime()));
        pst.setString(9, roleRight.getLicensor());
        pst.setString(10, roleRight.getId());
    }
 
    /**
     * 增加授权专用:不改变原有授权,只是添加新增授权;如果已存在,不作处理,没有存的入库添加
     * 
     * @param roleRights
     * @param roleId
     * @param rightType
     * @return
     */
    public boolean reAddRoleRight(final RoleRight[] roleRights, final String roleId, final int rightType) {
        return (Boolean) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException, SQLException {
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                /** 获取所选角色已有的模块授权 **/
                String hql = "from RoleRight where rightType = ? and roleId = ? ";
                Object[] values = new Object[2];
                values[0] = rightType;
                values[1] = roleId;
                List<RoleRight> myRoleRights = impl.findEntites(hql, values);
                List<String> funcIds = new ArrayList<String>();
                if (myRoleRights != null) {
                    for (RoleRight roleRight : myRoleRights) {
                        funcIds.add(roleRight.getFuncId());
                    }
                }
                List<RoleRight> roleRightList = new ArrayList<RoleRight>();
                for (RoleRight right : roleRights) {
                    if (!funcIds.contains(right.getFuncId())) {
                        roleRightList.add(right);
                    }
                }
                batchSaveRoleRight(roleRightList.toArray(new RoleRight[roleRightList.size()]), null, rightType);
                return true;
            }
        });
    }
 
    public boolean reAddRoleRightWithNoCheck(final RoleRight[] roleRights, final int rightType) {
        return (Boolean) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException, SQLException {
                batchSaveRoleRight(roleRights, null, rightType);
                return true;
            }
        });
    }
 
    /**
     * 获取角色功能模块权限树
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<RoleRight> getRoleRightList(final String roleId, final int rightType) {
        return (List<RoleRight>) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                String hql = "from RoleRight where rightType = ? and roleId = ? ";
                if (rightType == 0) {
                    hql = "from RoleRight where roleId = ? ";
                    Object[] values = new Object[1];
                    values[0] = roleId;
                    return impl.findEntites(hql, values);
                } else {
                    Object[] values = new Object[2];
                    values[0] = rightType;
                    values[1] = roleId;
                    return impl.findEntites(hql, values);
                }
            }
        });
    }
 
    /**
     * 
     * <p>
     * Description:根据授权类型获取权限
     * </p>
     * 
     * @author sunbo
     * @time 2013-2-21
     * @param rightType
     * @return
     */
    public List getRoleRightListByType(final String[] rightType) {
        return (List) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                String hql = "select * from PLROLERIGHT r where r.PLRIGHTTYPE in ('" + rightType[0] + "' , '"
                        + rightType[1] + "')";
                // Object[] values = new Object[rightType.length];
                // for(int i=0;i<rightType.length;i++){
                // values[i] = rightType[i];
                // }
                return impl.findEntites(hql, new String[] {}, "r", RoleRight.class);
            }
        });
    }
 
    /**
     * 获取用户对当前模块的权限
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<RoleRight> getRoleRightByModule(final String funcId, final String userName) {
 
        return (List<RoleRight>) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                StringBuffer sql = new StringBuffer();
                sql.append("select {t.*} from plroleright t where t.plroleoid in (");
                sql.append("  select u.plroleuid from pluserrole u where u.pluseruid in (");
                sql.append("    select pluid from pluser where plusername = '").append(userName).append("'");
                sql.append("  )");
                sql.append(") and t.plfuncoid = '").append(funcId).append("'");
                return impl.findEntites(sql.toString(), new Object[0], "t", RoleRight.class);
            }
        });
    }
 
    /**
     * 获取当前用户的所有权限
     * 
     * @param userName
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<RoleRight> getRoleRightByUserName(final String userName) {
        return (List<RoleRight>) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                StringBuffer sql = new StringBuffer();
                sql.append("  select {r.*} from plroleright r where r.plroleoid in (");
                sql.append("    select u.plroleuid from pluserrole u where u.pluseruid in (");
                sql.append("      select pluid from pluser where plusername = '").append(userName).append("'");
                sql.append("    )");
                sql.append("  ) ");
                return impl.findEntites(sql.toString(), new Object[0], "r", RoleRight.class);
            }
        });
    }
 
    @SuppressWarnings("unchecked")
    public List<RoleRight> getFunctionRoleRightByUserName(final String userName) {
        return (List<RoleRight>) new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                RoleRightDaoImpl impl = new RoleRightDaoImpl();
                StringBuffer sql = new StringBuffer();
                sql.append(
                        "  select {r.*} from plroleright r where r.plfuncoid in (select ploid from plfunction) and r.plroleoid in (");
                sql.append("    select u.plroleuid from pluserrole u where u.pluseruid in (");
                sql.append("      select pluid from pluser where plusername = '").append(userName).append("'");
                sql.append("    )");
                sql.append("  ) ");
                return impl.findEntites(sql.toString(), new Object[0], "r", RoleRight.class);
            }
        });
    }
}