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 mapObjTime = new HashMap(); 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 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(); } } //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 loadAll = daoImpl.findEntites(sql.toString(), new Object[0], "", PLCacheRecordEnt.class); mapObjTime.put(objType, timeNew); return loadAll; } } /** * 根据数据类型获取对应的详细信息 * * @param plOId * @return * @throws Throwable */ public List 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(); } } //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 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); } } }