TPssrSubcontentController.java 16 KB

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