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;
|
}
|
|
|
}
|