TPssrHygieneController.java 19 KB

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