TPssrMaterialRawController.java 14 KB

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