TTrainingbccController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package com.ruoyi.project.training.controller;
  2. import com.deepoove.poi.XWPFTemplate;
  3. import com.deepoove.poi.data.*;
  4. import com.deepoove.poi.data.style.CellStyle;
  5. import com.deepoove.poi.data.style.TableStyle;
  6. import com.ruoyi.common.utils.DateUtils;
  7. import com.ruoyi.common.utils.DictUtils;
  8. import com.ruoyi.common.utils.file.FileUploadUtils;
  9. import com.ruoyi.common.utils.file.FileUtils;
  10. import com.ruoyi.common.utils.poi.ExcelUtil;
  11. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  12. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  13. import com.ruoyi.framework.config.RuoYiConfig;
  14. import com.ruoyi.framework.web.controller.BaseController;
  15. import com.ruoyi.framework.web.domain.AjaxResult;
  16. import com.ruoyi.framework.web.page.TableDataInfo;
  17. import com.ruoyi.project.officeConvert.OfficeConvertController;
  18. import com.ruoyi.project.plant.domain.TStaffmgr;
  19. import com.ruoyi.project.plant.service.ITStaffmgrService;
  20. import com.ruoyi.project.system.domain.SysUser;
  21. import com.ruoyi.project.system.mapper.SysUserMapper;
  22. import com.ruoyi.project.document.domain.TPlantproglist;
  23. import com.ruoyi.project.document.service.ITPlantproglistService;
  24. import com.ruoyi.project.training.domain.TTrainingbcc;
  25. import com.ruoyi.project.training.domain.TTrainingbccDevice;
  26. import com.ruoyi.project.training.mapper.TTrainingbccDeviceMapper;
  27. import com.ruoyi.project.training.mapper.TTrainingbccMapper;
  28. import com.ruoyi.project.training.service.ITTrainingbccService;
  29. import io.jsonwebtoken.lang.Assert;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.security.access.prepost.PreAuthorize;
  32. import org.springframework.web.bind.annotation.*;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import javax.annotation.Resource;
  35. import java.io.File;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.time.LocalDate;
  39. import java.time.LocalDateTime;
  40. import java.time.format.DateTimeFormatter;
  41. import java.util.HashMap;
  42. import java.util.Iterator;
  43. import java.util.List;
  44. import java.util.Map;
  45. /**
  46. * 装置培训跟踪bController
  47. *
  48. * @author ssy
  49. * @date 2024-06-19
  50. */
  51. @RestController
  52. @RequestMapping("/training/trainingbcc")
  53. public class TTrainingbccController extends BaseController {
  54. @Autowired
  55. private ITTrainingbccService tTrainingbccService;
  56. @Resource
  57. private TTrainingbccDeviceMapper tTrainingbccDeviceMapper;
  58. @Resource
  59. private TTrainingbccMapper tTrainingbccMapper;
  60. @Autowired
  61. private ITStaffmgrService tStaffmgrService;
  62. @Autowired
  63. private SysUserMapper sysUserMapper;
  64. @Autowired
  65. private ITPlantproglistService tPlantproglistService;
  66. @Resource
  67. private OfficeConvertController officeConvertController;
  68. /**
  69. * 查询装置培训跟踪b列表
  70. */
  71. @GetMapping("/list")
  72. public TableDataInfo list(TTrainingbcc tTrainingbcc) {
  73. startPage();
  74. List<TTrainingbcc> list = tTrainingbccService.selectTTrainingbccList(tTrainingbcc);
  75. for (TTrainingbcc t : list
  76. ) {
  77. TTrainingbccDevice device = new TTrainingbccDevice();
  78. device.setRegularId(t.getId());
  79. int all = tTrainingbccDeviceMapper.countTraining(device);
  80. device.setExamState(1l);
  81. int finish = tTrainingbccDeviceMapper.countTraining(device);
  82. t.setHaveTraining(finish);
  83. t.setMustTraining(all);
  84. if (all != 0) {
  85. String per = String.valueOf(finish * 100 / all);
  86. t.setTrainingPercent(per);
  87. } else {
  88. t.setTrainingPercent("0");
  89. }
  90. // 检查统计数据与isFinish字段的一致性,不一致则更新
  91. boolean shouldBeFinished = (all > 0 && finish == all); // 有培训人员且全部完成
  92. boolean currentIsFinish = (t.getIsfinish() != null && t.getIsfinish() == 1L); // 当前标记为完成
  93. if (shouldBeFinished != currentIsFinish) {
  94. // 不一致,更新isFinish字段
  95. Long newIsFinish = shouldBeFinished ? 1L : 0L;
  96. t.setIsfinish(newIsFinish);
  97. // 更新数据库中的isFinish字段
  98. TTrainingbcc updateParam = new TTrainingbcc();
  99. updateParam.setId(t.getId());
  100. updateParam.setIsfinish(newIsFinish);
  101. tTrainingbccService.updateTTrainingbcc(updateParam);
  102. logger.info("更新培训完成状态 - 培训ID: {}, 原状态: {}, 新状态: {}, 统计: {}/{}",
  103. t.getId(), currentIsFinish ? "完成" : "未完成",
  104. shouldBeFinished ? "完成" : "未完成", finish, all);
  105. }
  106. }
  107. return getDataTable(list);
  108. }
  109. /**
  110. * 导出装置培训跟踪b列表
  111. */
  112. @PreAuthorize("@ss.hasPermi('training:trainingbcc:export')")
  113. @Log(title = "装置培训跟踪b", businessType = BusinessType.EXPORT)
  114. @GetMapping("/export")
  115. public AjaxResult export(TTrainingbcc tTrainingbcc) {
  116. List<TTrainingbcc> list = tTrainingbccService.selectTTrainingbccList(tTrainingbcc);
  117. ExcelUtil<TTrainingbcc> util = new ExcelUtil<TTrainingbcc>(TTrainingbcc.class);
  118. return util.exportExcel(list, "trainingbcc");
  119. }
  120. /**
  121. * 获取装置培训跟踪b详细信息
  122. */
  123. @PreAuthorize("@ss.hasPermi('training:trainingbcc:query')")
  124. @GetMapping(value = "/{id}")
  125. public AjaxResult getInfo(@PathVariable("id") Long id) {
  126. return AjaxResult.success(tTrainingbccService.selectTTrainingbccById(id));
  127. }
  128. /**
  129. * 获取装置培训跟踪b详细信息
  130. */
  131. @PreAuthorize("@ss.hasPermi('training:trainingbcc:query')")
  132. @GetMapping(value = "/staffIds/{id}")
  133. public AjaxResult getStaffIds(@PathVariable("id") Long id) {
  134. TTrainingbcc tTrainingbcc = tTrainingbccService.selectTTrainingbccById(id);
  135. TTrainingbccDevice param = new TTrainingbccDevice();
  136. param.setRegularId(id);
  137. List<TTrainingbccDevice> list = tTrainingbccDeviceMapper.selectTTrainingbccDeviceList(param);
  138. String staffIds = "";
  139. for (TTrainingbccDevice t : list
  140. ) {
  141. staffIds = staffIds + "," + t.getStaffId();
  142. }
  143. tTrainingbcc.setStaffIds(staffIds);
  144. return AjaxResult.success(tTrainingbcc);
  145. }
  146. /**
  147. * 新增装置培训跟踪b
  148. */
  149. @PreAuthorize("@ss.hasPermi('training:trainingbcc:add')")
  150. @Log(title = "装置培训跟踪b", businessType = BusinessType.INSERT)
  151. @PostMapping
  152. public AjaxResult add(@RequestBody TTrainingbcc tTrainingbcc) {
  153. tTrainingbccService.insertTTrainingbcc(tTrainingbcc);
  154. //培训岗位人员
  155. if (tTrainingbcc.getPosition() != null) {
  156. TStaffmgr s = new TStaffmgr();
  157. s.setActualposts(tTrainingbcc.getPosition());
  158. s.setDeptId(103l);
  159. List<TStaffmgr> trainingStaff = tStaffmgrService.selectAllTStaffmgrList(s);
  160. try {
  161. //判断离职
  162. if (!org.apache.commons.lang.StringUtils.isEmpty(tTrainingbcc.getYears())) {
  163. Iterator<TStaffmgr> iterator = trainingStaff.iterator();
  164. while (iterator.hasNext()) {
  165. TStaffmgr t = iterator.next();
  166. if (t.getLeftDate() != null && t.getDelFlag() == 9) {
  167. if (t.getLeftDate().getTime() - tTrainingbcc.getCourseStartdate().getTime() < 0l) {
  168. logger.debug(t.getName() + "离职时间小于培训时间");
  169. iterator.remove();
  170. }
  171. }
  172. }
  173. }
  174. } catch (Exception e) {
  175. logger.error(e.toString());
  176. }
  177. for (TStaffmgr staff : trainingStaff) {
  178. TTrainingbccDevice tTrainingDevice = new TTrainingbccDevice();
  179. tTrainingDevice.setStaffId(staff.getStaffid());
  180. tTrainingDevice.setRegularId(tTrainingbcc.getId());
  181. tTrainingDevice.setStartDate(tTrainingbcc.getCourseStartdate());
  182. tTrainingDevice.setSupplementary("0");
  183. tTrainingbccDeviceMapper.insertTTrainingbccDevice(tTrainingDevice);
  184. }
  185. }
  186. return toAjax(1);
  187. }
  188. /**
  189. * 修改装置培训跟踪b
  190. */
  191. @PreAuthorize("@ss.hasPermi('training:trainingbcc:edit')")
  192. @Log(title = "装置培训跟踪b", businessType = BusinessType.UPDATE)
  193. @PutMapping
  194. public AjaxResult edit(@RequestBody TTrainingbcc tTrainingbcc) {
  195. return toAjax(tTrainingbccService.updateTTrainingbcc(tTrainingbcc));
  196. }
  197. /**
  198. * 修改装置培训跟踪b
  199. */
  200. @PreAuthorize("@ss.hasPermi('training:trainingbcc:edit')")
  201. @Log(title = "装置培训跟踪b", businessType = BusinessType.UPDATE)
  202. @PostMapping(value = "/updateStaffIds")
  203. public AjaxResult updateStaffIds(@RequestBody TTrainingbcc tTrainingbcc) {
  204. return toAjax(tTrainingbccService.updateStaffIds(tTrainingbcc));
  205. }
  206. /**
  207. * 删除装置培训跟踪b
  208. */
  209. @PreAuthorize("@ss.hasPermi('training:trainingbcc:remove')")
  210. @Log(title = "装置培训跟踪b", businessType = BusinessType.DELETE)
  211. @DeleteMapping("/{ids}")
  212. public AjaxResult remove(@PathVariable Long[] ids) {
  213. tTrainingbccDeviceMapper.deleteTTrainingbccDeviceByTrainingbcc(ids);
  214. return toAjax(tTrainingbccService.deleteTTrainingbccByIds(ids));
  215. }
  216. @Log(title = "附件上传", businessType = BusinessType.UPDATE)
  217. @PostMapping("/uploadFile/{id}")
  218. public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @PathVariable Long id) throws IOException {
  219. if (!file.isEmpty()) {
  220. // 获取当前日期
  221. LocalDate currentDate = LocalDate.now();
  222. // 获取年份
  223. int year = currentDate.getYear();
  224. // 获取月份
  225. int month = currentDate.getMonthValue();
  226. String dir = "/elearn/" + year;
  227. String url = FileUploadUtils.upload(RuoYiConfig.getFilePath(dir), file);
  228. TTrainingbcc entity = tTrainingbccService.selectTTrainingbccById(id);
  229. entity.setFileUrl(url);
  230. entity.setFileName(file.getOriginalFilename());
  231. tTrainingbccService.updateTTrainingbcc(entity);
  232. return AjaxResult.success(id);
  233. }
  234. return AjaxResult.error("上传失败,请联系管理员");
  235. }
  236. @PreAuthorize("@ss.hasPermi('pssr:subcontent:export')")
  237. @Log(title = "导出签名表", businessType = BusinessType.EXPORT)
  238. @GetMapping("/exportSign")
  239. public AjaxResult exportSign(TTrainingbcc tTrainingbcc) throws Exception {
  240. TTrainingbcc tTraining = tTrainingbccMapper.selectTTrainingbccById(tTrainingbcc.getId());
  241. String tempUrl = "";
  242. // 模板路径
  243. if (tTraining.getTrainingType().equals("20")) {
  244. tempUrl = "static/word/training/signMOCbcc.docx"; // 模板文件
  245. } else {
  246. tempUrl = "static/word/training/signbcc.docx"; // 模板文件
  247. }
  248. //渲染文本
  249. Map<String, Object> params = getWordData(tTraining);
  250. // 生成word的路径
  251. String fileDir = RuoYiConfig.getProfile() + "/" + "tTrainingbcc";
  252. // 生成word的文件名称
  253. String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"));
  254. String fileName = time + tTraining.getId() + "_" + DateUtils.getDate() + ".docx";
  255. String wordPath = createWord(tempUrl, fileDir, fileName, params, "tTrainingbcc");
  256. return AjaxResult.success(officeConvertController.wordTransPdf(wordPath));
  257. }
  258. public Map<String, Object> getWordData(TTrainingbcc trainingbcc) {
  259. Map<String, Object> params = new HashMap<>();
  260. TableRenderData tableRenderData = new TableRenderData();
  261. TableStyle tableStyle = new TableStyle();
  262. tableStyle.setWidth("100%");
  263. tableStyle.setColWidths(new int[]{8, 10, 10, 12, 10, 8, 10, 10, 12, 10});
  264. tableRenderData.setTableStyle(tableStyle);
  265. // 创建表头行
  266. RowRenderData row1 = Rows.of("序号", "姓名", "部门", "签名", "日期", "序号", "姓名", "部门", "签名", "日期").center().textBold().rowExactHeight(1.1f).create();
  267. tableRenderData.addRow(row1);
  268. int plant = 0, ctm = 0, cta = 0, count = 1;
  269. PictureRenderData plantMgr = null;
  270. TTrainingbccDevice tTrainingbccDevice = new TTrainingbccDevice();
  271. tTrainingbccDevice.setRegularId(trainingbcc.getId());
  272. List<TTrainingbccDevice> list = tTrainingbccDeviceMapper.sortSelectTTrainingbccDeviceList(tTrainingbccDevice);
  273. for (int i = 0; i < list.size(); i++) {
  274. TTrainingbccDevice t1 = list.get(i);
  275. for (int j = i+1; j < list.size(); j++) {
  276. TTrainingbccDevice t2 = list.get(j);
  277. if (t1.getName().equals(t2.getName())) {
  278. t1.setName(t1.getName() +"1");
  279. t2.setName(t2.getName() + "2");
  280. }
  281. }
  282. }
  283. for (int i = 0; i < list.size(); i++) {
  284. //如果当前行为偶数,直接开始下一行
  285. if (i % 2 != 0) {
  286. continue;
  287. }
  288. TTrainingbccDevice device = list.get(i);
  289. TTrainingbccDevice device2 = null;
  290. if (i < list.size() - 1) {
  291. device2 = list.get(i + 1);
  292. }
  293. SysUser user1 = sysUserMapper.selectUserByStaffId(device.getStaffId());
  294. SysUser user2 = null;
  295. if (device2 != null) {
  296. user2 = sysUserMapper.selectUserByStaffId(device2.getStaffId());
  297. }
  298. // 获取员工部门信息
  299. String department1 = "CBP/C"; // 默认值
  300. String department2 = "CBP/C"; // 默认值
  301. if (device.getStaffId() != null) {
  302. TStaffmgr staffmgr1 = tStaffmgrService.selectTStaffmgrByStaffId(device.getStaffId());
  303. if (staffmgr1 != null && staffmgr1.getUnit() != null) {
  304. department1 = DictUtils.getDictLabel("STAFF_UNIT", staffmgr1.getUnit());
  305. if (department1 == null || department1.isEmpty()) {
  306. department1 = "CBP/C"; // 如果字典转换失败,使用默认值
  307. }
  308. }
  309. }
  310. if (device2 != null && device2.getStaffId() != null) {
  311. TStaffmgr staffmgr2 = tStaffmgrService.selectTStaffmgrByStaffId(device2.getStaffId());
  312. if (staffmgr2 != null && staffmgr2.getUnit() != null) {
  313. department2 = DictUtils.getDictLabel("STAFF_UNIT", staffmgr2.getUnit());
  314. if (department2 == null || department2.isEmpty()) {
  315. department2 = "CBP/C"; // 如果字典转换失败,使用默认值
  316. }
  317. }
  318. }
  319. RowRenderData row = null;
  320. String lineNum="";
  321. String lineNum2="";
  322. // 创建数据行
  323. lineNum = String.valueOf(i + 1);
  324. lineNum2 = String.valueOf(i + 2);
  325. if (device2!=null)
  326. row = Rows.of(lineNum, device.getName(), department1, "", DateUtils.dateTime(device.getFinishDate()),
  327. lineNum2, device2.getName(), department2, "", DateUtils.dateTime(device2.getFinishDate())).verticalCenter().textFontSize(8).rowExactHeight(1.1f).create();
  328. else
  329. row = Rows.of(lineNum, device.getName(), department1, "", DateUtils.dateTime(device.getFinishDate()),
  330. "", "", "", "", "").verticalCenter().horizontalCenter().textFontSize(8).rowExactHeight(1.1f).create();
  331. if (device.getExamState() == 1) {
  332. if (user1 != null)
  333. row.getCells().set(3, new CellRenderData().addParagraph(new ParagraphRenderData().addPicture(Pictures.ofLocal(FileUtils.fileName(user1.getSignUrl())).size(60, 20).create())));
  334. }
  335. if (device2!=null&&device2.getExamState() == 1) {
  336. if (user2 != null)
  337. row.getCells().set(8, new CellRenderData().addParagraph(new ParagraphRenderData().addPicture(Pictures.ofLocal(FileUtils.fileName(user2.getSignUrl())).size(60, 20).create())));
  338. }
  339. tableRenderData.addRow(row);
  340. }
  341. // 准备表格数据和其他参数
  342. params.put("course", trainingbcc.getCourse() != null ? trainingbcc.getCourse() : "NA"); // 培训课程名称
  343. params.put("courseid", trainingbcc.getCourseid() != null ? trainingbcc.getCourseid() : "NA"); // 培训材料编号
  344. // 处理培训材料版本:通过courseid查询TPlantproglist获取版本号
  345. String revision = "NA";
  346. if (trainingbcc.getCourseid() != null && !trainingbcc.getCourseid().trim().isEmpty()) {
  347. TPlantproglist plantproglist = new TPlantproglist();
  348. plantproglist.setFileno(trainingbcc.getCourseid().trim());
  349. List<TPlantproglist> plantproglistList = tPlantproglistService.selectTPlantproglistList(plantproglist);
  350. if (plantproglistList != null && !plantproglistList.isEmpty()) {
  351. TPlantproglist matchedPlantproglist = plantproglistList.get(0);
  352. if (matchedPlantproglist.getRevision() != null && !matchedPlantproglist.getRevision().trim().isEmpty()) {
  353. revision = matchedPlantproglist.getRevision();
  354. }
  355. }
  356. }
  357. params.put("revision", revision); // 培训材料版本
  358. params.put("duration", trainingbcc.getDuration() != null ? trainingbcc.getDuration() : "NA"); // 培训时长
  359. params.put("content", trainingbcc.getContent() != null ? trainingbcc.getContent() : "NA"); // 培训内容
  360. // 处理讲师信息:通过员工号查询讲师姓名
  361. String trainerNames = "NA";
  362. if (trainingbcc.getTrainer() != null && !trainingbcc.getTrainer().trim().isEmpty()) {
  363. String[] staffIds = trainingbcc.getTrainer().split(",");
  364. StringBuilder trainerNameList = new StringBuilder();
  365. for (String staffId : staffIds) {
  366. if (staffId.trim().isEmpty()) continue;
  367. TStaffmgr staffmgr = tStaffmgrService.selectTStaffmgrByStaffId(staffId.trim());
  368. if (staffmgr != null && staffmgr.getName() != null) {
  369. if (trainerNameList.length() > 0) {
  370. trainerNameList.append("、");
  371. }
  372. trainerNameList.append(staffmgr.getName());
  373. }
  374. }
  375. trainerNames = trainerNameList.length() > 0 ? trainerNameList.toString() : "NA";
  376. }
  377. params.put("trainer", trainerNames); // 培训讲师姓名
  378. // 处理培训日期:使用斜杠格式 yyyy/MM/dd
  379. String courseStartdateStr = "";
  380. if (trainingbcc.getCourseStartdate() != null) {
  381. String startDate = DateUtils.parseDateToStr("yyyy/MM/dd", trainingbcc.getCourseStartdate());
  382. if (trainingbcc.getCourseEnddate() != null) {
  383. String endDate = DateUtils.parseDateToStr("yyyy/MM/dd", trainingbcc.getCourseEnddate());
  384. // 如果开始和结束日期不同,显示日期范围
  385. if (!startDate.equals(endDate)) {
  386. courseStartdateStr = startDate + "-" + endDate;
  387. } else {
  388. courseStartdateStr = startDate;
  389. }
  390. } else {
  391. courseStartdateStr = startDate;
  392. }
  393. }
  394. params.put("courseStartdate", courseStartdateStr); // 培训日期
  395. params.put("table1", tableRenderData);
  396. return params;
  397. }
  398. private String createWord(String templatePath, String fileDir, String fileName, Map<String, Object> paramMap, String directory) throws IOException {
  399. Assert.notNull(templatePath, "word模板文件路径不能为空");
  400. Assert.notNull(fileDir, "生成的文件存放地址不能为空");
  401. Assert.notNull(fileName, "生成的文件名不能为空");
  402. File dir = new File(fileDir);
  403. if (!dir.exists()) {
  404. logger.info("目录不存在,创建文件夹{}!", fileDir);
  405. dir.mkdirs();
  406. }
  407. fileName = fileName.replaceAll("/", "_"); //替换文件中敏感字段
  408. logger.info("目录文件{}!", fileName);
  409. String filePath = fileDir + "/" + fileName;
  410. logger.info("目录{}!", filePath);
  411. logger.info("模板{}!", templatePath);
  412. // 读取模板渲染参数
  413. InputStream is = getClass().getClassLoader().getResourceAsStream(templatePath);
  414. XWPFTemplate template = XWPFTemplate.compile(is).render(paramMap);
  415. try {
  416. // 将模板参数写入路径
  417. template.writeToFile(filePath);
  418. template.close();
  419. is.close();
  420. } catch (Exception e) {
  421. logger.error("生成word异常{}", e.getMessage());
  422. e.printStackTrace();
  423. }
  424. String pathFileName = FileUploadUtils.getPathFileName(RuoYiConfig.getFilePath("/" + directory), fileName);
  425. return pathFileName;
  426. }
  427. }