dangsn
2024-06-11 dba1e53cd7652f1b973ffec118e5b3312278c814
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
package com.vci.server.framework.systemConfig.security;
 
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.hibernate.HibernateException;
import org.hibernate.criterion.Order;
 
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;
import com.vci.server.framework.systemConfig.stafforgmanage.user.User;
 
public class MachSecurityService  extends BaseService{
    public MachSecurityService() {
    }
 
    public MachSecurityService(UserEntity userEnt) {
        super(userEnt);
    }
 
    
    public void saveMachSecurity(final MachSecurity security) {
        new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                security.setUserEntity(userEntity);
                impl.save(security);
                return security;
            }
        });
    }
    
    public void saveMachSecurities(final MachSecurity[] securities) throws SQLException {
        String sql = "INSERT INTO PLMACHSECURITY (PLUID, PLNAME, MACADDRESS, IPADDRESS, PLSECRETGRADE, PLCREATETIME, PLCREATOR, PLMODIFYTIME, PLMODIFIER, PLDESC)" + 
                        " VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        PreparedStatement pst = null;
        int index = 1;
        int batchSize = 200;
        for (int i = 0; i < securities.length; i++) {
            index = 1;
            if(pst == null){
                pst = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
            }
            pst.setString(index++, securities[i].getId());
            pst.setString(index++, securities[i].getName());
            pst.setString(index++, securities[i].getMacAddress());
            pst.setString(index++, securities[i].getIpAddress());
            pst.setInt(index++, securities[i].getSecretGrade());
            pst.setTimestamp(index++, currentTime);
            pst.setString(index++, userEntity.getUserName());
            pst.setTimestamp(index++, currentTime);
            pst.setString(index++, userEntity.getUserName());
            pst.setTimestamp(index++, currentTime);
            pst.setString(index++, securities[i].getDescription());
 
            pst.addBatch();
            
            // 是否达到批量执行阀值
            if((i+1) % batchSize == 0){
                // 达到则执行
                pst.executeBatch();
            }
        }
        
        pst.executeBatch();
 
        closePreparedStatement(pst);
    }
    
 
    public void updateMachSecurities(MachSecurity[] securities) throws SQLException {
        String sql = "UPDATE PLMACHSECURITY SET PLNAME=?, MACADDRESS=?, IPADDRESS=?, PLSECRETGRADE=?, PLMODIFYTIME=?, PLMODIFIER=?, PLDESC=? WHERE PLUID=?";
        
        Timestamp curTime = new Timestamp(System.currentTimeMillis());
        PreparedStatement pst = null;
        int index = 1;
        int batchSize = 200;
        for (int i = 0; i < securities.length; i++) {
            index = 1;
            if(pst == null){
                pst = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);
            }
            pst.setString(index++, securities[i].getName());
            pst.setString(index++, securities[i].getMacAddress());
            pst.setString(index++, securities[i].getIpAddress());
            pst.setInt(index++, securities[i].getSecretGrade());
            pst.setString(index++, userEntity.getUserName());
            pst.setTimestamp(index++, curTime);
            pst.setString(index++, securities[i].getDescription());
            pst.setString(index++, securities[i].getId());
        
            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;
        }
    }
    
    
    public boolean deleteMachSecurity(final String id) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                MachSecurity security = impl.loadById(id);
                security.setUserEntity(userEntity);
                impl.delete(security);
                return true;
            }
        });
    }
    
    public boolean deleteMachSecurityByMQL(final String id) {
        return deleteMachSecurityByMQL(new String[]{id});
    }
    
    public boolean deleteMachSecurityByMQL(final String[] ids) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                int len = ids.length;
                String inStr = "";
                for (int i = 0; i < len; i++) {
                    inStr += "?";
                    if (i != len - 1) {
                        inStr += ",";
                    }
                }
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                
                String hql = "delete MachSecurity ms where ms.id in (" + inStr + ")";
                impl.deleteQueryObject(hql, ids, userEntity);
                return true;
            }
        });
    }
    
    public boolean updateMachSecurity(final MachSecurity security) {
        return (Boolean)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                MachSecurity secTmp = impl.getById(security.getId());
                if (secTmp == null) {
                    security.setUserEntity(userEntity);
                    impl.saveOrUpdate(security);
                } else {
                    secTmp.setName(security.getName());
                    secTmp.setIpAddress(security.getIpAddress());
                    secTmp.setMacAddress(security.getMacAddress());
                    secTmp.setSecretGrade(security.getSecretGrade());
                    secTmp.setDescription(security.getDescription());
                    secTmp.setModifyTime(security.getModifyTime() );
                    secTmp.setModifier(security.getModifier());
                    secTmp.setUserEntity(userEntity);
                    impl.saveOrUpdate(secTmp);
                }
                return true;
            }
        });
    }
 
    public MachSecurity selectMachSecurity(final String id) {
        return (MachSecurity)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                String hsql = "from MachSecurity ms where ms.id = :id";
                return impl.findEntity(hsql, "id", id);
            }
        });
    }
    
    public MachSecurity selectMachSecurityByIP(final String ip) {
        return (MachSecurity)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                String hsql = "from MachSecurity ms where ms.ipAddress = :ip";
                return impl.findEntity(hsql, "ip", ip);
            }
        });
    }
    
    public List getMachSecurityList() {
        return (List)new HibernateTemplate().run(new HibernateCallback() {
            public Object execute() throws HibernateException {
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                return impl.loadAll();
            }
        });
    }
 
    public int getMachSecurityTolal() {
        int res = 0;
        String sql = "SELECT COUNT(*) FROM PLMACHSECURITY S";
//        String sql = "SELECT COUNT(*) FROM PLROLE R WHERE R.PLTYPE in ('1','2')";
        res = (Integer)new JDBCTemplate().run(new JDBCCallback(
                new HibernateTemplate().getSessionConnection(), 
                sql, 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;
    }
    
 
    public int getMachSecurityTolal(final String name, final String ipAddress, final int security) {
        int res = 0;
        
        StringBuffer sbSQL = new StringBuffer();
 
        sbSQL.append(" select COUNT(*) from PLMACHSECURITY m ");
        
        if (security > 0) {
            sbSQL.append(" where m.PLSECRETGRADE= ").append(security);
        } else {
            sbSQL.append(" where 1=1 ");
        }
 
        if (!"".equals(name)) {
            sbSQL.append(" and m.PLNAME like '%" + name + "%' ");
        }
        
        if (!"".equals(ipAddress)) {
            sbSQL.append(" and m.IPADDRESS  like '%" + ipAddress + "%' ");
        }
        
        res = (Integer)new JDBCTemplate().run(new JDBCCallback(
                new HibernateTemplate().getSessionConnection(), 
                sbSQL.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;
    }
 
    
//    public int getMachSecurityTolal() {
//        return (Integer)new HibernateTemplate().run(new HibernateCallback() {
//            
//            public Object execute() throws HibernateException {
//                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
//                return impl.getCount();
//            }
//        });
//    }
 
    public List fetchMachSecurityByPage(final int pageNo, final int pageSize) {
        return (List)new HibernateTemplate().runExt(new HibernateCallbackExt() {
            
            public Object execute(Connection connection) throws HibernateException,
                    SQLException {
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                Order order = Order.asc("ipAddress");//按IP排序
                return impl.findList(pageNo, pageSize, order);
            }
        });
    }
 
    public List fetchMachSecurityByConditionPage(final String name, final String ipAddress, final int security, final int pageNo,
            final int pageSize) {
        return (List)new HibernateTemplate().runExt(new HibernateCallbackExt() {
            
            public Object execute(Connection connection) throws HibernateException,
                    SQLException {
                MachSecurityDAOImpl impl = new MachSecurityDAOImpl();
                
                int V_PAGE_ROWNUM_START = (pageNo - 1) * pageSize;
                int V_PAGE_ROWNUM_END = (pageNo) * pageSize;
 
                StringBuffer sb = new StringBuffer();
                sb.append(" SELECT * ");
                sb.append("   FROM (SELECT ROW_.*, ROWNUM RN ");
                sb.append("      FROM (");
 
                if (security > 0) {
                    sb.append(" select m.* from PLMACHSECURITY m where m.PLSECRETGRADE= ").append(security);
                } else {
                    sb.append(" select m.* from PLMACHSECURITY m where 1=1 ");
                }
 
                if (!"".equals(name)) {
                    sb.append(" and m.PLNAME like '%" + name + "%' ");
                }
                if (!"".equals(ipAddress)) {
                    sb.append(" and m.IPADDRESS  like '%" + ipAddress + "%' ");
                }
                
                sb.append(" order by m.IPADDRESS ");
 
                sb.append("  )ROW_)");
                sb.append("   WHERE RN <= ");
                sb.append(V_PAGE_ROWNUM_END);
                sb.append(" AND RN > ");
                sb.append(V_PAGE_ROWNUM_START);
 
                Object[] values = new Object[0];
 
                List<MachSecurity> list = impl.findEntites(sb.toString(), values, "u", MachSecurity.class);
                return list;
            }
        });
    }
}