_17TrackingService.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using XYY.Core.Standard.Mvc;
  7. using XYY.Data.Model.Standard.Tracking;
  8. using XYY.Data.Model.Standard.Tracking._17;
  9. namespace XYY.Service.Standard.TrackingService
  10. {
  11. public interface _17TrackingService
  12. {
  13. Task<ApiJsonModel<PostTrackingRequest>> Convert(_17TrackingMQData hook);
  14. }
  15. public class I17TrackingService : _17TrackingService
  16. {
  17. private string GetStatusString(int eventCode)
  18. {
  19. switch (eventCode)
  20. {
  21. case 0:
  22. return "notfound";
  23. case 10:
  24. case 20:
  25. return "transit";
  26. case 30:
  27. return "pickup";
  28. case 35:
  29. return "undelivered";
  30. case 40:
  31. return "delivered";
  32. case 50:
  33. return "exception";
  34. default:
  35. return "exception";
  36. }
  37. }
  38. public async Task<ApiJsonModel<PostTrackingRequest>> Convert(_17TrackingMQData hook)
  39. {
  40. if (hook is null)
  41. {
  42. throw new ArgumentNullException(nameof(hook));
  43. }
  44. if (hook.Event == "TRACKING_UPDATED")
  45. {
  46. try
  47. {
  48. var r = Newtonsoft.Json.JsonConvert.DeserializeObject<_17Data>(hook.Data);
  49. List<Z1Item> z1Items = new List<Z1Item>();
  50. if (r.track.z1 != null && r.track.z1.Count > 0)
  51. {
  52. z1Items.AddRange(r.track.z1);
  53. }
  54. if (r.track.z2 != null && r.track.z2.Count > 0)
  55. {
  56. z1Items.AddRange(r.track.z2);
  57. }
  58. if (r.track.z9 != null && r.track.z9.Count > 0)
  59. {
  60. z1Items.AddRange(r.track.z9);
  61. }
  62. PostTrackingRequest postTrackingRequest = new PostTrackingRequest()
  63. {
  64. LastMessage = r.track.z0.d + " " + r.track.z0.z,
  65. TargetTime = DateTime.Parse(r.track.z0.a),
  66. LastGetTime = DateTime.Now,
  67. Status = GetStatusString(r.track.e),
  68. TrackingNumber = r.number,
  69. List = z1Items.Select(x => new PostTrackingRequestItem
  70. {
  71. Event = x.z,
  72. Location = string.IsNullOrEmpty(x.d) ? x.c : x.d,
  73. TragetTime = DateTime.Parse(x.a)
  74. }).ToList()
  75. };
  76. var apiSet = NodeHelper.Convert(postTrackingRequest);
  77. if (apiSet.success)
  78. {
  79. var d = (PostTrackingRequest)apiSet.data;
  80. if (postTrackingRequest.Status == "delivered"
  81. && postTrackingRequest.TargetTime != null && d.DeliveryTime != null)
  82. {
  83. postTrackingRequest.SignTime = postTrackingRequest.TargetTime.Value;
  84. postTrackingRequest.SignDay = (postTrackingRequest.SignTime - d.DeliveryTime).Value.Days;
  85. }
  86. postTrackingRequest.DeliveryTime = d.DeliveryTime;
  87. postTrackingRequest.OnlineTime = d.OnlineTime;
  88. postTrackingRequest.OnlineDay = d.OnlineDay;
  89. postTrackingRequest.FailTime = d.FailTime;
  90. postTrackingRequest.FailMessage = d.FailMessage;
  91. postTrackingRequest.FailCNMessage = d.FailCNMessage;
  92. //postTrackingRequest.SignDay = d.SignDay;
  93. //postTrackingRequest.SignTime = d.SignTime;
  94. postTrackingRequest.PreInternetAccessDay = d.PreInternetAccessDay;
  95. postTrackingRequest.PreInternetAccessTime = d.PreInternetAccessTime;
  96. postTrackingRequest.Delivered = d.Delivered;
  97. return ApiJsonModel<PostTrackingRequest>.OK(postTrackingRequest);
  98. }
  99. else
  100. {
  101. return apiSet;
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. return ApiJsonModel<PostTrackingRequest>.Error("JSON 序列化失败" + ex.Message, null);
  107. }
  108. }
  109. else
  110. {
  111. return ApiJsonModel<PostTrackingRequest>.OK(null);
  112. }
  113. }
  114. }
  115. }