TPssrPowerController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package com.ruoyi.project.pssr.controller;
  2. import com.ruoyi.common.utils.DateUtils;
  3. import com.ruoyi.common.utils.StringUtils;
  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.*;
  12. import com.ruoyi.project.pssr.mapper.TPssrPowerMapper;
  13. import com.ruoyi.project.pssr.service.*;
  14. import com.ruoyi.project.system.domain.SysUser;
  15. import com.ruoyi.project.system.service.ISysUserService;
  16. import org.activiti.engine.ProcessEngine;
  17. import org.activiti.engine.ProcessEngines;
  18. import org.activiti.engine.TaskService;
  19. import org.activiti.engine.task.Task;
  20. import org.apache.commons.collections4.CollectionUtils;
  21. import org.apache.poi.ss.usermodel.Cell;
  22. import org.apache.poi.ss.usermodel.CellStyle;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.xssf.usermodel.XSSFSheet;
  25. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.security.access.prepost.PreAuthorize;
  28. import org.springframework.web.bind.annotation.*;
  29. import javax.annotation.Resource;
  30. import java.io.FileOutputStream;
  31. import java.io.IOException;
  32. import java.io.InputStream;
  33. import java.io.OutputStream;
  34. import java.util.*;
  35. /**
  36. * 临时电源Controller
  37. *
  38. * @author ssy
  39. * @date 2024-11-01
  40. */
  41. @RestController
  42. @RequestMapping("/pssr/power")
  43. public class TPssrPowerController extends BaseController {
  44. @Resource
  45. private TPssrPowerMapper tPssrPowerMapper;
  46. @Autowired
  47. private ITPssrFileService tPssrFileService;
  48. @Autowired
  49. private ITPssrTurndownService tPssrTurndownService;
  50. @Autowired
  51. private ITPssrPowerService tPssrPowerService;
  52. @Autowired
  53. private ITPssrApproveService tPssrApproveService;
  54. @Autowired
  55. private ITPssrSubcontentService tPssrSubcontentService;
  56. @Autowired
  57. private ISysUserService sysUserService;
  58. private String forShort = "lsdy";
  59. /**
  60. * 查询临时电源列表
  61. */
  62. @PreAuthorize("@ss.hasPermi('pssr:power:list')")
  63. @GetMapping("/list")
  64. public TableDataInfo list(TPssrPower tPssrPower) {
  65. try {
  66. TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(tPssrPower.getSubId());
  67. if (approve != null) {
  68. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  69. TaskService taskService = processEngine.getTaskService();
  70. Task task = processEngine.getTaskService()//获取任务service
  71. .createTaskQuery()//创建查询对象
  72. .taskAssignee(getUserId().toString())
  73. .processInstanceId(approve.getProcessId()).singleResult();
  74. if (task != null) {
  75. if (task.getName().equals("确认人1")){
  76. tPssrPower.setConfirm1(getUserId().toString());
  77. }else if (task.getName().equals("确认人2")){
  78. tPssrPower.setConfirm2(getUserId().toString());
  79. }
  80. }
  81. }
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. logger.error("待办确认人查询报错:{}",e.getMessage());
  85. }
  86. startPage();
  87. List<TPssrPower> list = tPssrPowerService.selectTPssrPowerList(tPssrPower);
  88. list.forEach(item -> {
  89. List<TPssrFile> tPssrFiles = tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), forShort);
  90. item.setFileList(tPssrFiles);
  91. item.setFileNum((long) tPssrFiles.size());
  92. if (item.getApproveStatus() != 2)
  93. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "lsdy"));
  94. });
  95. return getDataTable(list);
  96. }
  97. /**
  98. * 导出临时电源列表
  99. */
  100. @PreAuthorize("@ss.hasPermi('pssr:power:export')")
  101. @Log(title = "临时电源", businessType = BusinessType.EXPORT)
  102. @GetMapping("/export")
  103. public AjaxResult export(TPssrPower tPssrPower) {
  104. List<TPssrPower> list = tPssrPowerService.selectTPssrPowerList(tPssrPower);
  105. if (list.size() != 0) {
  106. return AjaxResult.success(exportTmpl(list));
  107. } else {
  108. return AjaxResult.error("暂无可导出数据");
  109. }
  110. }
  111. public String exportTmpl(List<TPssrPower> list) {
  112. OutputStream out = null;
  113. String filename = null;
  114. try {
  115. String tempUrl = "static/word/pssr/lsdy.xlsx"; // 模板文件
  116. InputStream is = null;
  117. is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl);
  118. XSSFWorkbook wb = null;
  119. wb = new XSSFWorkbook(is);
  120. XSSFSheet sheet = wb.getSheetAt(0);
  121. //填充数据
  122. int rowIndex = 3;
  123. int num = 1;
  124. Row originalRow = sheet.getRow(3);
  125. Cell originalcell = originalRow.getCell(0);
  126. // 获取单元格样式
  127. CellStyle originalStyle = originalcell.getCellStyle();
  128. for (TPssrPower t : list) {
  129. Row row = sheet.createRow(rowIndex);
  130. row.setHeight((short) 800);
  131. row.createCell(0).setCellValue(num);
  132. row.createCell(1).setCellValue(t.getCheckContent());
  133. row.createCell(2).setCellValue(t.getCheckResult());
  134. row.createCell(3);
  135. row.createCell(4);
  136. try {
  137. SysUser sysUser = sysUserService.selectUserById(Long.valueOf(t.getConfirm1()));
  138. SysUser sysUser2 = sysUserService.selectUserById(Long.valueOf(t.getConfirm2()));
  139. String confirm1 = sysUser.getSignUrl();
  140. String confirm2 = sysUser2.getSignUrl();
  141. ExcelUtils.insertPicture(wb, sheet, confirm1, row.getRowNum(), 3, 1, 1);
  142. ExcelUtils.insertPicture(wb, sheet, confirm2, row.getRowNum(), 4, 1, 1);
  143. } catch (Exception e) {
  144. }
  145. row.createCell(5).setCellValue(DateUtils.dateTime(t.getConfirmationDate()));
  146. row.createCell(6).setCellValue(t.getRemarks());
  147. //渲染样式
  148. for (int i = 0; i < 7; i++) {
  149. row.getCell(i).setCellStyle(originalStyle);
  150. }
  151. num++;
  152. rowIndex++;
  153. }
  154. filename = "PSSR_26_临时电源" + ".xlsx";
  155. out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
  156. wb.write(out);
  157. wb.close();
  158. } catch (IOException e) {
  159. e.printStackTrace();
  160. }
  161. return filename;
  162. }
  163. /**
  164. * 获取临时电源详细信息
  165. */
  166. @PreAuthorize("@ss.hasPermi('pssr:power:query')")
  167. @GetMapping(value = "/{id}")
  168. public AjaxResult getInfo(@PathVariable("id") Long id) {
  169. TPssrPower item = tPssrPowerService.selectTPssrPowerById(id);
  170. List<TPssrFile> tPssrFiles = tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), forShort);
  171. item.setFileList(tPssrFiles);
  172. item.setFileNum((long) tPssrFiles.size());
  173. if (item.getApproveStatus() != 2)
  174. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "lsdy"));
  175. return AjaxResult.success(item);
  176. }
  177. /**
  178. * 新增临时电源
  179. */
  180. @PreAuthorize("@ss.hasPermi('pssr:power:add')")
  181. @Log(title = "临时电源", businessType = BusinessType.INSERT)
  182. @PostMapping
  183. public AjaxResult add(@RequestBody TPssrPower tPssrPower) {
  184. if (StringUtils.isNotEmpty(tPssrPower.getConfirm1())&&tPssrPower.getConfirm1().equals(tPssrPower.getConfirm2())) {
  185. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  186. }
  187. tPssrPower.setApproveStatus(0L);
  188. return toAjax(tPssrPowerService.insertTPssrPower(tPssrPower));
  189. }
  190. /**
  191. * 修改临时电源
  192. */
  193. @PreAuthorize("@ss.hasPermi('pssr:power:edit')")
  194. @Log(title = "临时电源", businessType = BusinessType.UPDATE)
  195. @PutMapping
  196. public AjaxResult edit(@RequestBody TPssrPower tPssrPower) {
  197. TPssrPower entity = tPssrPowerService.selectTPssrPowerById(tPssrPower.getId());
  198. if (entity.getApproveStatus() != 1 && entity.getApproveStatus() != 0) {
  199. return AjaxResult.error("当前状态不可修改!");
  200. }
  201. if (tPssrPower.getConfirm1().equals(tPssrPower.getConfirm2())){
  202. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  203. }
  204. tPssrFileService.updateFileRelevance(tPssrPower.getFileIds(), "lsdy", tPssrPower.getId(), tPssrPower.getSubId());
  205. return toAjax(tPssrPowerService.updateTPssrPower(tPssrPower));
  206. }
  207. /**
  208. * 修改临时电源
  209. */
  210. @PreAuthorize("@ss.hasPermi('pssr:power:edit')")
  211. @Log(title = "临时电源", businessType = BusinessType.UPDATE)
  212. @PutMapping("/editBatch")
  213. public AjaxResult editb(@RequestBody TPssrPower tPssrPower) {
  214. if (tPssrPower.getConfirm1().equals(tPssrPower.getConfirm2())){
  215. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  216. }
  217. return toAjax(tPssrPowerMapper.updateTPssrPowerByIds(tPssrPower));
  218. }
  219. /**
  220. * 删除临时电源
  221. */
  222. @PreAuthorize("@ss.hasPermi('pssr:power:remove')")
  223. @Log(title = "临时电源", businessType = BusinessType.DELETE)
  224. @DeleteMapping("/{ids}")
  225. public AjaxResult remove(@PathVariable Long[] ids) {
  226. return toAjax(tPssrPowerService.deleteTPssrPowerByIds(ids));
  227. }
  228. /**
  229. * 确认临时电源
  230. */
  231. @PreAuthorize("@ss.hasPermi('pssr:power:edit')")
  232. @Log(title = "临时电源", businessType = BusinessType.UPDATE)
  233. @PutMapping("/confirmPower")
  234. public AjaxResult confirmPower(@RequestBody TPssrPower tPssrPower) {
  235. long queryStatus = 0;
  236. long approveStatus = 0;
  237. Date date = null;
  238. TPssrPower lock = new TPssrPower();
  239. if (tPssrPower.getTaskType() == 4) {
  240. //拆锁确认
  241. queryStatus = 1;
  242. approveStatus = 3;
  243. date = new Date();
  244. lock.setConfirm1(getUserId().toString());
  245. } else if (tPssrPower.getTaskType() == 5) {
  246. //上锁确认
  247. queryStatus = 3;
  248. approveStatus = 2;
  249. date = new Date();
  250. lock.setConfirm2(getUserId().toString());
  251. }
  252. // 修改状态
  253. if (tPssrPower.getIds() != null && tPssrPower.getIds().length > 0) {
  254. for (Long id : tPssrPower.getIds()) {
  255. TPssrPower item = tPssrPowerService.selectTPssrPowerById(id);
  256. item.setApproveStatus(approveStatus);
  257. if (queryStatus == 3) {
  258. item.setConfirmationDate(date);
  259. }
  260. tPssrPowerService.updateTPssrPower(item);
  261. }
  262. } else {
  263. lock.setSubId(tPssrPower.getSubId());
  264. lock.setApproveStatus(queryStatus);
  265. for (TPssrPower item : tPssrPowerService.selectTPssrPowerList(lock)) {
  266. if (queryStatus == 3) {
  267. item.setConfirmationDate(date);
  268. }
  269. item.setApproveStatus(approveStatus);
  270. tPssrPowerService.updateTPssrPower(item);
  271. }
  272. }
  273. //查询当前待审批的确认人
  274. TPssrPower entity = new TPssrPower();
  275. entity.setSubId(tPssrPower.getSubId());
  276. entity.setApproveStatus(queryStatus);
  277. for (TPssrPower item : tPssrPowerService.selectTPssrPowerList(entity)) {
  278. if (tPssrPower.getTaskType() == 4) {
  279. if (item.getConfirm1().equals(getUserId().toString())) {
  280. return AjaxResult.success();
  281. }
  282. } else if (tPssrPower.getTaskType() == 5) {
  283. if (item.getConfirm2().equals(getUserId().toString())) {
  284. return AjaxResult.success();
  285. }
  286. }
  287. }
  288. //无待审批任务结束当前用户流程
  289. // 因为流程关系所以approve一定会有且只有一条数据
  290. TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrPower.getSubId());
  291. TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
  292. return AjaxResult.success();
  293. }
  294. /**
  295. * 驳回临时电源
  296. */
  297. @PutMapping("/turnDownPower")
  298. public AjaxResult turnDownPower(@RequestBody List<TPssrPower> tPssrPower) {
  299. if (CollectionUtils.isNotEmpty(tPssrPower)) {
  300. String userId = getUserId().toString();
  301. Long subId = tPssrPower.get(0).getSubId();
  302. // 修改已选择数据的状态
  303. for (TPssrPower item : tPssrPower) {
  304. TPssrPower blind = new TPssrPower();
  305. blind.setId(item.getId());
  306. blind.setApproveStatus(1L);
  307. blind.setUpdatedate(new Date());
  308. blind.setUpdaterCode(getUserId().toString());
  309. tPssrPowerService.updateTPssrPower(blind);
  310. // 新增驳回原因数据
  311. TPssrTurndown turndown = new TPssrTurndown();
  312. turndown.setForShort(forShort);
  313. turndown.setSubId(item.getSubId());
  314. turndown.setItemId(item.getId());
  315. turndown.setReason(item.getReason());
  316. turndown.setCreatedate(new Date());
  317. turndown.setCreaterCode(getUserId().toString());
  318. tPssrTurndownService.insertTPssrTurndown(turndown);
  319. }
  320. // 查询当前流程
  321. TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);
  322. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  323. TaskService taskService = processEngine.getTaskService();
  324. Task task = processEngine.getTaskService()//获取任务service
  325. .createTaskQuery()//创建查询对象
  326. .taskAssignee(userId)
  327. .processInstanceId(approve.getProcessId()).singleResult();
  328. String taskId = task.getId();
  329. // 驳回 查询所有待审批的人员
  330. TPssrPower blind = new TPssrPower();
  331. blind.setSubId(subId);
  332. blind.setApproveStatus(1L);
  333. Set<String> installer = new HashSet<>();
  334. Set<String> remover = new HashSet<>();
  335. for (TPssrPower item : tPssrPowerService.selectTPssrPowerList(blind)) {
  336. // 安装人员
  337. installer.add(item.getConfirm1());
  338. //拆除人员
  339. remover.add(item.getConfirm2());
  340. }
  341. //处理流程节点
  342. Map<String, Object> param = new HashMap<>();
  343. param.put("condition", 1);
  344. param.put("confirmUsers1", new ArrayList<>(installer));
  345. param.put("confirmUsers2", new ArrayList<>(remover));
  346. //认领任务
  347. processEngine.getTaskService().claim(taskId, userId);
  348. taskService.addComment(taskId, approve.getProcessId(), "驳回");
  349. taskService.complete(taskId, param);
  350. // 修改审批表和sub表
  351. approve.setApproveStatus(1L);
  352. approve.setUpdatedate(new Date());
  353. approve.setUpdaterCode(getUserId().toString());
  354. tPssrApproveService.updateTPssrApprove(approve);
  355. TPssrSubcontent subcontent = new TPssrSubcontent();
  356. subcontent.setId(approve.getSubId());
  357. subcontent.setApproveStatus(1L);
  358. subcontent.setUpdatedate(new Date());
  359. subcontent.setUpdaterCode(getUserId().toString());
  360. tPssrSubcontentService.updateTPssrSubcontent(subcontent);
  361. return AjaxResult.success();
  362. }
  363. return AjaxResult.error();
  364. }
  365. }