TPssrMaterialController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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.listener.pssr.ConfirmTaskCreateListener;
  12. import com.ruoyi.project.pssr.domain.*;
  13. import com.ruoyi.project.pssr.mapper.TPssrMaterialMapper;
  14. import com.ruoyi.project.pssr.service.*;
  15. import com.ruoyi.project.system.domain.SysUser;
  16. import com.ruoyi.project.system.service.ISysUserService;
  17. import org.activiti.engine.*;
  18. import org.activiti.engine.impl.identity.Authentication;
  19. import org.activiti.engine.runtime.ProcessInstance;
  20. import org.activiti.engine.task.Task;
  21. import org.apache.commons.collections4.CollectionUtils;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.CellStyle;
  24. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  25. import org.apache.poi.ss.usermodel.Row;
  26. import org.apache.poi.ss.util.CellRangeAddress;
  27. import org.apache.poi.xssf.usermodel.XSSFSheet;
  28. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.security.access.prepost.PreAuthorize;
  31. import org.springframework.web.bind.annotation.*;
  32. import javax.annotation.Resource;
  33. import java.io.FileOutputStream;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.OutputStream;
  37. import java.util.*;
  38. /**
  39. * 原料Controller
  40. *
  41. * @author ssy
  42. * @date 2024-09-18
  43. */
  44. @RestController
  45. @RequestMapping("/pssr/material")
  46. public class TPssrMaterialController extends BaseController {
  47. @Autowired
  48. private ITPssrFileService tPssrFileService;
  49. @Autowired
  50. private ITPssrTurndownService tPssrTurndownService;
  51. @Autowired
  52. private ITPssrMaterialService tPssrMaterialService;
  53. @Autowired
  54. private ITPssrApproveService tPssrApproveService;
  55. @Autowired
  56. private ITPssrSubcontentService tPssrSubcontentService;
  57. @Autowired
  58. private RuntimeService runtimeService;
  59. @Autowired
  60. private HistoryService historyService;
  61. @Resource
  62. private TPssrMaterialMapper tPssrMaterialMapper;
  63. @Autowired
  64. private ISysUserService sysUserService;
  65. private String forShort = "yfl";
  66. @Autowired
  67. private ITPssrMaterialRawService tPssrMaterialRawService;
  68. /**
  69. * 查询原料列表
  70. */
  71. @PreAuthorize("@ss.hasPermi('pssr:material:list')")
  72. @GetMapping("/list")
  73. public TableDataInfo list(TPssrMaterial tPssrMaterial) {
  74. try {
  75. TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(tPssrMaterial.getSubId());
  76. if (approve != null) {
  77. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  78. TaskService taskService = processEngine.getTaskService();
  79. Task task = processEngine.getTaskService()//获取任务service
  80. .createTaskQuery()//创建查询对象
  81. .taskAssignee(getUserId().toString())
  82. .processInstanceId(approve.getProcessId()).singleResult();
  83. if (task != null) {
  84. if (task.getName().equals("确认人1")){
  85. tPssrMaterial.setConfirm1(getUserId().toString());
  86. }else if (task.getName().equals("确认人2")){
  87. tPssrMaterial.setConfirm2(getUserId().toString());
  88. }
  89. }
  90. }
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. logger.error("待办确认人查询报错:{}",e.getMessage());
  94. }
  95. startPage();
  96. List<TPssrMaterial> list = tPssrMaterialService.selectTPssrMaterialList(tPssrMaterial);
  97. list.forEach(item -> {
  98. List<TPssrFile> tPssrFiles = tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), forShort);
  99. item.setFileList(tPssrFiles);
  100. item.setFileNum((long) tPssrFiles.size());
  101. if (item.getApproveStatus() != 2)
  102. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "yfl"));
  103. });
  104. return getDataTable(list);
  105. }
  106. /**
  107. * 导出原料列表
  108. */
  109. @PreAuthorize("@ss.hasPermi('pssr:material:export')")
  110. @Log(title = "原料", businessType = BusinessType.EXPORT)
  111. @GetMapping("/export")
  112. public AjaxResult export(TPssrMaterial tPssrMaterial) {
  113. List<TPssrMaterial> list1 = tPssrMaterialService.selectTPssrMaterialList(tPssrMaterial);
  114. TPssrMaterialRaw materialRaw = new TPssrMaterialRaw();
  115. materialRaw.setSubId(tPssrMaterial.getSubId());
  116. List<TPssrMaterialRaw> list2 = tPssrMaterialRawService.selectTPssrMaterialRawList(materialRaw);
  117. if (list1.size() != 0 && list2.size() != 0) {
  118. return AjaxResult.success(exportTmpl(list1, list2));
  119. } else {
  120. return AjaxResult.error("暂无可导出数据");
  121. }
  122. }
  123. public String exportTmpl(List<TPssrMaterial> list1, List<TPssrMaterialRaw> list2) {
  124. OutputStream out = null;
  125. String filename = null;
  126. try {
  127. String tempUrl = "static/word/pssr/yfl.xlsx"; // 模板文件
  128. InputStream is = null;
  129. is = Thread.currentThread().getContextClassLoader().getResourceAsStream(tempUrl);
  130. XSSFWorkbook wb = null;
  131. wb = new XSSFWorkbook(is);
  132. XSSFSheet sheet = wb.getSheetAt(0);
  133. // Cracker commissioning material quantity
  134. // 填充数据
  135. int rowIndex = 3;
  136. int num = 1;
  137. Row originalRow = sheet.getRow(3);
  138. Cell originalcell = originalRow.getCell(0);
  139. CellStyle originalStyle = originalcell.getCellStyle();
  140. CellStyle originalStyleCopy = wb.createCellStyle();
  141. originalStyleCopy.cloneStyleFrom(originalStyle);
  142. originalStyleCopy.setAlignment(HorizontalAlignment.LEFT);
  143. for (TPssrMaterial t : list1) {
  144. Row row = sheet.createRow(rowIndex);
  145. row.setHeight((short) 800);
  146. row.createCell(0).setCellValue(num);
  147. row.createCell(1).setCellValue(t.getUtilityQuantity());
  148. row.createCell(2).setCellValue(t.getQuantity());
  149. row.createCell(3).setCellValue(t.getUnit());
  150. row.createCell(4).setCellValue(t.getRemarks());
  151. //渲染样式
  152. for (int i = 0; i < 5; i++) {
  153. row.getCell(i).setCellStyle(originalStyle);
  154. }
  155. num++;
  156. rowIndex++;
  157. }
  158. // Raw materials
  159. // 标题
  160. Row row = sheet.getRow(1);
  161. Cell cell = row.getCell(0);
  162. CellStyle style = cell.getCellStyle();
  163. Row title = sheet.createRow(rowIndex);
  164. title.setHeight((short) 800);
  165. title.createCell(0).setCellValue("Raw materials");
  166. title.createCell(1);
  167. title.createCell(2);
  168. title.createCell(3);
  169. title.createCell(4);
  170. for (int i = 0; i < 5; i++) {
  171. title.getCell(i).setCellStyle(style);
  172. }
  173. CellRangeAddress cellRangeAddress = new CellRangeAddress(rowIndex, rowIndex, 0, 4);
  174. sheet.addMergedRegion(cellRangeAddress);
  175. rowIndex++;
  176. Row row1 = sheet.getRow(2);
  177. Cell cell1 = row1.getCell(0);
  178. CellStyle style1 = cell1.getCellStyle();
  179. Row title1 = sheet.createRow(rowIndex);
  180. title1.setHeight((short) 800);
  181. title1.createCell(0).setCellValue("NO.");
  182. title1.createCell(1).setCellValue("Raw materials");
  183. title1.createCell(2).setCellValue("Quantity");
  184. title1.createCell(3).setCellValue("Unit");
  185. title1.createCell(4).setCellValue("Note");
  186. for (int i = 0; i < 5; i++) {
  187. title1.getCell(i).setCellStyle(style1);
  188. }
  189. rowIndex++;
  190. // 填充数据
  191. num = 1;
  192. for (TPssrMaterialRaw t2 : list2) {
  193. Row row2 = sheet.createRow(rowIndex);
  194. row2.setHeight((short) 800);
  195. row2.createCell(0).setCellValue(num);
  196. row2.createCell(1).setCellValue(t2.getRawMaterial());
  197. row2.createCell(2).setCellValue(t2.getQuantity());
  198. row2.createCell(3).setCellValue(t2.getUnit());
  199. row2.createCell(4).setCellValue(t2.getRemarks());
  200. //渲染样式
  201. for (int i = 0; i < 5; i++) {
  202. row2.getCell(i).setCellStyle(originalStyle);
  203. }
  204. num++;
  205. rowIndex++;
  206. }
  207. // 备注
  208. Row title2 = sheet.createRow(rowIndex);
  209. title2.setHeight((short) 800);
  210. title2.createCell(0).setCellValue("1. 以上蒸汽裂解装置开车时所需要的物料经确认全部都已获得或具备获得条件,确认人签字。");
  211. title2.createCell(1);
  212. title2.createCell(2);
  213. title2.createCell(3);
  214. title2.createCell(4);
  215. for (int i = 0; i < 5; i++) {
  216. title2.getCell(i).setCellStyle(originalStyleCopy);
  217. }
  218. CellRangeAddress cellRangeAddress1 = new CellRangeAddress(rowIndex, rowIndex, 0, 4);
  219. sheet.addMergedRegion(cellRangeAddress1);
  220. rowIndex++;
  221. Row title3 = sheet.createRow(rowIndex);
  222. title3.setHeight((short) 800);
  223. title3.createCell(0).setCellValue("2. 如果某些物料没有获得或不具备获得条件,请及时联系相关责任人按要求进行整改。");
  224. title3.createCell(1);
  225. title3.createCell(2);
  226. title3.createCell(3);
  227. title3.createCell(4);
  228. for (int i = 0; i < 5; i++) {
  229. title3.getCell(i).setCellStyle(originalStyleCopy);
  230. }
  231. CellRangeAddress cellRangeAddress2 = new CellRangeAddress(rowIndex, rowIndex, 0, 4);
  232. sheet.addMergedRegion(cellRangeAddress2);
  233. rowIndex++;
  234. // 确认人
  235. Row title4 = sheet.createRow(rowIndex);
  236. title4.setHeight((short) 800);
  237. title4.createCell(0);
  238. title4.createCell(1);
  239. title4.createCell(2);
  240. title4.createCell(3).setCellValue("确认人:");
  241. title4.createCell(4);
  242. try {
  243. if (list1.size() != 0) {
  244. SysUser sysUser = sysUserService.selectUserById(Long.valueOf(list1.get(0).getConfirm1()));
  245. String confirm = sysUser.getSignUrl();
  246. ExcelUtils.insertPicture(wb, sheet, confirm, rowIndex, 4, 0.25, 1);
  247. }
  248. } catch (Exception e) {
  249. }
  250. for (int i = 0; i < 5; i++) {
  251. title4.getCell(i).setCellStyle(originalStyleCopy);
  252. }
  253. rowIndex++;
  254. // 确认时间
  255. Row title5 = sheet.createRow(rowIndex);
  256. title5.setHeight((short) 800);
  257. title5.createCell(0);
  258. title5.createCell(1);
  259. title5.createCell(2);
  260. title5.createCell(3).setCellValue("确认时间:");
  261. if (list1.size() != 0) {
  262. title5.createCell(4).setCellValue(DateUtils.dateTime(list1.get(0).getConfirmationDate()));
  263. for (int i = 0; i < 5; i++) {
  264. title5.getCell(i).setCellStyle(originalStyleCopy);
  265. }
  266. }
  267. rowIndex++;
  268. filename = "PSSR_19_原辅料" + ".xlsx";
  269. out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
  270. wb.write(out);
  271. wb.close();
  272. } catch (IOException e) {
  273. e.printStackTrace();
  274. }
  275. return filename;
  276. }
  277. /**
  278. * 获取原料详细信息
  279. */
  280. @PreAuthorize("@ss.hasPermi('pssr:material:query')")
  281. @GetMapping(value = "/{id}")
  282. public AjaxResult getInfo(@PathVariable("id") Long id) {
  283. TPssrMaterial item = tPssrMaterialService.selectTPssrMaterialById(id);
  284. List<TPssrFile> tPssrFiles = tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), forShort);
  285. item.setFileList(tPssrFiles);
  286. item.setFileNum((long) tPssrFiles.size());
  287. if (item.getApproveStatus() != 2)
  288. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "yfl"));
  289. return AjaxResult.success(item);
  290. }
  291. /**
  292. * 新增原料
  293. */
  294. @PreAuthorize("@ss.hasPermi('pssr:material:add')")
  295. @Log(title = "原料", businessType = BusinessType.INSERT)
  296. @PostMapping
  297. public AjaxResult add(@RequestBody TPssrMaterial tPssrMaterial) {
  298. if (StringUtils.isNotEmpty(tPssrMaterial.getConfirm1()) && tPssrMaterial.getConfirm1().equals(tPssrMaterial.getConfirm2())) {
  299. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  300. }
  301. return toAjax(tPssrMaterialService.insertTPssrMaterial(tPssrMaterial));
  302. }
  303. /**
  304. * 修改原料
  305. */
  306. @PreAuthorize("@ss.hasPermi('pssr:material:edit')")
  307. @Log(title = "原料", businessType = BusinessType.UPDATE)
  308. @PutMapping
  309. public AjaxResult edit(@RequestBody TPssrMaterial tPssrMaterial) {
  310. TPssrMaterial entity = tPssrMaterialService.selectTPssrMaterialById(tPssrMaterial.getId());
  311. if (entity.getApproveStatus() != 1 && entity.getApproveStatus() != 0) {
  312. return AjaxResult.error("当前状态不可修改!");
  313. }
  314. if (tPssrMaterial.getConfirm1().equals(tPssrMaterial.getConfirm2())) {
  315. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  316. }
  317. tPssrFileService.updateFileRelevance(tPssrMaterial.getFileIds(), "yfl", tPssrMaterial.getId(), tPssrMaterial.getSubId());
  318. return toAjax(tPssrMaterialService.updateTPssrMaterial(tPssrMaterial));
  319. }
  320. /**
  321. * 修改原料
  322. */
  323. @PreAuthorize("@ss.hasPermi('pssr:material:edit')")
  324. @Log(title = "原料", businessType = BusinessType.UPDATE)
  325. @PutMapping("editBatch")
  326. public AjaxResult editBatch(@RequestBody TPssrMaterial tPssrMaterial) {
  327. if (tPssrMaterial.getConfirm1().equals(tPssrMaterial.getConfirm2())) {
  328. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  329. }
  330. return toAjax(tPssrMaterialMapper.updateTPssrMaterialByIds(tPssrMaterial));
  331. }
  332. /**
  333. * 删除原料
  334. */
  335. @PreAuthorize("@ss.hasPermi('pssr:material:remove')")
  336. @Log(title = "原料", businessType = BusinessType.DELETE)
  337. @DeleteMapping("/{ids}")
  338. public AjaxResult remove(@PathVariable Long[] ids) {
  339. return toAjax(tPssrMaterialService.deleteTPssrMaterialByIds(ids));
  340. }
  341. /**
  342. * 确认人身防护
  343. */
  344. @PreAuthorize("@ss.hasPermi('pssr:material:edit')")
  345. @Log(title = "人身防护", businessType = BusinessType.UPDATE)
  346. @PutMapping("/confirmMaterial")
  347. public AjaxResult confirmMaterial(@RequestBody TPssrMaterial tPssrMaterial) {
  348. Date date = null;
  349. TPssrMaterial material = new TPssrMaterial();
  350. if (tPssrMaterial.getIds() != null && tPssrMaterial.getIds().length > 0) {
  351. for (Long id : tPssrMaterial.getIds()) {
  352. material = tPssrMaterialService.selectTPssrMaterialById(id);
  353. material.setConfirmationDate(new Date());
  354. material.setApproveStatus(2L);
  355. material.setUpdatedate(new Date());
  356. material.setUpdaterCode(String.valueOf(getUserId()));
  357. tPssrMaterialService.updateTPssrMaterial(material);
  358. }
  359. } else {
  360. material.setSubId(tPssrMaterial.getSubId());
  361. material.setApproveStatus(1L);
  362. material.setConfirm1(getUserId().toString());
  363. for (TPssrMaterial item : tPssrMaterialService.selectTPssrMaterialList(material)) {
  364. item.setConfirmationDate(new Date());
  365. item.setApproveStatus(2L);
  366. item.setUpdatedate(new Date());
  367. item.setUpdaterCode(String.valueOf(getUserId()));
  368. tPssrMaterialService.updateTPssrMaterial(item);
  369. }
  370. }
  371. //查询当前待审批的确认人
  372. TPssrMaterial entity = new TPssrMaterial();
  373. entity.setSubId(tPssrMaterial.getSubId());
  374. entity.setApproveStatus(1L);
  375. TPssrMaterial Material = tPssrMaterialService.selectAllConfirmedPersonBySubId(entity);
  376. if (Material != null) {
  377. //如果当前用户还有待审批任务
  378. if (Material.getConfirm1().contains(getUserId().toString())) {
  379. return AjaxResult.success();
  380. }
  381. }
  382. //无待审批任务结束当前用户流程
  383. // 因为流程关系所以approve一定会有且只有一条数据
  384. TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrMaterial.getSubId());
  385. TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
  386. return AjaxResult.success();
  387. }
  388. /**
  389. * 驳回人身防护
  390. */
  391. @PutMapping("/turnDownMaterial")
  392. public AjaxResult turnDownMaterial(@RequestBody List<TPssrMaterial> tPssrMaterial) {
  393. if (CollectionUtils.isNotEmpty(tPssrMaterial)) {
  394. String userId = getUserId().toString();
  395. Long subId = tPssrMaterial.get(0).getSubId();
  396. // 修改已选择数据的状态
  397. for (TPssrMaterial item : tPssrMaterial) {
  398. TPssrMaterial blind = new TPssrMaterial();
  399. blind.setId(item.getId());
  400. blind.setApproveStatus(1L);
  401. blind.setUpdatedate(new Date());
  402. blind.setUpdaterCode(getUserId().toString());
  403. tPssrMaterialService.updateTPssrMaterial(blind);
  404. // 新增驳回原因数据
  405. TPssrTurndown turndown = new TPssrTurndown();
  406. turndown.setForShort(forShort);
  407. turndown.setSubId(item.getSubId());
  408. turndown.setItemId(item.getId());
  409. turndown.setReason(item.getReason());
  410. turndown.setCreatedate(new Date());
  411. turndown.setCreaterCode(getUserId().toString());
  412. tPssrTurndownService.insertTPssrTurndown(turndown);
  413. }
  414. // 查询当前流程
  415. TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);
  416. try {
  417. runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr1confirm");
  418. historyService.deleteHistoricProcessInstance(approve.getProcessId());
  419. } catch (Exception e) {
  420. logger.info("无运行时流程");
  421. }
  422. // 驳回 查询所有待审批的人员
  423. // 查询确认人
  424. TPssrMaterial entity = new TPssrMaterial();
  425. entity.setSubId(subId);
  426. entity.setApproveStatus(1L);
  427. TPssrMaterial PssrMaterial = tPssrMaterialService.selectAllConfirmedPersonBySubId(entity);
  428. String confirmer1s = null;
  429. if (PssrMaterial != null) {
  430. confirmer1s = PssrMaterial.getConfirm1();
  431. }
  432. logger.info("=======================confirmer1s:{}", confirmer1s);
  433. Set<String> confirmerUsers1 = new HashSet<>();
  434. if (StringUtils.isNotEmpty(confirmer1s)) {
  435. confirmerUsers1.addAll(Arrays.asList(confirmer1s.split(",")));
  436. }
  437. // 开始申请流程
  438. long businessKey = approve.getApproveId();
  439. //开始工作流、监听
  440. Authentication.setAuthenticatedUserId(userId);//设置当前申请人
  441. Map<String, Object> variables = new HashMap<>();
  442. variables.put("applyUser", userId);
  443. variables.put("confirmUsers", new ArrayList<>(confirmerUsers1));
  444. variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件
  445. variables.put("chargePerson", approve.getSubCharge());
  446. //采用key来启动流程定义并设置流程变量,返回流程实例
  447. ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr1confirm", String.valueOf(businessKey), variables);
  448. // 修改审批表和sub表
  449. approve.setProcessId(pi.getProcessInstanceId());
  450. approve.setApproveStatus(1L);
  451. approve.setUpdatedate(new Date());
  452. approve.setUpdaterCode(getUserId().toString());
  453. tPssrApproveService.updateTPssrApprove(approve);
  454. TPssrSubcontent subcontent = new TPssrSubcontent();
  455. subcontent.setId(approve.getSubId());
  456. subcontent.setApproveStatus(1L);
  457. subcontent.setUpdatedate(new Date());
  458. subcontent.setUpdaterCode(getUserId().toString());
  459. tPssrSubcontentService.updateTPssrSubcontent(subcontent);
  460. return AjaxResult.success();
  461. }
  462. return AjaxResult.error();
  463. }
  464. }