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
package com.vci.server.omd.common;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import com.vci.corba.omd.atm.AttribItem;
import com.vci.omd.dataType.VTDataType;
import com.vci.server.base.utility.AttributeHelper;
import com.vci.server.base.utility.OmdHelper;
import com.vci.server.cache.OMCacheProvider;
 
 
 
public class OmdViewTool {
    private static final String _TV = "_TV";
    private static final String _FV = "_FV";
    private static final String _V = "_V";
    public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 
    /**
     * Array ----> ArrayList
     * @param array
     * @return
     */
    public static List<String> arrayToList(String[] array){
        List<String> list = new ArrayList<String>();
        for(int i = 0; i < array.length; i++){
            list.add(array[i]);
        }
        return list;
    }
    
    /**
     * convert array to String append with ','.
     * @param array
     * @return
     */
    public static String arrayToString(String[] array){
        String str = "";
        if(array == null){
            return str;
        }
        for(int i = 0; i < array.length; i++){
            str += array[i];
            if(i < array.length - 1){
                str += ",";
            }
        }
        return str;
    }
    
 
    
 
    /**
     * 获取链接类型TO端业务类型视图名
     * @param ltName
     * @return
     */
    public static String getToViewName(String ltName){
        return ltName + _TV;
    }
    
    /**
     * 获取链接类型From端业务类型视图名
     * @param ltName
     * @return
     */
    public static String getFromViewName(String ltName){
        return ltName + _FV;
    }
    
    /**
     * 获取业务类型视图名
     * @param ltName
     * @return
     */
    public static String getBtViewName(String btName){
        return btName + _V;
    }
    
