lizijie
2021-08-17 cf311a43c3a6b941738de9ca5735e1acca451fc2
特殊设备删除接口
3 files modified
270 ■■■■■ changed files
screen-manage/src/main/java/com/moral/api/controller/SpecialDeviceController.java 79 ●●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/service/SpecialDeviceService.java 7 ●●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/service/impl/SpecialDeviceServiceImpl.java 184 ●●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/controller/SpecialDeviceController.java
@@ -1,7 +1,17 @@
package com.moral.api.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.moral.api.entity.Organization;
import com.moral.api.entity.SpecialDevice;
import com.moral.api.entity.SysDictData;
import com.moral.api.entity.Version;
import com.moral.api.mapper.SysDictDataMapper;
import com.moral.api.service.OrganizationService;
import com.moral.api.service.SpecialDeviceService;
import com.moral.api.service.VersionService;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.WebUtils;
import io.swagger.annotations.Api;
@@ -21,6 +31,15 @@
    @Resource
    private SpecialDeviceService specialDeviceService;
    @Resource
    private OrganizationService organizationService;
    @Resource
    private VersionService versionService;
    @Resource
    private SysDictDataMapper sysDictDataMapper;
    @RequestMapping(value = "getSpecialDeviceByCondition", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage getSpecialDeviceByCondition(HttpServletRequest request) {
@@ -35,7 +54,6 @@
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage insert(@RequestBody SpecialDevice specialDevice){
        System.out.println(specialDevice);
        Map<String,Object> resultMap = specialDeviceService.insert(specialDevice);
        String msg = resultMap.get("msg").toString();
        int code = Integer.parseInt(resultMap.get("code").toString());
@@ -44,4 +62,63 @@
        }
        return ResultMessage.fail(Integer.parseInt(resultMap.get("code").toString()),resultMap.get("msg").toString());
    }
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage update (@RequestBody SpecialDevice specialDevice){
        if (specialDevice.getId() == null) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        if (specialDevice.getMac() != null) {
            //判断mac是否已存在
            QueryWrapper<SpecialDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mac", specialDevice.getMac());
            queryWrapper.eq("is_delete",Constants.NOT_DELETE);
            if (specialDeviceService.getOne(queryWrapper) != null) {
                return ResultMessage.fail(ResponseCodeEnum.MAC_IS_EXIST.getCode(), ResponseCodeEnum.MAC_IS_EXIST.getMsg());
            }
        }
        if (specialDevice.getOrganizationId() != null) {
            //判断组织是否已存在
            QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", specialDevice.getOrganizationId());
            queryWrapper.eq("is_delete",Constants.NOT_DELETE);
            if (ObjectUtils.isEmpty(organizationService.getOne(queryWrapper))) {
                return ResultMessage.fail(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getCode(), ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
            }
        }
        if (specialDevice.getDeviceVersionId() != null) {
            //判断型号是否已存在
            QueryWrapper<Version> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", specialDevice.getDeviceVersionId());
            queryWrapper.eq("is_delete",Constants.NOT_DELETE);
            if (ObjectUtils.isEmpty(versionService.getOne(queryWrapper))) {
                return ResultMessage.fail(ResponseCodeEnum.VERSION_NOT_EXIST.getCode(), ResponseCodeEnum.VERSION_NOT_EXIST.getMsg());
            }
        }
        if (specialDevice.getSpecialType() != null) {
            //判断组织是否已存在
            QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_type_id", 27);
            queryWrapper.eq("dataKey", specialDevice.getSpecialType());
            queryWrapper.eq("is_delete",Constants.NOT_DELETE);
            if (ObjectUtils.isEmpty(sysDictDataMapper.selectOne(queryWrapper))) {
                return ResultMessage.fail(ResponseCodeEnum.DICTDATA_KEY_NOT_EXIST.getCode(), ResponseCodeEnum.DICTDATA_KEY_NOT_EXIST.getMsg());
            }
        }
        specialDeviceService.update(specialDevice);
        return ResultMessage.ok();
    }
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(@RequestBody Map map){
        if (map.get("id") == null){
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        specialDeviceService.delete(Integer.parseInt(map.get("id").toString()));
        return ResultMessage.ok();
    }
}
screen-manage/src/main/java/com/moral/api/service/SpecialDeviceService.java
@@ -22,6 +22,13 @@
    @Transactional
    Map<String,Object> insert(SpecialDevice specialDevice);
    @Transactional
    void update(SpecialDevice specialDevice);
    @Transactional
    void delete(Integer specialDeviceId);
    //根据设备id查询设备组织,维护人等信息
    Map<String, Object> selectDeviceInfoById(Integer deviceId);
}
screen-manage/src/main/java/com/moral/api/service/impl/SpecialDeviceServiceImpl.java
@@ -2,6 +2,7 @@
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.*;
@@ -24,6 +25,9 @@
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
@@ -258,12 +262,46 @@
        BeanUtils.copyProperties(specialDevice,insertSpecialDeviceHistory);
        if (ObjectUtils.isEmpty(specialDeviceHistory)){
            specialDeviceHistoryMapper.insert(insertSpecialDeviceHistory);
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("添加了特殊设备历史数据:").append(insertSpecialDeviceHistory.getName()).append(":").append("mac:").append(insertSpecialDeviceHistory.getMac());
            logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
        }else {
            /*UpdateWrapper<SpecialDeviceHistory> wrapper_insertSpecialDeviceHistory = new UpdateWrapper<>();
            wrapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
            wrapper_specialDeviceHistory.eq("mac",mac);
            wrapper_specialDeviceHistory.eq("organization_id",organizationId);*/
            specialDeviceHistoryMapper.update(insertSpecialDeviceHistory,wrapper_specialDeviceHistory);
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("修改了特殊设备:").append(specialDeviceHistory.getId()).append(":");
            Field[] fields = SpecialDevice.class.getDeclaredFields();
            for (Field field : fields) {
                if (field.getName().equals("id")) {
                    continue;
                }
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                String fieldName = field.getName();
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor(fieldName, SpecialDeviceHistory.class);
                    Method method = pd.getReadMethod();
                    Object o1 = method.invoke(specialDeviceHistory);
                    Object o2 = method.invoke(insertSpecialDeviceHistory);
                    if (o2 != null) {
                        content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(":");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
        }
        Map<String, Object> deviceInfo = selectDeviceInfoById(specialDevice.getId());
        //维护组织型号关系表
@@ -284,6 +322,136 @@
        resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
    @Transactional
    @Override
    public void update(SpecialDevice specialDevice) {
        Integer specialDeviceId = specialDevice.getId();
        SpecialDevice oldSpecialDevice = specialDeviceMapper.selectById(specialDeviceId);
        specialDeviceMapper.updateById(specialDevice);
        SpecialDevice updateSpecialDevice = specialDeviceMapper.selectById(specialDeviceId);
        String oleMac = oldSpecialDevice.getMac();
        //维护组织型号关系表
        Integer oldOrgId = oldSpecialDevice.getOrganizationId();
        Integer newOrgId = updateSpecialDevice.getOrganizationId();
        Integer oldVersionId = oldSpecialDevice.getDeviceVersionId();
        Integer newVersionId = updateSpecialDevice.getDeviceVersionId();
        if (!oldOrgId.equals(newOrgId) || !oldVersionId.equals(newVersionId)) {
            deleteOrganizationUnitAlarm(oldOrgId, oldVersionId);
            insertOrganizationUnitAlarm(newOrgId, newVersionId);
        }
        //从redis中删除设备信息
        delDeviceInfoFromRedis(oleMac);
        Map<String, Object> deviceInfo = selectDeviceInfoById(specialDeviceId);
        //设备信息存入redis
        setDeviceInfoToRedis(updateSpecialDevice.getMac(), deviceInfo);
        //刷新deviceInfo缓存
        CacheUtils.refreshSpecialDeviceAlarmInfo();
        QueryWrapper<SpecialDeviceHistory> wrapper_specialDeviceHistory = new QueryWrapper<>();
        wrapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
        wrapper_specialDeviceHistory.eq("mac",updateSpecialDevice.getMac());
        wrapper_specialDeviceHistory.eq("organization_id",updateSpecialDevice.getOrganizationId());
        SpecialDeviceHistory specialDeviceHistory = specialDeviceHistoryMapper.selectOne(wrapper_specialDeviceHistory);
        SpecialDeviceHistory updateSpecialDeviceHistory = new SpecialDeviceHistory();
        BeanUtils.copyProperties(updateSpecialDevice,updateSpecialDeviceHistory);
        updateSpecialDeviceHistory.setCreateTime(null);
        updateSpecialDeviceHistory.setUpdateTime(null);
        updateSpecialDeviceHistory.setIsDelete(null);
        if (ObjectUtils.isEmpty(specialDeviceHistory)){
            specialDeviceHistoryMapper.insert(updateSpecialDeviceHistory);
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("添加了特殊设备历史数据:").append(updateSpecialDeviceHistory.getName()).append(":").append("mac:").append(updateSpecialDeviceHistory.getMac());
            logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
        }else {
            /*UpdateWrapper<SpecialDeviceHistory> wrapper_insertSpecialDeviceHistory = new UpdateWrapper<>();
            wrapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
            wrapper_specialDeviceHistory.eq("mac",mac);
            wrapper_specialDeviceHistory.eq("organization_id",organizationId);*/
            specialDeviceHistoryMapper.update(updateSpecialDeviceHistory,wrapper_specialDeviceHistory);
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("修改了特殊设备:").append(specialDeviceHistory.getId()).append(":");
            Field[] fields = SpecialDevice.class.getDeclaredFields();
            for (Field field : fields) {
                if (field.getName().equals("id")) {
                    continue;
                }
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                String fieldName = field.getName();
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor(fieldName, SpecialDeviceHistory.class);
                    Method method = pd.getReadMethod();
                    Object o1 = method.invoke(specialDeviceHistory);
                    Object o2 = method.invoke(updateSpecialDeviceHistory);
                    if (o2 != null) {
                        content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(":");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
        }
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("修改了特殊设备:").append(specialDeviceId).append(":");
        Field[] fields = SpecialDevice.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals("id")) {
                continue;
            }
            if ("serialVersionUID".equals(field.getName())) {
                continue;
            }
            String fieldName = field.getName();
            PropertyDescriptor pd = null;
            try {
                pd = new PropertyDescriptor(fieldName, SpecialDevice.class);
                Method method = pd.getReadMethod();
                Object o1 = method.invoke(oldSpecialDevice);
                Object o2 = method.invoke(specialDevice);
                if (o2 != null) {
                    content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(":");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
    }
    @Override
    public void delete(Integer specialDeviceId) {
        SpecialDevice specialDevice = specialDeviceMapper.selectById(specialDeviceId);
        UpdateWrapper<SpecialDevice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",specialDeviceId).set("is_delete",Constants.DELETE);
        specialDeviceMapper.update(null,updateWrapper);
        String mac = specialDevice.getMac();
        //从redis中删除设备信息
        delDeviceInfoFromRedis(mac);
        //维护组织型号关系表
        Integer versionId = specialDevice.getDeviceVersionId();
        Integer orgId = specialDevice.getOrganizationId();
        deleteOrganizationUnitAlarm(orgId, versionId);
        //刷新deviceInfo缓存
        CacheUtils.refreshDeviceAlarmInfo();
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("删除了设备:").append(specialDevice.getName()).append(";").append("mac:").append(mac);
        logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE);
    }
    @Override
@@ -372,4 +540,20 @@
            }
        }
    }
    private void deleteOrganizationUnitAlarm(Integer orgId, Integer versionId) {
        QueryWrapper<SpecialDevice> queryOrganizationVersionWrapper = new QueryWrapper<>();
        queryOrganizationVersionWrapper.eq("organization_id", orgId);
        queryOrganizationVersionWrapper.eq("device_version_id", versionId);
        queryOrganizationVersionWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<SpecialDevice> specialDevices = specialDeviceMapper.selectList(queryOrganizationVersionWrapper);
        if (org.springframework.util.ObjectUtils.isEmpty(specialDevices)) {//如果为空,则组织没有该型号的设备了。
            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);
        }
    }
}