PlanManageController.java 17 KB

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