TPssrSubcontentController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package com.ruoyi.project.pssr.controller;
  2. import com.deepoove.poi.XWPFTemplate;
  3. import com.deepoove.poi.data.*;
  4. import com.deepoove.poi.data.style.Style;
  5. import com.deepoove.poi.data.style.TableStyle;
  6. import com.ruoyi.common.utils.DateUtils;
  7. import com.ruoyi.common.utils.file.FileUploadUtils;
  8. import com.ruoyi.common.utils.file.FileUtils;
  9. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  10. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  11. import com.ruoyi.framework.config.RuoYiConfig;
  12. import com.ruoyi.framework.web.controller.BaseController;
  13. import com.ruoyi.framework.web.domain.AjaxResult;
  14. import com.ruoyi.framework.web.page.TableDataInfo;
  15. import com.ruoyi.project.pssr.domain.TPssrAboveall;
  16. import com.ruoyi.project.pssr.domain.TPssrApprove;
  17. import com.ruoyi.project.pssr.domain.TPssrSubcontent;
  18. import com.ruoyi.project.pssr.service.ITPssrAboveallService;
  19. import com.ruoyi.project.pssr.service.ITPssrApproveService;
  20. import com.ruoyi.project.pssr.service.ITPssrSubcontentService;
  21. import com.ruoyi.project.system.domain.SysUser;
  22. import com.ruoyi.project.system.service.impl.SysUserServiceImpl;
  23. import io.jsonwebtoken.lang.Assert;
  24. import org.activiti.engine.HistoryService;
  25. import org.activiti.engine.ProcessEngine;
  26. import org.activiti.engine.ProcessEngines;
  27. import org.activiti.engine.TaskService;
  28. import org.activiti.engine.task.Task;
  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 java.io.File;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.time.LocalDateTime;
  36. import java.time.format.DateTimeFormatter;
  37. import java.util.Date;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.Map;
  41. /**
  42. * 开车条件确认Controller
  43. *
  44. * @author ssy
  45. * @date 2024-09-18
  46. */
  47. @RestController
  48. @RequestMapping("/pssr/subcontent")
  49. public class TPssrSubcontentController extends BaseController {
  50. @Autowired
  51. private ITPssrSubcontentService tPssrSubcontentService;
  52. @Autowired
  53. private ITPssrAboveallService tPssrAboveallService;
  54. @Autowired
  55. private ITPssrApproveService tPssrApproveService;
  56. @Autowired
  57. private SysUserServiceImpl sysUserService;
  58. @Autowired
  59. private TPssrApproveController tPssrApproveController;
  60. @Autowired
  61. private HistoryService historyService;
  62. @Autowired
  63. private TaskService taskService;
  64. /**
  65. * 查询开车条件确认列表
  66. */
  67. @PreAuthorize("@ss.hasPermi('pssr:subcontent:list')")
  68. @GetMapping("/list")
  69. public TableDataInfo list(TPssrSubcontent tPssrSubcontent) {
  70. startPage();
  71. List<TPssrSubcontent> list = tPssrSubcontentService.selectTPssrSubcontentList(tPssrSubcontent);
  72. return getDataTable(list);
  73. }
  74. /**
  75. * 导出开车条件确认列表
  76. */
  77. @PreAuthorize("@ss.hasPermi('pssr:subcontent:export')")
  78. @Log(title = "开车条件确认", businessType = BusinessType.EXPORT)
  79. @GetMapping("/export")
  80. public AjaxResult export(TPssrSubcontent tPssrSubcontent) throws IOException {
  81. List<TPssrSubcontent> list = tPssrSubcontentService.selectTPssrSubcontentList(tPssrSubcontent);
  82. // ExcelUtil<TPssrSubcontent> util = new ExcelUtil<TPssrSubcontent>(TPssrSubcontent.class);
  83. // return util.exportExcel(list, "subcontent");
  84. //渲染文本
  85. Map<String, Object> params = getWordData(list, tPssrSubcontent.getAboveallId());
  86. // 模板路径
  87. String templatePath = "static/word/pssr/pssrConfirm.docx";
  88. // 生成word的路径
  89. String fileDir = RuoYiConfig.getProfile() + "/" + "pssr";
  90. // 生成word的文件名称
  91. String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"));
  92. String fileName = time + tPssrSubcontent.getAboveallId() + "_" + DateUtils.getDate() + ".docx";
  93. String wordPath = createWord(templatePath, fileDir, fileName, params, "pssr");
  94. return AjaxResult.success(wordPath);
  95. }
  96. public Map<String, Object> getWordData(List<TPssrSubcontent> list, Long aboveallId) {
  97. TPssrApprove approve = null;
  98. TPssrAboveall aboveall = tPssrAboveallService.selectTPssrAboveallById(aboveallId);
  99. Map<String, Object> params = new HashMap<>();
  100. TableRenderData tableRenderData = new TableRenderData();
  101. TableStyle tableStyle = new TableStyle();
  102. tableStyle.setWidth("100%");
  103. tableStyle.setColWidths(new int[]{10, 13, 47, 10, 10, 10});
  104. tableRenderData.setTableStyle(tableStyle);
  105. // 创建表头行
  106. RowRenderData row1 = Rows.of("部门", "项目", "内容", "确认人", "确认时间", "备注").center().textBold().rowExactHeight(1.1f).create();
  107. tableRenderData.addRow(row1);
  108. int plant = 0, ctm = 0, cta = 0, count = 1;
  109. PictureRenderData plantMgr = null;
  110. for (int i = 0; i < list.size(); i++) {
  111. TPssrSubcontent subcontent = list.get(i);
  112. // 获取审批信息
  113. if (approve == null) {
  114. approve = tPssrApproveService.selectTPssrApproveByAboveallId(subcontent.getAboveallId());
  115. }
  116. // 创建数据行
  117. RowRenderData row = Rows.of(subcontent.getDeptUnit(), subcontent.getItem(), subcontent.getContent(), subcontent.getApproveStatus() == 2 ? "" : "", // 如果需要确认人签名图片,这里空着
  118. DateUtils.dateTime(subcontent.getConfirmationDate()), subcontent.getRemarks()).verticalCenter().rowExactHeight(1.1f).create();
  119. // 如果确认状态为2,插入确认人签名图片
  120. if (subcontent.getApproveStatus() == 2) {
  121. SysUser sysUser = sysUserService.selectUserById(Long.valueOf(subcontent.getConfirm()));
  122. if (sysUser != null) {
  123. row.getCells().set(3, new CellRenderData().addParagraph(new ParagraphRenderData().addPicture(Pictures.ofLocal(FileUtils.fileName(sysUser.getSignUrl())).size(120, 40).create())));
  124. }
  125. }
  126. tableRenderData.addRow(row);
  127. // 判断所属部门是否相同,记录索引
  128. if (subcontent.getDeptUnit().equals("Plant")) plant = count;
  129. if (subcontent.getDeptUnit().equals("CTM")) ctm = count;
  130. if (subcontent.getDeptUnit().equals("CTA")) cta = count;
  131. // 判断是否为最后一条或部门是否不相同
  132. if (i < list.size() - 1 && subcontent.getDeptUnit().equals(list.get(i + 1).getDeptUnit())) {
  133. count++;
  134. } else {
  135. count++;
  136. //根据当前数据所属部门添加经理审批行
  137. RowRenderData signRow = Rows.of("", "装置经理确认: 日期:", "", "", "", "").verticalCenter().textBold().rowExactHeight(1.1f).create();
  138. if (subcontent.getDeptUnit().equals("Plant")) {
  139. plant = count;
  140. signRow = Rows.of("", "装置经理确认: 日期:", "", "", "", "").verticalCenter().textBold().rowExactHeight(1.1f).create();
  141. }
  142. if (subcontent.getDeptUnit().equals("CTM")) {
  143. ctm = count + 1;
  144. signRow = Rows.of("", "CTM 经理确认: 日期:", "", "", "", "").verticalCenter().textBold().rowExactHeight(1.1f).create();
  145. }
  146. if (subcontent.getDeptUnit().equals("CTA")) {
  147. cta = count + 2;
  148. signRow = Rows.of("", "CTA 经理确认: 日期:", "", "", "", "").verticalCenter().textBold().rowExactHeight(1.1f).create();
  149. }
  150. // 添加经理审批签字数据
  151. if (approve != null) {
  152. SysUser sysUser = null;
  153. //根据当前所属部门查询部门经理
  154. if (subcontent.getDeptUnit().equals("Plant")) {
  155. sysUser = sysUserService.selectUserById(Long.valueOf(aboveall.getPlantMgr()));
  156. plantMgr = Pictures.ofLocal(FileUtils.fileName(sysUser.getSignUrl())).size(110, 30).create();
  157. }
  158. if (subcontent.getDeptUnit().equals("CTM")) {
  159. sysUser = sysUserService.selectUserById(Long.valueOf(aboveall.getCtmMgr()));
  160. }
  161. if (subcontent.getDeptUnit().equals("CTA")) {
  162. sysUser = sysUserService.selectUserById(Long.valueOf(aboveall.getCtaMgr()));
  163. }
  164. if (sysUser != null) {
  165. //经理签字图片
  166. PictureRenderData mgr = Pictures.ofLocal(FileUtils.fileName(sysUser.getSignUrl())).size(110, 30).create();
  167. //空数据列,为了后面合并
  168. TextRenderData text = Texts.of("").create();
  169. CellRenderData cell = new CellRenderData();
  170. cell.addParagraph(new ParagraphRenderData().addText(text));
  171. //数据列,该列内容为经理签字和日期
  172. CellRenderData cell2 = new CellRenderData();
  173. if (subcontent.getDeptUnit().equals("Plant"))
  174. cell2.addParagraph(new ParagraphRenderData().addText(Texts.of("装置经理确认:").create()).addPicture(mgr).addText(" 日期:" + DateUtils.dateTime(aboveall.getConfirmationDate())));
  175. if (subcontent.getDeptUnit().equals("CTM"))
  176. cell2.addParagraph(new ParagraphRenderData().addText(Texts.of("CTM 经理确认:").create()).addPicture(mgr).addText(" 日期:" + DateUtils.dateTime(aboveall.getConfirmationDate())));
  177. if (subcontent.getDeptUnit().equals("CTA"))
  178. cell2.addParagraph(new ParagraphRenderData().addText(Texts.of("CTA 经理确认:").create()).addPicture(mgr).addText(" 日期:" + DateUtils.dateTime(aboveall.getConfirmationDate())));
  179. signRow = Rows.of(cell, cell2, cell, cell, cell, cell).verticalCenter().textBold().rowExactHeight(1.1f).create();
  180. }
  181. }
  182. tableRenderData.addRow(signRow);
  183. }
  184. }
  185. // 设置第一列加粗样式
  186. Style boldStyle = new Style();
  187. boldStyle.setBold(true);
  188. // 遍历每一行,将第一列的文字加粗
  189. for (RowRenderData row : tableRenderData.getRows()) {
  190. // 获取第一列
  191. CellRenderData firstColumn = row.getCells().get(0);
  192. // 直接使用现有内容并应用加粗样式
  193. String text = firstColumn.getParagraphs().get(0).getContents().get(0).toString(); // 获取第一列的文本内容
  194. // 使用 Cells.of 创建带加粗样式的新单元格
  195. firstColumn = Cells.of(Texts.of(text).style(boldStyle).create()).center().create();
  196. // 将加粗后的单元格重新放回第一列
  197. row.getCells().set(0, firstColumn);
  198. }
  199. // 合并第一列和每个部门的最后一行
  200. MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(1, 0), MergeCellRule.Grid.of(plant, 0))
  201. .map(MergeCellRule.Grid.of(plant, 1), MergeCellRule.Grid.of(plant, 5))
  202. .map(MergeCellRule.Grid.of(plant + 1, 0), MergeCellRule.Grid.of(ctm, 0))
  203. .map(MergeCellRule.Grid.of(ctm, 1), MergeCellRule.Grid.of(ctm, 5))
  204. .map(MergeCellRule.Grid.of(ctm + 1, 0), MergeCellRule.Grid.of(cta, 0))
  205. .map(MergeCellRule.Grid.of(cta, 1), MergeCellRule.Grid.of(cta, 5)).build();
  206. tableRenderData.setMergeRule(rule);
  207. // 准备表格数据和其他参数
  208. params.put("unit", aboveall.getUnit() + "_" + aboveall.getUnitDes());
  209. params.put("subcontent", tableRenderData);
  210. params.put("plantMgr", plantMgr);
  211. params.put("confirmDate", DateUtils.dateTime(aboveall.getConfirmationDate()));
  212. return params;
  213. }
  214. private String createWord(String templatePath, String fileDir, String fileName, Map<String, Object> paramMap, String directory) throws IOException {
  215. Assert.notNull(templatePath, "word模板文件路径不能为空");
  216. Assert.notNull(fileDir, "生成的文件存放地址不能为空");
  217. Assert.notNull(fileName, "生成的文件名不能为空");
  218. File dir = new File(fileDir);
  219. if (!dir.exists()) {
  220. logger.info("目录不存在,创建文件夹{}!", fileDir);
  221. dir.mkdirs();
  222. }
  223. fileName = fileName.replaceAll("/", "_"); //替换文件中敏感字段
  224. logger.info("目录文件{}!", fileName);
  225. String filePath = fileDir + "/" + fileName;
  226. logger.info("目录{}!", filePath);
  227. logger.info("模板{}!", templatePath);
  228. // 读取模板渲染参数
  229. InputStream is = getClass().getClassLoader().getResourceAsStream(templatePath);
  230. XWPFTemplate template = XWPFTemplate.compile(is).render(paramMap);
  231. try {
  232. // 将模板参数写入路径
  233. template.writeToFile(filePath);
  234. template.close();
  235. is.close();
  236. } catch (Exception e) {
  237. logger.error("生成word异常{}", e.getMessage());
  238. e.printStackTrace();
  239. }
  240. String pathFileName = FileUploadUtils.getPathFileName(RuoYiConfig.getFilePath("/" + directory), fileName);
  241. return pathFileName;
  242. }
  243. /**
  244. * 获取开车条件确认详细信息
  245. */
  246. @PreAuthorize("@ss.hasPermi('pssr:subcontent:query')")
  247. @GetMapping(value = "/{id}")
  248. public AjaxResult getInfo(@PathVariable("id") Long id) {
  249. return AjaxResult.success(tPssrSubcontentService.selectTPssrSubcontentById(id));
  250. }
  251. /**
  252. * 新增开车条件确认
  253. */
  254. @PreAuthorize("@ss.hasPermi('pssr:subcontent:add')")
  255. @Log(title = "开车条件确认", businessType = BusinessType.INSERT)
  256. @PostMapping
  257. public AjaxResult add(@RequestBody TPssrSubcontent tPssrSubcontent) {
  258. return toAjax(tPssrSubcontentService.insertTPssrSubcontent(tPssrSubcontent));
  259. }
  260. /**
  261. * 修改开车条件确认
  262. */
  263. @PreAuthorize("@ss.hasPermi('pssr:subcontent:edit')")
  264. @Log(title = "开车条件确认", businessType = BusinessType.UPDATE)
  265. @PutMapping
  266. public AjaxResult edit(@RequestBody TPssrSubcontent tPssrSubcontent) {
  267. return toAjax(tPssrSubcontentService.updateTPssrSubcontent(tPssrSubcontent));
  268. }
  269. /**
  270. * 删除开车条件确认
  271. */
  272. @PreAuthorize("@ss.hasPermi('pssr:subcontent:remove')")
  273. @Log(title = "开车条件确认", businessType = BusinessType.DELETE)
  274. @DeleteMapping("/{ids}")
  275. public AjaxResult remove(@PathVariable Long[] ids) {
  276. return toAjax(tPssrSubcontentService.deleteTPssrSubcontentByIds(ids));
  277. }
  278. /**
  279. * 修改开车条件确认
  280. */
  281. @PreAuthorize("@ss.hasPermi('pssr:subcontent:edit')")
  282. @Log(title = "开车条件确认", businessType = BusinessType.UPDATE)
  283. @PutMapping("/handleApprove")
  284. public AjaxResult handleApprove(@RequestBody TPssrSubcontent tPssrSubcontent) {
  285. if (tPssrSubcontent.getIds() != null && tPssrSubcontent.getIds().size() > 0) {
  286. for (Long id : tPssrSubcontent.getIds()) {
  287. TPssrSubcontent subcontent = new TPssrSubcontent();
  288. subcontent.setId(id);
  289. subcontent.setApproveStatus(tPssrSubcontent.getApproveStatus());
  290. if (tPssrSubcontent.getApproveStatus() == 2) {
  291. subcontent.setConfirmationDate(new Date());
  292. }
  293. subcontent.setUpdatedate(new Date());
  294. subcontent.setUpdaterCode(getUserId().toString());
  295. tPssrSubcontentService.updateTPssrSubcontent(subcontent);
  296. }
  297. } else {
  298. TPssrSubcontent pssrSubcontent = new TPssrSubcontent();
  299. pssrSubcontent.setAboveallId(tPssrSubcontent.getAboveallId());
  300. pssrSubcontent.setApproveStatus(1L);
  301. for (TPssrSubcontent subcontent : tPssrSubcontentService.selectTPssrSubcontentList(pssrSubcontent)) {
  302. subcontent.setApproveStatus(tPssrSubcontent.getApproveStatus());
  303. if (tPssrSubcontent.getApproveStatus() == 2) {
  304. subcontent.setConfirmationDate(new Date());
  305. }
  306. subcontent.setUpdatedate(new Date());
  307. subcontent.setUpdaterCode(getUserId().toString());
  308. tPssrSubcontentService.updateTPssrSubcontent(subcontent);
  309. }
  310. }
  311. TPssrApprove approve = tPssrApproveService.selectTPssrApproveByAboveallId(tPssrSubcontent.getAboveallId());
  312. TPssrAboveall aboveall = tPssrAboveallService.selectTPssrAboveallById(tPssrSubcontent.getAboveallId());
  313. Task task = taskService.createTaskQuery().processInstanceId(approve.getProcessId()).singleResult();
  314. if (task.getName().equals("所有装置经理")) {
  315. if (aboveall.getPlantMgr() == getUserId()) {
  316. aboveall.setPlantConfirmdate(new Date());
  317. } else if (aboveall.getCtmMgr() == getUserId()) {
  318. aboveall.setCtmConfirmdate(new Date());
  319. } else if (aboveall.getCtaMgr() == getUserId()) {
  320. aboveall.setCtaConfirmdate(new Date());
  321. }
  322. }
  323. String condition = "0";
  324. if (task.getName().equals("装置经理")) {
  325. if (tPssrSubcontent.getApproveStatus() == 2) {
  326. aboveall.setApproveStatus(2L);
  327. condition = "0";
  328. } else if (tPssrSubcontent.getApproveStatus() == 0) {
  329. aboveall.setApproveStatus(0L);
  330. condition = "1";
  331. // 将选中的数据重新发起申请
  332. for (Long id : tPssrSubcontent.getIds()) {
  333. tPssrApproveController.doApprove(id);
  334. }
  335. }
  336. }
  337. tPssrAboveallService.updateTPssrAboveall(aboveall);
  338. //处理流程节点
  339. Map<String, Object> param = new HashMap<>();
  340. param.put("condition", condition);
  341. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  342. TaskService taskService = processEngine.getTaskService();
  343. //认领任务
  344. processEngine.getTaskService().claim(task.getId(), getUserId().toString());
  345. taskService.complete(task.getId(), param);
  346. return AjaxResult.success();
  347. }
  348. }