package com.moral.api.service.impl; 
 | 
  
 | 
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.*; 
 | 
import com.moral.api.mapper.*; 
 | 
import com.moral.api.pojo.redisBean.SpecialDeviceInfoDTO; 
 | 
import com.moral.api.pojo.vo.device.DeviceVO; 
 | 
import com.moral.api.service.SpecialDeviceService; 
 | 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 
 | 
import com.moral.api.util.AdjustDataUtils; 
 | 
import com.moral.api.util.CacheUtils; 
 | 
import com.moral.api.util.LogUtils; 
 | 
import com.moral.constant.Constants; 
 | 
import com.moral.constant.RedisConstants; 
 | 
import com.moral.constant.ResponseCodeEnum; 
 | 
import com.moral.util.DateUtils; 
 | 
import org.springframework.beans.BeanUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.data.redis.core.RedisTemplate; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
import org.springframework.web.context.request.RequestContextHolder; 
 | 
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.*; 
 | 
  
 | 
/** 
 | 
 * <p> 
 | 
 *  服务实现类 
 | 
 * </p> 
 | 
 * 
 | 
 * @author moral 
 | 
 * @since 2021-08-11 
 | 
 */ 
 | 
@Service 
 | 
public class SpecialDeviceServiceImpl extends ServiceImpl<SpecialDeviceMapper, SpecialDevice> implements SpecialDeviceService { 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private SpecialDeviceMapper specialDeviceMapper; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private ManageAccountMapper manageAccountMapper; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private VersionMapper versionMapper; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private SysDictDataMapper sysDictDataMapper; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private SpecialDeviceHistoryMapper specialDeviceHistoryMapper; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private OrganizationMapper organizationMapper; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private RedisTemplate redisTemplate; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private OrganizationUnitAlarmMapper organizationUnitAlarmMapper; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private VersionSensorUnitMapper versionSensorUnitMapper; 
 | 
  
 | 
    @Autowired(required = false) 
 | 
    private GovMonitorPointMapper govMonitorPointMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private AdjustDataUtils adjustDataUtils; 
 | 
  
 | 
    /* 
 | 
     * 从redis获取设备信息 
 | 
     * */ 
 | 
    private SpecialDeviceInfoDTO getDeviceInfoFromRedis(String mac) { 
 | 
        return (SpecialDeviceInfoDTO) redisTemplate.opsForHash().get(RedisConstants.SPECIAL_DEVICE, mac); 
 | 
    } 
 | 
  
 | 
    /* 
 | 
     * 设备信息存入redis 
 | 
     */ 
 | 
    private void setDeviceInfoToRedis(String mac, SpecialDeviceInfoDTO specialDeviceInfoDTO) { 
 | 
        redisTemplate.opsForHash().put(RedisConstants.SPECIAL_DEVICE, mac, specialDeviceInfoDTO); 
 | 
    } 
 | 
  
