package com.ld.igds.view.service; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.ld.igds.constant.Constant; import com.ld.igds.models.SnapSer; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.RedisUtil; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.*; @Component public class SnapSerService extends HibernateDao { /** * 抓拍分机缓存前缀 */ private String CACHE_KEY_PRE = "SNAP_SER"; /** * 断电抓拍标记前缀 */ //private String CACHE_KEY_TAG = "SNAP_TAG"; @Autowired private RedisUtil redisUtil; public List getAllSer(String companyId) { String hql = " from " + SnapSer.class.getName() + " where companyId=:companyId order by sn"; Map args = new HashMap(); args.put("companyId", companyId); return this.query(hql, args); } public void delSerById(SnapSer ser) { Session session = this.getSessionFactory().openSession(); try { session.delete(ser); } finally { session.flush(); session.close(); } } public void refreshCache(String companyId) { List list = this.getAllSer(companyId); if (null == list || list.isEmpty()) return; for (SnapSer ser : list) { this.setCacheSer(ser); } } public void setCacheSer(SnapSer snapSer) { if (null == snapSer.getDeptId()) snapSer.setDeptId("0000"); String key = Constant.APP_NAME + ":" + CACHE_KEY_PRE + ":" + snapSer.getDeptId() + ":" + snapSer.getSn(); redisUtil.set(key, snapSer); } public SnapSer getCacheByDeptId(String deptId) { String patten = Constant.APP_NAME + ":" + CACHE_KEY_PRE; Set keys = redisUtil.keys(patten); for (String key : keys) { if (key.indexOf(deptId) >= 0) { return (SnapSer) redisUtil.get(key); } } return null; } public SnapSer getCacheBySn(String sn) { String patten = Constant.APP_NAME + ":" + CACHE_KEY_PRE; Set keys = redisUtil.keys(patten); for (String key : keys) { if (key.indexOf(sn) >= 0) { return (SnapSer) redisUtil.get(key); } } return null; } public int updateInfoBySn(String status, String ip, Integer port, String sn) { Session session = null; int i = 0; try { session = this.getSessionFactory().openSession(); String hql = " update " + SnapSer.class.getName() + " set status=:status,ip=:ip,port=:port,updateTime=:updateTime where sn=:sn"; Query query = session.createQuery(hql); query.setString("status", status); query.setString("ip", ip); query.setInteger("port", port); query.setTimestamp("updateTime", new Date()); query.setString("sn", sn); i = query.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return i; } public void offLine(String sn) { Session session = null; try { session = this.getSessionFactory().openSession(); String hql = " update " + SnapSer.class.getName() + " set status=:status,updateTime=:updateTime where sn=:sn"; Query query = session.createQuery(hql); query.setString("status", Constant.YN_N); query.setTimestamp("updateTime", new Date()); query.setString("sn", sn); query.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } public void connectAdd(String sn, String ip, int port) { int result = updateInfoBySn(Constant.YN_Y, ip, port, sn); if (result == 0) { SnapSer ser = new SnapSer(); ser.setCompanyId(ContextUtil.getDefaultCompanyId()); ser.setName("自动添加"); ser.setPort(port); ser.setSn(sn); ser.setIp(ip); ser.setStatus(Constant.YN_Y); ser.setUpdateTime(new Date()); //添加设备 updateSer(ser); } //更新缓存 refreshCache(ContextUtil.getDefaultCompanyId()); } public void updateSer(SnapSer data) { if (null == data.getDeptId()) { data.setDeptId(ContextUtil.subDeptId(null)); } if (null == data.getCompanyId()) { data.setCompanyId(ContextUtil.getCompanyId()); } Session session = null; try { session = this.getSessionFactory().openSession(); if (null == data.getId()) { data.setId(ContextUtil.getUUID()); data.setStatus(Constant.YN_Y); session.save(data); } else { session.update(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } }