ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package com.vci.server.bof.service;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;
 
import org.hibernate.Session;
 
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.omd.data.AttributeValue;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.corba.omd.data.RevisionDataInfo;
import com.vci.omd.constants.BusinessConstants;
import com.vci.omd.utils.ObjectTool;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.base.utility.OmdHelper;
import com.vci.server.bof.utils.ServerObjectUtil;
import com.vci.server.bof.utils.TempTableUtil;
import com.vci.common.utility.ObjectUtility;
 
public class BOFactoryBatchServices extends BOFactoryBaseService{
 
    private static BOFactoryBatchServices instance = null;
    
    public static synchronized BOFactoryBatchServices getInstance() {
        if (instance == null) {
            instance = new BOFactoryBatchServices();
        }
 
        return instance;
    }
    
    /**
     * 批量对业务对象进行升版本
     * @param ids
     * @param type
     * @param currentUser
     * @return
     * @throws Exception
     * @throws Throwable
     */
    public BusinessObject[] batchRevisionBusinessObject(String[] ids, String btmName, String currentUser) throws Exception, Throwable {
        BusinessObject[] bos = getBatchBusinessObject(ids, btmName);
        AttribItem[] attrItems = getBOAttributeItem(btmName);
        
        String tableName = OmdHelper.getBTTableName(btmName);
        
        for (int i = 0; i < bos.length; i ++) {
            RevisionDataInfo info = BOFactoryServices.getInstance().getNextRevisionValue(tableName, bos[i].nameoid, 
                    ObjectTool.getBOAttributeValue(bos[i], BusinessConstants.SELECT_REVISIONRULE), false, "");
            bos[i].revisionSeq = info.revisionSeq;
            bos[i].revisionValue = info.revisionVal;
            bos[i].fromVersion = bos[i].oid;
            bos[i].oid = ObjectUtility.getNewObjectID36();
            bos[i].revisionid = ObjectUtility.getNewObjectID36();
            bos[i].modifier = currentUser;
            bos[i].creator = currentUser;
            bos[i].isFirstR = false;
            bos[i].isLastR = true;
            bos[i].isFirstV = true;
            bos[i].isLastV = true;
            bos[i].versionSeq = 1;
            bos[i].lcStatus = getLcFirstStatus(ObjectTool.getBOAttributeValue(bos[i], BusinessConstants.SELECT_LCTID));
            int len = bos[i].hisAttrValList.length;
            int alen = attrItems.length;
            bos[i].newAttrValList = new AttributeValue[alen];
            for (int j = 0; j < alen; j++) {
                bos[i].newAttrValList[j] = bos[i].hisAttrValList[j + len - alen];
            }
            bos[i] = BOFactoryServices.getInstance().createBusinessObject(bos[i], true, false);
        }
        
        return bos;
    }
    
    /**
     * 通过批量的oid和btmname获取对应的对象信息
     * @param oids
     * @param btmName
     * @return
     * @throws Exception
     * @throws Throwable
     */
    public BusinessObject[] getBatchBusinessObject(String[] oids, String btmName)
            throws Exception, Throwable {
        AttribItem[] attrItems = getBOAttributeItem(btmName);
 
        String tableName = OmdHelper.getBTTableName(btmName);
 
        //Session session = HibernateSessionFactory.getSession();
        Connection connection = HibernateSessionFactory.getSessionConnection();
        
        String sql = getBatchSelectBOSql(oids, tableName, attrItems);
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        
        List<BusinessObject> lstBO = new ArrayList<BusinessObject>();
        BusinessObject bo = new BusinessObject();
        while (rs.next()) {
            bo = ServerObjectUtil.createBOByRS(attrItems, rs);
            lstBO.add(bo);
        }
        
        return lstBO.toArray(new BusinessObject[0]);
        
//        SQLQuery query = session.createSQLQuery(sql);
//        query.executeUpdate();
//        List<?> queryList = query.list();
//        if (queryList == null || queryList.size() < 1) {
//            return new BusinessObject[0];
//        }
//        ServerBusinessObject sbo = new ServerBusinessObject();
//        String[] queryColumns = sbo.getQueryColumnAccordingToSQL(sql, "SELECT",
//                "FROM " + tableName);
//
//        BusinessObject[] bos = new BusinessObject[queryList.size()];
//        
//        for (int k = 0; k < queryList.size(); k++) {
//            ServerBusinessObject csbo = new ServerBusinessObject();
//            Object[] array = (Object[]) queryList.get(k);
//            String attrType = "";
//            int j = 0;
//            for (int i = 0; i < array.length; i++) {
//                if (i < array.length - attrItems.length) {
//                    attrType = "";
//                } else {
//                    attrType = attrItems[j++].vtDataType;
//                }
//                csbo.setAttributeValue(queryColumns[i], attrType, array[i]);
//            }
//
//            BusinessObject bo = csbo.getBusinessObject();
//            bos[k] = bo;
//        }
 
//        return bos;
    }
    
    public BusinessObject[] getBatchBusinessObjectByTempOIDs(String[] oids, String btmName)
            throws Exception, Throwable {
        AttribItem[] attrItems = getBOAttributeItem(btmName);
 
        String tableName = OmdHelper.getBTTableName(btmName);
 
        //Session session = HibernateSessionFactory.getSession();
        Connection connection = HibernateSessionFactory.getSessionConnection();
        TempTableUtil.insertTempOIDs(connection, oids);
 
        String sql = getBatchSelectBOSqlByTempOIDs(tableName, attrItems);
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        
        List<BusinessObject> lstBO = new ArrayList<BusinessObject>();
        BusinessObject bo = new BusinessObject();
        while (rs.next()) {
            bo = ServerObjectUtil.createBOByRS(attrItems, rs);
            lstBO.add(bo);
        }
        
        return lstBO.toArray(new BusinessObject[0]);
    }
 
