jiazx0107@163.com
2023-11-04 43ce77a85d8297a53dc25430fff10ead57296167
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
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<SnapSer> getAllSer(String companyId) {
        String hql = " from " + SnapSer.class.getName() + " where companyId=:companyId order by sn";
        Map<String, Object> args = new HashMap<String, Object>();
        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<SnapSer> 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<String> 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<String> 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();
        }
    }
}