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
package com.vci.server.cache;
 
import java.sql.Connection;
//import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.vci.common.log.ServerWithLog4j;
import com.vci.server.base.persistence.dao.BaseService;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.cache.dao.impl.PLCacheRecordEntDaoImp;
import com.vci.server.cache.object.PLCacheRecordEnt;
 
public class PLCacheRecordService extends BaseService{
    private static volatile PLCacheRecordService instance = null;
    
    private Map<String, Long> mapObjTime = new HashMap<String, Long>();  
    
    private PLCacheRecordService(){
        
    }
    
    public static PLCacheRecordService getInstance(){
        if(instance == null){
            synchronized (PLCacheRecordService.class){
                if (instance == null) {
                    instance = new PLCacheRecordService();
                }
            }
        }
        return instance;
    }
 
    /**
     * 根据数据类型获取对应的详细信息
     * 
     * @param plOId
     * @return
     * @throws Throwable 
     */
    public List<PLCacheRecordEnt> getCacheRecordByObjType(String objType) throws Throwable {
        
        long timeNew = getDataBaseCurrtenttime();
        
        // 解决线程安全的问题
        synchronized (this) {
            if (mapObjTime.containsKey(objType)) {
                long timeOld = mapObjTime.get(objType);
                
                if ((timeNew - timeOld) < 5000){
                    //ServerWithLog4j.logger.debug(String.format("====时间短暂,忽略【%s】缓存更新====", objType));
    
                    return new ArrayList<PLCacheRecordEnt>();
                }
            }
            
            //ServerWithLog4j.logger.debug(String.format("====更新【%s】缓存====", objType));
    
            PLCacheRecordEntDaoImp daoImpl = new PLCacheRecordEntDaoImp();
            StringBuilder sql = new StringBuilder("select * from plcachetemp p");
            if (objType != null && !objType.trim().equals("")) {
                sql.append(" where p.PLOBJTYPE = '" + objType + "'");
            }
            List<PLCacheRecordEnt> loadAll = daoImpl.findEntites(sql.toString(), new Object[0], "", PLCacheRecordEnt.class);
            
            mapObjTime.put(objType, timeNew);
            return loadAll;
        }
    }
    
    
    /**
     * 根据数据类型获取对应的详细信息
     * 
     * @param plOId
     * @return
     * @throws Throwable 
     */
    public List<PLCacheRecordEnt> getCacheRecordByObjType(String objType, long time) throws Throwable {
        // 两次查询间隔小于5000ms,则不查询是否存在修改;
        long timeNew = getDataBaseCurrtenttime();
        
        synchronized (this) {
            if (mapObjTime.containsKey(objType)) {
                long timeOld = mapObjTime.get(objType);
                
                if ((timeNew - timeOld) < 5000){
                    //ServerWithLog4j.logger.debug(String.format("====时间短暂,忽略【%s】缓存更新====", objType));
                    return new ArrayList<PLCacheRecordEnt>();
                }
            }
            
            //ServerWithLog4j.logger.debug(String.format("====更新【%s】缓存====", objType));
    
            PLCacheRecordEntDaoImp daoImpl = new PLCacheRecordEntDaoImp();
            
            StringBuilder sql = new StringBuilder("select * from plcachetemp p  where 1 =1 " +
                    "and p.PLOPERATETIME >= to_timestamp('" + new Timestamp(time) + "', 'yyyy-mm-dd hh24:mi:ss.ff')");
            if (objType != null && !objType.trim().equals("")) {
                sql.append(" and p.PLOBJTYPE = '" + objType + "'");
            }
            List<PLCacheRecordEnt> loadAll = daoImpl.findEntites(sql.toString(), new Object[0], "", PLCacheRecordEnt.class);
            
            mapObjTime.put(objType, timeNew);
            
            return loadAll;
        }
    }
    
    public boolean clearCacheRecord() throws Throwable {
        PLCacheRecordEntDaoImp daoImpl = new PLCacheRecordEntDaoImp();
        String sql = "truncate table plcachetemp";
        daoImpl.createSQLQuery(sql);
        
        long timeNew = getDataBaseCurrtenttime();
        
        synchronized (this) {
            for (String key : mapObjTime.keySet()) {
                mapObjTime.put(key, timeNew);
            }
        }
        return true;
    }
    
    /**
     * 删除指定类型下的数据
     * @param objType
     * @return
     * @throws SQLException
     */
    public boolean deleteHisTemp(String objType) throws SQLException {
        boolean flag = false;
        String sql = "delete from PLCACHETEMP where PLOBJTYPE = ? ";
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setString(1, objType);
        pst.executeUpdate();
        pst.close();
        flag = true;
        
        try {
            long timeNew = getDataBaseCurrtenttime();
            synchronized (this) {
                mapObjTime.put(objType, timeNew);
            }
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return flag;
    }
    
    public boolean deleteHisTempByIDs(String[] ids) throws SQLException {
        boolean flag = false;
        StringBuffer sb = new StringBuffer("delete from PLCACHETEMP where PLUID in(");
        for (int i = 0; i < ids.length; i++) {
            if (i != 0) {
                sb.append(",");
            }
            sb.append("'").append(ids[i]).append("'");
        }
        sb.append(")");
        //System.out.println(sb.toString());
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sb.toString());
        pst.executeUpdate();
        pst.close();
        flag = true;
        return flag;
    }
    /**
     * 获取数据库当前时间
     * 
     * @param 
     * @return
     * @throws Throwable 
     */
    public Long getDataBaseCurrtenttime() throws Throwable {
        Long currenttime=null;
        Timestamp tsTimestamp=null;
        String sql = "select systimestamp from dual";
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        ResultSet res=pst.executeQuery();
        if(res.next()){
            tsTimestamp=res.getTimestamp("systimestamp");
        }
        currenttime=tsTimestamp.getTime();
        if(null!=res){
            res.close();
        }
        if(null!=pst){
            pst.close();
        }
        return currenttime;
    }
    
    public void resetObjType(String objType) {
        ServerWithLog4j.logger.debug(String.format("====重置【%s】缓存====", objType));
        synchronized (this) {
            mapObjTime.remove(objType);
        }
    }
}