TPssrSafetyBreathController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package com.ruoyi.project.pssr.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ruoyi.common.utils.DateUtils;
  4. import com.ruoyi.common.utils.file.ExcelUtils;
  5. import com.ruoyi.common.utils.poi.ExcelUtil;
  6. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  7. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  8. import com.ruoyi.framework.web.controller.BaseController;
  9. import com.ruoyi.framework.web.domain.AjaxResult;
  10. import com.ruoyi.framework.web.page.TableDataInfo;
  11. import com.ruoyi.project.pssr.domain.TPssrSafetyBreath;
  12. import com.ruoyi.project.pssr.mapper.TPssrSafetyBreathMapper;
  13. import com.ruoyi.project.pssr.service.ITPssrFileService;
  14. import com.ruoyi.project.pssr.service.ITPssrSafetyBreathService;
  15. import com.ruoyi.project.pssr.service.ITPssrTurndownService;
  16. import com.ruoyi.project.system.domain.SysDept;
  17. import com.ruoyi.project.system.domain.SysUser;
  18. import com.ruoyi.project.system.service.ISysDeptService;
  19. import com.ruoyi.project.system.service.ISysUserService;
  20. import org.apache.poi.ss.usermodel.*;
  21. import org.apache.poi.xssf.usermodel.XSSFSheet;
  22. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.security.access.prepost.PreAuthorize;
  25. import org.springframework.web.bind.annotation.*;
  26. import org.springframework.web.multipart.MultipartFile;
  27. import javax.annotation.Resource;
  28. import java.io.FileOutputStream;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.io.OutputStream;
  32. import java.text.SimpleDateFormat;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import java.util.UUID;
  36. /**
  37. * 安全设施-呼吸阀Controller
  38. *
  39. * @author ssy
  40. * @date 2024-09-18
  41. */
  42. @RestController
  43. @RequestMapping("/pssr/safetyBreath")
  44. public class TPssrSafetyBreathController extends BaseController {
  45. @Resource
  46. private TPssrSafetyBreathMapper tPssrSafetyBreathMapper;
  47. @Autowired
  48. private ITPssrFileService tPssrFileService;
  49. @Autowired
  50. private ITPssrTurndownService tPssrTurndownService;
  51. @Autowired
  52. private ITPssrSafetyBreathService tPssrSafetyBreathService;
  53. @Autowired
  54. private ISysUserService sysUserService;
  55. private String forShort = "aqss-bh";
  56. @Autowired
  57. private ISysDeptService iSysDeptService;
  58. @Autowired
  59. private ISysUserService userService;
  60. /**
  61. * 批量导入
  62. */
  63. @PreAuthorize("@ss.hasPermi('pssr:safetyBreath:add')")
  64. @PostMapping("/importData")
  65. public AjaxResult importInterlockData(MultipartFile file, Long subId) throws IOException
  66. {
  67. //获取操作人员ID
  68. Long userId = getUserId();
  69. //报错行数统计
  70. List<Integer> failRow =new ArrayList<Integer>();
  71. Workbook workbook = ExcelUtils.getWorkBook(file);
  72. Sheet sheet = workbook.getSheetAt(0);
  73. List<TPssrSafetyBreath> list = new ArrayList<TPssrSafetyBreath>();
  74. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  75. //部门查询
  76. List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
  77. int rowNum = sheet.getPhysicalNumberOfRows();
  78. int failNumber = 0;
  79. for (int i = 2; i <= rowNum; i++) {
  80. try {
  81. logger.info("读取行数:" + i);
  82. Row row = sheet.getRow(i);
  83. int cellNum = row.getLastCellNum();
  84. TPssrSafetyBreath entity = new TPssrSafetyBreath();
  85. entity.setDeptId(userService.selectUserById(getUserId()).getDeptId());
  86. entity.setSubId(subId);
  87. entity.setApproveStatus(0L);
  88. for (int j = 0; j < cellNum; j++) {
  89. Cell cell = row.getCell(j);
  90. String cellValue = ExcelUtils.getCellValue(cell);
  91. logger.info("cellValue:" + cellValue);
  92. if (j == 0) {
  93. entity.setUnit(cellValue);
  94. } else if (j == 1) {
  95. entity.setPidNo(cellValue);
  96. } else if (j == 2) {
  97. entity.setDevNo(cellValue);
  98. } else if (j == 3) {
  99. entity.setVerify(cellValue);
  100. } else if (j == 4) {
  101. entity.setInValidity(cellValue);
  102. } else if (j == 5) {
  103. entity.setInstallLocation(cellValue);
  104. } else if (j == 6) {
  105. entity.setInstallAccuracy(cellValue);
  106. } else if (j == 7) {
  107. entity.setPutUse(cellValue);
  108. } else if (j == 8) {
  109. entity.setSetPressure(cellValue);
  110. } else if (j == 9) {
  111. entity.setUniformPressure(cellValue);
  112. } else if (j == 10) {
  113. if (cellValue.length() > 3) {
  114. entity.setConfirmationTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  115. }
  116. } else if (j == 11) {
  117. entity.setRemarks(cellValue);
  118. }
  119. }
  120. entity.setCreaterCode(userId.toString());
  121. logger.info("entity:" + entity);
  122. list.add(entity);
  123. }catch (Exception e){
  124. failNumber++;
  125. failRow.add(i+1);
  126. }
  127. }
  128. int successNumber = 0;
  129. int failNum = 0;
  130. for (TPssrSafetyBreath t : list
  131. ) {
  132. failNum++;
  133. try {
  134. tPssrSafetyBreathService.insertTPssrSafetyBreath(t);
  135. successNumber++;
  136. }catch (Exception e){
  137. failNumber++;
  138. logger.info("e:" + e);
  139. failRow.add(failNum+1);
  140. }
  141. }
  142. logger.info("list:" + JSON.toJSONString(list));
  143. logger.info("successNumber:" +String.valueOf(successNumber));
  144. logger.info("failNumber:" +String.valueOf(failNumber));
  145. logger.info("failRow:" +String.valueOf(failRow));
  146. return AjaxResult.success("导入成功行数:" + String.valueOf(successNumber));
  147. }
  148. /**
  149. * 查询安全设施-呼吸阀列表
  150. */
  151. @PreAuthorize("@ss.hasPermi('pssr:safetyBreath:list')")
  152. @GetMapping("/list")
  153. public TableDataInfo list(TPssrSafetyBreath tPssrSafetyBreath) {
  154. startPage();
  155. List<TPssrSafetyBreath> list = tPssrSafetyBreathService.selectTPssrSafetyBreathList(tPssrSafetyBreath);
  156. list.forEach(item -> {
  157. item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "aqss-bh"));
  158. if (item.getApproveStatus() != 2)
  159. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "aqss-bh"));
  160. });
  161. return getDataTable(list);
  162. }
  163. /**
  164. * 导出安全设施-呼吸阀列表
  165. */
  166. @PreAuthorize("@ss.hasPermi('pssr:safetyBreath:export')")
  167. @Log(title = "安全设施-呼吸阀", businessType = BusinessType.EXPORT)
  168. @GetMapping("/export")
  169. public AjaxResult export(TPssrSafetyBreath tPssrSafetyBreath) {
  170. List<TPssrSafetyBreath> list = tPssrSafetyBreathService.selectTPssrSafetyBreathList(tPssrSafetyBreath);
  171. return AjaxResult.success(exportTmpl(list));
  172. }
  173. public String exportTmpl(List<TPssrSafetyBreath> list) {
  174. OutputStream out = null;
  175. String filename = null;
  176. try {
  177. String tempUrl = "static/word/pssr/aqsshxf.xlsx"; // 模板文件
  178. InputStream is = null;
  179. is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl);
  180. XSSFWorkbook wb = null;
  181. wb = new XSSFWorkbook(is);
  182. XSSFSheet sheet = wb.getSheetAt(0);
  183. //填充数据
  184. int rowIndex = 3;
  185. int num = 1;
  186. Row originalRow = sheet.getRow(3);
  187. Cell originalcell = originalRow.getCell(0);
  188. // 获取单元格样式
  189. CellStyle originalStyle = originalcell.getCellStyle();
  190. for (TPssrSafetyBreath t : list) {
  191. Row row = sheet.createRow(rowIndex);
  192. row.setHeight((short) 800);
  193. row.createCell(0).setCellValue(num);
  194. row.createCell(1).setCellValue(t.getUnit());
  195. row.createCell(2).setCellValue(t.getPidNo());
  196. row.createCell(3).setCellValue(t.getDevNo());
  197. row.createCell(4).setCellValue(t.getVerify());
  198. row.createCell(5).setCellValue(t.getInValidity());
  199. row.createCell(6).setCellValue(t.getInstallLocation());
  200. row.createCell(7).setCellValue(t.getInstallAccuracy());
  201. row.createCell(8).setCellValue(t.getPutUse());
  202. row.createCell(9).setCellValue(t.getSetPressure());
  203. row.createCell(10).setCellValue(t.getUniformPressure());
  204. row.createCell(11);
  205. row.createCell(12);
  206. try {
  207. SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(t.getConfirmer1()));
  208. SysUser sysUser2 = sysUserService.selectUserById(Long.valueOf(t.getConfirmer2()));
  209. String confirm1 = sysUser1.getSignUrl();
  210. String confirm2 = sysUser2.getSignUrl();
  211. ExcelUtils.insertPicture(wb, sheet, confirm1, row.getRowNum(), 11, 1, 1);
  212. ExcelUtils.insertPicture(wb, sheet, confirm2, row.getRowNum(), 12, 1, 1);
  213. } catch (NumberFormatException e) {
  214. throw new RuntimeException(e);
  215. }
  216. row.createCell(13).setCellValue(DateUtils.dateTime(t.getConfirmationTime()));
  217. row.createCell(14).setCellValue(t.getRemarks());
  218. //渲染样式
  219. for (int i = 0; i < 15; i++) {
  220. row.getCell(i).setCellStyle(originalStyle);
  221. }
  222. num++;
  223. rowIndex++;
  224. }
  225. filename = "PSSR_14_安全设施_呼吸阀_" + UUID.randomUUID().toString() + ".xlsx";
  226. out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
  227. wb.write(out);
  228. wb.close();
  229. } catch (IOException e) {
  230. e.printStackTrace();
  231. }
  232. return filename;
  233. }
  234. /**
  235. * 获取安全设施-呼吸阀详细信息
  236. */
  237. @PreAuthorize("@ss.hasPermi('pssr:safetyBreath:query')")
  238. @GetMapping(value = "/{id}")
  239. public AjaxResult getInfo(@PathVariable("id") Long id) {
  240. TPssrSafetyBreath item = tPssrSafetyBreathService.selectTPssrSafetyBreathById(id);
  241. item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "aqss-bh"));
  242. if (item.getApproveStatus() != 2)
  243. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "aqss-bh"));
  244. return AjaxResult.success(item);
  245. }
  246. /**
  247. * 新增安全设施-呼吸阀
  248. */
  249. @PreAuthorize("@ss.hasPermi('pssr:safetyBreath:add')")
  250. @Log(title = "安全设施-呼吸阀", businessType = BusinessType.INSERT)
  251. @PostMapping
  252. public AjaxResult add(@RequestBody TPssrSafetyBreath tPssrSafetyBreath) {
  253. if (tPssrSafetyBreath.getConfirmer1().equals(tPssrSafetyBreath.getConfirmer2())){
  254. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  255. }
  256. tPssrSafetyBreath.setApproveStatus(0L);
  257. return toAjax(tPssrSafetyBreathService.insertTPssrSafetyBreath(tPssrSafetyBreath));
  258. }
  259. /**
  260. * 修改安全设施-呼吸阀
  261. */
  262. @PreAuthorize("@ss.hasPermi('pssr:safetyBreath:edit')")
  263. @Log(title = "安全设施-呼吸阀", businessType = BusinessType.UPDATE)
  264. @PutMapping
  265. public AjaxResult edit(@RequestBody TPssrSafetyBreath tPssrSafetyBreath) {
  266. if (tPssrSafetyBreath.getConfirmer1().equals(tPssrSafetyBreath.getConfirmer2())){
  267. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  268. }
  269. tPssrFileService.updateFileRelevance(tPssrSafetyBreath.getFileIds(), "aqss-bh", tPssrSafetyBreath.getId(), tPssrSafetyBreath.getSubId());
  270. return toAjax(tPssrSafetyBreathService.updateTPssrSafetyBreath(tPssrSafetyBreath));
  271. }
  272. /**
  273. * 修改安全设施-呼吸阀
  274. */
  275. @PreAuthorize("@ss.hasPermi('pssr:safetyBreath:edit')")
  276. @Log(title = "安全设施-呼吸阀", businessType = BusinessType.UPDATE)
  277. @PutMapping("/editBatch")
  278. public AjaxResult editb(@RequestBody TPssrSafetyBreath tPssrSafetyBreath) {
  279. if (tPssrSafetyBreath.getConfirmer1().equals(tPssrSafetyBreath.getConfirmer2())){
  280. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  281. }
  282. return toAjax(tPssrSafetyBreathMapper.updateTPssrSafetyBreathByIds(tPssrSafetyBreath));
  283. }
  284. /**
  285. * 删除安全设施-呼吸阀
  286. */
  287. @PreAuthorize("@ss.hasPermi('pssr:safetyBreath:remove')")
  288. @Log(title = "安全设施-呼吸阀", businessType = BusinessType.DELETE)
  289. @DeleteMapping("/{ids}")
  290. public AjaxResult remove(@PathVariable Long[] ids) {
  291. return toAjax(tPssrSafetyBreathService.deleteTPssrSafetyBreathByIds(ids));
  292. }
  293. }