jinpengyong
2021-09-14 db2b30bbe239427a89521cd858b2b191d56086ef
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.*;
@@ -64,6 +68,9 @@
    @Autowired(required = false)
    private VersionSensorUnitMapper versionSensorUnitMapper;
    @Autowired(required = false)
    private GovMonitorPointMapper govMonitorPointMapper;
    @Autowired
    private LogUtils logUtils;
@@ -121,6 +128,20 @@
            String updateTime = SDF.format(specialDevice.getUpdateTime());
            specialDeviceMap.put("createTime",createTime);
            specialDeviceMap.put("updateTime",updateTime);
            Map<String,Object> govMonitorPointMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDevice.getGuid()) && specialDevice.getGuid()!=null && !"".equals(specialDevice.getGuid())){
                String  guid = specialDevice.getGuid().toString();
                QueryWrapper<GovMonitorPoint> wapper_govMonitorPoint = new QueryWrapper<>();
                wapper_govMonitorPoint.eq("is_delete",Constants.NOT_DELETE);
                wapper_govMonitorPoint.eq("guid",guid);
                GovMonitorPoint govMonitorPoint = govMonitorPointMapper.selectOne(wapper_govMonitorPoint);
                if (!ObjectUtils.isEmpty(govMonitorPoint)){
                    govMonitorPointMap.put("id",govMonitorPoint.getId());
                    govMonitorPointMap.put("guid",govMonitorPoint.getGuid());
                    govMonitorPointMap.put("name",govMonitorPoint.getName());
                }
            }
            specialDeviceMap.put("govMonitorPoint",govMonitorPointMap);
            List<Map<String,Object>> operateList = new ArrayList<>();
            if (!ObjectUtils.isEmpty(specialDevice.getOperateIds()) && specialDevice.getOperateIds()!=null){
                String operateIds = specialDevice.getOperateIds();
@@ -133,6 +154,7 @@
                    QueryWrapper<ManageAccount> wapper_manageAccount = new QueryWrapper<>();
                    wapper_manageAccount.eq("is_delete",Constants.NOT_DELETE);
                    wapper_manageAccount.in("id",operateIdList);
                    wapper_manageAccount.select("id", "user_name");
                    List<ManageAccount> manageAccounts = manageAccountMapper.selectList(wapper_manageAccount);
                    for (ManageAccount manageAccount:manageAccounts) {
                        Map<String,Object> operateMap = new HashMap<>();
@@ -258,12 +280,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());
        //维护组织型号关系表
@@ -286,6 +342,136 @@
        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.refreshSpecialDeviceAlarmInfo();
        //操作日志记录
        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
    public Map<String, Object> selectDeviceInfoById(Integer deviceId) {
        String mac = specialDeviceMapper.selectById(deviceId).getMac();
@@ -302,6 +488,7 @@
        specialDeviceInfo.put("name", specialDevice.getName());
        specialDeviceInfo.put("mac", specialDevice.getMac());
        specialDeviceInfo.put("createTime", DateUtils.dateToDateString(specialDevice.getCreateTime()));
        specialDeviceInfo.put("guid", specialDevice.getGuid());
        //扩展字段
        //specialDeviceInfo.put("extend", device.getExtend());
@@ -342,6 +529,9 @@
        Organization organization = organizationMapper.selectById(specialDevice.getOrganizationId());
        orgInfo.put("id", organization.getId());
        orgInfo.put("name", organization.getName());
        orgInfo.put("areaCode", organization.getAreaCode());
        orgInfo.put("cityCode", organization.getCityCode());
        orgInfo.put("provinceCode", organization.getProvinceCode());
        specialDeviceInfo.put("organization", orgInfo);
        setDeviceInfoToRedis(mac, specialDeviceInfo);
@@ -372,4 +562,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);
        }
    }
}