wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package com.vci.server.framework.systemConfig.stafforgmanage.dept;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
 
import com.vci.common.objects.UserEntity;
import com.vci.server.base.persistence.dao.BaseService;
import com.vci.server.base.persistence.dao.HibernateCallback;
import com.vci.server.base.persistence.dao.HibernateCallbackExt;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.base.persistence.dao.HibernateTemplate;
import com.vci.server.base.persistence.dao.JDBCCallback;
import com.vci.server.base.persistence.dao.JDBCRunType;
import com.vci.server.base.persistence.dao.JDBCTemplate;
 
public class DepartmentService extends BaseService {
    public DepartmentService() {}
    public DepartmentService(UserEntity userEntity) {
        super(userEntity);
    }
 
    public void saveDepartment(final Department dept) {
        new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                dept.setUserEntity(userEntity);
                impl.save(dept);
                return dept;
            }
        });
    }
    
    public void batchSaveDepart(Department[] depts) throws HibernateException, SQLException {
        String sql = "INSERT INTO PLDEPT VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        PreparedStatement pst = null;
        int index = 1;
        int batchSize = 200;
        for (int i = 0; i < depts.length; i++) {
            index = 1;
            if(pst == null){
                pst = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
            }
            pst.setString(index++, depts[i].getId());
            pst.setString(index++, depts[i].getName());
            pst.setString(index++, depts[i].getNum());
            pst.setString(index++, depts[i].getCode());
            pst.setInt(index++, 0);
            pst.setString(index++, depts[i].getParentId());
            pst.setString(index++, depts[i].getDesc());
            pst.setTimestamp(index++, currentTime);
            pst.setString(index++, userEntity.getUserName());
            pst.setTimestamp(index++, currentTime);
            pst.setString(index++, userEntity.getUserName());
            pst.setString(index++, "");
            pst.addBatch();
            
            // 是否达到批量执行阀值
            if((i+1) % batchSize == 0){
                // 达到则执行
                pst.executeBatch();
            }
        }
        
        pst.executeBatch();
 
        closePreparedStatement(pst);
    }
    
    private void closePreparedStatement(PreparedStatement pst) throws SQLException{
        if(pst != null){
            pst.clearBatch();
            pst.clearParameters();
            pst.close();
            pst = null;
        }
    }
    
    @SuppressWarnings("rawtypes")
    public List deleteDepartment(final String id) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String sql = " select * from  PLDEPT d start with d.pluid = ? connect by prior d.pluid = d.plparentuid " ;
                Object[] values = new Object[1];
                values[0] = id;
                List list = impl.findEntites(sql, values, "PLDEPT", Department.class);
                for(int i=0;i<list.size();i++) {
                    Department object = (Department)list.get(i);
                    String id = object.getId();
                    Department entity = impl.loadById(id);
                    entity.setUserEntity(userEntity);
                    impl.delete(entity);
                }
                return list;
            }
        });
    }
    
    public boolean deleteDepartmentByHQL(final String id) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                String hql = "delete Department dept where dept.id=:id";
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                impl.deleteQueryObject(hql, "id", id, userEntity);
                return true;
            }
        });
    }
    
    /**
     * 更新部门同步状态
     * @param ids
     * @return
     */
    public boolean updateByDeptCode(final String[] ids) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                String hql1 = "update Department dept set dept.status = 2 where dept.num in(";
                int len1 = ids.length;
                for (int i = 0; i < len1; i++) {
                    hql1 += "?";
                    if (i != len1 - 1) {
                        hql1 += ",";
                    }
                }
                hql1 += ")";
                
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                impl.deleteQueryObject(hql1, ids, userEntity);
                return true;
            }
        });
    }
 
    public boolean updateDeptParentId(final String id, final String parentId) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                String hql1 = "update Department dept set dept.parentId = :parentId where dept.id = :id";
                String[] props = new String[2];
                props[0] = "parentId";
                props[1] = "id";
                
                String[] values = new String[2];
                values[0] = parentId;
                values[1] = id;
                
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                impl.deleteQueryObject(hql1, props, values, userEntity);
                return true;
            }
        });
    }
    
    public boolean deleteDepartmentByHQL(final String[] ids) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                String hql1 = "delete Department dept where dept.parentId in(";
                int len1 = ids.length;
                for (int i = 0; i < len1; i++) {
                    hql1 += "?";
                    if (i != len1 - 1) {
                        hql1 += ",";
                    }
                }
                hql1 += ")";
                
                String hql = "delete Department dept where dept.id in(";
                int len = ids.length;
                for (int i = 0; i < len; i++) {
                    hql += "?";
                    if (i != len - 1) {
                        hql += ",";
                    }
                }
                hql += ")";
                
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                impl.deleteQueryObject(hql1, ids, userEntity);
                impl.deleteQueryObject(hql, ids, userEntity);
                return true;
            }
        });
    }
    
    public boolean updateDepartment(final Department dept) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                Department curDept = impl.getById(dept.getId());
                if (curDept == null) {
                    dept.setUserEntity(userEntity);
                    impl.saveOrUpdate(dept);
                } else {
                    curDept.setName(dept.getName());
                    curDept.setNum(dept.getNum());
                    curDept.setCode(dept.getCode());
                    curDept.setStatus(curDept.getStatus());
                    curDept.setDesc(dept.getDesc());
                    curDept.setSpecialties(dept.getSpecialties());
                    curDept.setUpdateTime(dept.getUpdateTime());
                    curDept.setUpdateUser(dept.getUpdateUser());
                    curDept.setGrantor(dept.getGrantor());
                    curDept.setUserEntity(userEntity);
                    impl.saveOrUpdate(curDept);
                }
                
                return true;
            }
        });
    }
    
    public Department selectDepartmentById(final String id) {
        return (Department)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                return impl.loadById(id);
            }
        });
    }
    public Department fetchManageDept(final String rmTypeId) {
        return (Department)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String hsql = " select * from pldept d where d.PLUID in  " +
                        " (select ud.PlDEPTID from plmanagedept ud where ud.PLCLASSIFYID = '"+rmTypeId+"')";
                Object[] values = new Object[0];
                List<Department> list = impl.findEntites(hsql, values, "d", Department.class);
                if (list.size()> 0){
                    return list.get(0);
                }else{
                    return null;
                }
                
            }
        });
    }
    public Department fetchManageOfMaterialsDept(final String rmTypeId) {
        return (Department)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String hsql = " select * from pldept d where d.PLUID in  " +
                        " (select ud.PLmaterialsDeptId from plmanagedept ud where ud.PLCLASSIFYID = '"+rmTypeId+"')";
                Object[] values = new Object[0];
                List<Department> list = impl.findEntites(hsql, values, "d", Department.class);
                if (list.size()> 0){
                    return list.get(0);
                }else{
                    return null;
                }
                
            }
        });
    }
    public Department fetchDeptByUserId(final String userId) {
        return (Department)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String hsql = " select * from pldept d where d.PLUID in  " +
                        " (select ud.pldeptuid from pluserdept ud where ud.pluseruid = '"+userId+"')";
                Object[] values = new Object[0];
                List<Department> list = impl.findEntites(hsql, values, "d", Department.class);
                if (list.size()> 0){
                    return list.get(0);
                }else{
                    return null;
                }
                
            }
        });
    }
    
    public Department selectDepartmentByName(final String name) {
        return (Department)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String hsql = "from Department dept where dept.name = :name";
                return impl.findEntity(hsql, "name", name);
            }
        });
    }
    
    public Department fetchDeptByParentIdAndName(final String parentId ,final String name) {
        return (Department)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String hsql = "from Department dept where dept.name = '"+name+"' ";
                if (!"".equals(parentId)){
                    hsql += "and dept.parentId = '"+parentId+"'";
                }else{
                    hsql += "and dept.parentId is null";
                }
                    
                        
                return impl.findEntity(hsql);
            }
        });
    }
    
    @SuppressWarnings("rawtypes")
    public List getDepartmentList() {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                return impl.loadAll();
            }
        });
    }
    
    @SuppressWarnings("rawtypes")
    public List getDepartmentListByFilter(final boolean root, final String prtoid) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                List res = null;
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String hsql = "from Department dept where ";
                if(root){
                    hsql += " dept.parentId is null";
                    res = impl.findEntities(hsql);
                }else{
                    if(prtoid != null && !prtoid.equals("")){
                        hsql += " dept.parentId = ?";
                        Object[] values = { prtoid };
                        res = impl.findEntites(hsql, values);
                    }else if ("".equals(prtoid)){
                        hsql += " dept.parentId is null";
                        res = impl.findEntities(hsql);
                    }
                }
                return res;
            }
        });
    }
    
    @SuppressWarnings("rawtypes")
    public List getDepartmentPageList(final int pageNo, final int pageSize) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                return impl.findList(pageNo, pageSize);
            }
        });
    }
    @SuppressWarnings("rawtypes")
    public List getDepartmentListById(final String id) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                if("root".equals(id)) {
                    return impl.loadAll();
                }
                String sql = " select * from  PLDEPT d start with d.pluid = ? connect by prior d.pluid = d.plparentuid " ;
                
                Object[] values = new Object[1];
                values[0] = id;
                List list = impl.findEntites(sql, values, "PLDEPT", Department.class);
                return list;
                
            }
        });
    }
    @SuppressWarnings("rawtypes")
    public List getDepartmentListBySonId(final String id) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                if("root".equals(id)) {
                    return impl.loadAll();
                }
                String sql = " select * from  PLDEPT d start with d.pluid = ? connect by prior  d.plparentuid=d.pluid " ;
                Object[] values = new Object[1];
                values[0] = id;
                List list = impl.findEntites(sql, values, "PLDEPT", Department.class);
                return list;
                
            }
        });
    }
    @SuppressWarnings("rawtypes")
    public List getDepartmentListByType(final String type) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String sql = " select * from  PLDEPT d start with d.PLUSERTYPE = ? connect by prior d.PLOID = d.PLPARENTOID " ;
                int a = Integer.parseInt(type);
                String type1 = "";
                if (a<2){
                    a = a + 1;
                }
                type1 = "" + a + "";
                Object[] values = { type1 };
                List list = impl.findEntites(sql, values, "PLDEPT", Department.class);
                return list;
                
            }
        });
    }
    @SuppressWarnings("rawtypes")
    public List getDepartmentInfoRootByType(final boolean root, final String type) {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                List res = null;
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                String hsql = "from Department dept where 1=1 ";
                if(root){
                    hsql += " and dept.parentId is null and dept.userType = ? ";
                    int a = Integer.parseInt(type);
                    String type1 = "";
                    if (a<2){
                        a = a + 1;
                    }
                    type1 = "" + a + "";
                    Object[] values = { type1 };
                    res = impl.findEntites(hsql, values);
                }
                return res;
            }
        });
    }
    
    public boolean saveRight(final String deptId , final String[] userIds ){
        return (Boolean)new HibernateTemplate().runExt(new HibernateCallbackExt() {
                public Object execute(Connection conn) throws HibernateException,
                            SQLException {
                String  sql = "delete pluserdept ud where ud.pldeptuid = ? ";
                PreparedStatement pstmt = null;
                PreparedStatement pstmt1 = null;
                try {
                    pstmt = conn.prepareStatement(sql);
                    pstmt.setString(1, deptId);
                    pstmt.execute();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                } finally {
                    if (pstmt != null){
                        pstmt.close();
                    }
                }
                try {
                    for (int i = 0 ; i < userIds.length ; i ++){
                        String hql = "delete pluserdept ud where ud.pluseruid = ?";
                        pstmt1 = conn.prepareStatement(hql);
                        pstmt1.setString(1, userIds[i]);
                        pstmt1.execute();
                    }
                }catch (SQLException e1) {
                    e1.printStackTrace();
                } finally {
                    if (pstmt1 != null){
                        pstmt1.close();
                    }
                }
                try {
                    for (int i = 0 ; i < userIds.length ; i ++){
                        String hql = "insert into pluserdept values (? , ?)";
                        pstmt1 = conn.prepareStatement(hql);
                        pstmt1.setString(1, userIds[i]);
                        pstmt1.setString(2, deptId);
                        pstmt1.execute();
                    }
                }catch (SQLException e1) {
                    e1.printStackTrace();
                } finally {
                    if (pstmt1 != null){
                        pstmt1.close();
                    }
                }
                return true;
            }
      });
    }
    
    
    @SuppressWarnings("unchecked")
    public List<Department> fetchDeptByUserNames(final String[] userNames) {
        return (List<Department>)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                
                String names = "";
                int k = 0;
                for(String name : userNames) {
                    names += "'" + name + "'";
                    if(k++ < userNames.length - 1) {
                        names += ",";
                    }
                    
                }
                
                String sql = " select p.* from pldept p start with p.pluid in( " +
                             "   select up.pldeptuid from pluserdept up where up.pluseruid in( " +
                             "      select u.pluid from pluser u where u.plusername in("+ names +") "+
                             "   )" +
                             " )" +
                             " connect by prior p.plparentuid = p.pluid " ;
                
                Object[] values = new Object[0];
                List<Department> list = impl.findEntites(sql, values, "Department", Department.class);
                return list;
            }
        });
    }
    
    @SuppressWarnings("unchecked")
    public List<Department> fetchDeptByNum(final String num) {
        return (List<Department>)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                
                String sql = "select p.* from pldept p where p.plnum =  ?";
                
                Object[] values = new Object[1];
                values[0]= num;
                List<Department> list = impl.findEntites(sql, values, "Department", Department.class);
                return list;
            }
        });
    }
    
    @SuppressWarnings("unchecked")
    public List<Department> fetchDepartmentInfoByIds(final String otherFiterString){
        return (List<Department>)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                StringBuffer sql=new StringBuffer();
                sql.append(" select p.* from pldept p where 1=1  ");
                sql.append(otherFiterString);
                Object[] values = new Object[0];
                List<Department> list = impl.findEntites(sql.toString(), values, "Department", Department.class);
                return list;
            }
        });
    }
    
    public List fetchChildrenDeptByParentOid(final String prtoid,
            final boolean iscontains, final String otherFiterString){
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                DepartmentDAOImpl impl = new DepartmentDAOImpl();
                StringBuffer sb=new StringBuffer();
                sb.append(" select * from PLDEPT ");
                Object[] values = new Object[]{};
                if(iscontains){
                    sb.append(" start with " + (StringUtils.isBlank(prtoid)?" (plparentuid is null or plparentuid = '') ":("pluid = '"+prtoid+"'")));
                    sb.append(" connect by prior pluid= plparentuid ");
                }else{
                    sb.append(" and ( plparentuid='"+prtoid+"' or pluid='"+prtoid+"' )");
                }
                if(!"".equals(otherFiterString)){
                    sb.append( otherFiterString);
                }
                List list = impl.findEntites(sb.toString(), values, "PLDEPT", Department.class);
                return list;
            }
        });
    
    }
    
    @SuppressWarnings("unchecked")
    public List<Department>  gridDeptDataGrids(final String filter, int pageNo, int pageSize){
        final int V_PAGE_ROWNUM_START = (pageNo - 1) * pageSize;
        final int V_PAGE_ROWNUM_END = (pageNo ) * pageSize;
        return (List<Department>)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
            DepartmentDAOImpl impl = new DepartmentDAOImpl();
            StringBuffer sb=new StringBuffer();
            sb.append(" select * from   ");
            sb.append(" ( ");
            sb.append("  select pldept.*,rownum RN from pldept  ");
            sb.append(" where 1=1 ");
            sb.append( filter ); 
            sb.append("  ) ");
            sb.append(" WHERE RN >");
            sb.append(V_PAGE_ROWNUM_START);
            sb.append(" AND RN <= ");
            sb.append(V_PAGE_ROWNUM_END);
            Object[] values = new Object[0];
            List<Department> list = impl.findEntites(sb.toString(), values, "u", Department.class);
            return list;}
        });
    }
    
    public int gridDeptDataGridsCount(final String filter){
        StringBuffer sb=new StringBuffer();
        sb.append("  select count(*) from pldept  ");
        sb.append(" where 1=1 ");
        sb.append( filter ); 
         int res=0;
          res = (Integer)new JDBCTemplate().run(new JDBCCallback(
                new HibernateTemplate().getSessionConnection(), 
                sb.toString(), JDBCRunType.SQL, 0, false, false,new Object[]{}) {
            @Override
            public Object execute(ResultSet rst) throws SQLException {
                int ress = 0;
                while(rst.next()){
                    ress = rst.getInt(1);
                }
                return ress;
            }
        });
          return res;
    }
}