TasksQzController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. data.Code = ReturnCode.ProcedureSuccess;
  241. }
  242. else
  243. {
  244. data.Message = "修改失败";
  245. }
  246. }
  247. catch (Exception ex)
  248. {
  249. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  250. }
  251. finally
  252. {
  253. if (data.Code == ReturnCode.ProcedureSuccess)
  254. await _unitOfWork.CommitAsync();
  255. else
  256. _unitOfWork.Rollback();
  257. data.Code = ReturnCode.Success;
  258. }
  259. }
  260. return data;
  261. }
  262. /// <summary>
  263. /// 删除一个任务
  264. /// </summary>
  265. /// <param name="jobId"></param>
  266. /// <returns></returns>
  267. [HttpDelete]
  268. public async Task<ApiResult> Delete(Guid jobId)
  269. {
  270. var data = new ApiResult();
  271. _unitOfWork.BeginTransaction();
  272. try
  273. {
  274. var model = await _service.GetByIdAsync(jobId);
  275. bool bResult = await _unitOfWork.RegisterDeleted(model);
  276. if (bResult)
  277. {
  278. data.Message = "删除成功";
  279. var ResuleModel = await _schedulerCenter.StopScheduleJobAsync(model);
  280. data.Message = $"{data.Message}=>任务状态=>{ResuleModel.msg}";
  281. data.Code = ReturnCode.ProcedureSuccess;
  282. }
  283. else
  284. {
  285. data.Message = "删除失败";
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  291. }
  292. finally
  293. {
  294. if (data.Code == ReturnCode.ProcedureSuccess)
  295. await _unitOfWork.CommitAsync();
  296. else
  297. _unitOfWork.Rollback();
  298. data.Code = ReturnCode.Success;
  299. }
  300. return data;
  301. }
  302. /// <summary>
  303. /// 启动计划任务
  304. /// </summary>
  305. /// <param name="jobId"></param>
  306. /// <returns></returns>
  307. [HttpGet("StartJob/{jobId}")]
  308. public async Task<ApiResult> StartJob(Guid jobId)
  309. {
  310. var data = new ApiResult();
  311. var model = await _service.GetByIdAsync(jobId);
  312. if (model != null)
  313. {
  314. _unitOfWork.BeginTransaction();
  315. try
  316. {
  317. model.IsStart = true;
  318. bool bResult = await _unitOfWork.RegisterDirty(model);
  319. if (bResult)
  320. {
  321. data.Message = "更新成功";
  322. var ResuleModel = await _schedulerCenter.AddScheduleJobAsync(model);
  323. data.Code = ReturnCode.Success;
  324. if (ResuleModel.success)
  325. {
  326. data.Message = $"{data.Message}=>启动成功=>{ResuleModel.msg}";
  327. }
  328. else
  329. {
  330. data.Message = $"{data.Message}=>启动失败=>{ResuleModel.msg}";
  331. }
  332. data.Code = ReturnCode.ProcedureSuccess;
  333. }
  334. else
  335. {
  336. data.Message = "更新失败";
  337. }
  338. }
  339. catch (Exception ex)
  340. {
  341. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  342. }
  343. finally
  344. {
  345. if (data.Code == ReturnCode.ProcedureSuccess)
  346. await _unitOfWork.CommitAsync();
  347. else
  348. _unitOfWork.Rollback();
  349. data.Code = ReturnCode.Success;
  350. }
  351. }
  352. else
  353. {
  354. data.Message = "任务不存在";
  355. }
  356. return data;
  357. }
  358. /// <summary>
  359. /// 停止一个计划任务
  360. /// </summary>
  361. /// <param name="jobId"></param>
  362. /// <returns></returns>
  363. [HttpGet("StopJob/{jobId}")]
  364. public async Task<ApiResult> StopJob(Guid jobId)
  365. {
  366. var data = new ApiResult();
  367. try
  368. {
  369. var model = await _service.GetByIdAsync(jobId);
  370. if (model != null)
  371. {
  372. model.IsStart = false;
  373. bool bResult = await _unitOfWork.RegisterDirty(model);
  374. if (bResult)
  375. {
  376. data.Message = "更新成功";
  377. var ResuleModel = await _schedulerCenter.StopScheduleJobAsync(model);
  378. if (ResuleModel.success)
  379. {
  380. data.Message = $"{data.Message}=>停止成功=>{ResuleModel.msg}";
  381. }
  382. else
  383. {
  384. data.Message = $"{data.Message}=>停止失败=>{ResuleModel.msg}";
  385. }
  386. data.Code = ReturnCode.ProcedureSuccess;
  387. }
  388. else
  389. {
  390. data.Message = "更新失败";
  391. }
  392. }
  393. else
  394. {
  395. data.Message = "任务不存在";
  396. }
  397. }
  398. catch(Exception ex)
  399. {
  400. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  401. }
  402. finally
  403. {
  404. if (data.Code == ReturnCode.ProcedureSuccess)
  405. await _unitOfWork.CommitAsync();
  406. else
  407. _unitOfWork.Rollback();
  408. data.Code = ReturnCode.Success;
  409. }
  410. return data;
  411. }
  412. /// <summary>
  413. /// 暂停一个计划任务
  414. /// </summary>
  415. /// <param name="jobId"></param>
  416. /// <returns></returns>
  417. [HttpGet("PauseJob/{jobId}")]
  418. public async Task<ApiResult> PauseJob(Guid jobId)
  419. {
  420. var data = new ApiResult();
  421. var model = await _service.GetByIdAsync(jobId);
  422. if (model != null)
  423. {
  424. _unitOfWork.BeginTransaction();
  425. try
  426. {
  427. model.IsStart = false;
  428. bool bResult = await _unitOfWork.RegisterDirty(model);
  429. if (bResult)
  430. {
  431. data.Message = "更新成功";
  432. var ResuleModel = await _schedulerCenter.PauseJob(model);
  433. if (ResuleModel.success)
  434. {
  435. data.Message = $"{data.Message}=>暂停成功=>{ResuleModel.msg}";
  436. }
  437. else
  438. {
  439. data.Message = $"{data.Message}=>暂停失败=>{ResuleModel.msg}";
  440. }
  441. data.Code = ReturnCode.ProcedureSuccess;
  442. }
  443. else
  444. {
  445. data.Message = "更新失败";
  446. }
  447. }
  448. catch (Exception ex)
  449. {
  450. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  451. }
  452. finally
  453. {
  454. if (data.Code == ReturnCode.ProcedureSuccess)
  455. await _unitOfWork.CommitAsync();
  456. else
  457. _unitOfWork.Rollback();
  458. data.Code = ReturnCode.Success;
  459. }
  460. }
  461. else
  462. {
  463. data.Message = "任务不存在";
  464. }
  465. return data;
  466. }
  467. /// <summary>
  468. /// 恢复一个计划任务
  469. /// </summary>
  470. /// <param name="jobId"></param>
  471. /// <returns></returns>
  472. [HttpGet("ResumeJob/{jobId}")]
  473. public async Task<ApiResult> ResumeJob(Guid jobId)
  474. {
  475. var data = new ApiResult();
  476. var model = await _service.GetByIdAsync(jobId);
  477. if (model != null)
  478. {
  479. _unitOfWork.BeginTransaction();
  480. try
  481. {
  482. model.IsStart = true;
  483. bool bResult = await _unitOfWork.RegisterDirty(model);
  484. if (bResult)
  485. {
  486. data.Message = "更新成功";
  487. var ResuleModel = await _schedulerCenter.ResumeJob(model);
  488. if (ResuleModel.success)
  489. {
  490. data.Message = $"{data.Message}=>恢复成功=>{ResuleModel.msg}";
  491. }
  492. else
  493. {
  494. data.Message = $"{data.Message}=>恢复失败=>{ResuleModel.msg}";
  495. }
  496. data.Code = ReturnCode.ProcedureSuccess;
  497. }
  498. else
  499. {
  500. data.Message = "更新失败";
  501. }
  502. }
  503. catch (Exception ex)
  504. {
  505. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  506. }
  507. finally
  508. {
  509. if (data.Code == ReturnCode.ProcedureSuccess)
  510. await _unitOfWork.CommitAsync();
  511. else
  512. _unitOfWork.Rollback();
  513. data.Code = ReturnCode.Success;
  514. }
  515. }
  516. else
  517. {
  518. data.Message = "任务不存在";
  519. }
  520. return data;
  521. }
  522. /// <summary>
  523. /// 重启一个计划任务
  524. /// </summary>
  525. /// <param name="jobId"></param>
  526. /// <returns></returns>
  527. [HttpGet("ReCovery/{jobId}")]
  528. public async Task<ApiResult> ReCovery(Guid jobId)
  529. {
  530. var data = new ApiResult();
  531. var model = await _service.GetByIdAsync(jobId);
  532. if (model != null)
  533. {
  534. _unitOfWork.BeginTransaction();
  535. try
  536. {
  537. model.IsStart = true;
  538. bool bResult = await _unitOfWork.RegisterDirty(model);
  539. if (bResult)
  540. {
  541. data.Message = "更新成功";
  542. var ResuleModelStop = await _schedulerCenter.StopScheduleJobAsync(model);
  543. var ResuleModelStar = await _schedulerCenter.AddScheduleJobAsync(model);
  544. if (ResuleModelStar.success)
  545. {
  546. data.Message = $"{data.Message}=>停止:{ResuleModelStop.msg}=>启动:{ResuleModelStar.msg}";
  547. }
  548. else
  549. {
  550. data.Message = $"{data.Message}=>停止:{ResuleModelStop.msg}=>启动:{ResuleModelStar.msg}";
  551. }
  552. data.Code = ReturnCode.ProcedureSuccess;
  553. }
  554. else
  555. {
  556. data.Message = "更新失败";
  557. }
  558. }
  559. catch (Exception ex)
  560. {
  561. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  562. }
  563. finally
  564. {
  565. if (data.Code == ReturnCode.ProcedureSuccess)
  566. await _unitOfWork.CommitAsync();
  567. else
  568. _unitOfWork.Rollback();
  569. data.Code = ReturnCode.Success;
  570. }
  571. }
  572. else
  573. {
  574. data.Message = "任务不存在";
  575. }
  576. return data;
  577. }
  578. /// <summary>
  579. /// 立即执行任务
  580. /// </summary>
  581. /// <param name="jobId"></param>
  582. /// <returns></returns>
  583. [HttpGet("ExecuteJob/{jobId}")]
  584. public async Task<ApiResult> ExecuteJob(Guid jobId)
  585. {
  586. var data = new ApiResult();
  587. try
  588. {
  589. var model = await _service.GetByIdAsync(jobId);
  590. if (model != null)
  591. {
  592. var ResuleModel = await _schedulerCenter.ExecuteJobAsync(model);
  593. if (ResuleModel.success)
  594. {
  595. data.Message = $"{data.Message}=>执行成功=>{ResuleModel.msg}";
  596. }
  597. else
  598. {
  599. data.Message = $"{data.Message}=>执行失败=>{ResuleModel.msg}";
  600. }
  601. data.Code = ReturnCode.Success;
  602. }
  603. else
  604. {
  605. data.Message = "任务不存在";
  606. }
  607. }
  608. catch(Exception ex)
  609. {
  610. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  611. }
  612. finally
  613. {
  614. //if (data.Code == ReturnCode.ProcedureSuccess)
  615. // await _unitOfWork.CommitAsync();
  616. //else
  617. // _unitOfWork.Rollback();
  618. //data.Code = ReturnCode.Success;
  619. }
  620. return data;
  621. }
  622. }
  623. }