TPssrSafetyBrustController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.TPssrSafetyBrust;
  12. import com.ruoyi.project.pssr.mapper.TPssrSafetyBrustMapper;
  13. import com.ruoyi.project.pssr.service.ITPssrFileService;
  14. import com.ruoyi.project.pssr.service.ITPssrSafetyBrustService;
  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/safetyBrust")
  44. public class TPssrSafetyBrustController extends BaseController {
  45. @Resource
  46. private TPssrSafetyBrustMapper tPssrSafetyBrustMapper;
  47. @Autowired
  48. private ITPssrFileService tPssrFileService;
  49. @Autowired
  50. private ITPssrTurndownService tPssrTurndownService;
  51. @Autowired
  52. private ITPssrSafetyBrustService tPssrSafetyBrustService;
  53. @Autowired
  54. private ISysUserService sysUserService;
  55. private String forShort = "aqss-bt";
  56. @Autowired
  57. private ISysDeptService iSysDeptService;
  58. @Autowired
  59. private ISysUserService userService;
  60. /**
  61. * 批量导入
  62. */
  63. @PreAuthorize("@ss.hasPermi('pssr:safetyBrust: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<TPssrSafetyBrust> list = new ArrayList<TPssrSafetyBrust>();
  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.getPhysicalNumberOfCells();
  84. TPssrSafetyBrust entity = new TPssrSafetyBrust();
  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.setDevNo(cellValue);
  96. } else if (j == 2) {
  97. entity.setSafetyDev(cellValue);
  98. } else if (j == 3) {
  99. entity.setInstallLocation(cellValue);
  100. } else if (j == 4) {
  101. entity.setInstallAccuracy(cellValue);
  102. } else if (j == 5) {
  103. entity.setRightDirection(cellValue);
  104. } else if (j == 6) {
  105. entity.setPutUse(cellValue);
  106. } else if (j == 7) {
  107. entity.setSetPressure(cellValue);
  108. } else if (j == 8) {
  109. entity.setUniformPressure(cellValue);
  110. } else if (j == 9) {
  111. entity.setIs0kpa(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 (TPssrSafetyBrust t : list
  131. ) {
  132. failNum++;
  133. try {
  134. tPssrSafetyBrustService.insertTPssrSafetyBrust(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:safetyBrust:list')")
  152. @GetMapping("/list")
  153. public TableDataInfo list(TPssrSafetyBrust tPssrSafetyBrust) {
  154. startPage();
  155. List<TPssrSafetyBrust> list = tPssrSafetyBrustService.selectTPssrSafetyBrustList(tPssrSafetyBrust);
  156. list.forEach(item -> {
  157. item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "aqss-bt"));
  158. if (item.getApproveStatus() != 2)
  159. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "aqss-bt"));
  160. });
  161. return getDataTable(list);
  162. }
  163. /**
  164. * 导出安全设施-爆破片列表
  165. */
  166. @PreAuthorize("@ss.hasPermi('pssr:safetyBrust:export')")
  167. @Log(title = "安全设施-爆破片", businessType = BusinessType.EXPORT)
  168. @GetMapping("/export")
  169. public AjaxResult export(TPssrSafetyBrust tPssrSafetyBrust) {
  170. List<TPssrSafetyBrust> list = tPssrSafetyBrustService.selectTPssrSafetyBrustList(tPssrSafetyBrust);
  171. return AjaxResult.success(exportTmpl(list));
  172. }
  173. public String exportTmpl(List<TPssrSafetyBrust> list) {
  174. OutputStream out = null;
  175. String filename = null;
  176. try {
  177. String tempUrl = "static/word/pssr/aqssbpp.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 = 4;
  185. int num = 1;
  186. Row originalRow = sheet.getRow(4);
  187. Cell originalcell = originalRow.getCell(0);
  188. // 获取单元格样式
  189. CellStyle originalStyle = originalcell.getCellStyle();
  190. for (TPssrSafetyBrust 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.getDevNo());
  196. row.createCell(3).setCellValue(t.getSafetyDev());
  197. row.createCell(4).setCellValue(t.getInstallLocation());
  198. row.createCell(5).setCellValue(t.getInstallAccuracy());
  199. row.createCell(6).setCellValue(t.getRightDirection());
  200. row.createCell(7).setCellValue(t.getPutUse());
  201. row.createCell(8).setCellValue(t.getSetPressure());
  202. row.createCell(9).setCellValue(t.getUniformPressure());
  203. row.createCell(10).setCellValue(t.getIs0kpa());
  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:safetyBrust:query')")
  238. @GetMapping(value = "/{id}")
  239. public AjaxResult getInfo(@PathVariable("id") Long id) {
  240. TPssrSafetyBrust item = tPssrSafetyBrustService.selectTPssrSafetyBrustById(id);
  241. item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "aqss-bt"));
  242. if (item.getApproveStatus() != 2)
  243. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "aqss-bt"));
  244. return AjaxResult.success(item);
  245. }
  246. /**
  247. * 新增安全设施-爆破片
  248. */
  249. @PreAuthorize("@ss.hasPermi('pssr:safetyBrust:add')")
  250. @Log(title = "安全设施-爆破片", businessType = BusinessType.INSERT)
  251. @PostMapping
  252. public AjaxResult add(@RequestBody TPssrSafetyBrust tPssrSafetyBrust) {
  253. tPssrSafetyBrust.setApproveStatus(0L);
  254. return toAjax(tPssrSafetyBrustService.insertTPssrSafetyBrust(tPssrSafetyBrust));
  255. }
  256. /**
  257. * 修改安全设施-爆破片
  258. */
  259. @PreAuthorize("@ss.hasPermi('pssr:safetyBrust:edit')")
  260. @Log(title = "安全设施-爆破片", businessType = BusinessType.UPDATE)
  261. @PutMapping
  262. public AjaxResult edit(@RequestBody TPssrSafetyBrust tPssrSafetyBrust) {
  263. tPssrFileService.updateFileRelevance(tPssrSafetyBrust.getFileIds(), "aqss-bt", tPssrSafetyBrust.getId(), tPssrSafetyBrust.getSubId());
  264. return toAjax(tPssrSafetyBrustService.updateTPssrSafetyBrust(tPssrSafetyBrust));
  265. }
  266. /**
  267. * 修改安全设施-爆破片
  268. */
  269. @PreAuthorize("@ss.hasPermi('pssr:safetyBrust:edit')")
  270. @Log(title = "安全设施-爆破片", businessType = BusinessType.UPDATE)
  271. @PutMapping("/editBatch")
  272. public AjaxResult editb(@RequestBody TPssrSafetyBrust tPssrSafetyBrust) {
  273. return toAjax(tPssrSafetyBrustMapper.updateTPssrSafetyBrustByIds(tPssrSafetyBrust));
  274. }
  275. /**
  276. * 删除安全设施-爆破片
  277. */
  278. @PreAuthorize("@ss.hasPermi('pssr:safetyBrust:remove')")
  279. @Log(title = "安全设施-爆破片", businessType = BusinessType.DELETE)
  280. @DeleteMapping("/{ids}")
  281. public AjaxResult remove(@PathVariable Long[] ids) {
  282. return toAjax(tPssrSafetyBrustService.deleteTPssrSafetyBrustByIds(ids));
  283. }
  284. }