jinpengyong
2021-06-23 6f12948893fcc846dfb32aa861c473a842edcd56
Merge remote-tracking branch 'origin/dev' into dev_

# Conflicts:
# screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
6 files modified
181 ■■■■ changed files
screen-common/src/main/java/com/moral/constant/ResponseCodeEnum.java 3 ●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/controller/TestController.java 5 ●●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/entity/VersionSensorUnit.java 17 ●●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java 79 ●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/service/impl/VersionServiceImpl.java 72 ●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/util/CacheUtils.java 5 ●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/constant/ResponseCodeEnum.java
@@ -59,7 +59,8 @@
    ORGANIZATION_EXIST_MONITORPOINT(-44,"组织存在站点,无法删除"),
    CHILDREN_ORGANIZATION_EXIST_MONITORPOINT(-45,"子组织存在站点,无法删除"),
    DEVICE_IS_NULL(-46, "设备不存在"),
    TARGET_IS_NULL(-47, "操作目标不存在")
    TARGET_IS_NULL(-47, "操作目标不存在"),
    VERSION_USED(-48,"型号被使用,无法删除")
    ;
    private final Integer code;
    private final String  msg;
screen-manage/src/main/java/com/moral/api/controller/TestController.java
@@ -182,9 +182,4 @@
        writer.close();
        fis.close();
    }
    @GetMapping("testCacheUtils")
    public void testCacheUtils(){
        CacheUtils.flushDeviceAlarmUnit();
    }
}
screen-manage/src/main/java/com/moral/api/entity/VersionSensorUnit.java
@@ -7,6 +7,7 @@
import java.time.LocalDateTime;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -77,6 +78,22 @@
     */
    private String isDelete;
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        VersionSensorUnit that = (VersionSensorUnit) o;
        return Objects.equals(sensorCode, that.sensorCode) &&
                Objects.equals(unitKey, that.unitKey) &&
                Objects.equals(upper, that.upper) &&
                Objects.equals(lower, that.lower);
    }
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), sensorCode, unitKey, upper, lower);
    }
    @Override
    protected Serializable pkVal() {
screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -12,6 +12,7 @@
import com.moral.api.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.CacheUtils;
import com.moral.api.util.AdjustDataUtils;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
@@ -91,8 +92,7 @@
     * 从redis获取设备信息
     * */
    private Map<String, Object> getDeviceInfoFromRedis(String mac) {
        Map<String, Object> deviceInfo = (Map<String, Object>) redisTemplate.opsForValue().get(getDeviceKey(mac));
        return deviceInfo;
        return (Map<String, Object>) redisTemplate.opsForValue().get(getDeviceKey(mac));
    }
    /*
@@ -116,13 +116,6 @@
        return keysConnect(RedisConstants.DEVICE, mac);
    }
    /*
     * 从redis获取报警级别
     * */
    private Map<String, Object> getOrgAlarmConfigFromRedis(String orId) {
        return null;
    }
    //redis key前缀
    private String keysConnect(String... keys) {
        StringBuilder key = new StringBuilder(keys[0]);
@@ -141,13 +134,15 @@
        deviceMapper.insert(device);
        Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId());
        //维护组织型号关系表
        insertOrganizationUnitAlarm(orgId, device.getDeviceVersionId());
        insertOrganizationUnitAlarm(orgId,device.getDeviceVersionId());
        //新增设备信息存入redis
        String mac = device.getMac();
        //从redis中删除设备信息
        delDeviceInfoFromRedis(mac);
        //设备信息存入redis
        setDeviceInfoToRedis(mac, deviceInfo);
        //刷新deviceInfo缓存
        CacheUtils.flushDeviceAlarmInfo();
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
@@ -165,6 +160,12 @@
        String mac = device.getMac();
        //从redis中删除设备信息
        delDeviceInfoFromRedis(mac);
        //维护组织型号关系表
        Integer versionId = device.getDeviceVersionId();
        Integer orgId = device.getOrganizationId();
        deleteOrganizationUnitAlarm(orgId,versionId);
        //刷新deviceInfo缓存
        CacheUtils.flushDeviceAlarmInfo();
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
@@ -178,7 +179,7 @@
        Integer deviceId = device.getId();
        Device oldDevice = deviceMapper.selectById(deviceId);
        //判断是否更新了站点,如果更新了站点则查询对应站点的组织id进行更新
        if (!ObjectUtils.isEmpty(device.getMonitorPointId())) {
        if(!ObjectUtils.isEmpty(device.getMonitorPointId())){
            MonitorPoint monitorPoint = monitorPointMapper.selectById(device.getMonitorPointId());
            device.setOrganizationId(monitorPoint.getOrganizationId());
        }
@@ -186,19 +187,21 @@
        Device updateDevice = deviceMapper.selectById(deviceId);
        String mac = updateDevice.getMac();
        //维护组织型号关系表
        Integer oldOrgId = oldDevice.getOrganizationId();
        Integer newOrgId = updateDevice.getOrganizationId();
        Integer oldVersionId = oldDevice.getDeviceVersionId();
        Integer newVersionId = updateDevice.getDeviceVersionId();
        if (!oldOrgId.equals(newOrgId) || !oldVersionId.equals(newVersionId)) {
            deleteOrganizationUnitAlarm(oldOrgId, oldVersionId);
            insertOrganizationUnitAlarm(newOrgId, newVersionId);
        }
       Integer oldOrgId = oldDevice.getOrganizationId();
       Integer newOrgId = updateDevice.getOrganizationId();
       Integer oldVersionId = oldDevice.getDeviceVersionId();
       Integer newVersionId = updateDevice.getDeviceVersionId();
       if(!oldOrgId.equals(newOrgId)||!oldVersionId.equals(newVersionId)){
           deleteOrganizationUnitAlarm(oldOrgId,oldVersionId);
           insertOrganizationUnitAlarm(newOrgId,newVersionId);
       }
        //从redis中删除设备信息
        delDeviceInfoFromRedis(mac);
        Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId);
        //设备信息存入redis
        setDeviceInfoToRedis(mac, deviceInfo);
        //刷新deviceInfo缓存
        CacheUtils.flushDeviceAlarmInfo();
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
@@ -490,18 +493,18 @@
        return state;
    }
    private void insertOrganizationUnitAlarm(Integer orgId, Integer versionId) {
    private void insertOrganizationUnitAlarm(Integer orgId,Integer versionId){
        QueryWrapper<OrganizationUnitAlarm> queryOrganizationVersionWrapper = new QueryWrapper<>();
        queryOrganizationVersionWrapper.eq("organization_id", orgId);
        queryOrganizationVersionWrapper.eq("version_id", versionId);
        queryOrganizationVersionWrapper.eq("is_delete", Constants.NOT_DELETE);
        queryOrganizationVersionWrapper.eq("organization_id",orgId);
        queryOrganizationVersionWrapper.eq("version_id",versionId);
        queryOrganizationVersionWrapper.eq("is_delete",Constants.NOT_DELETE);
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryOrganizationVersionWrapper);
        if (ObjectUtils.isEmpty(organizationUnitAlarms)) {
            QueryWrapper<VersionSensorUnit> queryVersionSensorUnitWrapper = new QueryWrapper<>();
            queryVersionSensorUnitWrapper.eq("version_id", versionId);
            queryVersionSensorUnitWrapper.eq("is_delete", Constants.NOT_DELETE);
        if(ObjectUtils.isEmpty(organizationUnitAlarms)){
            QueryWrapper<VersionSensorUnit> queryVersionSensorUnitWrapper =new QueryWrapper<>();
            queryVersionSensorUnitWrapper.eq("version_id",versionId);
            queryVersionSensorUnitWrapper.eq("is_delete",Constants.NOT_DELETE);
            List<VersionSensorUnit> versionSensorUnits = versionSensorUnitMapper.selectList(queryVersionSensorUnitWrapper);
            if (!ObjectUtils.isEmpty(versionSensorUnits)) {
            if(!ObjectUtils.isEmpty(versionSensorUnits)){
                for (VersionSensorUnit versionSensorUnit : versionSensorUnits) {
                    OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm();
                    organizationUnitAlarm.setOrganizationId(orgId);
@@ -515,19 +518,19 @@
        }
    }
    private void deleteOrganizationUnitAlarm(Integer orgId, Integer versionId) {
    private void deleteOrganizationUnitAlarm(Integer orgId,Integer versionId){
        QueryWrapper<Device> queryOrganizationVersionWrapper = new QueryWrapper<>();
        queryOrganizationVersionWrapper.eq("organization_id", orgId);
        queryOrganizationVersionWrapper.eq("device_version_id", versionId);
        queryOrganizationVersionWrapper.eq("is_delete", Constants.NOT_DELETE);
        queryOrganizationVersionWrapper.eq("organization_id",orgId);
        queryOrganizationVersionWrapper.eq("device_version_id",versionId);
        queryOrganizationVersionWrapper.eq("is_delete",Constants.NOT_DELETE);
        List<Device> devices = deviceMapper.selectList(queryOrganizationVersionWrapper);
        if (ObjectUtils.isEmpty(devices)) {//如果为空,则组织没有该型号的设备了。
        if(ObjectUtils.isEmpty(devices)){//如果为空,则组织没有该型号的设备了。
            UpdateWrapper deleteWrapper = new UpdateWrapper();
            deleteWrapper.eq("organization_id", orgId);
            deleteWrapper.eq("version_id", versionId);
            deleteWrapper.eq("is_delete", Constants.NOT_DELETE);
            deleteWrapper.set("is_delete", Constants.DELETE);
            organizationUnitAlarmMapper.update(null, deleteWrapper);
            deleteWrapper.eq("organization_id",orgId);
            deleteWrapper.eq("version_id",versionId);
            deleteWrapper.eq("is_delete",Constants.NOT_DELETE);
            deleteWrapper.set("is_delete",Constants.DELETE);
            organizationUnitAlarmMapper.update(null,deleteWrapper);
        }
    }
}
screen-manage/src/main/java/com/moral/api/service/impl/VersionServiceImpl.java
@@ -165,6 +165,16 @@
        VersionDTO dto = new VersionDTO();
        //取参
        Integer id = form.getId();
        //查询型号是否被使用,如果被使用则无法删除
        QueryWrapper<OrganizationUnitAlarm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",Constants.NOT_DELETE);
        queryWrapper.eq("version_id",id);
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryWrapper);
        if(!ObjectUtils.isEmpty(organizationUnitAlarms)){
            dto.setCode(ResponseCodeEnum.VERSION_USED.getCode());
            dto.setMsg(ResponseCodeEnum.VERSION_USED.getMsg());
            return dto;
        }
        //查询要删除的版本用于记录日志
        Version oldVersion = versionMapper.selectById(id);
        //执行删除
@@ -191,21 +201,67 @@
        //取参
        List<VersionSensorUnit> sensorUnits = form.getSensorUnits();
        Integer versionId = form.getVersionId();
        //删除之前分配的单位和因子
        UpdateWrapper deleteWrapper = new UpdateWrapper();
        deleteWrapper.eq("version_id", versionId);
        deleteWrapper.set("is_delete", Constants.DELETE);
        versionSensorUnitMapper.update(null, deleteWrapper);
        //将前端传来的id转为code
        for (VersionSensorUnit sensorUnit : sensorUnits) {
            Sensor sensor = sensorMapper.selectById(sensorUnit.getSensorId());
            sensorUnit.setSensorCode(sensor.getCode());
        }
        //添加新分配的单位和因子
        for (VersionSensorUnit sensorUnit : sensorUnits) {
            sensorUnit.setVersionId(versionId);
        //查询之前分配的单位和因子
        QueryWrapper<VersionSensorUnit> queryOldWrapper = new QueryWrapper<>();
        queryOldWrapper.eq("version_id",versionId);
        queryOldWrapper.eq("is_delete",Constants.NOT_DELETE);
        List<VersionSensorUnit> oldSensorUnits = versionSensorUnitMapper.selectList(queryOldWrapper);
        //判断出变动的因子(要插入和要删除的)
        List<VersionSensorUnit> insertList = new ArrayList<>();
        List<VersionSensorUnit> deleteList = new ArrayList<>();
        sensorUnits.forEach(value->{
            value.setVersionId(versionId);
            if(!oldSensorUnits.contains(value))
                insertList.add(value);
        });
        oldSensorUnits.forEach(value->{
            if(!sensorUnits.contains(value))
                deleteList.add(value);
        });
        //删除记录
        UpdateWrapper deleteWrapper = new UpdateWrapper();
        List<Integer> deleteIds = new ArrayList<>();
        deleteList.forEach(value->deleteIds.add(value.getId()));
        deleteWrapper.in("id",deleteIds);
        deleteWrapper.set("is_delete", Constants.DELETE);
        versionSensorUnitMapper.update(null, deleteWrapper);
        //添加记录
        for (VersionSensorUnit sensorUnit : insertList) {
            versionSensorUnitMapper.insert(sensorUnit);
        }
        //维护组织型号关系表
        QueryWrapper<OrganizationUnitAlarm> queryOrgUnitAlarmWrapper = new QueryWrapper<>();
        queryOrgUnitAlarmWrapper.select("distinct organization_id");
        queryOrgUnitAlarmWrapper.eq("version_id",versionId);
        queryOrgUnitAlarmWrapper.eq("is_delete",Constants.NOT_DELETE);
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryOrgUnitAlarmWrapper);
        List<Integer> organizationIds = new ArrayList<>();
        organizationUnitAlarms.forEach(value->organizationIds.add(value.getOrganizationId()));
        for (Integer organizationId : organizationIds) {
            //删除因子
            for (VersionSensorUnit versionSensorUnit : deleteList) {
                UpdateWrapper deleteOrganizationUnitAlarmWrapper = new UpdateWrapper();
                deleteOrganizationUnitAlarmWrapper.eq("orgniazation_id",organizationId);
                deleteOrganizationUnitAlarmWrapper.eq("version_id",versionId);
                deleteOrganizationUnitAlarmWrapper.eq("sensor_code",versionSensorUnit.getSensorCode());
                organizationUnitAlarmMapper.update(null,deleteOrganizationUnitAlarmWrapper);
            }
            //新增因子
            for (VersionSensorUnit versionSensorUnit : insertList) {
                OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm();
                organizationUnitAlarm.setOrganizationId(organizationId);
                organizationUnitAlarm.setVersionId(versionId);
                organizationUnitAlarm.setSensorCode(versionSensorUnit.getSensorCode());
                organizationUnitAlarm.setUnitKey(versionSensorUnit.getUnitKey());
                organizationUnitAlarm.setShowUnitKey(versionSensorUnit.getUnitKey());
                organizationUnitAlarmMapper.insert(organizationUnitAlarm);
            }
        }
        //封装返回结果
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
screen-manage/src/main/java/com/moral/api/util/CacheUtils.java
@@ -71,7 +71,10 @@
        redisTemplate.delete(RedisConstants.DICT_TYPE_KEY);
    }
    public static void flushDeviceAlarmUnit() {
    public static void flushDeviceAlarmInfo() {
        //删除缓存
        redisTemplate.delete(RedisConstants.DEVICE_INFO);
        //重新添加缓存
        List<Device> devices = deviceMapper.queryDeviceUnitAlarmInfo();
        Map<String, SysDictData> unitMap = sysDictDataService.getDictDatasByType("unit");
        //查询对应的单位名称以及转换公式