using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XYY.Service.Standard.ChannelApi.Out
{
    public class FrPostApi
    {
        public string Out(string trackingNumber, decimal weight, DateTime outTime)
        {
            string url = "https://services.frayun.com/OutService.asmx/OutOrder";
            RestSharp.IRestClient client = new RestSharp.RestClient(url);
            RestSharp.IRestRequest request = new RestSharp.RestRequest(RestSharp.Method.POST);
            request.AddParameter("UserName", "out_ck");
            request.AddParameter("PassWord", "C3837DC980CB4D83A7CB2DB92A8D57B2");
            request.AddParameter("AirWayBillNo", trackingNumber);
            request.AddParameter("Weight", weight / 1000.00m);
            request.AddParameter("OutTime", outTime);

            var response = client.Execute(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\[.*\]");
                string c = regex.Match(response.Content).Value;
                List<Item> items = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Item>>(c);
                if (items.Count == 0)
                {
                    return "api 调用失败";
                }
                else
                {
                    if (string.IsNullOrEmpty(items[0].ErrorMsg))
                    {
                        return "ok";
                    }
                    else
                    {
                        return items[0].ErrorMsg;
                    }
                }
            }
            else
            {
                return response.ErrorMessage ?? response.Content;
            }
        }
    }

    //如果好用，请收藏地址，帮忙分享。
    public class Item
    {
        /// <summary>
        /// 
        /// </summary>
        public string DpdNo { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string ResultCode { get; set; }
        /// <summary>
        /// 身份验证失败
        /// </summary>
        public string ErrorMsg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string TrunNo { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public decimal Total { get; set; }
    }

}
