TasksQzController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Logging;
  3. using Ropin.Inspection.Api.Common;
  4. using Ropin.Inspection.Common.Accessor.Interface;
  5. using Ropin.Inspection.Model.Common;
  6. using Ropin.Inspection.Model.Entities;
  7. using Ropin.Inspection.Repository.Interface;
  8. using Ropin.Inspection.Service;
  9. using Ropin.Inspection.Service.Interface;
  10. using Ropin.Inspection.Tasks.QuartzNet;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Threading.Tasks;
  16. namespace Ropin.Inspection.Api.Controllers
  17. {
  18. /// <summary>
  19. /// 任务管理
  20. /// </summary>
  21. public class TasksQzController : BaseController
  22. {
  23. private readonly ILogger<TasksQzController> _logger;
  24. private readonly ITasksQzServices _service;
  25. private readonly ISchedulerCenter _schedulerCenter;
  26. private readonly IUnitOfWork _unitOfWork;
  27. private readonly IClaimsAccessor _claims;
  28. /// <summary>
  29. /// 构造函数
  30. /// </summary>
  31. /// <param name="service"></param>
  32. /// <param name="schedulerCenter"></param>
  33. /// <param name="logger"></param>
  34. /// <param name="unitOfWork"></param>
  35. /// <param name="claims"></param>
  36. public TasksQzController(ITasksQzServices service, ISchedulerCenter schedulerCenter, ILogger<TasksQzController> logger, IUnitOfWork unitOfWork, IClaimsAccessor claims)
  37. {
  38. _service = service;
  39. _logger = logger;
  40. _schedulerCenter = schedulerCenter;
  41. _unitOfWork = unitOfWork;
  42. _claims = claims;
  43. }
  44. /// <summary>
  45. /// 获取所有的任务
  46. /// </summary>
  47. /// <returns></returns>
  48. [HttpGet("GetTasksQzsAsync")]
  49. public async Task<ApiResult> GetTasksQzsAsync()
  50. {
  51. try
  52. {
  53. var data = await _service.GetAllAsync();
  54. var dataList = data.ToList();
  55. if (dataList.Count > 0)
  56. {
  57. foreach (var item in dataList)
  58. {
  59. item.Triggers = await _schedulerCenter.GetTaskStaus(item);
  60. }
  61. }
  62. return new ApiResult<IEnumerable<TasksQz>>(data);
  63. }
  64. catch (Exception ex)
  65. {
  66. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  67. }
  68. }
  69. /// <summary>
  70. /// 查询网点下所有的任务
  71. /// </summary>
  72. /// <returns></returns>
  73. [HttpGet("GetTasksQzsByStoreAsync")]
  74. public async Task<ApiResult> GetTasksQzsByStoreAsync(string storeCode, string name)
  75. {
  76. if (string.IsNullOrEmpty(storeCode))
  77. {
  78. return new ApiResult(ReturnCode.ArgsError, "网点Code");
  79. }
  80. try
  81. {
  82. var data = await _service.GetAllAsync();
  83. var dataList = data.Where(i=>i.C_StoreCode == storeCode).OrderByDescending(t=>t.CreateTime).ToList();
  84. if (dataList.Count > 0)
  85. {
  86. foreach (var item in dataList)
  87. {
  88. item.Triggers = await _schedulerCenter.GetTaskStaus(item);
  89. }
  90. }
  91. if (string.IsNullOrWhiteSpace(name))
  92. return new ApiResult<IEnumerable<TasksQz>>(dataList);
  93. else
  94. return new ApiResult<IEnumerable<TasksQz>>(dataList.Where(x => x.Name.Contains(name)));
  95. }
  96. catch (Exception ex)
  97. {
  98. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  99. }
  100. }
  101. /// <summary>
  102. /// 创建任务
  103. /// </summary>
  104. /// <param name="tasksQz"></param>
  105. /// <returns></returns>
  106. [Route("CreateTasksQzAsync")]
  107. [HttpPost]
  108. public async Task<ApiResult> CreateTasksQzAsync(TasksQz tasksQz)
  109. {
  110. var data = new ApiResult();
  111. if (tasksQz == null)
  112. {
  113. data.Code = ReturnCode.ArgsError;
  114. return data;
  115. }
  116. _unitOfWork.BeginTransaction();
  117. try
  118. {
  119. //await _service.CreateOneAsync(tasksQz);
  120. tasksQz.Id = Guid.NewGuid();
  121. tasksQz.CreateTime = DateTime.Now;
  122. tasksQz.CreateBy = _claims.ApiUserId;
  123. bool bResult = await _unitOfWork.RegisterNew(tasksQz);
  124. if (bResult)
  125. {
  126. data.Message = "添加成功";
  127. if (tasksQz.IsStart)
  128. {
  129. //如果是启动自动
  130. var ResuleModel = await _schedulerCenter.AddScheduleJobAsync(tasksQz);
  131. data.Code = ReturnCode.Success;
  132. if (ResuleModel.success)
  133. {
  134. data.Message = $"{data.Message}=>启动成功=>{ResuleModel.msg}";
  135. }
  136. else
  137. {
  138. data.Message = $"{data.Message}=>启动失败=>{ResuleModel.msg}";
  139. }
  140. }
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  146. }
  147. finally
  148. {
  149. if (data.Code == ReturnCode.Success)
  150. await _unitOfWork.CommitAsync();
  151. else
  152. _unitOfWork.Rollback();
  153. }
  154. return new ApiResult(ReturnCode.Success);
  155. }
  156. /// <summary>
  157. /// 通过ID获取计划任务信息
  158. /// </summary>
  159. /// <param name="id"></param>
  160. /// <returns></returns>
  161. [HttpGet("GetByIdAsync/{id}")]
  162. public async Task<ApiResult> GetByIdAsync(Guid id)
  163. {
  164. if (Guid.Empty == id)
  165. {
  166. return new ApiResult(ReturnCode.GeneralError);
  167. }
  168. try
  169. {
  170. var tasksQz = await _service.GetByIdAsync(id);
  171. return new ApiResult<TasksQz>(tasksQz);
  172. }
  173. catch (Exception ex)
  174. {
  175. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  176. }
  177. }
  178. ///// <summary>
  179. ///// 删除任务
  180. ///// </summary>
  181. ///// <param name="id"></param>
  182. ///// <returns></returns>
  183. //[HttpDelete("DeleteTasksQzAsync/{id}")]
  184. //public async Task<ApiResult> DeleteTasksQzAsync(Guid id)
  185. //{
  186. // if (Guid.Empty == id)
  187. // {
  188. // return new ApiResult(ReturnCode.GeneralError);
  189. // }
  190. // try
  191. // {
  192. // await _service.DeleteAsync(id);
  193. // }
  194. // catch (Exception ex)
  195. // {
  196. // return new ApiResult(ReturnCode.GeneralError, ex.Message);
  197. // }
  198. // return new ApiResult(ReturnCode.Success);
  199. //}
  200. /// <summary>
  201. /// 修改计划任务
  202. /// </summary>
  203. /// <param name="tasksQz"></param>
  204. /// <returns></returns>
  205. [HttpPut]
  206. public async Task<ApiResult> Put(TasksQz tasksQz)
  207. {
  208. var data = new ApiResult();
  209. if (tasksQz == null)
  210. {
  211. data.Code = ReturnCode.ArgsError;
  212. return data;
  213. }
  214. if (tasksQz == null || Guid.Empty == tasksQz.Id)
  215. {
  216. return new ApiResult(ReturnCode.GeneralError);
  217. }
  218. else
  219. {
  220. _unitOfWork.BeginTransaction();
  221. bool bResult = await _unitOfWork.RegisterDirty(tasksQz);
  222. try
  223. {
  224. if (bResult)
  225. {
  226. data.Message = "修改成功";
  227. if (tasksQz.IsStart)
  228. {
  229. var ResuleModelStop = await _schedulerCenter.StopScheduleJobAsync(tasksQz);
  230. data.Message = $"{data.Message}=>停止:{ResuleModelStop.msg}";
  231. var ResuleModelStar = await _schedulerCenter.AddScheduleJobAsync(tasksQz);
  232. data.Code = ReturnCode.ProcedureSuccess;
  233. data.Message = $"{data.Message}=>启动:{ResuleModelStar.msg}";
  234. }
  235. else
  236. {
  237. var ResuleModelStop = await _schedulerCenter.StopScheduleJobAsync(tasksQz);
  238. data.Message = $"{data.Message}=>停止:{ResuleModelStop.msg}";
  239. }
  240. }
  241. else
  242. {
  243. data.Message = "修改失败";
  244. }
  245. }
  246. catch (Exception ex)
  247. {
  248. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  249. }
  250. finally
  251. {
  252. if (data.Code == ReturnCode.ProcedureSuccess)
  253. await _unitOfWork.CommitAsync();
  254. else
  255. _unitOfWork.Rollback();
  256. data.Code = ReturnCode.Success;
  257. }
  258. }
  259. return data;
  260. }
  261. /// <summary>
  262. /// 删除一个任务
  263. /// </summary>
  264. /// <param name="jobId"></param>
  265. /// <returns></returns>
  266. [HttpDelete]
  267. public async Task<ApiResult> Delete(Guid jobId)
  268. {
  269. var data = new ApiResult();
  270. _unitOfWork.BeginTransaction();
  271. try
  272. {
  273. var model = await _service.GetByIdAsync(jobId);
  274. bool bResult = await _unitOfWork.RegisterDeleted(model);
  275. if (bResult)
  276. {
  277. data.Message = "删除成功";
  278. var ResuleModel = await _schedulerCenter.StopScheduleJobAsync(model);
  279. data.Message = $"{data.Message}=>任务状态=>{ResuleModel.msg}";
  280. data.Code = ReturnCode.ProcedureSuccess;
  281. }
  282. else
  283. {
  284. data.Message = "删除失败";
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  290. }
  291. finally
  292. {
  293. if (data.Code == ReturnCode.ProcedureSuccess)
  294. await _unitOfWork.CommitAsync();
  295. else
  296. _unitOfWork.Rollback();
  297. data.Code = ReturnCode.Success;
  298. }
  299. return data;
  300. }
  301. /// <summary>
  302. /// 启动计划任务
  303. /// </summary>
  304. /// <param name="jobId"></param>
  305. /// <returns></returns>
  306. [HttpGet("StartJob/{jobId}")]
  307. public async Task<ApiResult> StartJob(Guid jobId)
  308. {
  309. var data = new ApiResult();
  310. var model = await _service.GetByIdAsync(jobId);
  311. if (model != null)
  312. {
  313. _unitOfWork.BeginTransaction();
  314. try
  315. {
  316. model.IsStart = true;
  317. bool bResult = await _unitOfWork.RegisterDirty(model);
  318. if (bResult)
  319. {
  320. data.Message = "更新成功";
  321. var ResuleModel = await _schedulerCenter.AddScheduleJobAsync(model);
  322. data.Code = ReturnCode.Success;
  323. if (ResuleModel.success)
  324. {
  325. data.Message = $"{data.Message}=>启动成功=>{ResuleModel.msg}";
  326. }
  327. else
  328. {
  329. data.Message = $"{data.Message}=>启动失败=>{ResuleModel.msg}";
  330. }
  331. data.Code = ReturnCode.ProcedureSuccess;
  332. }
  333. else
  334. {
  335. data.Message = "更新失败";
  336. }
  337. }
  338. catch (Exception ex)
  339. {
  340. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  341. }
  342. finally
  343. {
  344. if (data.Code == ReturnCode.ProcedureSuccess)
  345. await _unitOfWork.CommitAsync();
  346. else
  347. _unitOfWork.Rollback();
  348. data.Code = ReturnCode.Success;
  349. }
  350. }
  351. else
  352. {
  353. data.Message = "任务不存在";
  354. }
  355. return data;
  356. }
  357. /// <summary>
  358. /// 停止一个计划任务
  359. /// </summary>
  360. /// <param name="jobId"></param>
  361. /// <returns></returns>
  362. [HttpGet("StopJob/{jobId}")]
  363. public async Task<ApiResult> StopJob(Guid jobId)
  364. {
  365. var data = new ApiResult();
  366. try
  367. {
  368. var model = await _service.GetByIdAsync(jobId);
  369. if (model != null)
  370. {
  371. model.IsStart = false;
  372. bool bResult = await _unitOfWork.RegisterDirty(model);
  373. if (bResult)
  374. {
  375. data.Message = "更新成功";
  376. var ResuleModel = await _schedulerCenter.StopScheduleJobAsync(model);
  377. if (ResuleModel.success)
  378. {
  379. data.Message = $"{data.Message}=>停止成功=>{ResuleModel.msg}";
  380. }
  381. else
  382. {
  383. data.Message = $"{data.Message}=>停止失败=>{ResuleModel.msg}";
  384. }
  385. data.Code = ReturnCode.ProcedureSuccess;
  386. }
  387. else
  388. {
  389. data.Message = "更新失败";
  390. }
  391. }
  392. else
  393. {
  394. data.Message = "任务不存在";
  395. }
  396. }
  397. catch(Exception ex)
  398. {
  399. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  400. }
  401. finally
  402. {
  403. if (data.Code == ReturnCode.ProcedureSuccess)
  404. await _unitOfWork.CommitAsync();
  405. else
  406. _unitOfWork.Rollback();
  407. data.Code = ReturnCode.Success;
  408. }
  409. return data;
  410. }
  411. /// <summary>
  412. /// 暂停一个计划任务
  413. /// </summary>
  414. /// <param name="jobId"></param>
  415. /// <returns></returns>
  416. [HttpGet("PauseJob/{jobId}")]
  417. public async Task<ApiResult> PauseJob(Guid jobId)
  418. {
  419. var data = new ApiResult();
  420. var model = await _service.GetByIdAsync(jobId);
  421. if (model != null)
  422. {
  423. _unitOfWork.BeginTransaction();
  424. try
  425. {
  426. model.IsStart = false;
  427. bool bResult = await _unitOfWork.RegisterDirty(model);
  428. if (bResult)
  429. {
  430. data.Message = "更新成功";
  431. var ResuleModel = await _schedulerCenter.PauseJob(model);
  432. if (ResuleModel.success)
  433. {
  434. data.Message = $"{data.Message}=>暂停成功=>{ResuleModel.msg}";
  435. }
  436. else
  437. {
  438. data.Message = $"{data.Message}=>暂停失败=>{ResuleModel.msg}";
  439. }
  440. data.Code = ReturnCode.ProcedureSuccess;
  441. }
  442. else
  443. {
  444. data.Message = "更新失败";
  445. }
  446. }
  447. catch (Exception ex)
  448. {
  449. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  450. }
  451. finally
  452. {
  453. if (data.Code == ReturnCode.ProcedureSuccess)
  454. await _unitOfWork.CommitAsync();
  455. else
  456. _unitOfWork.Rollback();
  457. data.Code = ReturnCode.Success;
  458. }
  459. }
  460. else
  461. {
  462. data.Message = "任务不存在";
  463. }
  464. return data;
  465. }
  466. /// <summary>
  467. /// 恢复一个计划任务
  468. /// </summary>
  469. /// <param name="jobId"></param>
  470. /// <returns></returns>
  471. [HttpGet("ResumeJob/{jobId}")]
  472. public async Task<ApiResult> ResumeJob(Guid jobId)
  473. {
  474. var data = new ApiResult();
  475. var model = await _service.GetByIdAsync(jobId);
  476. if (model != null)
  477. {
  478. _unitOfWork.BeginTransaction();
  479. try
  480. {
  481. model.IsStart = true;
  482. bool bResult = await _unitOfWork.RegisterDirty(model);
  483. if (bResult)
  484. {
  485. data.Message = "更新成功";
  486. var ResuleModel = await _schedulerCenter.ResumeJob(model);
  487. if (ResuleModel.success)
  488. {
  489. data.Message = $"{data.Message}=>恢复成功=>{ResuleModel.msg}";
  490. }
  491. else
  492. {
  493. data.Message = $"{data.Message}=>恢复失败=>{ResuleModel.msg}";
  494. }
  495. data.Code = ReturnCode.ProcedureSuccess;
  496. }
  497. else
  498. {
  499. data.Message = "更新失败";
  500. }
  501. }
  502. catch (Exception ex)
  503. {
  504. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  505. }
  506. finally
  507. {
  508. if (data.Code == ReturnCode.ProcedureSuccess)
  509. await _unitOfWork.CommitAsync();
  510. else
  511. _unitOfWork.Rollback();
  512. data.Code = ReturnCode.Success;
  513. }
  514. }
  515. else
  516. {
  517. data.Message = "任务不存在";
  518. }
  519. return data;
  520. }
  521. /// <summary>
  522. /// 重启一个计划任务
  523. /// </summary>
  524. /// <param name="jobId"></param>
  525. /// <returns></returns>
  526. [HttpGet("ReCovery/{jobId}")]
  527. public async Task<ApiResult> ReCovery(Guid jobId)
  528. {
  529. var data = new ApiResult();
  530. var model = await _service.GetByIdAsync(jobId);
  531. if (model != null)
  532. {
  533. _unitOfWork.BeginTransaction();
  534. try
  535. {
  536. model.IsStart = true;
  537. bool bResult = await _unitOfWork.RegisterDirty(model);
  538. if (bResult)
  539. {
  540. data.Message = "更新成功";
  541. var ResuleModelStop = await _schedulerCenter.StopScheduleJobAsync(model);
  542. var ResuleModelStar = await _schedulerCenter.AddScheduleJobAsync(model);
  543. if (ResuleModelStar.success)
  544. {
  545. data.Message = $"{data.Message}=>停止:{ResuleModelStop.msg}=>启动:{ResuleModelStar.msg}";
  546. }
  547. else
  548. {
  549. data.Message = $"{data.Message}=>停止:{ResuleModelStop.msg}=>启动:{ResuleModelStar.msg}";
  550. }
  551. data.Code = ReturnCode.ProcedureSuccess;
  552. }
  553. else
  554. {
  555. data.Message = "更新失败";
  556. }
  557. }
  558. catch (Exception ex)
  559. {
  560. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  561. }
  562. finally
  563. {
  564. if (data.Code == ReturnCode.ProcedureSuccess)
  565. await _unitOfWork.CommitAsync();
  566. else
  567. _unitOfWork.Rollback();
  568. data.Code = ReturnCode.Success;
  569. }
  570. }
  571. else
  572. {
  573. data.Message = "任务不存在";
  574. }
  575. return data;
  576. }
  577. /// <summary>
  578. /// 立即执行任务
  579. /// </summary>
  580. /// <param name="jobId"></param>
  581. /// <returns></returns>
  582. [HttpGet("ExecuteJob/{jobId}")]
  583. public async Task<ApiResult> ExecuteJob(Guid jobId)
  584. {
  585. var data = new ApiResult();
  586. try
  587. {
  588. var model = await _service.GetByIdAsync(jobId);
  589. if (model != null)
  590. {
  591. var ResuleModel = await _schedulerCenter.ExecuteJobAsync(model);
  592. if (ResuleModel.success)
  593. {
  594. data.Message = $"{data.Message}=>执行成功=>{ResuleModel.msg}";
  595. }
  596. else
  597. {
  598. data.Message = $"{data.Message}=>执行失败=>{ResuleModel.msg}";
  599. }
  600. data.Code = ReturnCode.Success;
  601. }
  602. else
  603. {
  604. data.Message = "任务不存在";
  605. }
  606. }
  607. catch(Exception ex)
  608. {
  609. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  610. }
  611. finally
  612. {
  613. if (data.Code == ReturnCode.ProcedureSuccess)
  614. await _unitOfWork.CommitAsync();
  615. else
  616. _unitOfWork.Rollback();
  617. data.Code = ReturnCode.Success;
  618. }
  619. return data;
  620. }
  621. }
  622. }