PlanManageController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package com.huaxu.task.controller;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.huaxu.client.UserCenterClient;
  5. import com.huaxu.model.AjaxMessage;
  6. import com.huaxu.model.LoginUser;
  7. import com.huaxu.model.Pagination;
  8. import com.huaxu.model.ResultStatus;
  9. import com.huaxu.order.entity.WorkFlowDetail;
  10. import com.huaxu.order.entity.WorkFlowLog;
  11. import com.huaxu.order.service.WorkFlowLogService;
  12. import com.huaxu.process.entity.ProcessDefinition;
  13. import com.huaxu.process.service.WorkFlowService;
  14. import com.huaxu.task.dto.PlanManageDto;
  15. import com.huaxu.task.entity.PlanManage;
  16. import com.huaxu.task.entity.UserEntity;
  17. import com.huaxu.task.service.PlanManageService;
  18. import com.huaxu.util.DatesUtil;
  19. import com.huaxu.util.UserUtil;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. import io.swagger.annotations.ApiParam;
  23. import org.activiti.engine.task.Task;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.util.StringUtils;
  26. import org.springframework.web.bind.annotation.*;
  27. import java.util.*;
  28. import java.util.stream.Collectors;
  29. @RestController
  30. @RequestMapping("/workflow/task/")
  31. @Api(tags = "任务管理")
  32. public class PlanManageController {
  33. @Autowired
  34. PlanManageService planManageService;
  35. @Autowired
  36. WorkFlowService workFlowService;
  37. @Autowired
  38. WorkFlowLogService workFlowLogService;
  39. @Autowired
  40. private UserCenterClient userCenterClient;
  41. @PostMapping("/plan/add")
  42. @ApiOperation(value = "新增巡检计划")
  43. public AjaxMessage addPlan(
  44. @ApiParam(value = "新增巡检计划", required = true) @RequestBody PlanManage planManage
  45. ) {
  46. LoginUser loginUser = UserUtil.getCurrentUser();
  47. String planId=DatesUtil.formatDate(new Date(),"yyyyMMddHHmmss")+String.valueOf((int) (Math.random()*(9999-1000)+1000));
  48. PlanManage record = new PlanManage();
  49. record.setUserId(planManage.getUserId());
  50. record.setPlanId(planId);
  51. record.setPlanName(planManage.getPlanName());
  52. record.setTaskType(planManage.getTaskType());
  53. record.setTaskChecked(planManage.getTaskChecked());
  54. record.setPlanStartDate(planManage.getPlanStartDate());
  55. record.setPlanEndDate(planManage.getPlanEndDate());
  56. record.setPlanStatus(0);
  57. record.setTaskContent(planManage.getTaskContent());
  58. record.setTaskAreaShape(planManage.getTaskAreaShape());
  59. record.setTaskAreaName(planManage.getTaskAreaName());
  60. record.setDateCreate(new Date());
  61. if(loginUser.getId() != null) {
  62. record.setPlanUserId(String.valueOf(loginUser.getId()));
  63. record.setCreateBy(String.valueOf(loginUser.getId()));
  64. }
  65. if(loginUser.getTenantId() != null) {
  66. record.setTenantId(loginUser.getTenantId());
  67. }
  68. if(loginUser.getCompanyId() != null){
  69. record.setCompanyOrgId(loginUser.getCompanyId().toString());
  70. }
  71. if(loginUser.getDepartmentId() != null){
  72. record.setDepartmentOrgId(loginUser.getDepartmentId().toString());
  73. }
  74. int rows = planManageService.insertSelective(record);
  75. if(rows > 0) {
  76. return new AjaxMessage(ResultStatus.OK);
  77. }
  78. return new AjaxMessage(ResultStatus.ERROR);
  79. }
  80. @GetMapping("/plan/select")
  81. @ApiOperation(value = "查询巡检计划")
  82. public AjaxMessage<Pagination<PlanManageDto>> selectPlan(
  83. @ApiParam(value = "页数", required = true)@RequestParam Integer pageNum,
  84. @ApiParam(value = "条数", required = true)@RequestParam Integer pageSize,
  85. @ApiParam(value = "任务类型(巡检:7,侧漏:8,养护:9)", required = false) @RequestParam(required = false) Integer taskType,
  86. @ApiParam(value = "状态(0:巡检计划;null:所有任务;1:执行中;2:完成)", required = false) @RequestParam(required = false) Integer planStatus,
  87. @ApiParam(value = "计划单号或计划名称", required = false) @RequestParam(required = false) String key,
  88. @ApiParam(value = "计划起始日期(yyyy-MM-dd)", required = false) @RequestParam(required = false) String startDate,
  89. @ApiParam(value = "计划截至日期(yyyy-MM-dd)", required = false) @RequestParam(required = false) String endDate){
  90. PlanManageDto planManageDto = new PlanManageDto();
  91. //根据用户编号,获取用户的权限
  92. LoginUser loginUser = UserUtil.getCurrentUser();
  93. planManageDto.setKey(key);
  94. if(!StringUtils.isEmpty(startDate)&& !StringUtils.isEmpty(endDate)) {
  95. planManageDto.setStartDate(startDate);
  96. planManageDto.setEndDate(endDate);
  97. }
  98. planManageDto.setPlanStatus(planStatus);
  99. planManageDto.setTaskType(taskType);
  100. planManageDto.setTenantId(loginUser.getTenantId());
  101. planManageDto.setProgramItems(loginUser.getProgramItemList());
  102. planManageDto.setUserType(loginUser.getType());
  103. //1是公司,2是公司及以下,3部门,4部门及以下,5自定义
  104. planManageDto.setPermissonType(loginUser.getPermissonType());
  105. IPage<PlanManageDto> iPage = new Page<>(pageNum, pageSize);
  106. iPage = planManageService.selectPage(iPage, planManageDto);
  107. Pagination<PlanManageDto> pages = new Pagination<>(iPage);
  108. setName(pages.getList());
  109. return new AjaxMessage<>(ResultStatus.OK, pages);
  110. }
  111. @GetMapping("/plan/pending")
  112. @ApiOperation(value = "查询待处理任务")
  113. public AjaxMessage<Pagination<PlanManageDto>> selectPendingTask(
  114. @ApiParam(value = "页数", required = true)@RequestParam Integer pageNum,
  115. @ApiParam(value = "条数", required = true)@RequestParam Integer pageSize,
  116. @ApiParam(value = "任务类型(巡检:7,侧漏:8,养护:9)", required = false) @RequestParam(required = false) Integer taskType,
  117. @ApiParam(value = "计划单号或计划名称", required = false) @RequestParam(required = false) String key,
  118. @ApiParam(value = "计划起始日期(yyyy-MM-dd)", required = false) @RequestParam(required = false) String startDate,
  119. @ApiParam(value = "计划截至日期(yyyy-MM-dd)", required = false) @RequestParam(required = false) String endDate){
  120. PlanManageDto planManageDto = new PlanManageDto();
  121. //根据用户编号,获取用户的权限
  122. LoginUser loginUser = UserUtil.getCurrentUser();
  123. planManageDto.setKey(key);
  124. if(!StringUtils.isEmpty(startDate)&& !StringUtils.isEmpty(endDate)) {
  125. planManageDto.setStartDate(startDate);
  126. planManageDto.setEndDate(endDate);
  127. }
  128. planManageDto.setTaskType(taskType);
  129. if(loginUser.getId() != null) {
  130. planManageDto.setCurrentUsers(String.format("%s", loginUser.getId()));
  131. }
  132. IPage<PlanManageDto> iPage = new Page<>(pageNum, pageSize);
  133. iPage = planManageService.selectPage(iPage, planManageDto);
  134. Pagination<PlanManageDto> pages = new Pagination<>(iPage);
  135. setName(pages.getList());
  136. return new AjaxMessage<>(ResultStatus.OK, pages);
  137. }
  138. @GetMapping("/plan/pending/power")
  139. @ApiOperation(value = "待处理权限")
  140. public AjaxMessage<Boolean> selectPendingPower(
  141. @ApiParam(value = "任务ID", required = true) @RequestParam(required = true) Integer id){
  142. String currentUsers = null;
  143. LoginUser loginUser = UserUtil.getCurrentUser();
  144. if(loginUser.getId() != null) {
  145. currentUsers = String.format("%s", loginUser.getId());
  146. }
  147. Map<String,Object> map = new HashMap<String,Object>();
  148. map.put("id",id);
  149. map.put("currentUsers",currentUsers);
  150. int rows = planManageService.selectPendingPower(map);
  151. if(rows>0){
  152. return new AjaxMessage<Boolean>(ResultStatus.OK, true);
  153. }
  154. return new AjaxMessage<Boolean>(ResultStatus.OK, false);
  155. }
  156. /**
  157. * APP查询任务详情
  158. * @return Response对象
  159. */
  160. @RequestMapping(value = "selectPlanDetail", method = RequestMethod.GET)
  161. @ApiOperation(value = "APP查询任务详情")
  162. public AjaxMessage<PlanManageDto> selectPlanDetail(
  163. @ApiParam(value = "任务id", required = true) @RequestParam Integer flowId) {
  164. PlanManageDto result=planManageService.selectPlanDetail(flowId);
  165. setName(new ArrayList<PlanManageDto>(){{
  166. this.add(result);
  167. }});
  168. return new AjaxMessage<>(ResultStatus.OK, result );
  169. }
  170. @GetMapping("/plan/submit")
  171. @ApiOperation(value = "提交巡检计划")
  172. public AjaxMessage submitPlan(
  173. @ApiParam(value = "任务ID", required = true) @RequestParam(required = true) Integer id,
  174. @ApiParam(value = "任务类型", required = true) @RequestParam(required = true) String taskType){
  175. LoginUser loginUser = UserUtil.getCurrentUser();
  176. PlanManage planManage = planManageService.selectByPrimaryKey(id);
  177. // ProcessDefinition processDefinition = workFlowService.findProcessDefinition(
  178. // loginUser.getTenantId(),
  179. // loginUser.getCompanyId(),
  180. // planManage.getTaskType());
  181. //流程查询, 测试参数:(test1 1 1)
  182. ProcessDefinition processDefinition = workFlowService.findProcessDefinition(
  183. loginUser.getTenantId(),
  184. loginUser.getCompanyId(),
  185. 2);
  186. if(processDefinition == null){
  187. return new AjaxMessage(ResultStatus.ERROR,"没有创建流程,不能提交!");
  188. }
  189. Map<String,Object> vars = new HashMap<>();
  190. vars.put("assineeFormUserId",planManage.getUserId());
  191. //巡检发送系统消息所需参数
  192. vars.put("任务类型",taskType);
  193. vars.put("任务编号",planManage.getPlanId());
  194. vars.put("templateId",4);
  195. vars.put("msgType",4);
  196. String startProcess = workFlowService.startProcess(processDefinition,vars);
  197. Task task = workFlowService.getProcessRuntimeTask(startProcess).get(0);
  198. Set<String> taskParticipator = workFlowService.getTaskParticipator(task.getId(),false,null);
  199. String currentUsers = "";
  200. for (String str : taskParticipator) {
  201. currentUsers += String.format("%s,",str);
  202. }
  203. //currentUsers+="235,209,";
  204. //更新计划表的流程相关字段值。
  205. planManage.setPlanStatus(1);//已执行
  206. if(loginUser.getId() != null) {
  207. planManage.setUpdateBy(String.valueOf(loginUser.getId()));
  208. }
  209. planManage.setPlanDate(new Date());// 提交时间
  210. planManage.setDateUpdate(new Date());// 更新时间
  211. planManage.setProcessInstanceId(task.getProcessInstanceId());
  212. planManage.setProcessDefId(task.getProcessDefinitionId());
  213. planManage.setCurrentTaskId(task.getId());
  214. planManage.setCurrentUsers(currentUsers);
  215. planManage.setCurrentTaskName(task.getName());
  216. int rows = planManageService.updateByPrimaryKeySelective(planManage);
  217. if(rows > 0) {
  218. return new AjaxMessage<>(ResultStatus.OK);
  219. }
  220. return new AjaxMessage<>(ResultStatus.ERROR);
  221. }
  222. @PostMapping("/plan/update")
  223. @ApiOperation(value = "修改巡检计划")
  224. public AjaxMessage updatePlan(@ApiParam(value = "修改巡检计划", required = true) @RequestBody PlanManage planManage) {
  225. PlanManage record = new PlanManage();
  226. record.setId(planManage.getId());
  227. record.setUserId(planManage.getUserId());
  228. record.setPlanName(planManage.getPlanName());
  229. record.setTaskType(planManage.getTaskType());
  230. record.setTaskChecked(planManage.getTaskChecked());
  231. record.setPlanStartDate(planManage.getPlanStartDate());
  232. record.setPlanEndDate(planManage.getPlanEndDate());
  233. record.setTaskContent(planManage.getTaskContent());
  234. record.setTaskAreaShape(planManage.getTaskAreaShape());
  235. record.setTaskAreaName(planManage.getTaskAreaName());
  236. LoginUser loginUser = UserUtil.getCurrentUser();
  237. if(loginUser.getId() != null) {
  238. record.setUpdateBy(String.valueOf(loginUser.getId()));
  239. }
  240. record.setDateUpdate(new Date());
  241. int rows = planManageService.updateByPrimaryKeySelective(record);
  242. if(rows > 0) {
  243. return new AjaxMessage(ResultStatus.OK);
  244. }
  245. return new AjaxMessage(ResultStatus.ERROR);
  246. }
  247. @RequestMapping(value="batchDelete" , method = RequestMethod.DELETE)
  248. @ApiOperation(value = "批量删除")
  249. public AjaxMessage<Integer> batchDelete( @ApiParam(value = "巡检计划ids") @RequestParam List<Integer> ids){
  250. return new AjaxMessage<>(ResultStatus.OK, planManageService.batchDelete(ids));
  251. }
  252. @GetMapping("/plan/delProcInst")
  253. @ApiOperation(value = "终止流程")
  254. public AjaxMessage deleteProcessInstance(
  255. @ApiParam(value = "任务ID", required = true) @RequestParam(required = true) Integer id,
  256. @ApiParam(value = "流程ID", required = true) @RequestParam(required = true) String processInstanceId,
  257. @ApiParam(value = "流程备注", required = false) @RequestParam(required = false) String remark) {
  258. workFlowService.stopProcessInstance(processInstanceId);
  259. PlanManage planManage = new PlanManage();
  260. planManage.setId(id);
  261. planManage.setPlanStatus(3);//任务终止状态
  262. planManage.setCurrentUsers("");
  263. planManageService.updateByPrimaryKeySelective(planManage);
  264. LoginUser currentUser = UserUtil.getCurrentUser();
  265. WorkFlowLog workFlowLog=new WorkFlowLog();
  266. workFlowLog.setFlowId(id);
  267. workFlowLog.setFlowType(1);
  268. workFlowLog.setFlowResult(remark);
  269. workFlowLog.setCreateBy(currentUser.getId()!=null?currentUser.getId().toString():null);
  270. workFlowLog.setDateCreate(new Date());
  271. workFlowLog.setHandleUserId(currentUser.getId()!=null?currentUser.getId().toString():null);
  272. workFlowLog.setHandleUserName(currentUser.getUsername());
  273. //记录日志
  274. workFlowLogService.insert(workFlowLog);
  275. return new AjaxMessage<>(ResultStatus.OK);
  276. }
  277. /**
  278. * 获取并设置用户、任务工单类型名称
  279. * @param
  280. */
  281. private void setName(List<PlanManageDto> planManageDtoList){
  282. try {
  283. if(planManageDtoList.size()>0) {
  284. List<Long> idList = new ArrayList<Long>();
  285. for (PlanManageDto plan : planManageDtoList) {
  286. if(plan.getUserId() != null) {
  287. idList.add(Long.parseLong(plan.getUserId()));
  288. }
  289. if(plan.getPlanUserId() != null){
  290. idList.add(Long.parseLong(plan.getPlanUserId()));
  291. }
  292. if(plan.getCreateBy() != null){
  293. idList.add(Long.parseLong(plan.getCreateBy()));
  294. }
  295. if(plan.getUpdateBy() != null){
  296. idList.add(Long.parseLong(plan.getUpdateBy()));
  297. }
  298. }
  299. Long[] ids = idList.toArray(new Long[0]);
  300. Map<Long,String> userMap = userCenterClient.findUserIdsByUserIds(ids).stream().collect(Collectors.toMap(UserEntity::getId, UserEntity::getUsername));
  301. Map<String,String> dictMap=userCenterClient.selectListByPCodes("SC_WORK_ORDER_TYPE,SC_TASK_TYPE");
  302. for(int i=0; i<planManageDtoList.size();i++){
  303. PlanManageDto plan = planManageDtoList.get(i);
  304. try {
  305. if (plan.getTaskType() != null) {
  306. plan.setTypeName(dictMap.get(plan.getTaskType().toString()));
  307. }
  308. if (plan.getUserId() != null) {
  309. plan.setUserName(userMap.get(Long.valueOf(plan.getUserId())));
  310. }
  311. if (plan.getPlanUserId() != null) {
  312. plan.setPlanUserName(userMap.get(Long.valueOf(plan.getPlanUserId())));
  313. }
  314. if (plan.getCreateBy() != null) {
  315. plan.setCreateByName(userMap.get(Long.valueOf(plan.getCreateBy())));
  316. }
  317. if (plan.getUpdateBy() != null) {
  318. plan.setUpdateByName(userMap.get(Long.valueOf(plan.getUpdateBy())));
  319. }
  320. if (plan.getWorkFlowDetail() != null) {
  321. WorkFlowDetail workFlowDetail = plan.getWorkFlowDetail();
  322. if (workFlowDetail.getCreateBy() != null) {
  323. workFlowDetail.setCreateByName(userMap.get(Long.valueOf(workFlowDetail.getCreateBy())));
  324. }
  325. if (workFlowDetail.getUpdateBy() != null) {
  326. workFlowDetail.setUpdateByName(userMap.get(Long.valueOf(workFlowDetail.getUpdateBy())));
  327. }
  328. }
  329. if (plan.getWorkFlowLogList() != null) {
  330. for (WorkFlowLog workFlowLog : plan.getWorkFlowLogList()) {
  331. try {
  332. if (workFlowLog.getCreateBy() != null) {
  333. workFlowLog.setCreateByName(userMap.get(Long.valueOf(workFlowLog.getCreateBy())));
  334. }
  335. if (workFlowLog.getUpdateBy() != null) {
  336. workFlowLog.setUpdateByName(userMap.get(Long.valueOf(workFlowLog.getUpdateBy())));
  337. }
  338. }catch (Exception e){
  339. }
  340. }
  341. }
  342. }catch(Exception e){
  343. }
  344. }
  345. }
  346. }catch (Exception e){
  347. e.printStackTrace();
  348. }
  349. }
  350. }