jiazx0107@163.com
2023-08-24 2884bf1f32b51129a83125ecfc2cf8526f71154f
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
package com.ld.igds.modbus.service;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
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.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.List;
 
 
@Component
public class HDeviceModbusService extends HibernateDao {
 
    @Resource
    private RedisUtil redisUtil;
 
 
    public List<DeviceModbus> listAll(String companyId) {
 
        String hql = " from " + DeviceModbus.class.getName() + " where 1=1 order by deviceCode+0";
 
        return this.query(hql);
    }
 
    public void flushCache(String companyId) {
 
        if (null == companyId) companyId = ContextUtil.getCompanyId();
 
        List<DeviceModbus> listAll = this.listAll(companyId);
 
        if (null == listAll || listAll.isEmpty()) return;
 
 
        String key;
        DeviceModbus entityDevice;
        for (DeviceModbus hibDevice : listAll) {
            entityDevice = new DeviceModbus();
            BeanUtils.copyProperties(hibDevice, entityDevice);
            key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode());
            redisUtil.set(key, entityDevice);
        }
    }
 
    public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode) {
        String key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode);
        return (DeviceModbus) redisUtil.get(key);
    }
 
    public void updateData(List<DeviceModbus> details) {
        if (null == details || details.isEmpty()) return;
 
        Session session = this.getSessionFactory().openSession();
        try {
            for (DeviceModbus device : details) {
 
                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 (null == device.getId()) {
                    device.setId(ContextUtil.getUUID());
                    session.save(device);
                } else {
                    session.update(device);
                }
            }
 
            flushCache(null);
 
        } catch (Exception e) {
 
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public String delData(DeviceModbus data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(data);
            flushCache(null);
        } catch (Exception e) {
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
 
}