TPssrSafetyBreathController.java 12 KB

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