| | |
| | | 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.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.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | |
| | | @Autowired |
| | | private DataConfigService dataConfigService; |
| | | |
| | | private InsSampleMapper insSampleMapper; |
| | | |
| | | private DocumentService documentService; |
| | | |
| | | private DeviceMetricRecordMapper deviceMetricRecordMapper; |
| | | |
| | | private DeviceMaintenanceMapper deviceMaintenanceMapper; |
| | | |
| | | @Override |
| | | public List<User> selectUserList() { |
| | |
| | | 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("导出失败"); |
| | | } |
| | | } |
| | | } |