9 files added
5 files modified
| | |
| | | this.pages = page.getPages();
|
| | | this.data= new ArrayList<>(list);
|
| | | this.size = page.size();
|
| | | }else{
|
| | | this.data = list;
|
| | | }
|
| | | }
|
| | |
|
New file |
| | |
| | | package com.moral.common.json; |
| | | |
| | | import com.alibaba.fastjson.serializer.ValueFilter; |
| | | |
| | | public class BooleanValueFilter implements ValueFilter{ |
| | | @Override |
| | | public Object process(Object object, String propertyName, Object propertyValue) { |
| | | if(propertyValue!=null&&propertyValue instanceof Boolean){ |
| | | return (Boolean)propertyValue?1:0; |
| | | } |
| | | return propertyValue; |
| | | } |
| | | } |
New file |
| | |
| | | package com.moral.common.util; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.alibaba.fastjson.serializer.SerializerFeature; |
| | | import com.moral.common.json.BooleanValueFilter; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.core.ValueOperations; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.Serializable; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Component |
| | | public class RedisUtils { |
| | | private static final String AlarmPrefix = "alarm_"; |
| | | public static String getAlarmKey(int organizationId){ |
| | | return AlarmPrefix+organizationId; |
| | | } |
| | | @Resource |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | /** |
| | | * 批量删除对应的value |
| | | * |
| | | * @param keys |
| | | */ |
| | | public void remove(final String... keys) { |
| | | for (String key : keys) { |
| | | remove(key); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 批量删除key |
| | | * |
| | | * @param pattern |
| | | */ |
| | | public void removePattern(final String pattern) { |
| | | Set<Serializable> keys = redisTemplate.keys(pattern); |
| | | if (keys.size() > 0) |
| | | redisTemplate.delete(keys); |
| | | } |
| | | |
| | | /** |
| | | * 删除对应的value |
| | | * |
| | | * @param key |
| | | */ |
| | | public void remove(final String key) { |
| | | if (exists(key)) { |
| | | redisTemplate.delete(key); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断缓存中是否有对应的value |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public boolean exists(final String key) { |
| | | return redisTemplate.hasKey(key); |
| | | } |
| | | |
| | | /** |
| | | * 读取缓存 |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public Object get(final String key) { |
| | | Object result = null; |
| | | ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); |
| | | result = operations.get(key); |
| | | return result; |
| | | } |
| | | /** |
| | | * 读取缓存,返回指定类型 |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public <T>T get(String key,Class<T> clazz) { |
| | | Object obj = get(key); |
| | | String json = obj!=null?obj.toString():null; |
| | | if(json!=null) { |
| | | return (T) parseObject(json,clazz); |
| | | } |
| | | return null; |
| | | } |
| | | /** |
| | | * 读取缓存,返回指定类型 |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public <T>T get(String key,TypeReference typeReference) { |
| | | Object obj = get(key); |
| | | String json = obj!=null?obj.toString():null; |
| | | if(json!=null) { |
| | | return (T) parseObject(json,typeReference); |
| | | } |
| | | return null; |
| | | } |
| | | private static <T>T parseObject(String key,Class<T> clazz) { |
| | | return (T) JSON.parseObject(key,clazz); |
| | | } |
| | | private static <T>T parseObject(String key,TypeReference typeReference) { |
| | | return (T) JSON.parseObject(key,typeReference); |
| | | } |
| | | public <T> List<T> getList(String pattern,TypeReference typeReference){ |
| | | List<String> jsonList = getList(pattern); |
| | | List<T> list = new ArrayList<T>(); |
| | | for(String json:jsonList) { |
| | | list.add((T)parseObject(json,typeReference)); |
| | | } |
| | | return list; |
| | | } |
| | | public <T> List<T> getList(String pattern,Class<T> clazz){ |
| | | List<String> jsonList = getList(pattern); |
| | | List<T> list = new ArrayList<>(); |
| | | for(String json:jsonList) { |
| | | list.add(parseObject(json,clazz)); |
| | | } |
| | | return list; |
| | | } |
| | | public List<String> getList(String pattern){ |
| | | Set<String> keys = keys(pattern); |
| | | List<String> list = new ArrayList<String>(); |
| | | for(String key:keys) { |
| | | Object obj = get(key); |
| | | String json = obj!=null?obj.toString():null; |
| | | if(json!=null) { |
| | | list.add(json); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | public Set<String> keys(String pattern) { |
| | | return redisTemplate.keys(pattern); |
| | | } |
| | | /** |
| | | * 写入缓存 |
| | | * |
| | | * @param key |
| | | * @param value |
| | | * @return |
| | | */ |
| | | public boolean set(final String key, Object value) { |
| | | boolean result = false; |
| | | try { |
| | | if(value!=null&&!(value instanceof String)) { |
| | | value = JSON.toJSONString(value,new BooleanValueFilter()); |
| | | } |
| | | ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); |
| | | operations.set(key, value); |
| | | result = true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 写入缓存 |
| | | * |
| | | * @param key |
| | | * @param value |
| | | * @return |
| | | */ |
| | | public boolean set(final String key, Object value, Long expireTime) { |
| | | boolean result = false; |
| | | try { |
| | | if(value!=null&&!(value instanceof String)) { |
| | | value = JSON.toJSONString(value,new BooleanValueFilter()); |
| | | } |
| | | ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); |
| | | operations.set(key, value); |
| | | redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); |
| | | result = true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | } |
New file |
| | |
| | | package com.moral.controller; |
| | | |
| | | |
| | | import com.moral.common.bean.ResultBean; |
| | | import com.moral.common.util.RedisUtil; |
| | | import com.moral.entity.alarm.AlarmConfig; |
| | | import com.moral.service.AlarmConfigService; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | @RestController |
| | | @RequestMapping("alarm-config") |
| | | @CrossOrigin(origins = "*", maxAge = 3600) |
| | | public class AlarmConfigController { |
| | | @Resource |
| | | AlarmConfigService alarmConfigService; |
| | | @GetMapping("get-by-oid") |
| | | public ResultBean<AlarmConfig> getAlarmConfigByOrganizationId(int organizationId){ |
| | | AlarmConfig alarmConfig = alarmConfigService.SelectByOrganizationId(organizationId); |
| | | ResultBean resultBean = new ResultBean(); |
| | | resultBean.setCode(ResultBean.SUCCESS); |
| | | resultBean.setData(alarmConfig); |
| | | return resultBean; |
| | | } |
| | | @PostMapping("add-or-modify") |
| | | public ResultBean addOrModify(@RequestBody AlarmConfig alarmConfig){ |
| | | alarmConfigService.addOrModify(alarmConfig); |
| | | ResultBean resultBean = new ResultBean(ResultBean.SUCCESS); |
| | | return resultBean; |
| | | } |
| | | } |
| | |
| | | */ |
| | | @Data |
| | | public class AlarmMode { |
| | | private int enable; |
| | | private boolean enable; |
| | | private List<AlarmStyle> level1; |
| | | private List<AlarmStyle> level2; |
| | | private List<AlarmStyle> level3; |
| | |
| | | /* |
| | | 是否启用警报1启用,0不启用 |
| | | */ |
| | | private int enable; |
| | | private boolean enable; |
| | | /* |
| | | 递增阀值,例如: [100,200,300] |
| | | */ |
| | |
| | | |
| | | public enum AlarmStyle { |
| | | //微信,邮件,短信,语音 |
| | | weixin,email,sms,vioce |
| | | weixin,email,sms,voice |
| | | } |
New file |
| | |
| | | package com.moral.mapper; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import com.moral.entity.alarm.AlarmConfig; |
| | | |
| | | public interface AlarmConfigMapper { |
| | | int deleteByPrimaryKey(Integer id); |
| | | |
| | | int insert(AlarmConfig record); |
| | | |
| | | int insertSelective(AlarmConfig record); |
| | | |
| | | AlarmConfig selectByPrimaryKey(Integer id); |
| | | //根据组织id获取三级警报阀值 |
| | | AlarmConfig selectByOrganizationId(Integer organizationId); |
| | | |
| | | int updateByPrimaryKeySelective(AlarmConfig record); |
| | | |
| | | int updateByPrimaryKey(AlarmConfig record); |
| | | } |
New file |
| | |
| | | package com.moral.mapper.type; |
| | | import java.sql.CallableStatement; |
| | | import java.sql.PreparedStatement; |
| | | import java.sql.ResultSet; |
| | | import java.sql.SQLException; |
| | | |
| | | import com.moral.common.json.BooleanValueFilter; |
| | | import com.moral.entity.alarm.AlarmConfig; |
| | | import com.moral.entity.alarm.AlarmConfigValue; |
| | | import org.apache.ibatis.type.BaseTypeHandler; |
| | | import org.apache.ibatis.type.JdbcType; |
| | | import org.apache.ibatis.type.MappedJdbcTypes; |
| | | import org.apache.ibatis.type.MappedTypes; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | |
| | | /** |
| | | * @author fengxiang |
| | | * @Time:2017年11月29日 上午9:38:51 |
| | | * @version 1.0 |
| | | */ |
| | | @MappedJdbcTypes({JdbcType.OTHER}) |
| | | @MappedTypes({AlarmConfigValue.class}) |
| | | public class AlarmConfigValueHandle extends BaseTypeHandler<AlarmConfigValue> { |
| | | |
| | | @Override |
| | | public AlarmConfigValue getNullableResult(ResultSet resultSet, String s) throws SQLException { |
| | | return JSON.parseObject(resultSet.getString(s),AlarmConfigValue.class); |
| | | } |
| | | |
| | | @Override |
| | | public AlarmConfigValue getNullableResult(ResultSet resultSet, int i) throws SQLException { |
| | | return JSON.parseObject(resultSet.getString(i),AlarmConfigValue.class); |
| | | } |
| | | |
| | | @Override |
| | | public AlarmConfigValue getNullableResult(CallableStatement cStatement, int i) throws SQLException { |
| | | return JSON.parseObject(cStatement.getString(i),AlarmConfigValue.class); |
| | | } |
| | | |
| | | @Override |
| | | public void setNonNullParameter(PreparedStatement preparedStatement, int i, AlarmConfigValue alarmConfigValue, JdbcType jdbcType) |
| | | throws SQLException { |
| | | preparedStatement.setString(i,JSON.toJSONString(alarmConfigValue,new BooleanValueFilter())); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.moral.service; |
| | | |
| | | import com.moral.entity.alarm.AlarmConfig; |
| | | |
| | | public interface AlarmConfigService { |
| | | public AlarmConfig SelectByOrganizationId(int organizationId); |
| | | |
| | | void addOrModify(AlarmConfig alarmConfig); |
| | | } |
New file |
| | |
| | | package com.moral.service.impl; |
| | | |
| | | import com.moral.common.bean.Constants; |
| | | import com.moral.common.util.RedisUtils; |
| | | import com.moral.entity.alarm.AlarmConfig; |
| | | import com.moral.entity.alarm.AlarmConfigValue; |
| | | import com.moral.entity.alarm.AlarmMode; |
| | | import com.moral.entity.alarm.AlarmSensorLevel; |
| | | import com.moral.mapper.AlarmConfigMapper; |
| | | import com.moral.service.AlarmConfigService; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Service |
| | | public class AlarmConfigServiceImpl implements AlarmConfigService{ |
| | | @Resource |
| | | RedisUtils redisUtils; |
| | | @Resource |
| | | AlarmConfigMapper alarmConfigMapper; |
| | | @Override |
| | | public AlarmConfig SelectByOrganizationId(int organizationId) { |
| | | return alarmConfigMapper.selectByOrganizationId(organizationId); |
| | | } |
| | | |
| | | @Override |
| | | public void addOrModify(AlarmConfig alarmConfig) { |
| | | try{ |
| | | if(alarmConfig.getId()==null){ |
| | | alarmConfig.setCreateTime(new Date()); |
| | | alarmConfig.setUpdateTime(new Date()); |
| | | alarmConfigMapper.insertSelective(alarmConfig); |
| | | }else{ |
| | | alarmConfig.setUpdateTime(new Date()); |
| | | alarmConfigMapper.updateByPrimaryKeySelective(alarmConfig); |
| | | } |
| | | String key = RedisUtils.getAlarmKey(alarmConfig.getOrganizationId()); |
| | | redisUtils.set(key,simplifyAlarmConfigValue(alarmConfig.getValue())); |
| | | } |
| | | catch (Exception ex){ |
| | | throw ex; |
| | | } |
| | | } |
| | | public AlarmConfigValue simplifyAlarmConfigValue(AlarmConfigValue value){ |
| | | Map<String,AlarmSensorLevel> alarmSensorLevelMap = value.getAlarmLevels(); |
| | | List<String> cleanKeys = new ArrayList<>(); |
| | | for(String key:alarmSensorLevelMap.keySet()){ |
| | | AlarmSensorLevel alarmSensorLevel = alarmSensorLevelMap.get(key); |
| | | if(!alarmSensorLevel.isEnable()){ |
| | | cleanKeys.add(key); |
| | | }else{ |
| | | List<Float> increments = alarmSensorLevel.getIncrement(); |
| | | if(increments!=null){ |
| | | boolean isIncrement = increments.size()<3|| |
| | | (increments.get(0)==0&&increments.get(1)==0&&increments.get(2)==0); |
| | | if(isIncrement){ |
| | | alarmSensorLevel.setIncrement(null); |
| | | } |
| | | } |
| | | List<Float> degressions = alarmSensorLevel.getDegression(); |
| | | if(degressions!=null){ |
| | | boolean isDegression = degressions.size()<3|| |
| | | (degressions.get(0)==0&°ressions.get(1)==0&°ressions.get(2)==0); |
| | | if(isDegression){ |
| | | alarmSensorLevel.setDegression(null); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | for (String key:cleanKeys){ |
| | | alarmSensorLevelMap.remove(key); |
| | | } |
| | | AlarmMode alarmMode = value.getAlarmMode(); |
| | | if(!alarmMode.isEnable()){ |
| | | alarmMode.setLevel1(null); |
| | | alarmMode.setLevel2(null); |
| | | alarmMode.setLevel3(null); |
| | | }else{ |
| | | if(alarmMode.getLevel1()!=null&&alarmMode.getLevel1().isEmpty()){ |
| | | alarmMode.setLevel1(null); |
| | | } |
| | | if(alarmMode.getLevel2()!=null&&alarmMode.getLevel2().isEmpty()){ |
| | | alarmMode.setLevel2(null); |
| | | } |
| | | if(alarmMode.getLevel3()!=null&&alarmMode.getLevel3().isEmpty()){ |
| | | alarmMode.setLevel3(null); |
| | | } |
| | | } |
| | | return value; |
| | | } |
| | | } |
| | |
| | | |
| | | mybatis: |
| | | mapper-locations: classpath*:/mapper/*Mapper.xml |
| | | type-aliases-packageS: com.moral.entity |
| | | config-location: classpath:/mapper/mybatis-config.xml |
New file |
| | |
| | | <?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.AlarmConfigMapper"> |
| | | <resultMap id="BaseResultMap" type="com.moral.entity.alarm.AlarmConfig"> |
| | | <id column="id" jdbcType="INTEGER" property="id" /> |
| | | <result column="organization_id" jdbcType="INTEGER" property="organizationId" /> |
| | | <result column="value" jdbcType="OTHER" property="value" typeHandler="AlarmConfigValueHandle" /> |
| | | <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> |
| | | <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" /> |
| | | </resultMap> |
| | | <sql id="Base_Column_List"> |
| | | id, organization_id, value, create_time, update_time |
| | | </sql> |
| | | <select id="selectByOrganizationId" parameterType="java.lang.Integer" resultMap="BaseResultMap"> |
| | | select |
| | | <include refid="Base_Column_List" /> |
| | | from alarm_config |
| | | where organization_id = #{organizationId,jdbcType=INTEGER} |
| | | limit 0,1 |
| | | </select> |
| | | <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> |
| | | select |
| | | <include refid="Base_Column_List" /> |
| | | from alarm_config |
| | | where id = #{id,jdbcType=INTEGER} |
| | | </select> |
| | | <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> |
| | | delete from alarm_config |
| | | where id = #{id,jdbcType=INTEGER} |
| | | </delete> |
| | | <insert id="insert" parameterType="com.moral.entity.alarm.AlarmConfig"> |
| | | insert into alarm_config (id, organization_id, value, |
| | | create_time, update_time) |
| | | values (#{id,jdbcType=INTEGER}, #{organizationId,jdbcType=INTEGER}, #{value,jdbcType=OTHER,typeHandler=AlarmConfigValueHandle}, |
| | | #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}) |
| | | </insert> |
| | | <insert id="insertSelective" parameterType="com.moral.entity.alarm.AlarmConfig"> |
| | | insert into alarm_config |
| | | <trim prefix="(" suffix=")" suffixOverrides=","> |
| | | <if test="id != null"> |
| | | id, |
| | | </if> |
| | | <if test="organizationId != null"> |
| | | organization_id, |
| | | </if> |
| | | <if test="value != null"> |
| | | value, |
| | | </if> |
| | | <if test="createTime != null"> |
| | | create_time, |
| | | </if> |
| | | <if test="updateTime != null"> |
| | | update_time, |
| | | </if> |
| | | </trim> |
| | | <trim prefix="values (" suffix=")" suffixOverrides=","> |
| | | <if test="id != null"> |
| | | #{id,jdbcType=INTEGER}, |
| | | </if> |
| | | <if test="organizationId != null"> |
| | | #{organizationId,jdbcType=INTEGER}, |
| | | </if> |
| | | <if test="value != null"> |
| | | #{value,jdbcType=OTHER,typeHandler=AlarmConfigValueHandle}, |
| | | </if> |
| | | <if test="createTime != null"> |
| | | #{createTime,jdbcType=TIMESTAMP}, |
| | | </if> |
| | | <if test="updateTime != null"> |
| | | #{updateTime,jdbcType=TIMESTAMP}, |
| | | </if> |
| | | </trim> |
| | | </insert> |
| | | <update id="updateByPrimaryKeySelective" parameterType="com.moral.entity.alarm.AlarmConfig"> |
| | | update alarm_config |
| | | <set> |
| | | <if test="organizationId != null"> |
| | | organization_id = #{organizationId,jdbcType=INTEGER}, |
| | | </if> |
| | | <if test="value != null"> |
| | | value = #{value,jdbcType=OTHER,typeHandler=AlarmConfigValueHandle}, |
| | | </if> |
| | | <if test="createTime != null"> |
| | | create_time = #{createTime,jdbcType=TIMESTAMP}, |
| | | </if> |
| | | <if test="updateTime != null"> |
| | | update_time = #{updateTime,jdbcType=TIMESTAMP}, |
| | | </if> |
| | | </set> |
| | | where id = #{id,jdbcType=INTEGER} |
| | | </update> |
| | | <update id="updateByPrimaryKey" parameterType="com.moral.entity.alarm.AlarmConfig"> |
| | | update alarm_config |
| | | set organization_id = #{organizationId,jdbcType=INTEGER}, |
| | | value = #{value,jdbcType=OTHER,typeHandler=AlarmConfigValueHandle}, |
| | | create_time = #{createTime,jdbcType=TIMESTAMP}, |
| | | update_time = #{updateTime,jdbcType=TIMESTAMP} |
| | | where id = #{id,jdbcType=INTEGER} |
| | | </update> |
| | | </mapper> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8" ?> |
| | | <!DOCTYPE configuration |
| | | PUBLIC "-//mybatis.org//DTD Config 3.0//EN" |
| | | "http://mybatis.org/dtd/mybatis-3-config.dtd"> |
| | | |
| | | <configuration> |
| | | <typeAliases> |
| | | <typeAlias type="com.moral.mapper.type.AlarmConfigValueHandle" alias="AlarmConfigValueHandle"/> |
| | | <package name="com.moral.entity" /> |
| | | </typeAliases> |
| | | </configuration> |