X2D1TaskProcessingController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web.Http;
  7. using XdCxRhDW.Dto;
  8. using XdCxRhDW.WebApi;
  9. using X2D1TaskServer.Service;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace X2D1TaskServer.Controllers
  13. {
  14. /// <summary>
  15. ///两星一地任务处理接口
  16. /// </summary>
  17. public class X2D1TaskProcessingController : BaseController
  18. {
  19. /*******************
  20. * !!!不要在Controller中放业务逻辑的全局变量
  21. * !!!不要在Controller写太复杂的业务逻辑
  22. * Controller主要就是调用Service层的东西。Service层执行业务逻辑和调用Repository层操作数据库
  23. * ********************/
  24. private readonly TaskHistoryService _tskHservice;
  25. private readonly TaskRealService _tskRservice;
  26. public X2D1TaskProcessingController(TaskHistoryService tskHservice, TaskRealService tskRservice)
  27. {
  28. _tskHservice = tskHservice;
  29. _tskRservice = tskRservice;
  30. }
  31. /// <summary>
  32. /// 开始执行两星一地无参任务
  33. /// </summary>
  34. /// <param name="dto">离线任务信息</param>
  35. /// <returns></returns>
  36. [HttpPost]
  37. public AjaxResult Run(X2D1TaskHandleDto dto)
  38. {
  39. try
  40. {
  41. if (dto.TaskType == EnumTaskTypeDto.History)
  42. _tskHservice.StartAsync(dto);
  43. else
  44. _tskRservice.StartAsync(dto);
  45. return Success();
  46. }
  47. catch (Exception ex)
  48. {
  49. Serilog.Log.Error(ex, $"【任务{dto.ID}】开始执行异常!");
  50. return Error($"【任务{dto.ID}】开始执行异常!");
  51. }
  52. }
  53. /// <summary>
  54. /// 停止执行两星一地无参任务
  55. /// </summary>
  56. /// <param name="dto"></param>
  57. /// <returns></returns>
  58. [HttpPost]
  59. public async Task<AjaxResult> Stop(TaskStopHandleDto dto)
  60. {
  61. try
  62. {
  63. await LogHelper.Warning($"用户停止了任务,ID={dto.ID}");
  64. if (dto.TaskType == EnumTaskTypeDto.History)
  65. _tskHservice.Stop(dto.ID);
  66. else
  67. _tskRservice.Stop(dto.ID);
  68. return Success();
  69. }
  70. catch (Exception ex)
  71. {
  72. Serilog.Log.Error(ex, $"【任务{dto.ID}】停止执行异常!");
  73. return Error($"【任务{dto.ID}】停止执行异常!");
  74. }
  75. }
  76. }
  77. }