TPssrMaterialRawController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package com.ruoyi.project.pssr.controller;
  2. import com.ruoyi.common.utils.StringUtils;
  3. import com.ruoyi.common.utils.poi.ExcelUtil;
  4. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  5. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  6. import com.ruoyi.framework.web.controller.BaseController;
  7. import com.ruoyi.framework.web.domain.AjaxResult;
  8. import com.ruoyi.framework.web.page.TableDataInfo;
  9. import com.ruoyi.project.listener.pssr.ConfirmTaskCreateListener;
  10. import com.ruoyi.project.pssr.domain.*;
  11. import com.ruoyi.project.pssr.mapper.TPssrMaterialRawMapper;
  12. import com.ruoyi.project.pssr.service.*;
  13. import org.activiti.engine.*;
  14. import org.activiti.engine.impl.identity.Authentication;
  15. import org.activiti.engine.runtime.ProcessInstance;
  16. import org.activiti.engine.task.Task;
  17. import org.apache.commons.collections4.CollectionUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.security.access.prepost.PreAuthorize;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.annotation.Resource;
  22. import java.util.*;
  23. /**
  24. * 原料Controller
  25. *
  26. * @author ssy
  27. * @date 2024-09-18
  28. */
  29. @RestController
  30. @RequestMapping("/pssr/materialRaw")
  31. public class TPssrMaterialRawController extends BaseController {
  32. @Autowired
  33. private ITPssrFileService tPssrFileService;
  34. @Autowired
  35. private ITPssrTurndownService tPssrTurndownService;
  36. @Autowired
  37. private ITPssrMaterialRawService tPssrMaterialRawService;
  38. @Autowired
  39. private ITPssrApproveService tPssrApproveService;
  40. @Autowired
  41. private ITPssrSubcontentService tPssrSubcontentService;
  42. @Autowired
  43. private RuntimeService runtimeService;
  44. @Autowired
  45. private HistoryService historyService;
  46. @Resource
  47. private TPssrMaterialRawMapper tPssrMaterialRawMapper;
  48. private String forShort = "yfl-raw";
  49. /**
  50. * 查询原料列表
  51. */
  52. @PreAuthorize("@ss.hasPermi('pssr:materialRaw:list')")
  53. @GetMapping("/list")
  54. public TableDataInfo list(TPssrMaterialRaw tPssrMaterialRaw) {
  55. try {
  56. TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(tPssrMaterialRaw.getSubId());
  57. if (approve != null) {
  58. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  59. TaskService taskService = processEngine.getTaskService();
  60. Task task = processEngine.getTaskService()//获取任务service
  61. .createTaskQuery()//创建查询对象
  62. .taskAssignee(getUserId().toString())
  63. .processInstanceId(approve.getProcessId()).singleResult();
  64. if (task != null) {
  65. if (task.getName().equals("确认人1")){
  66. tPssrMaterialRaw.setConfirm1(getUserId().toString());
  67. }else if (task.getName().equals("确认人2")){
  68. tPssrMaterialRaw.setConfirm2(getUserId().toString());
  69. }
  70. }
  71. }
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. logger.error("待办确认人查询报错:{}",e.getMessage());
  75. }
  76. startPage();
  77. List<TPssrMaterialRaw> list = tPssrMaterialRawService.selectTPssrMaterialRawList(tPssrMaterialRaw);
  78. list.forEach(item -> {
  79. List<TPssrFile> tPssrFiles = tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), forShort);
  80. item.setFileList(tPssrFiles);
  81. item.setFileNum((long) tPssrFiles.size());
  82. if (item.getApproveStatus() != 2)
  83. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "yfl-raw"));
  84. });
  85. return getDataTable(list);
  86. }
  87. /**
  88. * 导出原料列表
  89. */
  90. @PreAuthorize("@ss.hasPermi('pssr:materialRaw:export')")
  91. @Log(title = "原料", businessType = BusinessType.EXPORT)
  92. @GetMapping("/export")
  93. public AjaxResult export(TPssrMaterialRaw tPssrMaterialRaw) {
  94. List<TPssrMaterialRaw> list = tPssrMaterialRawService.selectTPssrMaterialRawList(tPssrMaterialRaw);
  95. ExcelUtil<TPssrMaterialRaw> util = new ExcelUtil<TPssrMaterialRaw>(TPssrMaterialRaw.class);
  96. return util.exportExcel(list, "materialRaw");
  97. }
  98. /**
  99. * 获取原料详细信息
  100. */
  101. @PreAuthorize("@ss.hasPermi('pssr:materialRaw:query')")
  102. @GetMapping(value = "/{id}")
  103. public AjaxResult getInfo(@PathVariable("id") Long id) {
  104. TPssrMaterialRaw item = tPssrMaterialRawService.selectTPssrMaterialRawById(id);
  105. List<TPssrFile> tPssrFiles = tPssrFileService.selectTPssrFileListByItem(item.getSubId(), item.getId(), forShort);
  106. item.setFileList(tPssrFiles);
  107. item.setFileNum((long) tPssrFiles.size());
  108. if (item.getApproveStatus() != 2)
  109. item.setReason(tPssrTurndownService.selectTPssrTurndownByItem(item.getSubId(), item.getId(), "yfl-raw"));
  110. return AjaxResult.success(item);
  111. }
  112. /**
  113. * 新增原料
  114. */
  115. @PreAuthorize("@ss.hasPermi('pssr:materialRaw:add')")
  116. @Log(title = "原料", businessType = BusinessType.INSERT)
  117. @PostMapping
  118. public AjaxResult add(@RequestBody TPssrMaterialRaw tPssrMaterialRaw) {
  119. if (StringUtils.isNotEmpty(tPssrMaterialRaw.getConfirm1())&&tPssrMaterialRaw.getConfirm1().equals(tPssrMaterialRaw.getConfirm2())) {
  120. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  121. }
  122. return toAjax(tPssrMaterialRawService.insertTPssrMaterialRaw(tPssrMaterialRaw));
  123. }
  124. /**
  125. * 修改原料
  126. */
  127. @PreAuthorize("@ss.hasPermi('pssr:materialRaw:edit')")
  128. @Log(title = "原料", businessType = BusinessType.UPDATE)
  129. @PutMapping
  130. public AjaxResult edit(@RequestBody TPssrMaterialRaw tPssrMaterialRaw) {
  131. TPssrMaterialRaw entity = tPssrMaterialRawService.selectTPssrMaterialRawById(tPssrMaterialRaw.getId());
  132. if (entity.getApproveStatus() != 1 && entity.getApproveStatus() != 0) {
  133. return AjaxResult.error("当前状态不可修改!");
  134. }
  135. if (tPssrMaterialRaw.getConfirm1().equals(tPssrMaterialRaw.getConfirm2())){
  136. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  137. }
  138. tPssrFileService.updateFileRelevance(tPssrMaterialRaw.getFileIds(), "yfl-raw", tPssrMaterialRaw.getId(), tPssrMaterialRaw.getSubId());
  139. return toAjax(tPssrMaterialRawService.updateTPssrMaterialRaw(tPssrMaterialRaw));
  140. }
  141. /**
  142. * 修改原料
  143. */
  144. @PreAuthorize("@ss.hasPermi('pssr:materialRaw:edit')")
  145. @Log(title = "原料", businessType = BusinessType.UPDATE)
  146. @PutMapping("editBatch")
  147. public AjaxResult editBatch(@RequestBody TPssrMaterialRaw tPssrMaterialRaw) {
  148. if (tPssrMaterialRaw.getConfirm1().equals(tPssrMaterialRaw.getConfirm2())){
  149. return AjaxResult.error("确认人不能为同一人,请重新选择!");
  150. }
  151. return toAjax(tPssrMaterialRawMapper.updateTPssrMaterialRawByIds(tPssrMaterialRaw));
  152. }
  153. /**
  154. * 删除原料
  155. */
  156. @PreAuthorize("@ss.hasPermi('pssr:materialRaw:remove')")
  157. @Log(title = "原料", businessType = BusinessType.DELETE)
  158. @DeleteMapping("/{ids}")
  159. public AjaxResult remove(@PathVariable Long[] ids) {
  160. return toAjax(tPssrMaterialRawService.deleteTPssrMaterialRawByIds(ids));
  161. }
  162. /**
  163. * 确认人身防护
  164. */
  165. @PreAuthorize("@ss.hasPermi('pssr:materialRaw:edit')")
  166. @Log(title = "人身防护", businessType = BusinessType.UPDATE)
  167. @PutMapping("/confirmMaterialRaw")
  168. public AjaxResult confirmMaterialRaw(@RequestBody TPssrMaterialRaw tPssrMaterialRaw) {
  169. TPssrMaterialRaw materialRaw = new TPssrMaterialRaw();
  170. if (tPssrMaterialRaw.getIds() != null && tPssrMaterialRaw.getIds().length > 0) {
  171. for (Long id : tPssrMaterialRaw.getIds()) {
  172. materialRaw = tPssrMaterialRawService.selectTPssrMaterialRawById(id);
  173. materialRaw.setConfirmationDate(new Date());
  174. materialRaw.setApproveStatus(2L);
  175. materialRaw.setUpdatedate(new Date());
  176. materialRaw.setUpdaterCode(String.valueOf(getUserId()));
  177. tPssrMaterialRawService.updateTPssrMaterialRaw(materialRaw);
  178. }
  179. } else {
  180. materialRaw.setSubId(tPssrMaterialRaw.getSubId());
  181. materialRaw.setApproveStatus(1L);
  182. materialRaw.setConfirm1(getUserId().toString());
  183. for (TPssrMaterialRaw item : tPssrMaterialRawService.selectTPssrMaterialRawList(materialRaw)) {
  184. item.setConfirmationDate(new Date());
  185. item.setApproveStatus(2L);
  186. item.setUpdatedate(new Date());
  187. item.setUpdaterCode(String.valueOf(getUserId()));
  188. tPssrMaterialRawService.updateTPssrMaterialRaw(item);
  189. }
  190. }
  191. //查询当前待审批的确认人
  192. TPssrMaterialRaw entity = new TPssrMaterialRaw();
  193. entity.setSubId(tPssrMaterialRaw.getSubId());
  194. entity.setApproveStatus(1L);
  195. TPssrMaterialRaw MaterialRaw = tPssrMaterialRawService.selectAllConfirmedPersonBySubId(entity);
  196. if (MaterialRaw != null) {
  197. //如果当前用户还有待审批任务
  198. if (tPssrMaterialRaw.getTaskType() == 4 && StringUtils.isNotEmpty(MaterialRaw.getConfirm1())) {
  199. if (MaterialRaw.getConfirm1().contains(getUserId().toString())) {
  200. return AjaxResult.success();
  201. }
  202. }
  203. if (tPssrMaterialRaw.getTaskType() == 5 && StringUtils.isNotEmpty(MaterialRaw.getConfirm2())) {
  204. if (MaterialRaw.getConfirm2().contains(getUserId().toString())) {
  205. return AjaxResult.success();
  206. }
  207. }
  208. }
  209. //无待审批任务结束当前用户流程
  210. // 因为流程关系所以approve一定会有且只有一条数据
  211. TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrMaterialRaw.getSubId());
  212. TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
  213. return AjaxResult.success();
  214. }
  215. /**
  216. * 驳回人身防护
  217. */
  218. @PutMapping("/turnDownMaterialRaw")
  219. public AjaxResult turnDownMaterialRaw(@RequestBody List<TPssrMaterialRaw> tPssrMaterialRaw) {
  220. if (CollectionUtils.isNotEmpty(tPssrMaterialRaw)) {
  221. String userId = getUserId().toString();
  222. Long subId = tPssrMaterialRaw.get(0).getSubId();
  223. // 修改已选择数据的状态
  224. for (TPssrMaterialRaw item : tPssrMaterialRaw) {
  225. TPssrMaterialRaw blind = new TPssrMaterialRaw();
  226. blind.setId(item.getId());
  227. blind.setApproveStatus(1L);
  228. blind.setUpdatedate(new Date());
  229. blind.setUpdaterCode(getUserId().toString());
  230. tPssrMaterialRawService.updateTPssrMaterialRaw(blind);
  231. // 新增驳回原因数据
  232. TPssrTurndown turndown = new TPssrTurndown();
  233. turndown.setForShort(forShort);
  234. turndown.setSubId(item.getSubId());
  235. turndown.setItemId(item.getId());
  236. turndown.setReason(item.getReason());
  237. turndown.setCreatedate(new Date());
  238. turndown.setCreaterCode(getUserId().toString());
  239. tPssrTurndownService.insertTPssrTurndown(turndown);
  240. }
  241. // 查询当前流程
  242. TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);
  243. try {
  244. runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr1confirm");
  245. historyService.deleteHistoricProcessInstance(approve.getProcessId());
  246. } catch (Exception e) {
  247. logger.info("无运行时流程");
  248. }
  249. // 驳回 查询所有待审批的人员
  250. // 查询确认人
  251. TPssrMaterialRaw entity = new TPssrMaterialRaw();
  252. entity.setSubId(subId);
  253. entity.setApproveStatus(1L);
  254. TPssrMaterialRaw PssrMaterialRaw = tPssrMaterialRawService.selectAllConfirmedPersonBySubId(entity);
  255. String confirmer1s = null;
  256. if (PssrMaterialRaw != null) {
  257. confirmer1s = PssrMaterialRaw.getConfirm1();
  258. }
  259. logger.info("=======================confirmer1s:{}", confirmer1s);
  260. Set<String> confirmerUsers1 = new HashSet<>();
  261. if (StringUtils.isNotEmpty(confirmer1s)) {
  262. confirmerUsers1.addAll(Arrays.asList(confirmer1s.split(",")));
  263. }
  264. // 开始申请流程
  265. long businessKey = approve.getApproveId();
  266. //开始工作流、监听
  267. Authentication.setAuthenticatedUserId(userId);//设置当前申请人
  268. Map<String, Object> variables = new HashMap<>();
  269. variables.put("applyUser", userId);
  270. variables.put("confirmUsers", new ArrayList<>(confirmerUsers1));
  271. variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件
  272. variables.put("chargePerson", approve.getSubCharge());
  273. //采用key来启动流程定义并设置流程变量,返回流程实例
  274. ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr1confirm", String.valueOf(businessKey), variables);
  275. // 修改审批表和sub表
  276. approve.setProcessId(pi.getProcessInstanceId());
  277. approve.setApproveStatus(1L);
  278. approve.setUpdatedate(new Date());
  279. approve.setUpdaterCode(getUserId().toString());
  280. tPssrApproveService.updateTPssrApprove(approve);
  281. TPssrSubcontent subcontent = new TPssrSubcontent();
  282. subcontent.setId(approve.getSubId());
  283. subcontent.setApproveStatus(1L);
  284. subcontent.setUpdatedate(new Date());
  285. subcontent.setUpdaterCode(getUserId().toString());
  286. tPssrSubcontentService.updateTPssrSubcontent(subcontent);
  287. return AjaxResult.success();
  288. }
  289. return AjaxResult.error();
  290. }
  291. }