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
package com.vci.server.omd.qt.cache;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
 
import com.alibaba.fastjson.JSONObject;
import com.vci.corba.omd.qtm.QTD;
import com.vci.corba.omd.qtm.QTInfo;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.cache.CacheNames;
import com.vci.server.cache.redis.RedisUtil;
import com.vci.server.omd.qt.dao.impl.QTDaoImpl;
import com.vci.server.omd.qt.entity.QTEntity;
 
public class QTDServerCacheUtil {
 
    private DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static volatile QTDServerCacheUtil instance = null;
    
    
    public static void initCache() {
        clearCache();
        
        getInstance().initQTD();
        getInstance().initQt();
    }
    
    public static void clearCache() {
        RedisUtil.getInstance().del(CacheNames.QTCACHE);
        RedisUtil.getInstance().del(CacheNames.QTDCACHE);
    }
    
    public static void setQTemplate(QTInfo qt) {
        if (qt == null)
            return;
        
        String jsonObj = JSONObject.toJSONString(qt);
        
        RedisUtil.getInstance().hset(CacheNames.QTCACHE, qt.qtName.toLowerCase(), jsonObj);
    }
    
    public static void setQTD(QTD qtd) {
        if (qtd == null)
            return;
        
        String jsonObj = JSONObject.toJSONString(qtd);
        
        RedisUtil.getInstance().hset(CacheNames.QTDCACHE, qtd.name.toLowerCase(), jsonObj);
    }
    
 
    public static void delQTemplate(String name) {
        if (StringUtils.isBlank(name))
            return;
        
        RedisUtil.getInstance().hdel(CacheNames.QTCACHE, name.toLowerCase());
    }
 
    public static void delQTD(String name) {
        if (StringUtils.isBlank(name))
            return;
        
        RedisUtil.getInstance().hdel(CacheNames.QTDCACHE, name.toLowerCase());
    }
    
    
    public static QTD getQTD(String name) {
        String attrObj = RedisUtil.getInstance().hget(CacheNames.QTDCACHE, name.toLowerCase());
        if (StringUtils.isBlank(attrObj))
            return null;
        
        return JSONObject.parseObject(attrObj, QTD.class);
    }
    
    public static boolean isExistsQTD(String name) {
        String json = RedisUtil.getInstance().hget(CacheNames.QTDCACHE, name.toLowerCase());
        
        if (StringUtils.isBlank(json))
            return false;
        return true;
    }
    
    public static QTD[] getBizTypeQTDs(String name) {
        Map<String, String> map = RedisUtil.getInstance().hgetAll(CacheNames.QTDCACHE);
        List<QTD> lstItem = new ArrayList<QTD>();
        
        String[] items = map.values().toArray(new String[0]);
        for (int i = 0; i < items.length; i++) {
            QTD qtd = JSONObject.parseObject(items[i], QTD.class);
            if (qtd.btmName.equalsIgnoreCase(name))
                lstItem.add(qtd);
        }
        
        return lstItem.toArray(new QTD[0]);
    }
    
    public static QTD[] getLinkTypeQTDs(String name) {
        Map<String, String> map = RedisUtil.getInstance().hgetAll(CacheNames.QTDCACHE);
        List<QTD> lstItem = new ArrayList<QTD>();
        
        String[] items = map.values().toArray(new String[0]);
        for (int i = 0; i < items.length; i++) {
            QTD qtd = JSONObject.parseObject(items[i], QTD.class);
            if (qtd.linkTypeName.equalsIgnoreCase(name))
                lstItem.add(qtd);
        }
        
        return lstItem.toArray(new QTD[0]);
    }
 
    
    public static QTD[] getAllQTD() {
        Map<String, String> map = RedisUtil.getInstance().hgetAll(CacheNames.QTDCACHE);
        
        List<QTD> lstItem = new ArrayList<QTD>();
        String[] items = map.values().toArray(new String[0]);
        for (int i = 0; i < items.length; i++) {
            lstItem.add(JSONObject.parseObject(items[i], QTD.class));
        }
        
        return lstItem.toArray(new QTD[0]);
    }
    
