using HKLib.Dto;
using HKLib.RabbitMQ.Config;
using HKLib.RabbitMQ.Publisher;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using XExten.Advance.HttpFramework.MultiCommon;
using XExten.Advance.HttpFramework.MultiFactory;
using XExten.Advance.LinqFramework;

namespace HKLib.Interfaces
{
    public class HKLibHelper
    {
        static Dictionary<string, string> Header = new Dictionary<string, string> {
            {"TenantId",Configer.TenantId }
        };
        /// <summary>
        /// 设置刷卡时间端
        /// </summary>
        public static bool SetTime(TimeSpilte input)
        {
            TimeSpilteDto dto = new TimeSpilteDto
            {
                AMEndTime = input.AMEndTime.ToShortTimeString(),
                AMStartTime = input.AMStartTime.ToShortTimeString(),
                PMEndTime = input.PMEndTime.ToShortTimeString(),
                PMStartTime = input.PMStartTime.ToShortTimeString(),
                ATEndTime = input.ATEndTime.ToShortTimeString(),
                ATStartTime = input.ATStartTime.ToShortTimeString(),
            };

            var data = IHttpMultiClient.HttpMulti
               .AddHeader(t => t.Headers = Header)
               .AddNode(t =>
               {
                   t.NodePath = $"{Configer.SaasRoute}api/one-card/eat-time-interval";
                   t.JsonParam = dto.ToJson();
                   t.ReqType = MultiType.PUT;
               }).Build().RunStringFirst();
            var res = data.ToModel<JObject>()["data"]["isSuccess"].ToString().AsBool();
            if (res)
            {
                ServiceQueryPush.QueryPush.PushMQ(input, "TimeChanged", MQEnum.Push);
                return res;
            }
            return false;
        }
        /// <summary>
        /// 获取时间
        /// </summary>
        /// <returns></returns>
        public static TimeSpilte GetTime()
        {
            var data = IHttpMultiClient.HttpMulti.AddNode(t =>
            {
                t.NodePath = $"{Configer.SaasRoute}api/one-card/eat-time-interval";
                t.ReqType = MultiType.GET;
            }).Build().RunStringFirst();
            return data.ToModel<JObject>()["data"]["data"].ToJson().ToModel<TimeSpilte>();
        }

        /// <summary>
        /// 添加机构
        /// </summary>
        public static string SetOrg(string input)
        {
            return IHttpMultiClient.HttpMulti
                .AddHeader(t => t.Headers = Header)
                .AddNode(t =>
              {
                  t.NodePath = $"{Configer.SaasRoute}api/one-card/member-tag";
                  t.ReqType = MultiType.POST;
                  t.JsonParam = (new { Name = input }).ToJson();
              }).Build().RunStringFirst().ToModel<JObject>()["data"]["data"]["id"].ToString();
        }

        //public static string GetKou()
        //{
        ///// api / GateInfo / gatelist ? StoreId = 0c32b2e2 - 0dc9 - 4941 - b73d - 3dc91f7268ab & Current = 1 & PageSize = 10
        //}

        /// <summary>
        /// 修改机构
        /// </summary>
        public static bool AlterOrg(string input, string Sid)
        {
            return IHttpMultiClient.HttpMulti.AddHeader(t => t.Headers = Header).AddNode(t =>
            {
                t.NodePath = $"{Configer.SaasRoute}api/one-card/member-tag";
                t.ReqType = MultiType.PUT;
                t.JsonParam = (new { Name = input, Id = Sid }).ToJson();
            }).Build().RunStringFirst().ToModel<JObject>()["data"]["isSuccess"].ToString().AsBool();
        }

