WellInfoController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package com.bowintek.practice.controller;
  2. import com.alibaba.druid.support.logging.Log;
  3. import com.aspose.cells.License;
  4. import com.aspose.cells.PdfSaveOptions;
  5. import com.aspose.cells.SaveFormat;
  6. import com.aspose.cells.SaveOptions;
  7. import com.bowintek.practice.AppConfig;
  8. import com.bowintek.practice.filter.exception.BaseResponse;
  9. import com.bowintek.practice.filter.exception.RespGenerstor;
  10. import com.bowintek.practice.model.WlUserExpendSetting;
  11. import com.bowintek.practice.services.service.AccountService;
  12. import com.bowintek.practice.services.service.OrganizationService;
  13. import com.bowintek.practice.services.service.WellInfoService;
  14. import com.bowintek.practice.util.RemoteHelper;
  15. import com.bowintek.practice.vo.query.WellInfoParams;
  16. import com.github.pagehelper.PageInfo;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.poi.hssf.usermodel.*;
  19. import org.apache.poi.hwpf.HWPFDocument;
  20. import org.apache.poi.hwpf.extractor.WordExtractor;
  21. import org.apache.poi.ss.usermodel.*;
  22. import org.apache.poi.ss.util.CellRangeAddress;
  23. import org.apache.poi.util.Units;
  24. import org.apache.poi.xssf.usermodel.*;
  25. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  26. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  27. import org.apache.poi.xwpf.usermodel.XWPFRun;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.web.bind.annotation.*;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.io.*;
  32. import java.net.URLEncoder;
  33. import java.nio.charset.StandardCharsets;
  34. import java.util.*;
  35. @RestController
  36. @RequestMapping(value = "/api/wellInfo")
  37. @Slf4j
  38. public class WellInfoController {
  39. @Autowired
  40. private WellInfoService wellInfoService;
  41. @Autowired
  42. private OrganizationService organizationService;
  43. @Autowired
  44. private AccountService accountService;
  45. @Autowired
  46. private RemoteHelper remoteHelper;
  47. @Autowired
  48. private AppConfig appConfig;
  49. @ResponseBody
  50. @PostMapping("/getList")
  51. public BaseResponse<PageInfo<HashMap<String, Object>>> getList(@RequestBody WellInfoParams params) throws Exception {
  52. PageInfo<HashMap<String, Object>> result = wellInfoService.getList(params.getPage(), params.getRows(), params);
  53. return RespGenerstor.success(result);
  54. }
  55. @ResponseBody
  56. @GetMapping("/getMapList")
  57. public BaseResponse<PageInfo<HashMap<String, Object>>> getMapList(@RequestParam("page") int page, @RequestParam("rows") int rows,
  58. WellInfoParams params) throws Exception {
  59. PageInfo<HashMap<String, Object>> result = wellInfoService.getMapList(page, rows, params);
  60. return RespGenerstor.success(result);
  61. }
  62. @GetMapping("/getOrganizationTree")
  63. public BaseResponse getOrganizationTree() {
  64. return RespGenerstor.success(organizationService.getListTree("HBlQAIBGob"));
  65. }
  66. @GetMapping("/getWellInfo")
  67. public BaseResponse getWellInfo(String well_id) {
  68. HashMap<String, Object> wellInfo = wellInfoService.getWellInfo(well_id);
  69. List<HashMap<String, Object>> boreholeInterList = wellInfoService.selectBoreholeInterList(well_id);
  70. List<HashMap<String, Object>> testHistoryList = wellInfoService.selectTestHistoryList(well_id);
  71. List<HashMap<String, Object>> testHistorySummary = wellInfoService.selectTestHistorySummary(well_id);
  72. HashMap<String, Object> lastTestHistory = wellInfoService.getLastTestHistory(well_id);
  73. List<HashMap<String, Object>> analyticalAssaysList = wellInfoService.selectAnalyticalAssaysList(well_id);
  74. List<HashMap<String, Object>> analyticalAssaysSummary = wellInfoService.selectAnalyticalAssaysSummary(well_id);
  75. List<HashMap<String, Object>> historyAssignmentList = wellInfoService.selectHistoryAssignmentList(well_id);
  76. List<HashMap<String, Object>> historyAssignmentSummary = wellInfoService.selectHistoryAssignmentSummary(well_id);
  77. HashMap<String, Object> lastHistoryAssignment = wellInfoService.getLastHistoryAssignment(well_id);
  78. List<HashMap<String, Object>> dataIndexList = wellInfoService.selectDataIndexList(well_id);
  79. HashMap<String, Object> result = new HashMap<>();
  80. result.put("dataModel", wellInfo);
  81. result.put("boreholeInterList", boreholeInterList);//业务解释列表
  82. result.put("testHistoryList", testHistoryList);//测试历史列表
  83. result.put("testHistorySummary", testHistorySummary);//测试历史汇总
  84. result.put("lastTestHistory", lastTestHistory);//最后一次测试历史
  85. result.put("analyticalAssaysList", analyticalAssaysList);//分析化验列表
  86. result.put("analyticalAssaysSummary", analyticalAssaysSummary);//分析化验汇总
  87. result.put("historyAssignmentList", historyAssignmentList);//作业简史列表
  88. result.put("historyAssignmentSummary", historyAssignmentSummary);//作业简史汇总
  89. result.put("lastHistoryAssignment", lastHistoryAssignment);//最后一次作业简史记录
  90. result.put("dataIndexList", dataIndexList);
  91. return RespGenerstor.success(result);
  92. }
  93. @ResponseBody
  94. @PostMapping("/saveExpendSetting")
  95. public BaseResponse saveExpendSetting(@RequestBody WlUserExpendSetting model) {
  96. int count = 0;
  97. try {
  98. model.setUserID(accountService.getLoginUserID());
  99. count = wellInfoService.saveExpendSetting(model);
  100. } catch (Exception e) {
  101. return RespGenerstor.fail("-1", "程序异常:" + e.getMessage());
  102. }
  103. return RespGenerstor.success(count);
  104. }
  105. @GetMapping("/getExpendSetting")
  106. public BaseResponse getExpendSetting(String wellId) {
  107. return RespGenerstor.success(wellInfoService.getExpendSetting(wellId, accountService.getLoginUserID()));
  108. }
  109. @GetMapping("/getWellDocumentList")
  110. public BaseResponse getWellDocumentList(String wellId) {
  111. List<HashMap<String, Object>> dataIndexList = wellInfoService.selectDataIndexList(wellId);
  112. return RespGenerstor.success(dataIndexList);
  113. }
  114. @GetMapping("/getConstructUnitTree")
  115. public BaseResponse getConstructUnitTree() {
  116. return RespGenerstor.success(wellInfoService.getConstructUnitTree());
  117. }
  118. @GetMapping("/downFile")
  119. public void downFile(HttpServletResponse response, Integer isShow, String filePath, String fileName) throws IOException {
  120. Map<String, String> getParams = new HashMap<>();
  121. getParams.put("fileName", fileName);
  122. getParams.put("filePath", filePath);
  123. String reData = remoteHelper.getJson(getParams, appConfig.hdfshelperurl + "read", "UTF-8");
  124. byte[] bytes = Base64.getMimeDecoder().decode(reData);
  125. OutputStream os = response.getOutputStream();
  126. if (isShow != null && 1 == isShow) {
  127. //如果是预览需要把doc转docx,xls转xlsx,前端预览插件不支持这两种格式
  128. if (fileName.toLowerCase().endsWith(".doc")) {
  129. bytes = doc2Docx(bytes);
  130. } else if (fileName.toLowerCase().endsWith(".xls")) {
  131. bytes = xls2pdf(bytes);
  132. }
  133. }
  134. response.setContentType("multipart/form-data;charset=UTF-8");
  135. response.addHeader("Content-Disposition",
  136. "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));// 设置文件名
  137. response.setHeader("Content-Length", String.valueOf(bytes.length));
  138. os.write(bytes);
  139. if (null != os) {
  140. os.flush();
  141. os.close();
  142. }
  143. }
  144. private byte[] xls2pdf(byte[] bytes) {
  145. long old = System.currentTimeMillis();
  146. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  147. try {
  148. License aposeLic = new License();
  149. InputStream license = ClassLoader.getSystemClassLoader().getResourceAsStream("license-cell.xml");
  150. aposeLic.setLicense(license);
  151. com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(new ByteArrayInputStream(bytes));
  152. PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
  153. pdfSaveOptions.setOnePagePerSheet(true);
  154. int[] autoDrawSheets = {3};
  155. //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
  156. autoDraw(wb, autoDrawSheets);
  157. wb.save(outputStream, pdfSaveOptions);
  158. bytes = outputStream.toByteArray();
  159. outputStream.flush();
  160. long now = System.currentTimeMillis();
  161. log.info("共耗时:{}", ((now - old) / 1000.0));
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. } finally {
  165. if (outputStream != null) {
  166. try {
  167. outputStream.close();
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. } ;
  171. }
  172. }
  173. return bytes;
  174. }
  175. /**
  176. * 设置打印的sheet 自动拉伸比例
  177. *
  178. * @param wb
  179. * @param page 自动拉伸的页的sheet数组
  180. */
  181. public static void autoDraw(com.aspose.cells.Workbook wb, int[] page) {
  182. if (null != page && page.length > 0) {
  183. for (int i = 0; i < page.length; i++) {
  184. wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
  185. wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
  186. }
  187. }
  188. }
  189. private byte[] xls2xlsx(byte[] bytes) throws IOException {
  190. InputStream fis = new ByteArrayInputStream(bytes);
  191. try (HSSFWorkbook xlsWorkbook = new HSSFWorkbook(fis);
  192. XSSFWorkbook xlsxWorkbook = new XSSFWorkbook()) {
  193. for (int i = 0; i < xlsWorkbook.getNumberOfSheets(); i++) {
  194. HSSFSheet xlsSheet = xlsWorkbook.getSheetAt(i);
  195. XSSFSheet xlsxSheet = xlsxWorkbook.createSheet(xlsSheet.getSheetName());
  196. for (int j = 0; j <= xlsSheet.getLastRowNum(); j++) {
  197. HSSFRow xlsRow = xlsSheet.getRow(j);
  198. if (xlsRow == null) {
  199. continue;
  200. }
  201. XSSFRow xlsxRow = xlsxSheet.createRow(xlsRow.getRowNum());
  202. for (int k = 0; k < xlsRow.getLastCellNum(); k++) {
  203. HSSFCell xlsCell = xlsRow.getCell(k);
  204. if (xlsCell == null) {
  205. continue;
  206. }
  207. XSSFCell xlsxCell = xlsxRow.createCell(xlsCell.getColumnIndex());
  208. setCellValue(xlsxCell, xlsCell, xlsWorkbook);
  209. copyCellStyle(xlsxWorkbook, xlsxCell, xlsWorkbook, xlsCell);
  210. }
  211. }
  212. // 复制单元格合并信息
  213. List<CellRangeAddress> mergedRegions = xlsSheet.getMergedRegions();
  214. for (CellRangeAddress mergedRegion : mergedRegions) {
  215. CellRangeAddress targetMergedRegion = new CellRangeAddress(
  216. mergedRegion.getFirstRow(),
  217. mergedRegion.getLastRow(),
  218. mergedRegion.getFirstColumn(),
  219. mergedRegion.getLastColumn()
  220. );
  221. xlsxSheet.addMergedRegion(targetMergedRegion);
  222. }
  223. // 拷贝图片
  224. copyPicture(xlsSheet, xlsxSheet);
  225. // 复制列宽
  226. int columnCount = 0;
  227. if (xlsxSheet.getRow(0) != null) {
  228. // 假设第一行包含所有列,根据第一行的列数获取列数
  229. columnCount = xlsxSheet.getRow(0).getLastCellNum();
  230. }
  231. for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
  232. xlsxSheet.setColumnWidth(columnIndex, xlsSheet.getColumnWidth(columnIndex));
  233. }
  234. }
  235. try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();) {
  236. xlsxWorkbook.write(outputStream);
  237. bytes = outputStream.toByteArray();
  238. outputStream.close();
  239. xlsxWorkbook.close();
  240. }
  241. log.info("xls文件转换完成");
  242. } catch (IOException e) {
  243. e.printStackTrace();
  244. }
  245. return bytes;
  246. }
  247. // 拷贝图片
  248. public void copyPicture(HSSFSheet source, XSSFSheet destination) {
  249. // 获取sheet中的图片信息
  250. List<Map<String, Object>> mapList = getPicturesFromHSSFSheet(source);
  251. XSSFDrawing drawing = destination.createDrawingPatriarch();
  252. for (Map<String, Object> pictureMap : mapList) {
  253. HSSFClientAnchor hssfClientAnchor = (HSSFClientAnchor) pictureMap.get("pictureAnchor");
  254. HSSFRow startRow = source.getRow(hssfClientAnchor.getRow1());
  255. float startRowHeight = startRow == null ? source.getDefaultRowHeightInPoints() : startRow.getHeightInPoints();
  256. HSSFRow endRow = source.getRow(hssfClientAnchor.getRow1());
  257. float endRowHeight = endRow == null ? source.getDefaultRowHeightInPoints() : endRow.getHeightInPoints();
  258. // hssf的单元格,每个单元格无论宽高,都被分为 宽 1024个单位 高 256个单位。
  259. // 32.00f 为默认的单元格单位宽度 单元格宽度 / 默认宽度 为像素宽度
  260. XSSFClientAnchor xssfClientAnchor = drawing.createAnchor(
  261. (int) (source.getColumnWidth(hssfClientAnchor.getCol1()) / 32.00f
  262. / 1024 * hssfClientAnchor.getDx1() * Units.EMU_PER_PIXEL),
  263. (int) (startRowHeight / 256 * hssfClientAnchor.getDy1() * Units.EMU_PER_POINT),
  264. (int) (source.getColumnWidth(hssfClientAnchor.getCol2()) / 32.00f
  265. / 1024 * hssfClientAnchor.getDx2() * Units.EMU_PER_PIXEL),
  266. (int) (endRowHeight / 256 * hssfClientAnchor.getDy2() * Units.EMU_PER_POINT),
  267. hssfClientAnchor.getCol1(),
  268. hssfClientAnchor.getRow1(),
  269. hssfClientAnchor.getCol2(),
  270. hssfClientAnchor.getRow2());
  271. xssfClientAnchor.setAnchorType(hssfClientAnchor.getAnchorType());
  272. drawing.createPicture(xssfClientAnchor,
  273. destination.getWorkbook().addPicture((byte[]) pictureMap.get("pictureByteArray"),
  274. Integer.parseInt(pictureMap.get("pictureType").toString())));
  275. }
  276. }
  277. /**
  278. * 获取图片和位置 (xls)
  279. */
  280. public List<Map<String, Object>> getPicturesFromHSSFSheet(HSSFSheet sheet) {
  281. List<Map<String, Object>> mapList = new ArrayList<>();
  282. if (sheet.getDrawingPatriarch() == null) {
  283. return mapList;
  284. }
  285. List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
  286. for (HSSFShape shape : list) {
  287. if (shape instanceof HSSFPicture) {
  288. Map<String, Object> map = new HashMap<>();
  289. HSSFPicture picture = (HSSFPicture) shape;
  290. HSSFClientAnchor cAnchor = picture.getClientAnchor();
  291. HSSFPictureData pdata = picture.getPictureData();
  292. map.put("pictureAnchor", cAnchor);
  293. map.put("pictureByteArray", pdata.getData());
  294. map.put("pictureType", pdata.getPictureType());
  295. map.put("pictureSize", picture.getImageDimension());
  296. mapList.add(map);
  297. }
  298. }
  299. return mapList;
  300. }
  301. private void setCellValue(Cell newCell, Cell oldCell, HSSFWorkbook xlsWorkbook) {
  302. if (oldCell == null) {
  303. return;
  304. }
  305. try {
  306. switch (oldCell.getCellType()) {
  307. case STRING:
  308. newCell.setCellValue(oldCell.getStringCellValue());
  309. break;
  310. case NUMERIC:
  311. if (DateUtil.isCellDateFormatted(oldCell)) {
  312. newCell.setCellValue(oldCell.getDateCellValue());
  313. } else {
  314. newCell.setCellValue(oldCell.getNumericCellValue());
  315. }
  316. break;
  317. case BLANK:
  318. newCell.setCellType(CellType.BLANK);
  319. break;
  320. case BOOLEAN:
  321. newCell.setCellValue(oldCell.getBooleanCellValue());
  322. break;
  323. case FORMULA:
  324. // HSSFFormulaEvaluator evaluator=new HSSFFormulaEvaluator(xlsWorkbook);
  325. // CellValue tempCellValue = evaluator.evaluate(oldCell);
  326. newCell.setCellValue(oldCell.getNumericCellValue());
  327. break;
  328. default:
  329. }
  330. } catch (Exception ex) {
  331. System.out.println("setCellValue出错:" + ex.getMessage());
  332. log.info("setCellValue出错:" + ex.getMessage());
  333. }
  334. }
  335. private void copyCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell newCell, HSSFWorkbook hssfWorkbook, HSSFCell oldCell) {
  336. HSSFCellStyle oldCellStyle = oldCell.getCellStyle();
  337. // 创建一个XSSFCellStyle(新Excel格式)
  338. XSSFCellStyle newCellStyle = xssfWorkbook.createCellStyle();
  339. // 复制对齐方式
  340. newCellStyle.setAlignment(oldCellStyle.getAlignment());
  341. newCellStyle.setVerticalAlignment(oldCellStyle.getVerticalAlignment());
  342. // 复制字体属性
  343. XSSFFont newFont = xssfWorkbook.createFont();
  344. HSSFFont oldFont = oldCellStyle.getFont(hssfWorkbook);
  345. newFont.setFontName(oldFont.getFontName());
  346. newFont.setFontHeightInPoints(oldFont.getFontHeightInPoints());
  347. newFont.setColor(oldFont.getColor());
  348. newCellStyle.setFont(newFont);
  349. // 复制填充颜色
  350. newCellStyle.setFillPattern(oldCellStyle.getFillPattern());
  351. newCellStyle.setFillForegroundColor(oldCellStyle.getFillForegroundColor());
  352. newCellStyle.setFillBackgroundColor(oldCellStyle.getFillBackgroundColor());
  353. // 复制数据格式
  354. newCellStyle.setDataFormat(oldCellStyle.getDataFormat());
  355. // 文本换行
  356. newCellStyle.setWrapText(oldCellStyle.getWrapText());
  357. newCellStyle.setBorderBottom(oldCellStyle.getBorderBottom());
  358. newCellStyle.setBorderLeft(oldCellStyle.getBorderLeft());
  359. newCellStyle.setBorderRight(oldCellStyle.getBorderRight());
  360. newCellStyle.setBorderTop(oldCellStyle.getBorderTop());
  361. newCellStyle.setBottomBorderColor(oldCellStyle.getBottomBorderColor());
  362. newCellStyle.setDataFormat(oldCellStyle.getDataFormat());
  363. newCellStyle.setFillBackgroundColor(oldCellStyle.getFillBackgroundColor());
  364. newCellStyle.setFillPattern(oldCellStyle.getFillPattern());
  365. newCellStyle.setHidden(oldCellStyle.getHidden());
  366. newCellStyle.setIndention(oldCellStyle.getIndention());
  367. newCellStyle.setLeftBorderColor(oldCellStyle.getLeftBorderColor());
  368. newCellStyle.setLocked(oldCellStyle.getLocked());
  369. newCellStyle.setQuotePrefixed(oldCellStyle.getQuotePrefixed());
  370. newCellStyle.setReadingOrder(ReadingOrder.forLong(oldCellStyle.getReadingOrder()));
  371. newCellStyle.setRightBorderColor(oldCellStyle.getRightBorderColor());
  372. newCellStyle.setRotation(oldCellStyle.getRotation());
  373. newCell.setCellStyle(newCellStyle);
  374. }
  375. private byte[] doc2Docx(byte[] bytes) throws IOException {
  376. try {
  377. // 将doc文件的内容读取到XWPFDocument对象中
  378. InputStream fis = new ByteArrayInputStream(bytes);
  379. HWPFDocument doc = new HWPFDocument(fis);
  380. WordExtractor wordExtractor = new WordExtractor(doc);
  381. String text = wordExtractor.getText();
  382. XWPFDocument docx = new XWPFDocument();
  383. XWPFParagraph paragraph = docx.createParagraph();
  384. XWPFRun run = paragraph.createRun();
  385. run.setText(text);
  386. // 保存XWPFDocument对象到docx文件中
  387. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  388. docx.write(outputStream);
  389. bytes = outputStream.toByteArray();
  390. doc.close();
  391. docx.close();
  392. outputStream.close();
  393. } catch (Exception ex) {
  394. log.info("doc2Docx文件转换失败:" + ex.getMessage());
  395. }
  396. return bytes;
  397. }
  398. }