    /**
     * 获取创建链接类型TO端业务类型视图的sql
     * @param ltName
     * @return
     */
    public static String getBTSViewSql(String[] btNames, String viewName){
        if(btNames.length < 1){
            return "";
        }
        StringBuilder stb = new StringBuilder("create or replace view ");
        try {
            Set<String> allAttrName = getUserAttrNameSet(btNames);
            Map<String, StringBuilder> sqlMap = getBTSqlMap(allAttrName, btNames);
            stb.append(viewName);
            stb.append(" as ");
            for(String btName : sqlMap.keySet()){
                stb.append(sqlMap.get(btName));
                stb.append(" union all ");
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return stb.substring(0, stb.lastIndexOf(" union all "));
    }
    
    /**
     * 获取业务类型属性总集合
     * @param btNames
     * @return
     */
    private static Set<String> getUserAttrNameSet(String[] btNames){
        Set<String> set = new LinkedHashSet<String>();
        try{
            for(String btName : btNames){
                //String[] abNames = BtmHelper.getAbNames(btName);
                String[] abNames = OMCacheProvider.getBTAttributes(btName);
                for(String abName : abNames){
                    set.add(abName);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return set;
    }
    
    /**
     * 获取各个业务类型查询属性总集合的sql
     * 当业务业务类型中不包含某个属性attrA时, '' as attrA
     * @param allAttrName
     * @param btNames
     * @return
     */
    private static Map<String, StringBuilder> getBTSqlMap(Set<String> allAttrName,
            String[] btNames) {
        //记录业务类型和与其对应的属性列表
        Map<String, List<String>> btAttrMap = new HashMap<String, List<String>>();
        //记录业务类型和与其对应的查询sql
        Map<String, StringBuilder> btSqlMap = new HashMap<String, StringBuilder>();
        //modify by weidy@2021-11-15,在沈飞的发现一个现象是,拆分了的视图可能某个字段全部为null,这样多个视图链接到一起的时候,会出现类型不一致问题
        String[] sysAbItems = OmdHelper.getBTSysANames();
        String querySysAttrSql = "select " + arrayToString(sysAbItems);
        for(String btName : btNames){
            //String[] abNames = BtmHelper.getAbNames(btName);
            String[] abNames = OMCacheProvider.getBTAttributes(btName);
             
            btAttrMap.put(btName, arrayToList(abNames));
            btSqlMap.put(btName, new StringBuilder(querySysAttrSql));
        }
        
        Map<String, String> mapAttrNull = new HashMap<String, String>();
        
        for(String attrName : allAttrName){
            for(String btName : btNames){
                btSqlMap.get(btName).append(",");
                if(btAttrMap.get(btName).contains(attrName)){
                    btSqlMap.get(btName).append(attrName);
                }else{
                    String attrNull = "";
                    
                    if (mapAttrNull.containsKey(attrName)) {
                        attrNull = mapAttrNull.get(attrName);
                    } else {
                        attrNull = CreateAttrNull(attrName);
                        mapAttrNull.put(attrName, attrNull);
                    }
                    btSqlMap.get(btName).append(attrNull);
                }
            }
        }
        
        for(String btName : btNames){
            btSqlMap.get(btName).append(" from " + OmdHelper.getBTTableName(btName));
        }
        return btSqlMap;
    }
    
    /**
     * 创建null AS 指定类型属性
     * @param attrName
     * @return
     */
    private static String CreateAttrNull(String attrName) {
        try {
 
            AttribItem ai = OMCacheProvider.getAttribute(attrName);
 
            String vtType = ai.vtDataType;
            int length = 50;
            String lengthStr = AttributeHelper.getOtherValueByType(ai.other, "length");
 
            if ((lengthStr != null) && (!lengthStr.equals(""))) {
                length = Integer.valueOf(lengthStr).intValue();
            }
 
            String sqlType = "";
 
            if (VTDataType.VTSTRING.equalsIgnoreCase(vtType)) {
                sqlType = "VARCHAR2(" + length + ")";
            } else if (VTDataType.VTLONG.equalsIgnoreCase(vtType) || VTDataType.VTINTEGER.equalsIgnoreCase(vtType)) {
                sqlType = "NUMBER";
            } else if (VTDataType.VTDOUBLE.equalsIgnoreCase(vtType)) {
                sqlType = "NUMBER";
            } else if (VTDataType.VTBOOLEAN.equalsIgnoreCase(vtType)) {
                sqlType = "VARCHAR2(8)";
            } else if (VTDataType.VTIMAGE.equalsIgnoreCase(vtType)) {
                sqlType = "VARCHAR2(255)";
            } else if (VTDataType.VTDATE.equalsIgnoreCase(vtType)) {
                sqlType = "DATE";
            } else if (VTDataType.VTDATETIME.equalsIgnoreCase(vtType) || VTDataType.VTTIME.equalsIgnoreCase(vtType)) {
                sqlType = "TIMESTAMP";
            } else if (VTDataType.VTFILEPATH.equalsIgnoreCase(vtType) || VTDataType.VTNOTE.equalsIgnoreCase(vtType)) {
                sqlType = "VARCHAR2(255)";
            } else if (VTDataType.VTCLOB.equalsIgnoreCase(vtType)) {
                sqlType = "CLOB";
            } else {
                sqlType = "VARCHAR2(" + length + ")";
            }
 
            // btSqlMap.get(btName).append("cast(null as " + sqlType + " ) as " + attrName);
            String attrNull = "cast(null as " + sqlType + " ) as " + attrName;
 
            return attrNull;
        } catch (Throwable e) {
            e.printStackTrace();
        }
 
        return "";
    }
    
//    public static String getOtherValueByType(String other, String type) {
//        String[] otherArray = other.split(";");
//        for (int i = 0; i < otherArray.length; i++) {
//            String otherValue = otherArray[i];
//            if (otherValue.contains(type)) {
//                return otherValue.substring(otherValue.indexOf("=") + 2, otherValue.length());
//            }
//        }
//        return null;
//    }
 
    /**
     * 视图cols 
     * viewName.col as col2
     * @param ltName
     * @return
     */
    public static List<String> getViewColsWithAlias(String[] btNames, String viewName) {
        List<String> cols = new ArrayList<String>();
        String[] sysAbItems = OmdHelper.getBTSysANames();
        for(String sysAbItem : sysAbItems){
            cols.add(viewName + "." + sysAbItem + " AS " + sysAbItem + "2");
        }
        Set<String> userAttrNameSet = getUserAttrNameSet(btNames);
        for(String userAttrName : userAttrNameSet){
            cols.add(viewName + "." + userAttrName + " AS " + userAttrName + "2");
        }
        return cols;
    }
 
}