TPssrSubcontentController.java 19 KB

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