    /**
     * 通过批量的oid和btmname获取对应的对象信息
     * @param oids
     * @param btmName
     * @return
     * @throws Exception
     * @throws Throwable
     */
    public BusinessObject[] getBatchBusinessObjects(String btmName, int page, int pageSize)
            throws Exception, Throwable {
        AttribItem[] attrItems = getBOAttributeItem(btmName);
 
        String tableName = OmdHelper.getBTTableName(btmName);
 
        Session session = HibernateSessionFactory.getSession();
        String sql = getPageSelectBOSql(tableName, attrItems, page, pageSize);
        Statement stmt = HibernateSessionFactory.getSessionConnection().createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        
        List<BusinessObject> lstBO = new ArrayList<BusinessObject>();
        BusinessObject bo = new BusinessObject();
        while (rs.next()) {
            bo = ServerObjectUtil.createBOByRS(attrItems, rs);
            lstBO.add(bo);
        }
        
        return lstBO.toArray(new BusinessObject[0]);
    }    
    /**
     * 构建查询SQL语句
     * 
     * @param oid
     * @param btmName
     * @param queryColumns
     * @return
     */
    private String getBatchSelectBOSql(String[] oids, String btmName,
            AttribItem[] attrItems) {
        String sql = "";
        StringBuffer sbFields = new StringBuffer(
                "SELECT OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
                        + "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
                        + "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION");//
//                        + "CHECKINBY,CHECKINTIME,CHECKOUTBY,CHECKOUTTIME,");
        for (int i = 0; i < attrItems.length; i++) {
            sbFields.append(",");
            sbFields.append(attrItems[i].name);
        }
        
        sbFields.append(" FROM ").append(btmName).append(" p where p.OID in (");
        for (int i = 0; i < oids.length; i++) {
            if ((i != 0) && (i % 500 != 0)) {
                sbFields.append(",");
            }
            sbFields.append("'").append(oids[i]).append("'");
            if (i + 1 == oids.length) {
                break;
            }
            if((i + 1) % 500 == 0){
                sbFields.append(") or p.OID in (");
            }
        }
        sbFields.append(")");
        sql = sbFields.toString();
        return sql;
    }
    
    private String getBatchSelectBOSqlByTempOIDs(String btmName, AttribItem[] attrItems) {
        String sql = "";
        StringBuffer sbFields = new StringBuffer(
                "SELECT OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
                        + "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
                        + "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION");
                        //+ "CHECKINBY,CHECKINTIME,CHECKOUTBY,CHECKOUTTIME");
        for (int i = 0; i < attrItems.length; i++) {
            sbFields.append(",");
            sbFields.append(attrItems[i].name);
        }
        sbFields.append(" FROM ").append(btmName).append(" p where p.OID in (select oid from pl_tempoid)");
 
        sql = sbFields.toString();
        return sql;
    }
    
    /**
     * 分页查询指定类型的对象
     * @param btmName
     * @param attrItems
     * @param page
     * @param pageSize
     * @return
     */
    private String getPageSelectBOSql(String btmName, AttribItem[] attrItems, int page, int pageSize) {
        String sql = "";
        StringBuffer sbFields = new StringBuffer(
                "SELECT OID,REVISIONOID,NAMEOID,BtmName,isLastR,isFirstR,isLastV,isFirstV,Creator,CreateTime,LastModifier,"
                        + "LastModifyTime,RevisionRule,VersionRule,RevisionSeq,RevisionValue,"
                        + "VersionSeq,VersionValue,LCTID,LCStatus,TS,ID,NAME,DESCRIPTION,OWNER,COPYFROMVERSION");
//                        + "CHECKINBY,CHECKINTIME,CHECKOUTBY,CHECKOUTTIME,");
        for (int i = 0; i < attrItems.length; i++) {
            sbFields.append(",");
            sbFields.append(attrItems[i].name);
        }
        
        sbFields.append(" FROM ").append(btmName).append(" p ");
        
        if (page > 0 && pageSize > 0) {
            int start = (page - 1) * pageSize;
            int end = page * pageSize;
            sbFields.append("where rownum between ").append(start).append(" and ").append(end);
        }
        
        sql = sbFields.toString();
        return sql;
    }
    
//    public static void main(String[] args) {
//        BOFactoryBatchServices s = new BOFactoryBatchServices();
//        StringBuffer sbFields = new StringBuffer();
//        sbFields.append(" FROM ").append("abc").append(" p where p.OID in (");
//        String[] oids = new String[5];
//        for (int i = 0; i < 5; i++) {
//            oids[i] = "oid" + i;
//        }
//        for (int i = 0; i < oids.length; i++) {
//            if (i != 0 && i%3 != 0) {
//                sbFields.append(",");
//            }
//            sbFields.append("'").append(oids[i]).append("'");
//            if (i + 1 == oids.length) {
//                break;
//            }
//            if((i + 1) % 3 == 0){
//                sbFields.append(") or p.OID in (");
//            }
//        }
//        sbFields.append(")");
//        
//        System.out.println(sbFields.toString());
//    }
}