YYC
2023-09-07 7243e0b4bd9a267a633d2e481e17646509b6868d
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
package com.ld.igds.modbus.service;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.modbus.ModbusConstant;
import com.ld.igds.models.DeviceModbus;
import com.ld.igds.models.GasModbus;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
@Component
public class HModbusService extends HibernateDao {
 
    @Resource
    private RedisUtil redisUtil;
 
 
    public List<DeviceModbus> listAllDevice(String companyId) {
 
        String hql = " from " + DeviceModbus.class.getName() + " where 1=1 order by deviceCode+0";
 
        return this.query(hql);
    }
 
    public void flushCacheDevice(String companyId) {
 
        if (null == companyId) companyId = ContextUtil.getDefaultCompanyId();
 
        List<DeviceModbus> listAll = this.listAllDevice(companyId);
 
        if (null == listAll || listAll.isEmpty()) return;
 
 
        String key;
        DeviceModbus entityDevice;
        for (DeviceModbus hibDevice : listAll) {
            entityDevice = new DeviceModbus();
            BeanUtils.copyProperties(hibDevice, entityDevice);
 
            if (null == hibDevice.getBizType()) hibDevice.setBizType(BizType.AREATION.getCode());
            if (BizType.AREATION.getCode().equals(hibDevice.getBizType())) {
                key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode());
            } else {
                key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode(), hibDevice.getBizType());
            }
            redisUtil.set(key, entityDevice);
        }
    }
 
    public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode) {
        return getCacheDeviceModbus(companyId, deviceCode, null);
    }
 
    public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode, String bizType) {
        String key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode);
        if (null != bizType && !BizType.AREATION.equals(bizType)) {
            key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode, bizType);
        }
        return (DeviceModbus) redisUtil.get(key);
    }
 
    public void updateDataDevice(List<DeviceModbus> details) {
        if (null == details || details.isEmpty()) return;
 
        Session session = this.getSessionFactory().openSession();
        try {
            for (DeviceModbus device : details) {
 
 
                device = updateInitData(device);
 
                if (null == device.getId()) {
                    device.setId(ContextUtil.getUUID());
                    session.save(device);
                } else {
                    session.update(device);
                }
            }
 
            flushCacheDevice(null);
 
        } catch (Exception e) {
 
        } finally {
            session.flush();
            session.close();
        }
    }
 
    private DeviceModbus updateInitData(DeviceModbus device) {
        if (StringUtils.isEmpty(device.getOpen())) device.setOpen(Constant.YN_N);
        if (StringUtils.isEmpty(device.getOpenFun())) device.setOpenFun(ModbusConstant.FUN_99);
 
        if (StringUtils.isEmpty(device.getOpenEnd())) device.setOpenEnd(Constant.YN_N);
        if (StringUtils.isEmpty(device.getOpenEndFun())) device.setOpenEndFun(ModbusConstant.FUN_99);
 
        if (StringUtils.isEmpty(device.getOpenError())) device.setOpenError(Constant.YN_N);
        if (StringUtils.isEmpty(device.getOpenErrorFun())) device.setOpenErrorFun(ModbusConstant.FUN_99);
 
        if (StringUtils.isEmpty(device.getClose())) device.setClose(Constant.YN_N);
        if (StringUtils.isEmpty(device.getCloseFun())) device.setCloseFun(ModbusConstant.FUN_99);
 
        if (StringUtils.isEmpty(device.getCloseEnd())) device.setCloseEnd(Constant.YN_N);
        if (StringUtils.isEmpty(device.getCloseEndFun())) device.setCloseEndFun(ModbusConstant.FUN_99);
 
        if (StringUtils.isEmpty(device.getCloseError())) device.setCloseError(Constant.YN_N);
        if (StringUtils.isEmpty(device.getCloseErrorFun())) device.setCloseErrorFun(ModbusConstant.FUN_99);
 
        if (StringUtils.isEmpty(device.getStop())) device.setStop(Constant.YN_N);
        if (StringUtils.isEmpty(device.getStopFun())) device.setStopFun(ModbusConstant.FUN_99);
 
 
        if (StringUtils.isEmpty(device.getBizType())) device.setBizType(BizType.AREATION.getCode());
 
        return device;
    }
 
    public String delDataDevice(DeviceModbus data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(data);
            flushCacheDevice(null);
        } catch (Exception e) {
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    /**
     * 根据类型获取Modbus能耗设备配置信息
     * @param bizType
     * @param serId
     * @return
     */
    public List<DeviceModbus> getDataByBizType(String bizType, String serId) {
 
        String hql = " from " + DeviceModbus.class.getName() + " where bizType=:bizType and serId =:serId";
 
        Map<String, Object> param = new HashMap<>();
        param.put("bizType", bizType);
        param.put("serId", serId);
 
        return this.query(hql, param);
    }
 
 
    public DeviceModbus getGasData(String companyId, String depotId, String bizType) {
 
        String hql = " from " + DeviceModbus.class.getName() + " where bizType=:bizType and deviceCode =:deviceCode";
 
        Map<String, Object> param = new HashMap<>();
        param.put("bizType", bizType);
        param.put("deviceCode", depotId);
        List<DeviceModbus> list = this.query(hql, param);
 
        if (null == list || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
 
 
    public List<GasModbus> listGasModBus(String depotId) {
        String hql = " from " + GasModbus.class.getName();
 
        if (null == depotId) {
            return this.query(hql);
        } else {
            hql += " where depotId=:depotId order by passcode";
            Map<String, Object> param = new HashMap<>();
            param.put("depotId", depotId);
            return this.query(hql, param);
        }
    }
 
    public void updateGasModbus(DeviceModbus data) {
        Session session = this.getSessionFactory().openSession();
        try {
 
            data = updateInitData(data);
            data.setBizType(BizType.GAS.getCode());
 
            if (StringUtils.isEmpty(data.getId())) {
                data.setId(ContextUtil.getUUID());
                session.save(data);
            } else {
                session.update(data);
            }
            flushCacheDevice(null);
 
            this.updateListGas(session, data);
 
        } catch (Exception e) {
        } finally {
            session.flush();
            session.close();
        }
    }
 
    private void updateListGas(Session session, DeviceModbus data) {
        List<GasModbus> list = data.getListGas();
        if (null == list || list.isEmpty()) return;
        for (GasModbus gas : list) {
            gas.setDepotId(data.getDeviceCode());
            if (StringUtils.isEmpty(gas.getId())) {
                gas.setId(ContextUtil.getUUID());
                session.save(gas);
            } else {
                session.update(gas);
            }
        }
    }
 
 
    public String delDataGas(GasModbus data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(data);
        } catch (Exception e) {
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
}