| | |
| | | package com.ruoyi.device.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.json.JSONObject; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.config.Configure; |
| | | import com.ruoyi.basic.mapper.LaboratoryMapper; |
| | | import com.ruoyi.basic.mapper.StructureItemParameterMapper; |
| | | import com.ruoyi.basic.pojo.Laboratory; |
| | | import com.ruoyi.basic.pojo.StructureItemParameter; |
| | | import com.ruoyi.common.core.domain.Result; |
| | | import com.ruoyi.common.core.domain.entity.User; |
| | |
| | | import com.ruoyi.device.pojo.*; |
| | | import com.ruoyi.device.service.DataConfigService; |
| | | import com.ruoyi.device.service.DeviceService; |
| | | import com.ruoyi.device.service.DocumentService; |
| | | import com.ruoyi.device.service.DeviceDocumentsService; |
| | | import com.ruoyi.device.utils.DataAcquisition; |
| | | import com.ruoyi.framework.exception.ErrorException; |
| | | import com.ruoyi.inspect.mapper.InsSampleMapper; |
| | | import com.ruoyi.inspect.pojo.InsProduct; |
| | | import com.ruoyi.inspect.util.HackLoopTableRenderPolicy; |
| | | import com.ruoyi.system.mapper.SysDictDataMapper; |
| | | import com.ruoyi.system.mapper.UserMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.logging.log4j.util.Strings; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Isolation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | private UserMapper userMapper; |
| | | |
| | | private LaboratoryMapper laboratoryMapper; |
| | | |
| | | private SysDictDataMapper sysDictDataMapper; |
| | | |
| | | private StructureItemParameterMapper structureItemParameterMapper; |
| | | |
| | | private DataConfigService dataConfigService; |
| | | |
| | | private QrShowServiceImpl qrShowService; |
| | | |
| | | private InsSampleMapper insSampleMapper; |
| | | |
| | | private DocumentService documentService; |
| | | private DeviceDocumentsService documentService; |
| | | |
| | | private DeviceMetricRecordMapper deviceMetricRecordMapper; |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<Device> selectDevicePrincipal() { |
| | | return deviceMapper.selectDevicePrincipal(); |
| | | } |
| | | |
| | | @Override |
| | | public List<Device> selectDeviceByCategory(String inspectionItem, String inspectionItemSubclass, String laboratory) { |
| | | List<Integer> id; |
| | | |
| | |
| | | } |
| | | deviceDto.setAuthorizedPersonName(name); |
| | | //查询设备校准信息 |
| | | DeviceMetricRecord calibrate = qrShowService.getDeviceMetricRecord(id, "calibrate"); |
| | | DeviceMetricRecord calibrate = getDeviceMetricRecord(id, "calibrate"); |
| | | deviceDto.setCalibrateNo(calibrate.getCertificateSerialNumber()); |
| | | |
| | | // 到了停用日期,自动将状态改为停用 |
| | |
| | | } |
| | | } |
| | | return deviceDto; |
| | | } |
| | | |
| | | /** |
| | | * 查询设备校准/核查记录 |
| | | * @param deviceId |
| | | * @param type |
| | | * @return |
| | | */ |
| | | public DeviceMetricRecord getDeviceMetricRecord(int deviceId, String type){ |
| | | return Optional.ofNullable( |
| | | deviceMetricRecordMapper.selectOne(Wrappers.<DeviceMetricRecord>lambdaQuery() |
| | | .eq(DeviceMetricRecord::getDeviceId, deviceId) |
| | | .eq(DeviceMetricRecord::getType, type) |
| | | .orderByDesc(DeviceMetricRecord::getCreateTime) |
| | | .last("limit 1"))).orElse(new DeviceMetricRecord()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | // 设备信息 |
| | | Device device = baseMapper.selectById(deviceId); |
| | | // 设备档案 |
| | | List<Document> documentList = documentService.list(Wrappers.<Document>lambdaQuery().eq(Document::getDeviceId, deviceId)); |
| | | List<DeviceDocuments> documentList = documentService.list(Wrappers.<DeviceDocuments>lambdaQuery().eq(DeviceDocuments::getDeviceId, deviceId)); |
| | | // 设备校准表 |
| | | List<DeviceMetricRecord> deviceMetricRecordList = deviceMetricRecordMapper.selectList(Wrappers.<DeviceMetricRecord>lambdaQuery().eq(DeviceMetricRecord::getDeviceId, deviceId)); |
| | | // 设备维修表 |
| | |
| | | * @param documentList 档案列表 |
| | | * @param documentExportWordDtoList 返回给word的数据列表 |
| | | */ |
| | | private static void extracted(List<Document> documentList, List<DocumentExportWordDto> documentExportWordDtoList) { |
| | | private static void extracted(List<DeviceDocuments> documentList, List<DocumentExportWordDto> documentExportWordDtoList) { |
| | | // 给档案加序号 并且分为左右两个列表在word中显示 |
| | | for (int i = 0; i < documentList.size(); i++) { |
| | | // 创建word表格中一行的数据对象 |
| | | DocumentExportWordDto documentExportWordDto = new DocumentExportWordDto(); |
| | | // 获取档案信息 |
| | | Document document = documentList.get(i); |
| | | DeviceDocuments document = documentList.get(i); |
| | | // 格式化日期 |
| | | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | // 根据序号 分别加入两个列表 |
| | |
| | | throw new RuntimeException("导出失败"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class) |
| | | public void importExcel(List<Device> batch) { |
| | | if (CollUtil.isEmpty(batch)) { |
| | | return; |
| | | } |
| | | |
| | | // 提前获取设备名称、型号和管理编号集合 |
| | | Set<String> deviceNames = batch.stream().map(Device::getDeviceName).collect(Collectors.toSet()); |
| | | Set<String> specificationModels = batch.stream().map(Device::getSpecificationModel).collect(Collectors.toSet()); |
| | | Set<String> managementNumbers = batch.stream().map(Device::getManagementNumber).collect(Collectors.toSet()); |
| | | |
| | | // 批量查询关联数据(用户、实验室) |
| | | Map<String, Integer> userMap = queryUserMap(batch); |
| | | Map<String, Integer> labMap = queryLabMap(batch); |
| | | |
| | | // 内存处理:转换关联字段并去重,同时拆分插入和更新列表 |
| | | List<Device> toInsert = new ArrayList<>(); |
| | | List<Device> toUpdate = new ArrayList<>(); |
| | | processBatch(batch, userMap, labMap, deviceNames, specificationModels, managementNumbers, toInsert, toUpdate); |
| | | |
| | | // 批量操作数据库 |
| | | batchOperate(toInsert, toUpdate); |
| | | } |
| | | |
| | | /** |
| | | * 判断该设备是否可以数采 |
| | | * @param managementNumber |
| | | * @param request |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result<?> determineWhetherToCollectData(String managementNumber, HttpServletRequest request) { |
| | | String ip = DataAcquisition.getIp(request); |
| | | List<Device> device = baseMapper.selectList(Wrappers.<Device>lambdaQuery() |
| | | .eq(Device::getIp, ip)); |
| | | if (ObjectUtils.isEmpty(device)) { |
| | | return Result.success(false); |
| | | } |
| | | if (ObjectUtils.isEmpty(device.get(0).getFileType()) || ObjectUtils.isEmpty(device.get(0).getCollectUrl())) { |
| | | return Result.success(false); |
| | | } else { |
| | | return Result.success(true); |
| | | } |
| | | } |
| | | |
| | | private Map<String, Integer> queryUserMap(List<Device> batch) { |
| | | Set<String> managerNames = batch.stream() |
| | | .map(Device::getEquipmentManagerName) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | if (CollUtil.isEmpty(managerNames)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | return userMapper.selectList( |
| | | new LambdaQueryWrapper<User>().in(User::getName, managerNames) |
| | | ).stream().collect(Collectors.toMap(User::getName, User::getId)); |
| | | } |
| | | |
| | | private Map<String, Integer> queryLabMap(List<Device> batch) { |
| | | Set<String> labNames = batch.stream() |
| | | .map(Device::getSubordinateDepartments) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | if (CollUtil.isEmpty(labNames)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | return laboratoryMapper.selectList( |
| | | new LambdaQueryWrapper<Laboratory>().in(Laboratory::getLaboratoryName, labNames) |
| | | ).stream().collect(Collectors.toMap(Laboratory::getLaboratoryName, Laboratory::getId)); |
| | | } |
| | | |
| | | private void processBatch(List<Device> batch, Map<String, Integer> userMap, Map<String, Integer> labMap, |
| | | Set<String> deviceNames, Set<String> specificationModels, Set<String> managementNumbers, |
| | | List<Device> toInsert, List<Device> toUpdate) { |
| | | // 预计算唯一键(设备名称+型号+管理编号) |
| | | Map<String, Device> existDevices = deviceMapper.selectList( |
| | | new LambdaQueryWrapper<Device>() |
| | | .in(Device::getDeviceName, deviceNames) |
| | | .in(Device::getSpecificationModel, specificationModels) |
| | | .in(Device::getManagementNumber, managementNumbers) |
| | | ).stream().collect(Collectors.toMap( |
| | | d -> d.getDeviceName() + "|" + d.getSpecificationModel() + "|" + d.getManagementNumber(), |
| | | Function.identity() |
| | | )); |
| | | |
| | | for (Device device : batch) { |
| | | // 转换关联字段 |
| | | device.setEquipmentManager(userMap.get(device.getEquipmentManagerName())); |
| | | device.setSubordinateDepartmentsId(labMap.get(device.getSubordinateDepartments())); |
| | | |
| | | // 设备状态 |
| | | if (ObjectUtils.isNotEmpty(device.getDeviceStatusName())) { |
| | | try { |
| | | // 查字典对应的值 |
| | | String status = sysDictDataMapper.selectDictValue("device_status", device.getDeviceStatusName()); |
| | | device.setDeviceStatus(Integer.parseInt(status)); |
| | | } catch (Exception e) { |
| | | // 处理异常,例如记录日志 |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | // 生成唯一键 |
| | | String key = device.getDeviceName() + "|" + device.getSpecificationModel() + "|" + device.getManagementNumber(); |
| | | if (existDevices.containsKey(key)) { |
| | | Device exist = existDevices.get(key); |
| | | BeanUtils.copyProperties(device, exist, "id"); |
| | | toUpdate.add(exist); |
| | | } else { |
| | | toInsert.add(device); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void batchOperate(List<Device> toInsert, List<Device> toUpdate) { |
| | | // 批量插入 |
| | | if (CollUtil.isNotEmpty(toInsert)) { |
| | | deviceMapper.insertBatchSomeColumn(toInsert); |
| | | } |
| | | |
| | | // 批量更新 |
| | | if (CollUtil.isNotEmpty(toUpdate)) { |
| | | updateBatchById(toUpdate); |
| | | } |
| | | } |
| | | } |