        /// <summary>
        /// 根据用户命获取下拉用户列表
        /// </summary>
        /// <param name="keyword"></param>
        public static List<UserListDto> GetUserList(string keyword)
        {
            return IHttpMultiClient.HttpMulti.AddHeader(t => t.Headers = Header).AddNode(t =>
             {
                 t.NodePath = $"{Configer.SaasRoute}api/one-card/staff-list";
                 t.ReqType = MultiType.POST;
                 t.JsonParam = (new { realName = keyword }).ToJson();
             }).Build().RunStringFirst().ToModel<JObject>()["data"]["data"].ToJson().ToModel<List<UserListDto>>();
        }
        /// <summary>
        /// 添加用户并绑卡
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool AddUserAndBindCard(UserDto input)
        {
            return IHttpMultiClient.HttpMulti.AddHeader(t => t.Headers = Header).AddNode(t =>
            {
                t.NodePath = $"{Configer.SaasRoute}api/one-card/mmenber-card";
                t.ReqType = MultiType.POST;
                t.JsonParam = (new { tagId = input.OrgId, realName = input.Name, phone = input.Phone, cardNum = input.CardNo }).ToJson();
            }).Build().RunStringFirst().ToModel<JObject>()["data"]["isSuccess"].ToString().AsBool();
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        public static bool DisableUser(string userName)
        {
            return IHttpMultiClient.HttpMulti.AddHeader(t => t.Headers = Header).AddNode(t =>
            {
                t.NodePath = $"{Configer.SaasRoute}api/one-card/mmenber-card";
                t.ReqType = MultiType.POST;
                t.JsonParam = (new { realName = userName }).ToJson();
            }).Build().RunStringFirst().ToModel<JObject>()["data"]["isSuccess"].ToString().AsBool();
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="input"></param>
        public static bool AddUser(UserDto input)
        {
            return IHttpMultiClient.HttpMulti.AddHeader(t => t.Headers = Header).AddNode(t =>
               {
                   t.NodePath = $"{Configer.SaasRoute}api/one-card/staff";
                   t.ReqType = MultiType.POST;
                   t.JsonParam = (new { tagId = input.OrgId, realName = input.Name, phone = input.Phone }).ToJson();
               }).Build().RunStringFirst().ToModel<JObject>()["data"]["isSuccess"].ToString().AsBool();
        }
        /// <summary>
        /// 变更卡状态 销户
        /// </summary>
        /// <param name="input"></param>
        public static bool CardStutasChange(CardStutasDto input)
        {
            input.CardNo = GetUserList(input.keywrod).FirstOrDefault(t => t.Name.Equals(input.keywrod)).Cards.FirstOrDefault(t => t.State == 1).CardNum;

            var x = IHttpMultiClient.HttpMulti.AddHeader(t => t.Headers = Header).AddNode(t =>
             {
                 t.NodePath = $"{Configer.SaasRoute}api/one-card/mmenber-card-state";
                 t.ReqType = MultiType.PUT;
                 t.JsonParam = (new { CardNum = input.CardNo, State = input.Stutas }).ToJson();
             }).Build().RunStringFirst().ToModel<JObject>()["data"]["isSuccess"].ToString().AsBool();
            if (x)
            {
                ServiceQueryPush.QueryPush.PushMQ(input, "CardStutasChanged", MQEnum.Top);
                return x;
            }
            return false;
        }
        /// <summary>
        /// 报表数据 
        /// type=1 数据参考
        /// 
        /// {
        ///"name": "张三",
        /// "phone": "18208263734",
        ///  "date": "2022-09-24",
        ///  "tag": null,
        ///  "amCount": 3,
        ///  "pmCount": 0,
        ///   "atCount": 0,
        ///   "totalCount": 3
        /// }
        /// 
        /// type=2 数据参考
        /// 
        /// {
        ///"name": null,
        ///"phone": null,
        ///"date": "2022-09-13",
        ///"tag": null,
        ///"amCount": 3,
        ///"pmCount": 1,
        ///"atCount": 0,
        ///"totalCount": 4
        ///}
        /// 
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="type">1 计次就餐天消费表(每人),2每月餐段消费报表,3月餐段汇总表</param>
        public static List<ReportCountDto> Report(ReportDto input, int type = 1)
        {
            var Route = Configer.SaasRoute;
            if (type == 1)
                Route += "api/one-card/day-consume-info";
            if (type == 2)
                Route += "api/one-card/consume-info";
            if (type == 3)
                Route += "api/one-card/time-interval-consume-info";
            var data = IHttpMultiClient.HttpMulti.AddHeader(t => t.Headers = Header).AddNode(t =>
              {
                  t.NodePath = Route;
                  t.ReqType = MultiType.POST;
                  t.JsonParam = (new { StartTime = input.StartTime.ToShortDateString(), EndTime = input.EndTime.ToShortDateString() }).ToJson();
              }).Build().RunStringFirst();
            return data.ToModel<JObject>()["data"]["data"].ToJson().ToModel<List<ReportCountDto>>();
        }
    }
}
