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