TPssrProtectionController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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.TPssrApprove;
  13. import com.ruoyi.project.pssr.domain.TPssrProtection;
  14. import com.ruoyi.project.pssr.domain.TPssrSubcontent;
  15. import com.ruoyi.project.pssr.domain.TPssrTurndown;
  16. import com.ruoyi.project.pssr.mapper.TPssrProtectionMapper;
  17. import com.ruoyi.project.pssr.service.*;
  18. import com.ruoyi.project.system.domain.SysDept;
  19. import com.ruoyi.project.system.domain.SysUser;
  20. import com.ruoyi.project.system.service.ISysDeptService;
  21. import com.ruoyi.project.system.service.ISysUserService;
  22. import org.activiti.engine.HistoryService;
  23. import org.activiti.engine.RuntimeService;
  24. import org.activiti.engine.impl.identity.Authentication;
  25. import org.activiti.engine.runtime.ProcessInstance;
  26. import org.apache.commons.collections4.CollectionUtils;
  27. import org.apache.poi.ss.usermodel.*;
  28. import org.apache.poi.xssf.usermodel.XSSFSheet;
  29. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.security.access.prepost.PreAuthorize;
  32. import org.springframework.web.bind.annotation.*;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import javax.annotation.Resource;
  35. import java.io.FileOutputStream;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.io.OutputStream;
  39. import java.text.SimpleDateFormat;
  40. import java.util.*;
  41. /**
  42. * 人身防护Controller
  43. *
  44. * @author ssy
  45. * @date 2024-09-18
  46. */
  47. @RestController
  48. @RequestMapping("/pssr/protection")
  49. public class TPssrProtectionController extends BaseController {
  50. @Resource
  51. private TPssrProtectionMapper tPssrProtectionMapper;
  52. @Autowired
  53. private ITPssrFileService tPssrFileService;
  54. @Autowired
  55. private ITPssrTurndownService tPssrTurndownService;
  56. @Autowired
  57. private ITPssrProtectionService tPssrProtectionService;
  58. @Autowired
  59. private ITPssrApproveService tPssrApproveService;
  60. @Autowired
  61. private ITPssrSubcontentService tPssrSubcontentService;
  62. @Autowired
  63. private RuntimeService runtimeService;
  64. @Autowired
  65. private HistoryService historyService;
  66. @Autowired
  67. private ISysUserService sysUserService;
  68. private String forShort = "rsfh";
  69. @Autowired
  70. private ISysDeptService iSysDeptService;
  71. @Autowired
  72. private ISysUserService userService;
  73. /**
  74. * 批量导入
  75. */
  76. @PreAuthorize("@ss.hasPermi('pssr:protection:add')")
  77. @PostMapping("/importData")
  78. public AjaxResult importInterlockData(MultipartFile file, Long subId) throws IOException
  79. {
  80. //获取操作人员ID
  81. Long userId = getUserId();
  82. //报错行数统计
  83. List<Integer> failRow =new ArrayList<Integer>();
  84. Workbook workbook = ExcelUtils.getWorkBook(file);
  85. Sheet sheet = workbook.getSheetAt(0);
  86. List<TPssrProtection> list = new ArrayList<TPssrProtection>();
  87. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  88. //部门查询
  89. List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
  90. int rowNum = sheet.getPhysicalNumberOfRows();
  91. int failNumber = 0;
  92. for (int i = 2; i <= rowNum; i++) {
  93. try {
  94. logger.info("读取行数:" + i);
  95. Row row = sheet.getRow(i);
  96. int cellNum = row.getPhysicalNumberOfCells();
  97. TPssrProtection entity = new TPssrProtection();
  98. entity.setDeptId(userService.selectUserById(getUserId()).getDeptId());
  99. entity.setSubId(subId);
  100. entity.setApproveStatus(0L);
  101. for (int j = 0; j < cellNum; j++) {
  102. Cell cell = row.getCell(j);
  103. String cellValue = ExcelUtils.getCellValue(cell);
  104. logger.info("cellValue:" + cellValue);
  105. if (j == 0) {
  106. entity.setCheckContent(cellValue);
  107. } else if (j == 1) {
  108. entity.setCheckResult(cellValue);
  109. } else if (j == 2) {
  110. if (cellValue.length() > 3) {
  111. entity.setConfirmationDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  112. }
  113. } else if (j == 3) {
  114. entity.setRemarks(cellValue);
  115. }
  116. }
  117. entity.setCreaterCode(userId.toString());
  118. logger.info("entity:" + entity);
  119. list.add(entity);
  120. }catch (Exception e){
  121. failNumber++;
  122. failRow.add(i+1);
  123. }
  124. }
  125. int successNumber = 0;
  126. int failNum = 0;
  127. for (TPssrProtection t : list
  128. ) {
  129. failNum++;
  130. try {
  131. tPssrProtectionService.insertTPssrProtection(t);
  132. successNumber++;
  133. }catch (Exception e){
  134. failNumber++;
  135. logger.info("e:" + e);
  136. failRow.add(failNum+1);
  137. }
  138. }
  139. logger.info("list:" + JSON.toJSONString(list));
  140. logger.info("successNumber:" +String.valueOf(successNumber));
  141. logger.info("failNumber:" +String.valueOf(failNumber));
  142. logger.info("failRow:" +String.valueOf(failRow));
  143. return AjaxResult.success("导入成功行数:" + String.valueOf(successNumber));
  144. }
  145. /**
  146. * 查询人身防护列表
  147. */
  148. @PreAuthorize("@ss.hasPermi('pssr:protection:list')")
  149. @GetMapping("/list")
  150. public TableDataInfo list(TPssrProtection tPssrProtection) {
  151. startPage();
  152. List<TPssrProtection> list = tPssrProtectionService.selectTPssrProtectionList(tPssrProtection);
  153. list.forEach(item -> {
  154. item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "rsfh"));
  155. if (item.getApproveStatus() != 2)
  156. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "rsfh"));
  157. });
  158. return getDataTable(list);
  159. }
  160. /**
  161. * 导出人身防护列表
  162. */
  163. @PreAuthorize("@ss.hasPermi('pssr:protection:export')")
  164. @Log(title = "人身防护", businessType = BusinessType.EXPORT)
  165. @GetMapping("/export")
  166. public AjaxResult export(TPssrProtection tPssrProtection) {
  167. List<TPssrProtection> list = tPssrProtectionService.selectTPssrProtectionList(tPssrProtection);
  168. return AjaxResult.success(exportTmpl(list));
  169. }
  170. public String exportTmpl(List<TPssrProtection> list) {
  171. OutputStream out = null;
  172. String filename = null;
  173. try {
  174. String tempUrl = "static/word/pssr/rsfh1.xlsx"; // 模板文件
  175. InputStream is = null;
  176. is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl);
  177. XSSFWorkbook wb = null;
  178. wb = new XSSFWorkbook(is);
  179. XSSFSheet sheet = wb.getSheetAt(0);
  180. //填充数据
  181. int rowIndex = 2;
  182. int num = 1;
  183. Row originalRow = sheet.getRow(2);
  184. Cell originalcell = originalRow.getCell(0);
  185. // 获取单元格样式
  186. CellStyle originalStyle = originalcell.getCellStyle();
  187. for (TPssrProtection t : list) {
  188. Row row = sheet.createRow(rowIndex);
  189. row.setHeight((short) 800);
  190. row.createCell(0).setCellValue(num);
  191. row.createCell(1).setCellValue(t.getCheckContent());
  192. row.createCell(2).setCellValue(t.getCheckResult());
  193. row.createCell(3);
  194. row.createCell(4);
  195. try {
  196. SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(t.getConfirm1()));
  197. SysUser sysUser2 = sysUserService.selectUserById(Long.valueOf(t.getConfirm2()));
  198. String confirm1 = sysUser1.getSignUrl();
  199. String confirm2 = sysUser2.getSignUrl();
  200. ExcelUtils.insertPicture(wb, sheet, confirm1, row.getRowNum(), 3, 1, 1);
  201. ExcelUtils.insertPicture(wb, sheet, confirm2, row.getRowNum(), 4, 1, 1);
  202. } catch (NumberFormatException e) {
  203. throw new RuntimeException(e);
  204. }
  205. row.createCell(5).setCellValue(DateUtils.dateTime(t.getConfirmationDate()));
  206. row.createCell(6).setCellValue(t.getRemarks());
  207. //渲染样式
  208. for (int i = 0; i < 7; i++) {
  209. row.getCell(i).setCellStyle(originalStyle);
  210. }
  211. num++;
  212. rowIndex++;
  213. }
  214. filename = ExcelUtil.encodingFilename("Protection");
  215. out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
  216. wb.write(out);
  217. wb.close();
  218. } catch (IOException e) {
  219. e.printStackTrace();
  220. }
  221. return filename;
  222. }
  223. /**
  224. * 获取人身防护详细信息
  225. */
  226. @PreAuthorize("@ss.hasPermi('pssr:protection:query')")
  227. @GetMapping(value = "/{id}")
  228. public AjaxResult getInfo(@PathVariable("id") Long id) {
  229. TPssrProtection item = tPssrProtectionService.selectTPssrProtectionById(id);
  230. item.setFileList(tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), "rsfh"));
  231. if (item.getApproveStatus() != 2)
  232. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "rsfh"));
  233. return AjaxResult.success(item);
  234. }
  235. /**
  236. * 新增人身防护
  237. */
  238. @PreAuthorize("@ss.hasPermi('pssr:protection:add')")
  239. @Log(title = "人身防护", businessType = BusinessType.INSERT)
  240. @PostMapping
  241. public AjaxResult add(@RequestBody TPssrProtection tPssrProtection) {
  242. tPssrProtection.setApproveStatus(0L);
  243. tPssrProtection.setCreaterCode(String.valueOf(getUserId()));
  244. tPssrProtection.setCreatedate(new Date());
  245. return toAjax(tPssrProtectionService.insertTPssrProtection(tPssrProtection));
  246. }
  247. /**
  248. * 修改人身防护
  249. */
  250. @PreAuthorize("@ss.hasPermi('pssr:protection:edit')")
  251. @Log(title = "人身防护", businessType = BusinessType.UPDATE)
  252. @PutMapping
  253. public AjaxResult edit(@RequestBody TPssrProtection tPssrProtection) {
  254. tPssrFileService.updateFileRelevance(tPssrProtection.getFileIds(), "rsfh", tPssrProtection.getId(), tPssrProtection.getSubId());
  255. return toAjax(tPssrProtectionService.updateTPssrProtection(tPssrProtection));
  256. }
  257. /**
  258. * 删除人身防护
  259. */
  260. @PreAuthorize("@ss.hasPermi('pssr:protection:remove')")
  261. @Log(title = "人身防护", businessType = BusinessType.DELETE)
  262. @DeleteMapping("/{ids}")
  263. public AjaxResult remove(@PathVariable Long[] ids) {
  264. return toAjax(tPssrProtectionService.deleteTPssrProtectionByIds(ids));
  265. }
  266. /**
  267. * 确认人身防护
  268. */
  269. @PreAuthorize("@ss.hasPermi('pssr:protection:edit')")
  270. @Log(title = "人身防护", businessType = BusinessType.UPDATE)
  271. @PutMapping("/confirmProtection")
  272. public AjaxResult confirmProtection(@RequestBody TPssrProtection tPssrProtection) {
  273. long queryStatus = 0;
  274. long approveStatus = 0;
  275. Date date = null;
  276. TPssrProtection protection = new TPssrProtection();
  277. if (tPssrProtection.getTaskType() == 4) {
  278. //确认人1确认
  279. queryStatus = 1;
  280. approveStatus = 3;
  281. protection.setConfirm1(getUserId().toString());
  282. date = new Date();
  283. } else if (tPssrProtection.getTaskType() == 5) {
  284. //确认人2确认
  285. queryStatus = 3;
  286. approveStatus = 2;
  287. protection.setConfirm2(getUserId().toString());
  288. date = new Date();
  289. }
  290. if (tPssrProtection.getIds() != null && tPssrProtection.getIds().length > 0) {
  291. for (Long id : tPssrProtection.getIds()) {
  292. protection = tPssrProtectionService.selectTPssrProtectionById(id);
  293. if (protection.getConfirmationDate() == null) {
  294. protection.setConfirmationDate(new Date());
  295. }
  296. protection.setApproveStatus(approveStatus);
  297. protection.setUpdatedate(new Date());
  298. protection.setUpdaterCode(String.valueOf(getUserId()));
  299. tPssrProtectionService.updateTPssrProtection(protection);
  300. }
  301. } else {
  302. protection.setSubId(tPssrProtection.getSubId());
  303. protection.setApproveStatus(queryStatus);
  304. for (TPssrProtection item : tPssrProtectionService.selectTPssrProtectionList(protection)) {
  305. if (item.getConfirmationDate() == null) {
  306. item.setConfirmationDate(new Date());
  307. }
  308. item.setApproveStatus(approveStatus);
  309. item.setUpdatedate(new Date());
  310. item.setUpdaterCode(String.valueOf(getUserId()));
  311. tPssrProtectionService.updateTPssrProtection(item);
  312. }
  313. }
  314. //查询当前待审批的确认人
  315. TPssrProtection entity = new TPssrProtection();
  316. entity.setSubId(tPssrProtection.getSubId());
  317. entity.setApproveStatus(queryStatus);
  318. TPssrProtection pssrProtection = tPssrProtectionService.selectAllConfirmedPersonBySubId(entity);
  319. if (pssrProtection != null) {
  320. //如果当前用户还有待审批任务
  321. if (tPssrProtection.getTaskType() == 4 && StringUtils.isNotEmpty(pssrProtection.getConfirm1())) {
  322. if (pssrProtection.getConfirm1().contains(getUserId().toString())) {
  323. return AjaxResult.success();
  324. }
  325. }
  326. if (tPssrProtection.getTaskType() == 5 && StringUtils.isNotEmpty(pssrProtection.getConfirm2())) {
  327. if (pssrProtection.getConfirm2().contains(getUserId().toString())) {
  328. return AjaxResult.success();
  329. }
  330. }
  331. }
  332. //无待审批任务结束当前用户流程
  333. // 因为流程关系所以approve一定会有且只有一条数据
  334. TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrProtection.getSubId());
  335. TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
  336. return AjaxResult.success();
  337. }
  338. /**
  339. * 驳回人身防护
  340. */
  341. @PutMapping("/turnDownProtection")
  342. public AjaxResult turnDownProtection(@RequestBody List<TPssrProtection> tPssrProtection) {
  343. if (CollectionUtils.isNotEmpty(tPssrProtection)) {
  344. String userId = getUserId().toString();
  345. Long subId = tPssrProtection.get(0).getSubId();
  346. // 修改已选择数据的状态
  347. for (TPssrProtection item : tPssrProtection) {
  348. TPssrProtection blind = new TPssrProtection();
  349. blind.setId(item.getId());
  350. blind.setApproveStatus(1L);
  351. blind.setUpdatedate(new Date());
  352. blind.setUpdaterCode(getUserId().toString());
  353. tPssrProtectionService.updateTPssrProtection(blind);
  354. // 新增驳回原因数据
  355. TPssrTurndown turndown = new TPssrTurndown();
  356. turndown.setForShort(forShort);
  357. turndown.setSubId(item.getSubId());
  358. turndown.setItemId(item.getId());
  359. turndown.setReason(item.getReason());
  360. turndown.setCreatedate(new Date());
  361. turndown.setCreaterCode(getUserId().toString());
  362. tPssrTurndownService.insertTPssrTurndown(turndown);
  363. }
  364. // 查询当前流程
  365. TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);
  366. try {
  367. runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr2confirm");
  368. historyService.deleteHistoricProcessInstance(approve.getProcessId());
  369. } catch (Exception e) {
  370. logger.info("无运行时流程");
  371. }
  372. // 驳回 查询所有待审批的人员
  373. TPssrProtection blind = new TPssrProtection();
  374. blind.setSubId(subId);
  375. blind.setApproveStatus(1L);
  376. Set<String> confirmUsers1 = new HashSet<>();
  377. Set<String> confirmUsers2 = new HashSet<>();
  378. for (TPssrProtection item : tPssrProtectionService.selectTPssrProtectionList(blind)) {
  379. // 确认人1
  380. confirmUsers1.add(item.getConfirm1());
  381. //确认人2
  382. confirmUsers2.add(item.getConfirm2());
  383. }
  384. // 开始申请流程
  385. long businessKey = approve.getApproveId();
  386. //开始工作流、监听
  387. Authentication.setAuthenticatedUserId(userId);//设置当前申请人
  388. Map<String, Object> variables = new HashMap<>();
  389. variables.put("applyUser", userId);
  390. variables.put("confirmUsers1", confirmUsers1);
  391. variables.put("confirmUsers2", confirmUsers2);
  392. variables.put("chargePerson", approve.getSubCharge());
  393. //采用key来启动流程定义并设置流程变量,返回流程实例
  394. ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr2confirm", String.valueOf(businessKey), variables);
  395. // 修改审批表和sub表
  396. approve.setProcessId(pi.getProcessInstanceId());
  397. approve.setApproveStatus(1L);
  398. approve.setUpdatedate(new Date());
  399. approve.setUpdaterCode(getUserId().toString());
  400. tPssrApproveService.updateTPssrApprove(approve);
  401. TPssrSubcontent subcontent = new TPssrSubcontent();
  402. subcontent.setId(approve.getSubId());
  403. subcontent.setApproveStatus(1L);
  404. subcontent.setUpdatedate(new Date());
  405. subcontent.setUpdaterCode(getUserId().toString());
  406. tPssrSubcontentService.updateTPssrSubcontent(subcontent);
  407. return AjaxResult.success();
  408. }
  409. return AjaxResult.error();
  410. }
  411. }