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

namespace XYY.Service.Standard.Finance.RedisCache
{

    public struct PaymentRequest
    {
        public int CurrentOverdueDays
        {
            get; set;
        }
        public DateTime TransactionTime
        {
            get; set;
        }
        public string BillId
        {
            get; set;
        }
        public double Fee
        {
            get; set;
        }
        public string ChargeName
        {
            get; set;
        }
        public int CustomerId
        {
            get; set;
        }
        public string FeeDescription
        {
            get; set;
        }

        public double SettlementRate
        {
            get; set;
        }

        public string SettlementCurrency
        {
            get; set;
        }

        public double AmountinOriginalCurrency
        {
            get; set;
        }

        public string Currency
        {
            get; set;
        }


    }

    public class ChargesRequestItem
    {
        public DateTime TransactionTime;

        public string SystemNO;

        public double Fee;

        public string ChargeName;
        public int CustomerId { get; set; }
        public string FeeDescription { get; internal set; }
    }
    //如果好用，请收藏地址，帮忙分享。
    public class ChargesItem
    {
        /// <summary>
        /// 
        /// </summary>
        public int code { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string success { get; set; }
        /// <summary>
        /// 操作成功！
        /// </summary>
        public string message { get; set; }
        /// <summary>
        /// 成功
        /// </summary>
        public string data { get; set; }
    }
    public enum RefreshRedisType
    {
        /// <summary>
        /// 快递资费
        /// </summary>
        RefreshOtherRedisCache = 100,
        /// <summary>
        /// 资费公式
        /// </summary>
        RefreshFormulaRedisCache = 200,
        /// <summary>
        /// 资费版本
        /// </summary>
        RefreshExpressBaseRedisCache = 300,
        /// <summary>
        /// 客户税率
        /// </summary>
        RefreshUserRateRedisCache = 400,
        /// <summary>
        /// 欧盟税率
        /// </summary>
        RefreshExchangeRedisCache = 500,
    }
    public static class ChargesService
    {
        static string host = "http://120.24.149.148:9503/";
        static ChargesService()
        {
            //#if DEBUG
            //host = "http://localhost:9503/";
            //#endif
        }
        /// <summary>
        /// 刷新redis缓存操作
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="expressId"></param>
        /// <param name="redisType"></param>
        /// <returns></returns>
        public static string RefreshRedisData(int customerId, int expressId, RefreshRedisType redisType)
        {
            string param = $"?customerId={customerId}&expressId={expressId}";
            if (redisType == RefreshRedisType.RefreshUserRateRedisCache || redisType == RefreshRedisType.RefreshExchangeRedisCache) { param = string.Empty; }
            RestSharp.IRestClient client = new RestSharp.RestClient($"{host}api/RedisCacheMange/{redisType.GetName()}{param}");
            RestSharp.IRestRequest request = new RestSharp.RestRequest(method: RestSharp.Method.POST);
            request.AddHeader("Authorization", "token 132A7468DE079C6CEB59F383A661E612");
            var reponse = client.Execute(request);
            if (reponse.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<ChargesItem>(reponse.Content).data;
            }
            else
            {
                try
                {
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<ChargesItem>(reponse.Content ?? reponse.ErrorMessage).data;
                }
                catch
                {
                    return reponse.ErrorMessage ?? reponse.Content;
                }
            }
        }

        public static string Payment(PaymentRequest x)
        {
            var RequestDto = new
            {
                x.TransactionTime,
                AccountingDate = x.TransactionTime,
                BillingWeight = 0,
                BillType = "线下付款",
                BillNo = x.BillId,
                SettlementAmount = x.Fee,
                x.SettlementRate,
                x.SettlementCurrency,
                x.AmountinOriginalCurrency,
                x.Currency,
                x.ChargeName,
                x.FeeDescription,
                x.CustomerId
            };
            RestSharp.IRestClient client = new RestSharp.RestClient($"{host}api/Payment/Payment");
            RestSharp.IRestRequest request = new RestSharp.RestRequest(method: RestSharp.Method.POST);
            request.AddHeader("Authorization", "token 132A7468DE079C6CEB59F383A661E612");
            request.AddJsonBody(RequestDto);
            var reponse = client.Execute(request);
            if (reponse.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<ChargesItem>(reponse.Content).data;
            }
            else
            {
                try
                {
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<ChargesItem>(reponse.Content ?? reponse.ErrorMessage).data;
                }
                catch
                {
                    return reponse.ErrorMessage ?? reponse.Content;
                }
            }
        }

        /// <summary>
        /// 扣收款
        /// </summary>
        /// <param name="charges"></param>
        /// <returns></returns>
        public static string Deduction(List<ChargesRequestItem> charges)
        {
            var RequestDto = charges.Select(x => new
            {
                x.TransactionTime,
                AccountingDate = x.TransactionTime,
                BillingWeight = 0,
                BillType = "全程派送-扣收款",
                BillNo = x.SystemNO,
                IncomeOrESxpense = "收入",
                TransactionType = 1,
                SettlementAmount = x.Fee,
                x.ChargeName,
                FeeDescription = x.FeeDescription,
                x.CustomerId,

            }).ToList();
            RestSharp.IRestClient client = new RestSharp.RestClient($"{host}api/CustomerDeduction/Deduction");
            RestSharp.IRestRequest request = new RestSharp.RestRequest(method: RestSharp.Method.POST);
            request.AddHeader("Authorization", "token 132A7468DE079C6CEB59F383A661E612");
            request.AddJsonBody(RequestDto);
            var reponse = client.Execute(request);
            if (reponse.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<ChargesItem>(reponse.Content).data;
            }
            else
            {
                try
                {
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<ChargesItem>(reponse.Content ?? reponse.ErrorMessage).data;
                }
                catch
                {
                    return reponse.ErrorMessage ?? reponse.Content;
                }
            }
        }

        /// <summary>
        /// 补收款
        /// </summary>
        /// <param name="charges"></param>
        /// <returns></returns>
        public static string Supplement(List<ChargesRequestItem> charges)
        {
            var RequestDto = charges.Select(x => new
            {
                x.TransactionTime,
                AccountingDate = x.TransactionTime,
                BillingWeight = 0,
                BillType = "全程派送-补收款",
                BillNo = x.SystemNO,
                IncomeOrESxpense = "支出",
                TransactionType = 1,
                SettlementAmount = x.Fee,
                x.ChargeName,
                FeeDescription = x.FeeDescription,
                x.CustomerId
            }).ToList();
            RestSharp.IRestClient client = new RestSharp.RestClient($"{host}api/CustomerSupplement/Supplement");
            RestSharp.IRestRequest request = new RestSharp.RestRequest(method: RestSharp.Method.POST);
            request.AddHeader("Authorization", "token 132A7468DE079C6CEB59F383A661E612");
            request.AddJsonBody(RequestDto);
            var reponse = client.Execute(request);
            if (reponse.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<ChargesItem>(reponse.Content).data;
            }
            else
            {
                try
                {
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<ChargesItem>(reponse.Content ?? reponse.ErrorMessage).data;
                }
                catch
                {
                    return reponse.ErrorMessage ?? reponse.Content;
                }
            }
        }
    }
}