 | 
    /* 
 | 
     * 从redis删除设备信息 
 | 
     */ 
 | 
    private void delDeviceInfoFromRedis(String mac) { 
 | 
        redisTemplate.opsForHash().delete(RedisConstants.SPECIAL_DEVICE, mac); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Map<String, Object> getDataByCondition(Map map) { 
 | 
        Map<String,Object> resultMap = new HashMap<>(); 
 | 
        if (!map.containsKey("current")||!map.containsKey("size")){ 
 | 
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); 
 | 
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); 
 | 
            return resultMap; 
 | 
        } 
 | 
        int current = Integer.parseInt(map.get("current").toString()); 
 | 
        int size = Integer.parseInt(map.get("size").toString()); 
 | 
        Page<SpecialDevice> page = new Page<>(current,size); 
 | 
        QueryWrapper<SpecialDevice> wrapper_Condition = new QueryWrapper<>(); 
 | 
        wrapper_Condition.eq("is_delete",Constants.NOT_DELETE); 
 | 
        if (!ObjectUtils.isEmpty(map.get("organization_id"))){ 
 | 
            wrapper_Condition.eq("organization_id",map.get("organization_id").toString()); 
 | 
        } 
 | 
        if (!ObjectUtils.isEmpty(map.get("keyword"))){ 
 | 
            wrapper_Condition.and(wc -> wc.like("name",map.get("keyword").toString()).or().like("mac",map.get("keyword").toString())); 
 | 
            //wrapper_Condition.like("name",map.get("keyword").toString()).or().like("mac",map.get("keyword").toString()); 
 | 
        } 
 | 
        wrapper_Condition.orderByDesc("create_time"); 
 | 
        Page resultPage = specialDeviceMapper.selectPage(page,wrapper_Condition); 
 | 
        int totalNumber = specialDeviceMapper.selectCount(wrapper_Condition); 
 | 
        List<SpecialDevice> specialDevices = resultPage.getRecords(); 
 | 
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
 | 
        List<Map<String,Object>> specialDeviceList = new ArrayList<>(); 
 | 
        for (SpecialDevice specialDevice:specialDevices) { 
 | 
            Map specialDeviceMap = JSON.parseObject(JSON.toJSONString(specialDevice),Map.class); 
 | 
            String createTime = SDF.format(specialDevice.getCreateTime()); 
 | 
            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(); 
 | 
                String[] operateIdArr = operateIds.split(","); 
 | 
                if (operateIdArr.length>0){ 
 | 
                    List<Integer> operateIdList = new ArrayList<>(); 
 | 
                    for (int i = 0; i < operateIdArr.length; i++){ 
 | 
                        operateIdList.add(Integer.parseInt(operateIdArr[i])); 
 | 
                    } 
 | 
                    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<>(); 
 | 
                        operateMap.put("id",manageAccount.getId()); 
 | 
                        operateMap.put("name",manageAccount.getUserName()); 
 | 
                        operateList.add(operateMap); 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
            specialDeviceMap.put("operates",operateList); 
 | 
            Map<String,Object> deviceVersionMap = new HashMap<>(); 
 | 
            if (!ObjectUtils.isEmpty(specialDevice.getDeviceVersionId()) && specialDevice.getDeviceVersionId()!=null && !"".equals(specialDevice.getDeviceVersionId())){ 
 | 
                int versionId = Integer.parseInt(specialDevice.getDeviceVersionId().toString()); 
 | 
                QueryWrapper<Version> wapper_version = new QueryWrapper<>(); 
 | 
                wapper_version.eq("is_delete",Constants.NOT_DELETE); 
 | 
                wapper_version.eq("id",versionId); 
 | 
                Version version = versionMapper.selectOne(wapper_version); 
 | 
                if (!ObjectUtils.isEmpty(version)){ 
 | 
                    deviceVersionMap.put("id",version.getId()); 
 | 
                    deviceVersionMap.put("name",version.getName()); 
 | 
                } 
 | 
            } 
 | 
            specialDeviceMap.put("version",deviceVersionMap); 
 | 
            Map<String,Object> specialTypeMap = new HashMap<>(); 
 | 
            if (!ObjectUtils.isEmpty(specialDevice.getSpecialType()) && specialDevice.getSpecialType()!=null && !"".equals(specialDevice.getSpecialType())){ 
 | 
                int specialTypeId = Integer.parseInt(specialDevice.getSpecialType().toString()); 
 | 
                QueryWrapper<SysDictData> wapper_sysDictData = new QueryWrapper<>(); 
 | 
                wapper_sysDictData.eq("is_delete",Constants.NOT_DELETE); 
 | 
                wapper_sysDictData.eq("dict_type_id",27); 
 | 
                wapper_sysDictData.eq("dataKey",specialTypeId); 
 | 
                SysDictData sysDictData = sysDictDataMapper.selectOne(wapper_sysDictData); 
 | 
                if (!ObjectUtils.isEmpty(sysDictData)){ 
 | 
                    specialTypeMap.put("id",sysDictData.getId()); 
 | 
                    specialTypeMap.put("dataKey",sysDictData.getDataKey()); 
 | 
                    specialTypeMap.put("name",sysDictData.getDataValue()); 
 | 
                } 
 | 
            } 
 | 
            specialDeviceMap.put("specialType",specialTypeMap); 
 | 
            Map<String,Object> organizationMap = new HashMap<>(); 
 | 
            if (!ObjectUtils.isEmpty(specialDevice.getOrganizationId()) && specialDevice.getOrganizationId()!=null && !"".equals(specialDevice.getOrganizationId())){ 
 | 
                int organizationId = Integer.parseInt(specialDevice.getOrganizationId().toString()); 
 | 
                QueryWrapper<Organization> wapper_organization = new QueryWrapper<>(); 
 | 
                wapper_organization.eq("is_delete",Constants.NOT_DELETE); 
 | 
                wapper_organization.eq("id",organizationId); 
 | 
                Organization organization = organizationMapper.selectOne(wapper_organization); 
 | 
                if (!ObjectUtils.isEmpty(organization)){ 
 | 
                    organizationMap.put("id",organization.getId()); 
 | 
                    organizationMap.put("name",organization.getName()); 
 | 
                } 
 | 
            } 
 | 
            specialDeviceMap.put("organization",organizationMap); 
 | 
            List<Map<String,Object>> organizationList = new ArrayList<>(); 
 | 
            QueryWrapper<SpecialDeviceHistory> wapper_specialDeviceHistory = new QueryWrapper<>(); 
 | 
            wapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE); 
 | 
            wapper_specialDeviceHistory.eq("mac",specialDevice.getMac()); 
 | 
            List<SpecialDeviceHistory> specialDeviceHistories = specialDeviceHistoryMapper.selectList(wapper_specialDeviceHistory); 
 | 
            if (!ObjectUtils.isEmpty(specialDeviceHistories)){ 
 | 
                List organizationIdList = new ArrayList(); 
 | 
                for (SpecialDeviceHistory specialDeviceHistory:specialDeviceHistories) { 
 | 
                    organizationIdList.add(specialDeviceHistory.getOrganizationId()); 
 | 
                } 
 | 
                QueryWrapper<Organization> wapper_organizationHistory = new QueryWrapper<>(); 
 | 
                wapper_organizationHistory.eq("is_delete",Constants.NOT_DELETE); 
 | 
                wapper_organizationHistory.in("id",organizationIdList); 
 | 
                List<Organization> organizations = organizationMapper.selectList(wapper_organizationHistory); 
 | 
                for (Organization organization:organizations) { 
 | 
                    Map<String,Object> organizationHistoryMap = new HashMap<>(); 
 | 
                    organizationHistoryMap.put("id",organization.getId()); 
 | 
                    organizationHistoryMap.put("name",organization.getName()); 
 | 
                    organizationList.add(organizationHistoryMap); 
 | 
                } 
 | 
            } 
 | 
            specialDeviceMap.put("organizations",organizationList); 
 | 
            specialDeviceList.add(specialDeviceMap); 
 | 
        } 
 | 
        resultMap.put("specialDevices",specialDeviceList); 
 | 
        resultMap.put("totalNumber",totalNumber); 
 | 
        resultMap.put("current",current); 
 | 
        int totalPageNumber = totalNumber/size; 
 | 
        if(totalNumber%size != 0){ 
 | 
            totalPageNumber += 1; 
 | 
        } 
 | 
        resultMap.put("totalPageNumber",totalPageNumber); 
 | 
        return resultMap; 
 | 
    } 
 | 
  
 | 
    @Transactional 
 | 
    @Override 
 | 
    public Map<String, Object> insert(SpecialDevice specialDevice) { 
 | 
        Map<String,Object> resultMap = new HashMap<>(); 
 | 
        String name = specialDevice.getName(); 
 | 
        String mac = specialDevice.getMac(); 
 | 
        int organizationId = specialDevice.getOrganizationId(); 
 | 
        int deviceVersionId = specialDevice.getDeviceVersionId(); 
 | 
        String operateIds = specialDevice.getOperateIds(); 
 | 
        String specialType = specialDevice.getSpecialType(); 
 | 
        if (ObjectUtils.isEmpty(name) || ObjectUtils.isEmpty(mac) || ObjectUtils.isEmpty(specialType) || ObjectUtils.isEmpty(organizationId) || ObjectUtils.isEmpty(deviceVersionId)){ 
 | 
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); 
 | 
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); 
 | 
            return resultMap; 
 | 
        } 
 | 
        if (name.equals("") || mac.equals("") || specialType.equals("")){ 
 | 
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_NOT_REQUIREMENT.getCode()); 
 | 
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_NOT_REQUIREMENT.getMsg()); 
 | 
            return resultMap; 
 | 
        } 
 | 
        QueryWrapper<SpecialDevice> wrapper_mac = new QueryWrapper<>(); 
 | 
        wrapper_mac.eq("is_delete",Constants.NOT_DELETE); 
 | 
        wrapper_mac.eq("mac",mac); 
 | 
        int macNum = specialDeviceMapper.selectCount(wrapper_mac); 
 | 
        if (macNum>0){ 
 | 
            resultMap.put("code",ResponseCodeEnum.MAC_IS_EXIST.getCode()); 
 | 
            resultMap.put("msg",ResponseCodeEnum.MAC_IS_EXIST.getMsg()); 
 | 
            return resultMap; 
 | 
        } 
 | 
        specialDeviceMapper.insert(specialDevice); 
 | 
        QueryWrapper<SpecialDeviceHistory> wrapper_specialDeviceHistory = new QueryWrapper<>(); 
 | 
        wrapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE); 
 | 
        wrapper_specialDeviceHistory.eq("mac",mac); 
 | 
        wrapper_specialDeviceHistory.eq("organization_id",organizationId); 
 | 
        SpecialDeviceHistory specialDeviceHistory = specialDeviceHistoryMapper.selectOne(wrapper_specialDeviceHistory); 
 | 
        SpecialDeviceHistory insertSpecialDeviceHistory = new SpecialDeviceHistory(); 
 | 
        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()); 
 | 
        SpecialDeviceInfoDTO specialDeviceInfoDTO = selectDeviceInfoDTOById(specialDevice.getId()); 
 | 
        //维护组织型号关系表 
 | 
        insertOrganizationUnitAlarm(specialDevice.getOrganizationId(), specialDevice.getDeviceVersionId()); 
 | 
        //新增设备信息存入redis 
 | 
        String mac1 = specialDevice.getMac(); 
 | 
        //从redis中删除设备信息 
 | 
        delDeviceInfoFromRedis(mac1); 
 | 
        //设备信息存入redis 
 | 
        setDeviceInfoToRedis(mac1, specialDeviceInfoDTO); 
 | 
        //刷新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.INSERT_OPERATE_TYPE); 
 | 
        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); 
 | 
        SpecialDeviceInfoDTO specialDeviceInfoDTO = selectDeviceInfoDTOById(specialDevice.getId()); 
 | 
        //设备信息存入redis 
 | 
        setDeviceInfoToRedis(updateSpecialDevice.getMac(), specialDeviceInfoDTO); 
 | 
        //刷新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) { 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public SpecialDeviceInfoDTO selectDeviceInfoDTOById(Integer deviceId) { 
 | 
        String mac = specialDeviceMapper.selectById(deviceId).getMac(); 
 | 
        SpecialDeviceInfoDTO specialDeviceInfoDTO = new SpecialDeviceInfoDTO(); 
 | 
        SpecialDeviceInfoDTO specialDeviceInfoDTORedis = getDeviceInfoFromRedis(mac); 
 | 
        //先从redis中取 
 | 
        if (specialDeviceInfoDTORedis != null) { 
 | 
            return specialDeviceInfoDTORedis; 
 | 
        } 
 | 
  
 | 
        //存放特殊设备信息 
 | 
        SpecialDevice specialDeviceInfo = new SpecialDevice(); 
 | 
        SpecialDevice specialDevice = specialDeviceMapper.selectById(deviceId); 
 | 
        System.out.println(specialDevice); 
 | 
        /*specialDeviceInfo.setId(specialDevice.getId()); 
 | 
        specialDeviceInfo.setName(specialDevice.getName()); 
 | 
        specialDeviceInfo.setMac(specialDevice.getMac()); 
 | 
        specialDeviceInfo.setCreateTime(specialDevice.getCreateTime());*/ 
 | 
        specialDeviceInfoDTO.setSpecialDevice(specialDevice); 
 | 
  
 | 
        //存放政府站点信息 
 | 
        GovMonitorPoint govMonitorPointInfo = new GovMonitorPoint(); 
 | 
        QueryWrapper<GovMonitorPoint> wrapper_govMonitorPoint = new QueryWrapper<>(); 
 | 
        wrapper_govMonitorPoint.eq("is_delete",Constants.NOT_DELETE); 
 | 
        wrapper_govMonitorPoint.eq("guid",specialDevice.getGuid()); 
 | 
        GovMonitorPoint govMonitorPoint = govMonitorPointMapper.selectOne(wrapper_govMonitorPoint); 
 | 
        /*govMonitorPointInfo.setId(govMonitorPoint.getId()); 
 | 
        govMonitorPointInfo.setGuid(govMonitorPoint.getGuid()); 
 | 
        govMonitorPointInfo.setName(govMonitorPoint.getName());*/ 
 | 
        specialDeviceInfoDTO.setGovMonitorPoint(govMonitorPoint); 
 | 
  
 | 
        //存放型号信息 
 | 
        Version versionInfo = new Version(); 
 | 
        Version version = versionMapper.selectById(specialDevice.getDeviceVersionId()); 
 | 
        /*versionInfo.setId(version.getId()); 
 | 
        versionInfo.setName(version.getName());*/ 
 | 
        specialDeviceInfoDTO.setVersion(version); 
 | 
  
 | 
        //存放维护人 
 | 
        List<ManageAccount> operatorsInfo = new ArrayList<>(); 
 | 
        String operateIds = specialDevice.getOperateIds(); 
 | 
        if (!ObjectUtils.isEmpty(operateIds)){ 
 | 
            String[] operateIdArr = operateIds.split(","); 
 | 
            if (operateIdArr.length > 0){ 
 | 
                List<Integer> operateIdList = new ArrayList<>(); 
 | 
                for (int i = 0; i < operateIdArr.length; i++){ 
 | 
                    operateIdList.add(Integer.parseInt(operateIdArr[i])); 
 | 
                } 
 | 
                QueryWrapper<ManageAccount> wapper_manageAccount = new QueryWrapper<>(); 
 | 
                wapper_manageAccount.eq("is_delete",Constants.NOT_DELETE); 
 | 
                wapper_manageAccount.in("id",operateIdList); 
 | 
                operatorsInfo = manageAccountMapper.selectList(wapper_manageAccount); 
 | 
            } 
 | 
        } 
 | 
        specialDeviceInfoDTO.setOperates(operatorsInfo); 
 | 
  
 | 
        //存放组织 
 | 
        Organization organizationInfo = new Organization(); 
 | 
        organizationInfo = organizationMapper.selectById(specialDevice.getOrganizationId()); 
 | 
        specialDeviceInfoDTO.setOrganization(organizationInfo); 
 | 
  
 | 
        return specialDeviceInfoDTO; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Map<String, Object> adjustSpecialDeviceData(Map<String, Object> deviceData) { 
 | 
//        String mac = deviceData.remove("mac").toString(); 
 | 
        String mac = deviceData.get("mac").toString(); 
 | 
        //从redis获取校准公式 
 | 
        Map<String, Object> adjustFormula = redisTemplate.opsForHash().entries(RedisConstants.ADJUST + "_" + mac); 
 | 
        if (!org.springframework.util.ObjectUtils.isEmpty(adjustFormula)) { 
 | 
            SpecialDeviceInfoDTO specialDeviceInfo = getDeviceInfoFromRedis(mac); 
 | 
            if (org.springframework.util.ObjectUtils.isEmpty(specialDeviceInfo)) { 
 | 
                QueryWrapper<SpecialDevice> queryWrapper = new QueryWrapper<>(); 
 | 
                queryWrapper.eq("mac", mac).eq("is_delete", Constants.NOT_DELETE); 
 | 
                SpecialDevice specialDevice = specialDeviceMapper.selectOne(queryWrapper); 
 | 
                if (specialDevice != null) { 
 | 
                    specialDeviceInfo = selectDeviceInfoDTOById(specialDevice.getId()); 
 | 
                    setDeviceInfoToRedis(mac, specialDeviceInfo); 
 | 
                } 
 | 
            } 
 | 
            //获取设备绑定的国控站信息 
 | 
            //Map<String, Object> govMpInfo = (Map<String, Object>) specialDeviceInfo.getGovMonitorPoint(); 
 | 
            Map<String, Object> govMpInfo = JSON.parseObject(JSON.toJSONString(specialDeviceInfo.getGovMonitorPoint()),Map.class); 
 | 
            Map<String, Object> aqiMap = null; 
 | 
            if (null != govMpInfo && null != govMpInfo.get("guid")) { 
 | 
                aqiMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, govMpInfo.get("guid").toString()); 
 | 
            } 
 | 
            return adjustDataUtils.adjust(deviceData, adjustFormula, org.springframework.util.ObjectUtils.isEmpty(aqiMap) ? null : aqiMap,"0"); 
 | 
        } 
 | 
        deviceData.remove("DataTime"); 
 | 
        return deviceData; 
 | 
    } 
 | 
  
 | 
    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); 
 | 
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryOrganizationVersionWrapper); 
 | 
        if (org.springframework.util.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 (!org.springframework.util.ObjectUtils.isEmpty(versionSensorUnits)) { 
 | 
                for (VersionSensorUnit versionSensorUnit : versionSensorUnits) { 
 | 
                    OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm(); 
 | 
                    organizationUnitAlarm.setOrganizationId(orgId); 
 | 
                    organizationUnitAlarm.setVersionId(versionId); 
 | 
                    organizationUnitAlarm.setSensorCode(versionSensorUnit.getSensorCode()); 
 | 
                    organizationUnitAlarm.setUnitKey(versionSensorUnit.getUnitKey()); 
 | 
                    organizationUnitAlarm.setShowUnitKey(versionSensorUnit.getUnitKey()); 
 | 
                    organizationUnitAlarmMapper.insert(organizationUnitAlarm); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    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); 
 | 
        } 
 | 
    } 
 | 
} 
 |