|  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | import com.moral.api.config.mybatis.wrapper.NullFilterWrapper; | 
 |  |  | import com.moral.api.entity.User; | 
 |  |  | import com.moral.api.entity.Version; | 
 |  |  | import com.moral.api.entity.VersionSensorUnit; | 
 |  |  | import com.moral.api.entity.*; | 
 |  |  | import com.moral.api.mapper.OrganizationUnitAlarmMapper; | 
 |  |  | import com.moral.api.mapper.SensorMapper; | 
 |  |  | import com.moral.api.mapper.VersionMapper; | 
 |  |  | import com.moral.api.mapper.VersionSensorUnitMapper; | 
 |  |  | import com.moral.api.pojo.dto.version.VersionDTO; | 
 |  |  | 
 |  |  | import com.moral.api.pojo.form.version.*; | 
 |  |  | import com.moral.api.service.VersionService; | 
 |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
 |  |  | import com.moral.api.util.CacheUtils; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | import com.moral.constant.ResponseCodeEnum; | 
 |  |  | import com.moral.util.ConvertUtils; | 
 |  |  | 
 |  |  |     VersionMapper versionMapper; | 
 |  |  |     @Autowired | 
 |  |  |     VersionSensorUnitMapper versionSensorUnitMapper; | 
 |  |  |     @Autowired | 
 |  |  |     SensorMapper sensorMapper; | 
 |  |  |     @Autowired | 
 |  |  |     OrganizationUnitAlarmMapper organizationUnitAlarmMapper; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public VersionQueryDTO query(VersionQueryForm form) { | 
 |  |  | 
 |  |  |         dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  |         return dto; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |      | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public VersionDTO update(VersionUpdateForm form) { | 
 |  |  | 
 |  |  |         QueryWrapper<Version> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.setEntity(existVersion); | 
 |  |  |         Version existVersionResult = versionMapper.selectOne(wrapper); | 
 |  |  |         if(!ObjectUtils.isEmpty(existVersionResult)){ | 
 |  |  |         if (!ObjectUtils.isEmpty(existVersionResult)) { | 
 |  |  |             dto.setCode(ResponseCodeEnum.VERSION_EXIST.getCode()); | 
 |  |  |             dto.setMsg(ResponseCodeEnum.VERSION_EXIST.getMsg()); | 
 |  |  |             return dto; | 
 |  |  | 
 |  |  |         VersionDTO dto = new VersionDTO(); | 
 |  |  |         //取参 | 
 |  |  |         Integer id = form.getId(); | 
 |  |  |         //查询型号是否被使用,如果被使用则无法删除 | 
 |  |  |         QueryWrapper<OrganizationUnitAlarm> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         queryWrapper.select("id"); | 
 |  |  |         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); | 
 |  |  |         //执行删除 | 
 |  |  | 
 |  |  |         versionMapper.updateById(deleteVersion); | 
 |  |  |         //删除型号和因子单位对应表 | 
 |  |  |         UpdateWrapper deleteSensorUnitWrapper = new UpdateWrapper(); | 
 |  |  |         deleteSensorUnitWrapper.eq("version_id",id); | 
 |  |  |         deleteSensorUnitWrapper.set("is_delete",Constants.DELETE); | 
 |  |  |         versionSensorUnitMapper.update(null,deleteSensorUnitWrapper); | 
 |  |  |         deleteSensorUnitWrapper.eq("version_id", id); | 
 |  |  |         deleteSensorUnitWrapper.set("is_delete", Constants.DELETE); | 
 |  |  |         versionSensorUnitMapper.update(null, deleteSensorUnitWrapper); | 
 |  |  |         //封装返回结果 | 
 |  |  |         dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
 |  |  |         dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  | 
 |  |  |         //取参 | 
 |  |  |         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) { | 
 |  |  |             sensorUnit.setVersionId(versionId); | 
 |  |  |             Sensor sensor = sensorMapper.selectById(sensorUnit.getSensorId()); | 
 |  |  |             sensorUnit.setSensorCode(sensor.getCode()); | 
 |  |  |         } | 
 |  |  |         //查询之前分配的单位和因子 | 
 |  |  |         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); | 
 |  |  |         }); | 
 |  |  |         //删除记录 | 
 |  |  |         if (!ObjectUtils.isEmpty(deleteList)) { | 
 |  |  |             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("organization_id", organizationId); | 
 |  |  |                 deleteOrganizationUnitAlarmWrapper.eq("version_id", versionId); | 
 |  |  |                 deleteOrganizationUnitAlarmWrapper.eq("sensor_code", versionSensorUnit.getSensorCode()); | 
 |  |  |                 deleteOrganizationUnitAlarmWrapper.set("is_delete", Constants.DELETE); | 
 |  |  |                 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); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //刷新deviceInfo缓存 | 
 |  |  |         CacheUtils.refreshDeviceAlarmInfo(); | 
 |  |  |         //封装返回结果 | 
 |  |  |         dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
 |  |  |         dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public VersionDTO queryVersionById(Integer id) { | 
 |  |  |     public VersionQueryDTO queryVersionById(VersionQueryByIdForm form) { | 
 |  |  |         //创建返回对象 | 
 |  |  |         VersionDTO dto = new VersionDTO(); | 
 |  |  |         VersionQueryDTO dto = new VersionQueryDTO(); | 
 |  |  |         //取参 | 
 |  |  |         Integer id = form.getId(); | 
 |  |  |         Integer size = form.getSize(); | 
 |  |  |         Integer page = form.getPage(); | 
 |  |  |         //查询版本对应的因子和单位 | 
 |  |  |         List<Integer> versionsIds = new ArrayList<>(); | 
 |  |  |         versionsIds.add(id); | 
 |  |  |         List<Version> version = new ArrayList<>(); | 
 |  |  |         version = versionMapper.queryVersionsAndSensorUnitByIds(versionsIds); | 
 |  |  |         List<Version> versions = new ArrayList<>(); | 
 |  |  |         versions = versionMapper.queryVersionsAndSensorUnitByIds(versionsIds); | 
 |  |  |         //根据分页信息进行选择因子 | 
 |  |  |         Version version = versions.get(0); | 
 |  |  |         List<Sensor> querySensors = version.getSensors(); | 
 |  |  |         if (!ObjectUtils.isEmpty(querySensors)) { | 
 |  |  |             int startIndex = (page - 1) * size; | 
 |  |  |             int endIndex = startIndex + size - 1; | 
 |  |  |             List<Sensor> resultSensors = new ArrayList<>(); | 
 |  |  |  | 
 |  |  |             for (int i = startIndex; i <= endIndex; i++) { | 
 |  |  |                 if (i >= querySensors.size()) | 
 |  |  |                     break; | 
 |  |  |                 resultSensors.add(querySensors.get(i)); | 
 |  |  |             } | 
 |  |  |             version.setSensors(resultSensors); | 
 |  |  |         } | 
 |  |  |         //封装返回对象 | 
 |  |  |         dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
 |  |  |         dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  |         dto.setVersion(version.get(0)); | 
 |  |  |         if (ObjectUtils.isEmpty(querySensors)) { | 
 |  |  |             dto.setTotal(0); | 
 |  |  |             dto.setPages(0); | 
 |  |  |         } else { | 
 |  |  |             dto.setTotal(querySensors.size()); | 
 |  |  |             double querySize = (double) querySensors.size(); | 
 |  |  |             double dSize = (double) size; | 
 |  |  |             dto.setPages((int) Math.ceil(querySize / dSize)); | 
 |  |  |         } | 
 |  |  |         dto.setSize(size); | 
 |  |  |         dto.setCurrent(page); | 
 |  |  |         VersionDTO versionDTO = new VersionDTO(); | 
 |  |  |         versionDTO.setVersion(version); | 
 |  |  |         dto.setVersionDTOS(Arrays.asList(versionDTO)); | 
 |  |  |         return dto; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public VersionQueryDTO queryByOrganizationId(Integer organizationId) { | 
 |  |  |         //创建返回对象 | 
 |  |  |         VersionQueryDTO dto = new VersionQueryDTO(); | 
 |  |  |         //查询型号id | 
 |  |  |         QueryWrapper<OrganizationUnitAlarm> queryVersionIdsWrapper = new QueryWrapper<>(); | 
 |  |  |         queryVersionIdsWrapper.select("DISTINCT version_id").eq("is_delete", Constants.NOT_DELETE).eq("organization_id", organizationId); | 
 |  |  |         List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryVersionIdsWrapper); | 
 |  |  |         List<Integer> versionIds = new ArrayList<>(); | 
 |  |  |         for (OrganizationUnitAlarm organizationUnitAlarm : organizationUnitAlarms) { | 
 |  |  |             versionIds.add(organizationUnitAlarm.getVersionId()); | 
 |  |  |         } | 
 |  |  |         //根据型号id查询型号 | 
 |  |  |         List<Version> versions = new ArrayList<>(); | 
 |  |  |         if (!ObjectUtils.isEmpty(versionIds)) | 
 |  |  |             versions = versionMapper.selectBatchIds(versionIds); | 
 |  |  |         //封装返回对象 | 
 |  |  |         List<VersionDTO> versionDTOS = new ArrayList<>(); | 
 |  |  |         versions.forEach(value -> versionDTOS.add(new VersionDTO(value))); | 
 |  |  |         dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
 |  |  |         dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
 |  |  |         dto.setVersionDTOS(versionDTOS); | 
 |  |  |         return dto; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } | 
 |  |  |  |