TPssrSubcontentController.java 18 KB

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