    public static QTInfo getQTemplate(String name) {
        String attrObj = RedisUtil.getInstance().hget(CacheNames.QTCACHE, name.toLowerCase());
        if (StringUtils.isBlank(attrObj))
            return null;
        
        return JSONObject.parseObject(attrObj, QTInfo.class);
    }
    
    public static QTInfo[] getObjTypeQTs(String name) {
        Map<String, String> map = RedisUtil.getInstance().hgetAll(CacheNames.QTCACHE);
        
        List<QTInfo> lstItem = new ArrayList<QTInfo>();
        String[] items = map.values().toArray(new String[0]);
        for (int i = 0; i < items.length; i++) {
            lstItem.add(JSONObject.parseObject(items[i], QTInfo.class));
        }
        
        return lstItem.toArray(new QTInfo[0]);
    }
 
    
    public static QTInfo[] getAllQTemplate() {
        Map<String, String> allVols = RedisUtil.getInstance().hgetAll(CacheNames.QTCACHE);
        
        List<QTInfo> lstItem = new ArrayList<QTInfo>();
        String[] items = allVols.values().toArray(new String[0]);
        for (int i = 0; i < items.length; i++) {
            lstItem.add(JSONObject.parseObject(items[i], QTInfo.class));
        }
        
        return lstItem.toArray(new QTInfo[0]);
    }
    
 
    public static boolean isExistsQT(String name) {
        String json = RedisUtil.getInstance().hget(CacheNames.QTCACHE, name.toLowerCase());
        
        if (StringUtils.isBlank(json))
            return false;
        return true;
    }
    
    private QTDServerCacheUtil() {
    }
    
    private static QTDServerCacheUtil getInstance() {
        if (instance == null) {
            synchronized (QTDServerCacheUtil.class) {
                if (instance == null) {
                    instance = new QTDServerCacheUtil();
                }
            }
        }
        
        return instance;
    }
    
    private void initQTD() {
        String sql = "select * from PL_QTEMPLATEDEF t where 1=1";
        try{
            Session session = HibernateSessionFactory.getSession();
            SQLQuery query = session.createSQLQuery(sql);
            List<?> list = query.list();
            for(int i = 0; i < list.size(); i++){
                QTD qtd = new QTD();
                Object[] obj = (Object[]) list.get(i);
                qtd.name = (String)obj[0];
                qtd.creator = (String)obj[1];
                qtd.createTime = ((Date)obj[2]).getTime();
                qtd.btmName = (String)obj[3] == null ? "" : (String)obj[3];
                String abNames = ((String)obj[4]);
                if(abNames == null || abNames.equals("")){
                    qtd.abNames = new String[0];
                }else{
                    qtd.abNames = ((String)obj[4]).split(",");
                }
                qtd.linkTypeName = (String)obj[5] == null ? "" : (String)obj[5];
                
                setQTD(qtd);
            }
        }catch(Throwable e){
            e.printStackTrace();
            //throw getLocalVciError("PLMQTD-00003", e);
        }
    }
    
    private void initQt() {
        String hsql = "from QTEntity";
        List<QTEntity> qtEntityList = new QTDaoImpl().findEntities(hsql);
        //qts = new QTInfo[qtEntityList.size()];
        for (int i = 0; i < qtEntityList.size(); i++) {
             QTInfo wapper = getQTWrapper(qtEntityList.get(i));
             
             setQTemplate(wapper);
        }
    }
    
    
    private QTInfo getQTWrapper(QTEntity qtEntity){
        if(qtEntity == null){
            return null;
        }
        QTInfo qtWrapper = new QTInfo();
        
        qtWrapper.qtName = qtEntity.getQtName();
        qtWrapper.btmName = qtEntity.getBtmName();
        qtWrapper.creator = qtEntity.getCreator();
        qtWrapper.createTime = qtEntity.getCreateTime().getTime();
        qtWrapper.levelFlag = qtEntity.getLevelFlag();
        String qtUIText = qtEntity.getQtUIText();
        qtWrapper.qtUIText = (qtUIText == null ? "" : qtUIText);
        String qtText = qtEntity.getQtText();
        qtWrapper.qtText = (qtText == null ? "" : qtText);
        
        return qtWrapper;
    }
 
}