GenerateFirstBillsJob.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Newtonsoft.Json;
  2. using Quartz;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using XYY.Tool.QuotaReduction;
  10. using XYY.Tool.TimingTask.Model;
  11. namespace XYY.Tool.TimingTask.jobs
  12. {
  13. public class GenerateFirstBillsJob:IJob
  14. {
  15. public Task Execute(IJobExecutionContext context)
  16. {
  17. int count = 0; bool isSucess = false; string msg = string.Empty;
  18. do
  19. {
  20. try
  21. {
  22. RestSharp.RestClient client = new RestSharp.RestClient(JobConfig.GenerateFirstBillsJobApi);
  23. RestSharp.RestRequest request = new RestSharp.RestRequest(RestSharp.Method.POST);
  24. request.AddHeader("Authorization", "token 57AEDE5487260443D6BF2182D31286B1");
  25. var response = client.Execute(request);
  26. var responseResult = JsonConvert.DeserializeObject<ApiJsonModel>(response.Content);
  27. if (responseResult.success)
  28. {
  29. isSucess = responseResult.success;
  30. }
  31. else
  32. {
  33. count++;
  34. }
  35. //重复赋值,取最后一次错误
  36. msg = $"头程账单:" + JobConfig.GenerateFirstBillsJobApi + $"\r\n执行结果:{isSucess}\r\n" ;
  37. if (!string.IsNullOrEmpty(responseResult.data.ToString()))
  38. {
  39. var info = JsonConvert.DeserializeObject<List<Tuple<int, DateTime, DateTime, string>>>(responseResult.data.ToString());
  40. foreach (var item in info)
  41. {
  42. msg += $"客户Id:{item.Item1},起:{item.Item2},止:{item.Item3},原因:{item.Item4}\r\n";
  43. }
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. count++;
  49. }
  50. Console.WriteLine(msg);
  51. //睡眠再继续
  52. if (count <= JobConfig.GenerateFirstBillsJobTryTime && !isSucess) { Thread.Sleep(JobConfig.GenerateFirstBillsJobTryTimeInterval); }
  53. }
  54. while (count <= JobConfig.GenerateFirstBillsJobTryTime && !isSucess);
  55. //失败请求,发送钉钉消息
  56. if (!isSucess) { dingApiRequest.sendDingMessageToChat("GenerateFirstBillsJob", msg); }
  57. return null;
  58. }
  59. }
  60. }