TMeasureThicknessController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package com.ruoyi.project.sems.controller;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.math.BigDecimal;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.*;
  8. import com.alibaba.fastjson.JSON;
  9. import com.ruoyi.common.utils.DateUtils;
  10. import com.ruoyi.common.utils.document.ChartUtil;
  11. import com.ruoyi.common.utils.document.DocumentHandler;
  12. import com.ruoyi.common.utils.document.PDFTemplateUtil;
  13. import com.ruoyi.common.utils.file.ExcelUtils;
  14. import com.ruoyi.common.utils.file.FileUploadUtils;
  15. import com.ruoyi.framework.config.RuoYiConfig;
  16. import com.ruoyi.project.sems.domain.TMeasureRecord;
  17. import com.ruoyi.project.sems.domain.TSpecdevCc;
  18. import com.ruoyi.project.sems.service.ITMeasureRecordService;
  19. import com.ruoyi.project.system.domain.SysDept;
  20. import com.ruoyi.project.system.domain.SysDictData;
  21. import freemarker.template.Template;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.apache.poi.ss.usermodel.Cell;
  24. import org.apache.poi.ss.usermodel.Row;
  25. import org.apache.poi.ss.usermodel.Sheet;
  26. import org.apache.poi.ss.usermodel.Workbook;
  27. import org.jfree.data.category.DefaultCategoryDataset;
  28. import org.springframework.security.access.prepost.PreAuthorize;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.web.bind.annotation.*;
  31. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  32. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  33. import com.ruoyi.project.sems.domain.TMeasureThickness;
  34. import com.ruoyi.project.sems.service.ITMeasureThicknessService;
  35. import com.ruoyi.framework.web.controller.BaseController;
  36. import com.ruoyi.framework.web.domain.AjaxResult;
  37. import com.ruoyi.common.utils.poi.ExcelUtil;
  38. import com.ruoyi.framework.web.page.TableDataInfo;
  39. import org.springframework.web.multipart.MultipartFile;
  40. import javax.servlet.http.HttpServletRequest;
  41. import javax.servlet.http.HttpServletResponse;
  42. /**
  43. * 定点测厚Controller
  44. *
  45. * @author ruoyi
  46. * @date 2021-07-07
  47. */
  48. @RestController
  49. @RequestMapping("/sems/thickness")
  50. public class TMeasureThicknessController extends BaseController
  51. {
  52. @Autowired
  53. private ITMeasureThicknessService tMeasureThicknessService;
  54. @Autowired
  55. private ITMeasureRecordService tMeasureRecordService;
  56. /**
  57. * 查询定点测厚列表
  58. */
  59. @PreAuthorize("@ss.hasPermi('sems:thickness:list')")
  60. @GetMapping("/list")
  61. public TableDataInfo list(TMeasureThickness tMeasureThickness)
  62. {
  63. startPage();
  64. List<TMeasureThickness> list = tMeasureThicknessService.selectTMeasureThicknessList(tMeasureThickness);
  65. for (TMeasureThickness m : list) {
  66. List<TMeasureRecord> measureRecords = tMeasureRecordService.queryRecords(m.getId().toString());
  67. if (measureRecords.size() > 0) {
  68. m.setFirstMeasureDate(measureRecords.get(measureRecords.size() - 1).getMeasureDate());
  69. m.setNewMeasureDate(measureRecords.get(0).getMeasureDate());
  70. // BigDecimal measureCycle = new BigDecimal(m.getMeasureCycle());
  71. // BigDecimal yearNum = new BigDecimal("31536000000");
  72. // Long cycle = measureCycle.multiply(yearNum).longValue();
  73. // Long newTime = measureRecords.get(0).getMeasureDate().getTime();
  74. // Long nextTime = newTime;
  75. // Date dat = new Date(nextTime);
  76. // GregorianCalendar nextMeasureDate = new GregorianCalendar();
  77. // nextMeasureDate.setTime(dat);
  78. // java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
  79. // m.setNextMeasureDate(format.format(nextMeasureDate.getTime()));
  80. }else {
  81. m.setFirstMeasureDate(null);
  82. m.setNewMeasureDate(null);
  83. m.setNextMeasureDate(null);
  84. }
  85. //寿命预警
  86. if (m.getNextWarnDate() != null && !StringUtils.isEmpty(m.getEstRemain())) {
  87. int day = this.formatDate(m.getNextWarnDate(),new Date());
  88. BigDecimal remain = new BigDecimal(m.getEstRemain()).multiply(new BigDecimal("365")).setScale(4,BigDecimal.ROUND_HALF_DOWN);
  89. logger.info(day +"::::" + remain);
  90. if (remain.compareTo(new BigDecimal(day)) == -1) {
  91. m.setWarnFlag(1);
  92. }else {
  93. m.setWarnFlag(0);
  94. }
  95. }else {
  96. m.setWarnFlag(0);
  97. }
  98. }
  99. return getDataTable(list);
  100. }
  101. /**
  102. * 批量导入
  103. */
  104. @PreAuthorize("@ss.hasPermi('sems:thickness:add')")
  105. @Log(title = "侧厚批量导入", businessType = BusinessType.INSERT)
  106. @PostMapping("/importData")
  107. public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException
  108. {
  109. //获取操作人员ID
  110. Long userId = getUserId();
  111. //报错行数统计
  112. List<Integer> failRow =new ArrayList<Integer>();
  113. Workbook workbook = ExcelUtils.getWorkBook(file);
  114. Sheet sheet = workbook.getSheetAt(0);
  115. List<TMeasureThickness> list = new ArrayList<TMeasureThickness>();
  116. //字典查询
  117. int rowNum = sheet.getPhysicalNumberOfRows();
  118. int failNumber = 0;
  119. for (int i = 1; i < rowNum; i++) {
  120. try {
  121. logger.info("读取行数:" + i);
  122. Row row = sheet.getRow(i);
  123. int cellNum = row.getLastCellNum();
  124. TMeasureThickness entity = new TMeasureThickness();
  125. for (int j = 0; j < cellNum; j++) {
  126. Cell cell = row.getCell(j);
  127. if (cell == null) {
  128. continue;
  129. }
  130. String cellValue = ExcelUtils.getCellValue(cell);
  131. logger.info("cellValue:" + cellValue);
  132. if (j == 0) {
  133. //序号
  134. } else if (j == 1) {
  135. entity.setPlantCode(cellValue);//装置名称
  136. } else if (j == 2) {
  137. entity.setUnitCode(cellValue);//单元名称
  138. } else if (j == 3) {
  139. entity.setTagno(cellValue);//单位内编号
  140. } else if (j == 4) {
  141. entity.setPosition(cellValue);//测厚部位CML
  142. } else if (j == 5) {
  143. entity.setMeasureNo(cellValue);//检测编号
  144. } else if (j == 6) {
  145. entity.setEquipmentName(cellValue);//设备/管线名称
  146. } else if (j == 7) {
  147. entity.setMaterial(cellValue);//材质
  148. } else if (j == 8) {
  149. entity.setNominalTickness(cellValue);//公称壁厚(mm)
  150. } else if (j == 9) {
  151. entity.setThicknessMin(cellValue);//最小允许壁厚(mm)
  152. } else if (j == 10) {
  153. entity.setStCorrosion(cellValue);//短期腐蚀速率(mm/year)
  154. } else if (j == 11) {
  155. entity.setLtCorrosion(cellValue);//长期腐蚀速率(mm/year)
  156. } else if (j == 12) {
  157. if (cellValue.length() > 3) {//下次测厚日期
  158. entity.setNextWarnDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  159. }
  160. } else if (j == 13) {
  161. entity.setInspectionMethod(cellValue);//检测方法
  162. } else if (j == 14) {
  163. entity.setCorAllowance(cellValue);//腐蚀裕度(mm)
  164. } else if (j == 15) {
  165. entity.setOriginalThickness(cellValue);//原始壁厚(mm)
  166. } else if (j == 16) {
  167. entity.setMedium(cellValue);//介质
  168. } else if (j == 17) {
  169. entity.setPressure(cellValue);//压力(MPa)
  170. } else if (j == 18) {
  171. entity.setSpecification(cellValue); //规格
  172. } else if (j == 19) {
  173. entity.setFlowRate(cellValue);//流速(m/s)
  174. } else if (j == 20) {
  175. //温度(℃)
  176. entity.setTemperature(cellValue);
  177. } else if (j == 21) {
  178. //腐蚀类型
  179. entity.setCorrosionType(cellValue);
  180. } else if (j == 22) {
  181. entity.setAnalysis(cellValue);//原因分析
  182. }else if (j == 23) {
  183. entity.setMethodCause(cellValue);//治理方法及依据
  184. } else if (j == 24) {
  185. entity.setEffectTracing(cellValue); //效果跟踪
  186. }else if (j == 25) {
  187. entity.setRaiser(cellValue);//提出人
  188. }else if (j == 26) {
  189. if (cellValue.length() > 3) {//提出时间
  190. entity.setRaiserDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  191. }
  192. }else if (j == 27) {
  193. entity.setOtherContent(cellValue);//其他检测方法内容
  194. }else if (j == 28) {
  195. entity.setRecorder(cellValue);//记录人
  196. }else if (j == 29) {
  197. if (cellValue.length() > 3) {//记录时间
  198. entity.setRecorderDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  199. }
  200. }else if (j == 30) {
  201. entity.setRemarks(cellValue);//备注
  202. }
  203. }
  204. entity.setCreateBy(userId.toString());
  205. logger.info("entity:" + entity);
  206. list.add(entity);
  207. }catch (Exception e){
  208. failNumber++;
  209. logger.info("e:" + JSON.toJSONString(e));
  210. failRow.add(i + 1);
  211. }
  212. }
  213. int successNumber = 0;
  214. int failNum = 0;
  215. for (TMeasureThickness t : list
  216. ) {
  217. failNum++;
  218. try {
  219. tMeasureThicknessService.insertTMeasureThickness(t);
  220. successNumber++;
  221. }catch (Exception e){
  222. failNumber++;
  223. logger.info("e:" + e);
  224. failRow.add(failNum + 1);
  225. }
  226. }
  227. logger.info("list:" + JSON.toJSONString(list));
  228. logger.info("successNumber:" +String.valueOf(successNumber));
  229. logger.info("failNumber:" +String.valueOf(failNumber));
  230. logger.info("failRow:" +String.valueOf(failRow));
  231. return AjaxResult.success(String.valueOf(successNumber), failRow);
  232. }
  233. /**
  234. * 导出定点测厚列表
  235. */
  236. @PreAuthorize("@ss.hasPermi('sems:thickness:export')")
  237. @Log(title = "定点测厚", businessType = BusinessType.EXPORT)
  238. @GetMapping("/export")
  239. public AjaxResult export(TMeasureThickness tMeasureThickness)
  240. {
  241. List<TMeasureThickness> list = tMeasureThicknessService.selectTMeasureThicknessList(tMeasureThickness);
  242. ExcelUtil<TMeasureThickness> util = new ExcelUtil<TMeasureThickness>(TMeasureThickness.class);
  243. return util.exportExcel(list, "thickness");
  244. }
  245. /**
  246. * 获取定点测厚详细信息
  247. */
  248. @PreAuthorize("@ss.hasPermi('sems:thickness:query')")
  249. @GetMapping(value = "/{id}")
  250. public AjaxResult getInfo(@PathVariable("id") Long id)
  251. {
  252. //生成图表
  253. String chartUrl = "/profile/" + this.getRecordUrl(id.toString());
  254. TMeasureThickness t = tMeasureThicknessService.selectTMeasureThicknessById(id);
  255. t.setRecordUrl(chartUrl);
  256. return AjaxResult.success(t);
  257. }
  258. /**
  259. * 新增定点测厚
  260. */
  261. @PreAuthorize("@ss.hasPermi('sems:thickness:add')")
  262. @Log(title = "定点测厚", businessType = BusinessType.INSERT)
  263. @PostMapping
  264. public AjaxResult add(@RequestBody TMeasureThickness tMeasureThickness)
  265. {
  266. return toAjax(tMeasureThicknessService.insertTMeasureThickness(tMeasureThickness));
  267. }
  268. /**
  269. * 修改定点测厚
  270. */
  271. @PreAuthorize("@ss.hasPermi('sems:thickness:edit')")
  272. @Log(title = "定点测厚", businessType = BusinessType.UPDATE)
  273. @PutMapping
  274. public AjaxResult edit(@RequestBody TMeasureThickness tMeasureThickness)
  275. {
  276. return toAjax(tMeasureThicknessService.updateTMeasureThickness(tMeasureThickness));
  277. }
  278. /**
  279. * 删除定点测厚
  280. */
  281. @PreAuthorize("@ss.hasPermi('sems:thickness:remove')")
  282. @Log(title = "定点测厚", businessType = BusinessType.DELETE)
  283. @DeleteMapping("/{ids}")
  284. public AjaxResult remove(@PathVariable Long[] ids)
  285. {
  286. return toAjax(tMeasureThicknessService.deleteTMeasureThicknessByIds(ids));
  287. }
  288. /**
  289. * 定点测厚图片上传
  290. */
  291. @Log(title = "定点测厚图片上传", businessType = BusinessType.UPDATE)
  292. @PostMapping("/uploadFile")
  293. public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws IOException
  294. {
  295. if (!file.isEmpty())
  296. {
  297. String avatar = FileUploadUtils.upload(RuoYiConfig.getFilePath("/thickness"), file);
  298. return AjaxResult.success(avatar);
  299. }
  300. return AjaxResult.error("上传图片异常,请联系管理员");
  301. }
  302. @RequestMapping("/measure/exportPDF")
  303. public String measure(@RequestParam String id, HttpServletRequest request, HttpServletResponse response) {
  304. OutputStream out= null;
  305. try {
  306. getRecordUrl(id);
  307. out = response.getOutputStream();
  308. logger.info("id:" + id);
  309. //获取信息,就是上面的结构
  310. HashMap<String, Object> map = new HashMap<>();
  311. DocumentHandler dh = new DocumentHandler();
  312. Template t = dh.getTemplate();
  313. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  314. TMeasureThickness measureThicknessEntity = tMeasureThicknessService.selectTMeasureThicknessById(Long.parseLong(id));
  315. //生成图表
  316. String chartUrl = this.getRecordUrl(id);
  317. map.put("chartUrl", chartUrl);
  318. logger.info(chartUrl);
  319. //测量方法
  320. String[] inspectionMethod = measureThicknessEntity.getInspectionMethod().split(",");
  321. logger.info("inspectionMethod:" + inspectionMethod);
  322. for (String m : inspectionMethod) {
  323. if (m.equals("1")){ measureThicknessEntity.setMethodUT(true); }
  324. if (m.equals("2")){ measureThicknessEntity.setMethodPEC(true); }
  325. if (m.equals("3")){ measureThicknessEntity.setMethodRT(true); }
  326. if (m.equals("4")){ measureThicknessEntity.setMethodEMAT(true); }
  327. if (m.equals("5")){ measureThicknessEntity.setMethodOther(true); }
  328. }
  329. map.put("measure", measureThicknessEntity);
  330. logger.info("measure:" + measureThicknessEntity);
  331. if(StringUtils.isNotEmpty(measureThicknessEntity.getPhoto())){
  332. String[] photos = measureThicknessEntity.getPhoto().split(",");
  333. if (photos.length == 1) {
  334. map.put("photo1", photos[0].replaceAll("/profile/",""));
  335. }else if (photos.length == 2) {
  336. map.put("photo1", photos[0].replaceAll("/profile/",""));
  337. map.put("photo2", photos[1].replaceAll("/profile/",""));
  338. }else if (photos.length == 3) {
  339. map.put("photo1", photos[0].replaceAll("/profile/",""));
  340. map.put("photo2", photos[1].replaceAll("/profile/",""));
  341. map.put("photo3", photos[2].replaceAll("/profile/",""));
  342. }
  343. }
  344. if(StringUtils.isNotEmpty(measureThicknessEntity.getLocationUrl())){
  345. String[] locationUrls = measureThicknessEntity.getLocationUrl().split(",");
  346. if (locationUrls.length == 1) {
  347. map.put("locationUrl1", locationUrls[0].replaceAll("/profile/",""));
  348. }else if (locationUrls.length == 2) {
  349. map.put("locationUrl1", locationUrls[0].replaceAll("/profile/",""));
  350. map.put("locationUrl2", locationUrls[1].replaceAll("/profile/",""));
  351. logger.info(locationUrls[0]);
  352. }else if (locationUrls.length == 3) {
  353. map.put("locationUrl1", locationUrls[0].replaceAll("/profile/",""));
  354. map.put("locationUrl2", locationUrls[1].replaceAll("/profile/",""));
  355. map.put("locationUrl3", locationUrls[2].replaceAll("/profile/",""));
  356. }
  357. }
  358. //测量记录分行
  359. List<TMeasureRecord> measureRecords = tMeasureRecordService.queryRecords(id);
  360. List<List<TMeasureRecord>> result = new ArrayList<List<TMeasureRecord>>();
  361. int sourceSize = measureRecords.size();
  362. int size = (measureRecords.size() / 7) + 1;
  363. if (measureRecords.size() % 7 == 0) {
  364. size = size - 1;
  365. }
  366. for (int i = 0; i < size; i++) {
  367. List<TMeasureRecord> subset = new ArrayList<TMeasureRecord>();
  368. for (int j = i * 7; j < (i + 1) * 7; j++) {
  369. if (j < sourceSize) {
  370. subset.add(measureRecords.get(j));
  371. }
  372. }
  373. if (subset.size() < 7) {
  374. int supplement = 7 - subset.size();
  375. for (int m = 0; m < supplement; m++) {
  376. TMeasureRecord nullRecord = new TMeasureRecord();
  377. subset.add(nullRecord);
  378. }
  379. }
  380. result.add(subset);
  381. }
  382. map.put("result", result);
  383. logger.info("result:" + result);
  384. String name = measureThicknessEntity.getPlantCode() +"-" +measureThicknessEntity.getTagno() + "-" + measureThicknessEntity.getPosition();
  385. PDFTemplateUtil.exportPdf("measureFMaker.ftl", name + ".pdf","file:"+RuoYiConfig.getProfile(), map, response);
  386. // PDFTemplateUtil.exportPdf("measureFMaker.ftl", name + ".pdf",RuoYiConfig.getProfile(), map, response);
  387. } catch (Exception e) {
  388. e.printStackTrace();
  389. }finally {
  390. if(out!=null)
  391. try {
  392. out.close();
  393. } catch (IOException e) {
  394. e.printStackTrace();
  395. }
  396. }
  397. return null;
  398. }
  399. public String getRecordUrl(String id){
  400. TMeasureThickness measureThicknessEntity = tMeasureThicknessService.selectTMeasureThicknessById(Long.parseLong(id));
  401. List<TMeasureRecord> measureRecords = tMeasureRecordService.queryRecords(id);
  402. Collections.reverse(measureRecords);
  403. //种类数据集
  404. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  405. SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
  406. for (TMeasureRecord t: measureRecords
  407. ) {
  408. dataset.addValue(t.getMeasureValue() , "数值Value" , sdf.format(t.getMeasureDate()));
  409. if (measureThicknessEntity.getThicknessMin() != null){
  410. dataset.addValue(Double.parseDouble(measureThicknessEntity.getThicknessMin()) , "最小允许壁厚" , sdf.format(t.getMeasureDate()));
  411. }
  412. }
  413. String url = RuoYiConfig.getProfile() + "/" + id +".jpg";
  414. ChartUtil.createLinePort("" , dataset ,"" ,"" , url ) ;
  415. return id +".jpg";
  416. }
  417. private int formatDate(Date nowDate, Date sqlDate){
  418. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  419. String newDate = df.format(nowDate);
  420. String sqlDate1 = df.format(sqlDate);
  421. try {
  422. nowDate = df.parse(newDate);
  423. sqlDate = df.parse(sqlDate1);
  424. } catch (Exception e) {
  425. e.printStackTrace();
  426. }
  427. Long time = nowDate.getTime();
  428. Long time2 = sqlDate.getTime();
  429. int day = (int) ((time - time2) / (24*3600*1000));
  430. return day;
  431. }
  432. }