123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using XYY.Core.Standard.Mvc;
- using XYY.Data.Model.Standard.Tracking;
- using XYY.Data.Model.Standard.Tracking._17;
- namespace XYY.Service.Standard.TrackingService
- {
- public interface _17TrackingService
- {
- Task<ApiJsonModel<PostTrackingRequest>> Convert(_17TrackingMQData hook);
- }
- public class I17TrackingService : _17TrackingService
- {
- private string GetStatusString(int eventCode)
- {
- switch (eventCode)
- {
- case 0:
- return "notfound";
- case 10:
- case 20:
- return "transit";
- case 30:
- return "pickup";
- case 35:
- return "undelivered";
- case 40:
- return "delivered";
- case 50:
- return "exception";
- default:
- return "exception";
- }
- }
- public async Task<ApiJsonModel<PostTrackingRequest>> Convert(_17TrackingMQData hook)
- {
- if (hook is null)
- {
- throw new ArgumentNullException(nameof(hook));
- }
- if (hook.Event == "TRACKING_UPDATED")
- {
- try
- {
- var r = Newtonsoft.Json.JsonConvert.DeserializeObject<_17Data>(hook.Data);
- List<Z1Item> z1Items = new List<Z1Item>();
- if (r.track.z1 != null && r.track.z1.Count > 0)
- {
- z1Items.AddRange(r.track.z1);
- }
- if (r.track.z2 != null && r.track.z2.Count > 0)
- {
- z1Items.AddRange(r.track.z2);
- }
- if (r.track.z9 != null && r.track.z9.Count > 0)
- {
- z1Items.AddRange(r.track.z9);
- }
- PostTrackingRequest postTrackingRequest = new PostTrackingRequest()
- {
- LastMessage = r.track.z0.d + " " + r.track.z0.z,
- TargetTime = DateTime.Parse(r.track.z0.a),
- LastGetTime = DateTime.Now,
- Status = GetStatusString(r.track.e),
- TrackingNumber = r.number,
- List = z1Items.Select(x => new PostTrackingRequestItem
- {
- Event = x.z,
- Location = string.IsNullOrEmpty(x.d) ? x.c : x.d,
- TragetTime = DateTime.Parse(x.a)
- }).ToList()
- };
- var apiSet = NodeHelper.Convert(postTrackingRequest);
- if (apiSet.success)
- {
- var d = (PostTrackingRequest)apiSet.data;
- if (postTrackingRequest.Status == "delivered"
- && postTrackingRequest.TargetTime != null && d.DeliveryTime != null)
- {
- postTrackingRequest.SignTime = postTrackingRequest.TargetTime.Value;
- postTrackingRequest.SignDay = (postTrackingRequest.SignTime - d.DeliveryTime).Value.Days;
- }
- postTrackingRequest.DeliveryTime = d.DeliveryTime;
- postTrackingRequest.OnlineTime = d.OnlineTime;
- postTrackingRequest.OnlineDay = d.OnlineDay;
- postTrackingRequest.FailTime = d.FailTime;
- postTrackingRequest.FailMessage = d.FailMessage;
- postTrackingRequest.FailCNMessage = d.FailCNMessage;
- //postTrackingRequest.SignDay = d.SignDay;
- //postTrackingRequest.SignTime = d.SignTime;
- postTrackingRequest.PreInternetAccessDay = d.PreInternetAccessDay;
- postTrackingRequest.PreInternetAccessTime = d.PreInternetAccessTime;
- postTrackingRequest.Delivered = d.Delivered;
- return ApiJsonModel<PostTrackingRequest>.OK(postTrackingRequest);
- }
- else
- {
- return apiSet;
- }
- }
- catch (Exception ex)
- {
- return ApiJsonModel<PostTrackingRequest>.Error("JSON 序列化失败" + ex.Message, null);
- }
- }
- else
- {
- return ApiJsonModel<PostTrackingRequest>.OK(null);
- }
- }
- }
- }
|