src/main/java/com/moral/entity/Dictionary.java
New file @@ -0,0 +1,33 @@ package com.moral.entity; public class Dictionary { private String dictName; private Integer dictValue; private Integer isDelete; public String getDictName() { return dictName; } public void setDictName(String dictName) { this.dictName = dictName == null ? null : dictName.trim(); } public Integer getDictValue() { return dictValue; } public void setDictValue(Integer dictValue) { this.dictValue = dictValue; } public Integer getIsDelete() { return isDelete; } public void setIsDelete(Integer isDelete) { this.isDelete = isDelete; } } src/main/java/com/moral/entity/DictionaryData.java
New file @@ -0,0 +1,73 @@ package com.moral.entity; public class DictionaryData { private Integer id; private Integer dictValue; private String dictDataName; private Object dictDataValue; private Integer isFixed; private Integer isDelete; private Integer parentId; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getDictValue() { return dictValue; } public void setDictValue(Integer dictValue) { this.dictValue = dictValue; } public String getDictDataName() { return dictDataName; } public void setDictDataName(String dictDataName) { this.dictDataName = dictDataName == null ? null : dictDataName.trim(); } public Object getDictDataValue() { return dictDataValue; } public void setDictDataValue(Object dictDataValue) { this.dictDataValue = dictDataValue; } public Integer getIsFixed() { return isFixed; } public void setIsFixed(Integer isFixed) { this.isFixed = isFixed; } public Integer getIsDelete() { return isDelete; } public void setIsDelete(Integer isDelete) { this.isDelete = isDelete; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } } src/main/java/com/moral/mapper/DictionaryDataMapper.java
New file @@ -0,0 +1,8 @@ package com.moral.mapper; import com.moral.common.mapper.BaseMapper; import com.moral.entity.DictionaryData; public interface DictionaryDataMapper extends BaseMapper<DictionaryData> { DictionaryData selectByPrimaryKey(Integer id); } src/main/java/com/moral/mapper/DictionaryMapper.java
New file @@ -0,0 +1,8 @@ package com.moral.mapper; import com.moral.common.mapper.BaseMapper; import com.moral.entity.Dictionary; public interface DictionaryMapper extends BaseMapper<Dictionary> { Dictionary selectByPrimaryKey(String dictName); } src/main/java/com/moral/mapper/MonitorPointMapper.java
@@ -10,6 +10,6 @@ public interface MonitorPointMapper extends BaseMapper<MonitorPoint>{ List<MonitorPoint> selectWithAreaNameByExample(Example example); List<MonitorPoint> getMonitorPointsByAreaName(Map<String, Object> parameters); List<MonitorPoint> selectByMap(Map<String, Object> params); List<Integer> selectOrganizationIds(int id); } src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -12,6 +12,7 @@ import com.moral.common.bean.PageResult; import com.moral.common.util.ExampleUtil; import com.moral.common.util.RedisUtils; import com.moral.mapper.MonitorPointMapper; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; @@ -32,7 +33,8 @@ @Resource private DeviceMapper deviceMapper; @Resource private MonitorPointMapper monitorPointMapper; @Resource private AccountService accountService; private Class ENTITY_CLASS = Device.class; @@ -205,8 +207,20 @@ /* 刷新 redis 设备的信息 */ private void refreshDeviceInRedis(String mac){ getDeviceWithOrgIdsByMac(mac); private void refreshDeviceInRedis(Device device){ if(!StringUtils.isBlank(device.getMac())){ Device simpleDevice = new Device(); simpleDevice.setId(device.getId()); simpleDevice.setDeviceVersion(device.getDeviceVersion()); simpleDevice.setMac(device.getMac()); simpleDevice.setMonitorPointId(device.getMonitorPointId()); if(device.getMonitorPointId()!=null){ List<Integer> orgIds = monitorPointMapper.selectOrganizationIds(device.getMonitorPointId()); simpleDevice.setOrganizationIds(orgIds); } String key = "device_"+device.getMac(); redisUtils.set(key,simpleDevice); } } @Override public PageBean queryByPageBean(PageBean pageBean) { @@ -256,7 +270,7 @@ }else{ deviceMapper.updateByPrimaryKeySelective(device); //刷新redis里设备信息 refreshDeviceInRedis(device.getMac()); refreshDeviceInRedis(device); } } catch (Exception ex){ src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -12,6 +12,9 @@ import com.moral.common.bean.Constants; import com.moral.common.bean.PageBean; import com.moral.common.util.*; import com.moral.entity.Device; import com.moral.mapper.DeviceMapper; import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; import com.moral.entity.MonitorPoint; @@ -24,6 +27,8 @@ public class MonitorPointServiceImpl implements MonitorPointService { @Resource private MonitorPointMapper monitorPointMapper; @Resource private DeviceMapper deviceMapper; @Resource RedisUtils redisUtils; private static Class ENTITY_CLASS = MonitorPoint.class; @@ -84,15 +89,43 @@ monitorPoint.setIsDelete(Constants.IS_DELETE_FALSE); monitorPointMapper.insertSelective(monitorPoint); }else{ // TODO 如果组织id改变需要刷新redis里设备信息 monitorPointMapper.updateByPrimaryKeySelective(monitorPoint); // 刷新当前监控点下设备 在redis里设备信息 refreshDevicesInRedis(monitorPoint.getId()); } } catch (Exception ex){ throw ex; } } /* 刷新当前监控点下设备 在redis里设备信息 */ private void refreshDevicesInRedis(int monitorPointId){ Device device = new Device(); device.setMonitorPointId(monitorPointId); List<Device> deviceList = deviceMapper.select(device); if (!CollectionUtils.isEmpty(deviceList)){ List<Integer> orgIds = monitorPointMapper.selectOrganizationIds(monitorPointId); if (!CollectionUtils.isEmpty(orgIds)){ deviceList.stream().forEach(dev ->{ if(!StringUtils.isNullOrEmpty(dev.getMac())){ String key = "device_"+dev.getMac(); // 简化的设备信息 用以缓存redis Device simpleDevice = new Device(); simpleDevice.setId(dev.getId()); simpleDevice.setDeviceVersion(dev.getDeviceVersion()); simpleDevice.setMac(dev.getMac()); simpleDevice.setMonitorPointId(dev.getMonitorPointId()); // 设置新组织关系,防止读写分离时数据库同步延迟 simpleDevice.setOrganizationIds(orgIds); redisUtils.set(key,simpleDevice); } }); } } } @Override public void deleteByIds(Integer... ids) { MonitorPoint monitorPoint = new MonitorPoint(); src/main/resources/mapper/DeviceMapper.xml
@@ -194,7 +194,7 @@ </resultMap> <!-- resultMap引用 --> <select id="selectOrganizationIds" resultType="INTEGER"> call proc_organization_id_select(#{id,jdbcType=INTEGER}); call proc_organizationIds_GetByDeviceId(#{id,jdbcType=INTEGER}); </select> <select id="selectWithOrgIdsByMac" parameterType="java.lang.String" resultMap="BaseResultWithOrgIdsMap"> select src/main/resources/mapper/DictionaryDataMapper.xml
New file @@ -0,0 +1,16 @@ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.moral.mapper.DictionaryDataMapper" > <resultMap id="BaseResultMap" type="com.moral.entity.DictionaryData" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="dict_value" property="dictValue" jdbcType="INTEGER" /> <result column="dict_data_name" property="dictDataName" jdbcType="VARCHAR" /> <result column="dict_data_value" property="dictDataValue" jdbcType="OTHER" /> <result column="is_fixed" property="isFixed" jdbcType="INTEGER" /> <result column="is_delete" property="isDelete" jdbcType="INTEGER" /> <result column="parent_id" property="parentId" jdbcType="INTEGER" /> </resultMap> <sql id="Base_Column_List" > id, dict_value, dict_data_name, dict_data_value, is_fixed, is_delete, parent_id </sql> </mapper> src/main/resources/mapper/DictionaryMapper.xml
New file @@ -0,0 +1,12 @@ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.moral.mapper.DictionaryMapper" > <resultMap id="BaseResultMap" type="com.moral.entity.Dictionary" > <id column="dict_name" property="dictName" jdbcType="VARCHAR" /> <result column="dict_value" property="dictValue" jdbcType="INTEGER" /> <result column="is_delete" property="isDelete" jdbcType="INTEGER" /> </resultMap> <sql id="Base_Column_List" > dict_name, dict_value, is_delete </sql> </mapper> src/main/resources/mapper/MonitorPointMapper.xml
@@ -115,4 +115,7 @@ ]]> </where> </select> <select id="selectOrganizationIds" parameterType="integer" resultType="integer"> call proc_organizationIds_GetByMonitorPointId(#{id,jdbcType=NUMERIC}) </select> </mapper>