TValveCsocscController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package com.cpms.project.process.controller;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.cpms.common.annotation.Log;
  4. import com.cpms.common.core.controller.BaseController;
  5. import com.cpms.common.core.domain.AjaxResult;
  6. import com.cpms.common.core.page.TableDataInfo;
  7. import com.cpms.common.enums.BusinessType;
  8. import com.cpms.common.utils.DateUtils;
  9. import com.cpms.common.utils.file.ExcelUtils;
  10. import com.cpms.common.utils.poi.ExcelUtil;
  11. import com.cpms.project.process.domain.TValveCsocsc;
  12. import com.cpms.project.process.domain.TValveNonc;
  13. import com.cpms.project.process.service.ITValveCsocscService;
  14. import org.apache.poi.ss.usermodel.*;
  15. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  16. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.security.access.prepost.PreAuthorize;
  19. import org.springframework.web.bind.annotation.*;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import javax.servlet.http.HttpServletResponse;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. /**
  27. * 阀门铅封开关状态记录Controller
  28. *
  29. * @author admin
  30. * @date 2024-04-11
  31. */
  32. @RestController
  33. @RequestMapping("/process/csocsc")
  34. public class TValveCsocscController extends BaseController {
  35. @Autowired
  36. private ITValveCsocscService tValveCsocscService;
  37. /**
  38. * 查询阀门铅封开关状态记录列表
  39. */
  40. @PreAuthorize("@ss.hasPermi('process:csocsc:list')")
  41. @GetMapping("/list")
  42. public TableDataInfo list(TValveCsocsc tValveCsocsc) {
  43. startPage();
  44. List<TValveCsocsc> list = tValveCsocscService.selectTValveCsocscList(tValveCsocsc);
  45. return getDataTable(list);
  46. }
  47. /**
  48. * 导出阀门铅封开关状态记录列表
  49. */
  50. @PreAuthorize("@ss.hasPermi('process:csocsc:export')")
  51. @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.EXPORT)
  52. @PostMapping("/export")
  53. public void export(HttpServletResponse response, TValveCsocsc tValveCsocsc) {
  54. List<TValveCsocsc> list = tValveCsocscService.selectTValveCsocscList(tValveCsocsc);
  55. ExcelUtil<TValveCsocsc> util = new ExcelUtil<TValveCsocsc>(TValveCsocsc.class);
  56. util.exportExcel(response, list, "阀门铅封开关状态记录数据");
  57. }
  58. /**
  59. * 导出阀门铅封开关状态记录列表
  60. */
  61. @PreAuthorize("@ss.hasPermi('process:csocsc:export')")
  62. @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.EXPORT)
  63. @PostMapping("/exportTmpl")
  64. public void exportTmpl(HttpServletResponse response, TValveCsocsc tValveCsocsc) {
  65. List<TValveCsocsc> list = tValveCsocscService.selectTValveCsocscList(tValveCsocsc);
  66. try {
  67. String tempUrl = "static/template/process/csocscExport.xlsx"; // 模板文件
  68. InputStream is = null;
  69. is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl);
  70. XSSFWorkbook wb1 = null;
  71. wb1 = new XSSFWorkbook(is);
  72. SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 1000);
  73. Sheet sheet;
  74. if (wb instanceof SXSSFWorkbook) {
  75. SXSSFWorkbook sxssfWorkbook = (SXSSFWorkbook) wb;
  76. sheet = sxssfWorkbook.getXSSFWorkbook().getSheetAt(0);
  77. } else {
  78. sheet = wb.getSheetAt(0);
  79. }
  80. //填充数据
  81. int rowIndex = 5;
  82. int num = 1;
  83. Row originalRow = sheet.getRow(5);
  84. Cell originalcell = originalRow.getCell(0);
  85. // 获取单元格样式
  86. CellStyle originalStyle = originalcell.getCellStyle();
  87. for (TValveCsocsc t: list
  88. ) {
  89. Row row = sheet.createRow(rowIndex);
  90. row.createCell(0).setCellValue(num);
  91. row.createCell(1).setCellValue(t.getUnit());
  92. row.createCell(2).setCellValue(t.getVtNo());
  93. row.createCell(3).setCellValue(t.getPidNo());
  94. row.createCell(4).setCellValue(t.getLocationDes());
  95. row.createCell(5).setCellValue(t.getMedium());
  96. row.createCell(6).setCellValue(t.getValveType());
  97. row.createCell(7).setCellValue(t.getValveSize());
  98. row.createCell(8).setCellValue(t.getIdentifier());
  99. row.createCell(9).setCellValue(t.getValvePosition());
  100. row.createCell(10).setCellValue(t.getFirmlySecured());
  101. row.createCell(11).setCellValue(t.getResponsibility());
  102. row.createCell(12).setCellValue(t.getCheckedBy());
  103. row.createCell(13).setCellValue(t.getCheckBeforeSu());
  104. row.createCell(14).setCellValue(t.getPidStatus());
  105. row.createCell(15).setCellValue(t.getRiskLevel());
  106. row.createCell(16).setCellValue(t.getSheReview());
  107. row.createCell(17).setCellValue(t.getRemarks());
  108. //渲染样式
  109. for (int i = 0; i < 18; i++) {
  110. row.getCell(i).setCellStyle(originalStyle);
  111. }
  112. num++;
  113. rowIndex++;
  114. }
  115. // 生成文件返回下载地址
  116. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  117. response.setCharacterEncoding("utf-8");
  118. wb.write(response.getOutputStream());
  119. wb.close();
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. /**
  125. * 获取阀门铅封开关状态记录详细信息
  126. */
  127. @PreAuthorize("@ss.hasPermi('process:csocsc:query')")
  128. @GetMapping(value = "/{id}")
  129. public AjaxResult getInfo(@PathVariable("id") Long id) {
  130. return success(tValveCsocscService.selectTValveCsocscById(id));
  131. }
  132. /**
  133. * 新增阀门铅封开关状态记录
  134. */
  135. @PreAuthorize("@ss.hasPermi('process:csocsc:add')")
  136. @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.INSERT)
  137. @PostMapping
  138. public AjaxResult add(@RequestBody TValveCsocsc tValveCsocsc) {
  139. return toAjax(tValveCsocscService.insertTValveCsocsc(tValveCsocsc));
  140. }
  141. /**
  142. * 修改阀门铅封开关状态记录
  143. */
  144. @PreAuthorize("@ss.hasPermi('process:csocsc:edit')")
  145. @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.UPDATE)
  146. @PutMapping
  147. public AjaxResult edit(@RequestBody TValveCsocsc tValveCsocsc) {
  148. return toAjax(tValveCsocscService.updateTValveCsocsc(tValveCsocsc));
  149. }
  150. /**
  151. * 删除阀门铅封开关状态记录
  152. */
  153. @PreAuthorize("@ss.hasPermi('process:csocsc:remove')")
  154. @Log(title = "阀门铅封开关状态记录", businessType = BusinessType.DELETE)
  155. @DeleteMapping("/{ids}")
  156. public AjaxResult remove(@PathVariable Long[] ids) {
  157. return toAjax(tValveCsocscService.deleteTValveCsocscByIds(ids));
  158. }
  159. @Log(title = "阀门铅封开关状态记录批量导入", businessType = BusinessType.INSERT)
  160. @PostMapping("/importData")
  161. public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException {
  162. //获取操作人员ID
  163. Long userId = getUserId();
  164. //报错行数统计
  165. List<Integer> failRow = new ArrayList<>();
  166. Workbook workbook = ExcelUtils.getWorkBook(file);
  167. Sheet sheet = workbook.getSheetAt(0);
  168. List<TValveCsocsc> list = new ArrayList<>();
  169. int rowNum = sheet.getPhysicalNumberOfRows();
  170. int failNumber = 0;
  171. for (int i = 2; i < rowNum; i++) {
  172. try {
  173. logger.info("读取行数:" + i);
  174. Row row = sheet.getRow(i);
  175. int cellNum = row.getLastCellNum();
  176. TValveCsocsc entity = new TValveCsocsc();
  177. for (int j = 0; j < cellNum; j++) {
  178. Cell cell = row.getCell(j);
  179. if (cell == null) {
  180. continue;
  181. }
  182. String cellValue = ExcelUtils.getCellValue(cell);
  183. logger.info("cellValue:" + cellValue);
  184. if (j == 0) {
  185. entity.setUnit(cellValue);
  186. } else if (j == 1) {
  187. entity.setVtNo(cellValue);
  188. } else if (j == 2) {
  189. entity.setPidNo(cellValue);
  190. } else if (j == 3) {
  191. entity.setLocationDes(cellValue);
  192. } else if (j == 4) {
  193. entity.setMedium(cellValue);
  194. } else if (j == 5) {
  195. entity.setValveType(cellValue);
  196. } else if (j == 6) {
  197. entity.setValveSize(cellValue);
  198. } else if (j == 7) {
  199. entity.setIdentifier(cellValue);
  200. } else if (j == 8) {
  201. entity.setValvePosition(cellValue);
  202. } else if (j == 9) {
  203. entity.setFirmlySecured(cellValue);
  204. } else if (j == 10) {
  205. entity.setResponsibility(cellValue);
  206. } else if (j == 11) {
  207. entity.setCheckedBy(cellValue);
  208. } else if (j == 12) {
  209. entity.setCheckBeforeSu(cellValue);
  210. } else if (j == 13) {
  211. entity.setPidStatus(cellValue);
  212. } else if (j == 14) {
  213. entity.setRiskLevel(cellValue);
  214. } else if (j == 15) {
  215. entity.setSheReview(cellValue);
  216. } else if (j == 16) {
  217. entity.setCheckDate(DateUtils.parseDate(cellValue));
  218. } else if (j == 17) {
  219. entity.setRemarks(cellValue);
  220. }
  221. }
  222. entity.setCreaterCode(String.valueOf(userId));
  223. logger.info("entity:" + entity);
  224. list.add(entity);
  225. } catch (Exception e) {
  226. failNumber++;
  227. logger.info("e:" + JSON.toJSONString(e));
  228. failRow.add(i + 1);
  229. }
  230. }
  231. int successNumber = 0;
  232. int failNum = 0;
  233. for (TValveCsocsc t : list
  234. ) {
  235. failNum++;
  236. try {
  237. //根据使用证、注册编号、位号,进行数据更新
  238. add(t);
  239. successNumber++;
  240. } catch (Exception e) {
  241. failNumber++;
  242. logger.info("e:" + e);
  243. failRow.add(failNum + 1);
  244. }
  245. }
  246. logger.info("list:" + JSON.toJSONString(list));
  247. logger.info("successNumber:" + successNumber);
  248. logger.info("failNumber:" + failNumber);
  249. logger.info("failRow:" + failRow);
  250. return AjaxResult.success(String.valueOf(successNumber), failRow);
  251. }
  252. }