| | |
| | | package com.yuanchu.mom.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson2.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.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.config.Configure; |
| | | import com.yuanchu.mom.common.GetLook; |
| | | import com.yuanchu.mom.common.PrintChina; |
| | | import com.yuanchu.mom.dto.ADto; |
| | | import com.yuanchu.mom.dto.BDto; |
| | | import com.yuanchu.mom.dto.DeviceDto; |
| | | import com.yuanchu.mom.dto.*; |
| | | import com.yuanchu.mom.exception.ErrorException; |
| | | import com.yuanchu.mom.mapper.DeviceMapper; |
| | | import com.yuanchu.mom.mapper.StructureItemParameterMapper; |
| | | import com.yuanchu.mom.mapper.UserMapper; |
| | | import com.yuanchu.mom.pojo.DataConfig; |
| | | import com.yuanchu.mom.pojo.Device; |
| | | import com.yuanchu.mom.pojo.StructureItemParameter; |
| | | import com.yuanchu.mom.pojo.User; |
| | | import com.yuanchu.mom.mapper.*; |
| | | import com.yuanchu.mom.pojo.*; |
| | | import com.yuanchu.mom.service.DataConfigService; |
| | | import com.yuanchu.mom.service.DeviceService; |
| | | import com.yuanchu.mom.service.DocumentService; |
| | | import com.yuanchu.mom.utils.DataAcquisition; |
| | | import com.yuanchu.mom.utils.HackLoopTableRenderPolicy; |
| | | import com.yuanchu.mom.utils.QueryWrappers; |
| | | import com.yuanchu.mom.vo.Result; |
| | | 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.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.net.URLEncoder; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | |
| | | private DeviceMapper deviceMapper; |
| | | |
| | | private EnumServiceImpl enumService; |
| | | |
| | | private UserMapper userMapper; |
| | | |
| | | private StructureItemParameterMapper structureItemParameterMapper; |
| | |
| | | @Autowired |
| | | private DataConfigService dataConfigService; |
| | | |
| | | private QrShowServiceImpl qrShowService; |
| | | |
| | | private InsSampleMapper insSampleMapper; |
| | | |
| | | private DocumentService documentService; |
| | | |
| | | private DeviceMetricRecordMapper deviceMetricRecordMapper; |
| | | |
| | | private DeviceMaintenanceMapper deviceMaintenanceMapper; |
| | | |
| | | @Override |
| | | public Map<String, Object> selectDeviceParameter(Page page, DeviceDto itemParameter) { |
| | | public List<User> selectUserList() { |
| | | List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>().like(User::getDepartLimsId, "1")); |
| | | return users; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> selectDeviceParameter(Page page, DeviceDto itemParameter,Boolean laboratoryNameIsNull) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("head", PrintChina.printChina(DeviceDto.class)); |
| | | Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectDeviceParameter"); |
| | | if (map1.get("look") == 1) itemParameter.setCreateUser(map1.get("userId")); |
| | | IPage<DeviceDto> iPage = deviceMapper.selectDeviceParameterPage(page, QueryWrappers.queryWrappers(itemParameter)); |
| | | IPage<DeviceDto> iPage = deviceMapper.selectDeviceParameterPage(page, QueryWrappers.queryWrappers(itemParameter),laboratoryNameIsNull); |
| | | map.put("body", iPage); |
| | | return map; |
| | | } |
| | |
| | | |
| | | @Override |
| | | public List<Device> selectDeviceByCategory(String inspectionItem, String inspectionItemSubclass) { |
| | | Integer id; |
| | | List<Integer> id; |
| | | try { |
| | | if (ObjectUtils.isEmpty(inspectionItemSubclass)) { |
| | | id = structureItemParameterMapper.selectOne(Wrappers.<StructureItemParameter>lambdaQuery() |
| | | // id = structureItemParameterMapper.selectOne(Wrappers.<StructureItemParameter>lambdaQuery() |
| | | // .eq(StructureItemParameter::getInspectionItem, inspectionItem) |
| | | // .last("limit 1").select(StructureItemParameter::getId)).getId(); |
| | | id = structureItemParameterMapper.selectList(Wrappers.<StructureItemParameter>lambdaQuery() |
| | | .eq(StructureItemParameter::getInspectionItem, inspectionItem) |
| | | .last("limit 1").select(StructureItemParameter::getId)).getId(); |
| | | .select(StructureItemParameter::getId)).stream().map(StructureItemParameter::getId).collect(Collectors.toList()); |
| | | |
| | | }else { |
| | | id = structureItemParameterMapper.selectOne(Wrappers.<StructureItemParameter>lambdaQuery() |
| | | // id = structureItemParameterMapper.selectOne(Wrappers.<StructureItemParameter>lambdaQuery() |
| | | // .eq(StructureItemParameter::getInspectionItem, inspectionItem) |
| | | // .eq(StructureItemParameter::getInspectionItemSubclass, inspectionItemSubclass) |
| | | // .last("limit 1").select(StructureItemParameter::getId)).getId(); |
| | | id = structureItemParameterMapper.selectList(Wrappers.<StructureItemParameter>lambdaQuery() |
| | | .eq(StructureItemParameter::getInspectionItem, inspectionItem) |
| | | .eq(StructureItemParameter::getInspectionItemSubclass, inspectionItemSubclass) |
| | | .last("limit 1").select(StructureItemParameter::getId)).getId(); |
| | | .select(StructureItemParameter::getId)).stream().map(StructureItemParameter::getId).collect(Collectors.toList()); |
| | | } |
| | | } catch (Exception e) { |
| | | return null; |
| | |
| | | for (Device device : devices) { |
| | | String[] ids = device.getInsProductIds().split(","); |
| | | for (String i : ids) { |
| | | if (i.equals(id + "")) { |
| | | devices2.add(device); |
| | | break; |
| | | if (ObjectUtils.isNotEmpty(i)) { |
| | | if (id.contains(Integer.parseInt(i))) { |
| | | devices2.add(device); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Device selectDeviceByCode(String code) { |
| | | return deviceMapper.selectOne(Wrappers.<Device>lambdaQuery().eq(Device::getFactoryNo, code).last("limit 1")); |
| | | public Device selectDeviceByCode(Integer id) { |
| | | DeviceDto deviceDto = deviceMapper.selectDeviceByCode(id); |
| | | List<Integer> ids = new ArrayList<>(); |
| | | if (Strings.isNotEmpty(deviceDto.getAuthorizedPerson())) { |
| | | if (deviceDto.getAuthorizedPerson().equals("null")) { |
| | | deviceDto.setAuthorizedPerson("[]"); |
| | | } |
| | | ids = JSON.parseArray(deviceDto.getAuthorizedPerson(), Integer.class); |
| | | } |
| | | String name = ""; |
| | | if (!ids.isEmpty()) { |
| | | name = userMapper.selectBatchIds(ids).stream().map(User::getName).collect(Collectors.joining(",")); |
| | | } |
| | | deviceDto.setAuthorizedPersonName(name); |
| | | //查询设备校准信息 |
| | | DeviceMetricRecord calibrate = qrShowService.getDeviceMetricRecord(id, "calibrate"); |
| | | //deviceDto.setCalibrationDate(qrShowService.formatDate(calibrate.getCalibrationDate(),"yyyy-MM-dd")); |
| | | deviceDto.setCalibrateNo(calibrate.getCertificateSerialNumber()); |
| | | |
| | | // 到了停用日期,自动将状态改为停用 |
| | | if (deviceDto.getNextCalibrationDate() != null) { |
| | | if (LocalDateTime.now().isAfter(deviceDto.getNextCalibrationDate())) { |
| | | List<Enums> enums = enumService.selectEnumByCategory("设备状态"); |
| | | List<Enums> status = enums.stream().filter(item -> item.getLabel().equals("停用")).collect(Collectors.toList()); |
| | | deviceDto.setDeviceStatus(Integer.parseInt(status.get(0).getValue())); |
| | | deviceMapper.updateById(deviceDto); |
| | | } |
| | | } |
| | | return deviceDto; |
| | | } |
| | | |
| | | @Override |
| | |
| | | String ip = DataAcquisition.getIp(request); |
| | | List<Device> device = baseMapper.selectList(Wrappers.<Device>lambdaQuery() |
| | | .eq(Device::getIp, ip)); |
| | | // 1、检验 |
| | | if(device.size() > 1) { |
| | | String str = ""; |
| | | for (Device device1 : device) { |
| | |
| | | if (ObjectUtils.isEmpty(device.get(0).getFileType()) || ObjectUtils.isEmpty(device.get(0).getCollectUrl())) { |
| | | throw new ErrorException("未给该:" + device.get(0).getDeviceName() + "设备配置采集路径或文件后缀!"); |
| | | } |
| | | // 2、取设备关联的检验项,检验子项 |
| | | List<DataConfig> list1 = new ArrayList<>(); |
| | | inspectionItemSubclass.forEach(i -> { |
| | | List<DataConfig> list = dataConfigService.list(Wrappers.<DataConfig>lambdaQuery() |
| | |
| | | .orderBy(false, false, DataConfig::getId)); |
| | | list1.addAll(list); |
| | | }); |
| | | Map<String, String> map = DataAcquisition.dataAcquisitionEntrance(list1, device.get(0), entrustCode, sampleCode, ip); |
| | | List<DataConfig> collect = list1.stream().distinct().collect(Collectors.toList()); |
| | | // 3、采集,取数据 |
| | | Map<String, Object> map = DataAcquisition.dataAcquisitionEntrance(collect, device.get(0), entrustCode, sampleCode, ip); |
| | | // 4、造循环次数,参与公式计算 |
| | | if (ObjectUtils.isNotEmpty(map)) { |
| | | String frequency = DataAcquisition.createFrequency(entrustCode, sampleCode); |
| | | map.put("frequency", frequency); |
| | | Map<String, Object> frequency = DataAcquisition.createFrequency(entrustCode, sampleCode, map); |
| | | return Result.success(frequency); |
| | | } else { |
| | | return Result.success(null); |
| | | } |
| | | return Result.success(map); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | return aDtos; |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> treeDevice(String deviceName) { |
| | | List<Map<String, Object>> listMap = deviceMapper.treeDevice(deviceName); |
| | | return listMap; |
| | | } |
| | | |
| | | @Override |
| | | public Result<?> determineWhetherToCollectData(String managementNumber, HttpServletRequest request) { |
| | | String ip = DataAcquisition.getIp(request); |
| | | Device device = baseMapper.selectOne(Wrappers.<Device>lambdaQuery() |
| | | .eq(Device::getIp, ip)); |
| | | if (ObjectUtils.isEmpty(device)) { |
| | | return Result.success(false); |
| | | } |
| | | if (ObjectUtils.isEmpty(device.getFileType()) || ObjectUtils.isEmpty(device.getCollectUrl())) { |
| | | return Result.success(false); |
| | | } else { |
| | | return Result.success(true); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void exportDeviceFile(Integer deviceId, HttpServletResponse response) { |
| | | |
| | | // 设备信息 |
| | | Device device = baseMapper.selectById(deviceId); |
| | | // 设备档案 |
| | | List<Document> documentList = documentService.list(Wrappers.<Document>lambdaQuery().eq(Document::getDeviceId, deviceId)); |
| | | // 设备校准表 |
| | | List<DeviceMetricRecord> deviceMetricRecordList = deviceMetricRecordMapper.selectList(Wrappers.<DeviceMetricRecord>lambdaQuery().eq(DeviceMetricRecord::getDeviceId, deviceId)); |
| | | // 设备维修表 |
| | | List<DeviceMaintenance> deviceMaintenanceList = deviceMaintenanceMapper.selectList(Wrappers.<DeviceMaintenance>lambdaQuery().eq(DeviceMaintenance::getDeviceId, deviceId)); |
| | | |
| | | |
| | | // 返回给word的数据列表 分为左右两列数据 |
| | | List<DocumentExportWordDto> documentExportWordDtoList = new ArrayList<>(); |
| | | // 给档案加序号 并左右分为左右两列在word中显示 |
| | | extracted(documentList, documentExportWordDtoList); |
| | | |
| | | |
| | | // 将校准表和维修表放入一个对象中方便word表格中显示 |
| | | List<DeviceMetricRecordAndMaintenanceDto> deviceMetricRecordAndMaintenanceDtoList = getDeviceMetricRecordAndMaintenanceDtoList(deviceMetricRecordList, deviceMaintenanceList); |
| | | |
| | | |
| | | // 获取路径 |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/word/device-document.docx"); |
| | | Configure configure = Configure.builder() |
| | | .bind("document", new HackLoopTableRenderPolicy()) |
| | | .bind("deviceMetricRecordAndMaintenanceDtoList", new HackLoopTableRenderPolicy()) |
| | | .build(); |
| | | XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render( |
| | | new HashMap<String, Object>() {{ |
| | | put("device", device); |
| | | put("document", documentExportWordDtoList); // 档案 |
| | | put("deviceMetricRecordAndMaintenanceDtoList", deviceMetricRecordAndMaintenanceDtoList); // 校准表 和 维修表 |
| | | }}); |
| | | |
| | | try { |
| | | response.setContentType("application/msword"); |
| | | String fileName = URLEncoder.encode( |
| | | device.getDeviceName() + "档案", "UTF-8"); |
| | | response.setHeader("Content-disposition", |
| | | "attachment;filename=" + fileName + ".docx"); |
| | | OutputStream os = response.getOutputStream(); |
| | | template.write(os); |
| | | os.flush(); |
| | | os.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("导出失败"); |
| | | } |
| | | } |
| | | |
| | | private List<DeviceMetricRecordAndMaintenanceDto> getDeviceMetricRecordAndMaintenanceDtoList(List<DeviceMetricRecord> deviceMetricRecordList, List<DeviceMaintenance> deviceMaintenanceList) { |
| | | // 设备校准表和设备维修表的集合 |
| | | List<DeviceMetricRecordAndMaintenanceDto> deviceMetricRecordAndMaintenanceDtoList = new ArrayList<>(); |
| | | // 设备校准表和设备维修表的长度可能不一样 取最大值 不够的用空数据填充 |
| | | int metricRecordSize = deviceMetricRecordList.size(); |
| | | int maintenanceSize = deviceMaintenanceList.size(); |
| | | int size = Math.max(metricRecordSize, maintenanceSize); |
| | | // 给 校验和维修对象 赋值 |
| | | for (int i = 0; i < size; i++) { |
| | | // 校验和维修对象 |
| | | DeviceMetricRecordAndMaintenanceDto deviceMetricRecordAndMaintenanceDto = new DeviceMetricRecordAndMaintenanceDto(); |
| | | // 设置序号 |
| | | deviceMetricRecordAndMaintenanceDto.setIndex(i + 1); |
| | | |
| | | // 校准表数据 |
| | | if (metricRecordSize > i) { |
| | | // 设置日期格式 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | // 获取设备校准表数据 |
| | | DeviceMetricRecord deviceMetricRecord = deviceMetricRecordList.get(i); |
| | | // 设置校准日期 |
| | | deviceMetricRecordAndMaintenanceDto.setCalibrationDateString(sdf.format(deviceMetricRecord.getCalibrationDate())); |
| | | // 设置证书编号 |
| | | deviceMetricRecordAndMaintenanceDto.setCertificateNumber(deviceMetricRecord.getCertificateSerialNumber()); |
| | | // 设置有效期 |
| | | deviceMetricRecordAndMaintenanceDto.setValidityDateString(sdf.format(deviceMetricRecord.getNextCalibrationDate())); |
| | | // 设置校准有效日期 |
| | | deviceMetricRecordAndMaintenanceDto.setValidityDateString(sdf.format(deviceMetricRecord.getConfirmDate())); |
| | | // 设置检验结果 |
| | | deviceMetricRecordAndMaintenanceDto.setJudgement(deviceMetricRecord.getStatus()); |
| | | } |
| | | |
| | | // 维修表数据 |
| | | if (maintenanceSize > i) { |
| | | // 获取设备维修表数据 |
| | | DeviceMaintenance deviceMaintenance = deviceMaintenanceList.get(i); |
| | | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | // 维修日期 |
| | | deviceMetricRecordAndMaintenanceDto.setMaintenanceDateString(deviceMaintenance.getDate().format(dateTimeFormatter)); |
| | | // 处理方法 |
| | | deviceMetricRecordAndMaintenanceDto.setHandlingMethod(deviceMaintenance.getContent()); |
| | | // 备注 |
| | | deviceMetricRecordAndMaintenanceDto.setComments(deviceMaintenance.getComments()); |
| | | } |
| | | |
| | | deviceMetricRecordAndMaintenanceDtoList.add(deviceMetricRecordAndMaintenanceDto); |
| | | } |
| | | return deviceMetricRecordAndMaintenanceDtoList; |
| | | } |
| | | |
| | | /** |
| | | * 给档案加序号 并左右分为左右两列在word中显示 |
| | | * |
| | | * @param documentList 档案列表 |
| | | * @param documentExportWordDtoList 返回给word的数据列表 |
| | | */ |
| | | private static void extracted(List<Document> documentList, List<DocumentExportWordDto> documentExportWordDtoList) { |
| | | // 给档案加序号 并且分为左右两个列表在word中显示 |
| | | for (int i = 0; i < documentList.size(); i++) { |
| | | // 创建word表格中一行的数据对象 |
| | | DocumentExportWordDto documentExportWordDto = new DocumentExportWordDto(); |
| | | // 获取档案信息 |
| | | Document document = documentList.get(i); |
| | | // 格式化日期 |
| | | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | // 根据序号 分别加入两个列表 |
| | | if (i % 2 == 0) { |
| | | // 奇数在左列 |
| | | documentExportWordDto.setIndex1(i + 1); |
| | | documentExportWordDto.setName1(document.getName()); |
| | | documentExportWordDto.setQuantity1(document.getQuantity()); |
| | | documentExportWordDto.setPageCount1(document.getPageCount()); |
| | | documentExportWordDto.setArchiveDateString1(document.getProvideDate().format(dateTimeFormatter)); |
| | | } else { |
| | | // 偶数在右列 |
| | | documentExportWordDto.setIndex2(i + 1); |
| | | documentExportWordDto.setName2(document.getName()); |
| | | documentExportWordDto.setQuantity2(document.getQuantity()); |
| | | documentExportWordDto.setPageCount2(document.getPageCount()); |
| | | documentExportWordDto.setArchiveDateString2(document.getProvideDate().format(dateTimeFormatter)); |
| | | } |
| | | // 把一行数据对象加入列表 |
| | | documentExportWordDtoList.add(documentExportWordDto); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void exportEquipmentDetails(HttpServletResponse response) { |
| | | List<Device> deviceList = baseMapper.selectList(null); |
| | | List<DeviceExport> deviceExportList = new ArrayList<>(); |
| | | |
| | | int index = 1; |
| | | for (Device device : deviceList) { |
| | | Integer equipmentManager = device.getEquipmentManager(); |
| | | String equipmentManagerName = null; |
| | | if (equipmentManager != null) { |
| | | User user = userMapper.selectById(equipmentManager); |
| | | if (user != null) { |
| | | equipmentManagerName = user.getName(); |
| | | } |
| | | } |
| | | DeviceExport deviceExport = new DeviceExport(); |
| | | BeanUtils.copyProperties(device, deviceExport); |
| | | deviceExport.setIndex(index); |
| | | deviceExport.setEquipmentManagerName(equipmentManagerName); |
| | | deviceExportList.add(deviceExport); |
| | | index++; |
| | | } |
| | | |
| | | // 获取路径 |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/word/quipment-details.docx"); |
| | | Configure configure = Configure.builder() |
| | | .bind("deviceList", new HackLoopTableRenderPolicy()) |
| | | .build(); |
| | | XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render( |
| | | new HashMap<String, Object>() {{ |
| | | put("deviceList", deviceExportList); |
| | | }}); |
| | | |
| | | try { |
| | | response.setContentType("application/msword"); |
| | | String fileName = URLEncoder.encode( |
| | | "仪器设备一览表", "UTF-8"); |
| | | response.setHeader("Content-disposition", |
| | | "attachment;filename=" + fileName + ".docx"); |
| | | OutputStream os = response.getOutputStream(); |
| | | template.write(os); |
| | | os.flush(); |
| | | os.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("导出失败"); |
| | | } |
| | | } |
| | | } |