using NPOI.Util.ArrayExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using BPA.KitChen.GroupMeal.SqlSugar;
using Furion.DependencyInjection;
using Furion.DataEncryption;
using Furion.FriendlyException;
using BPA.KitChen.GroupMeal.Application.Service.AExternalPlatform.Enum;
using BPA.KitChen.GroupMeal.Application.Service.AExternalPlatform.BaseDto;
using BPA.KitChen.GroupMeal.Core.Common;
using Furion;
using Newtonsoft.Json;
using BPA.KitChen.GroupMeal.Core.Entity;
using BPA.KitChen.GroupMeal.Application.Service.AExternalPlatform.Service.CheckService.Services.Dtos;

namespace BPA.SAAS.Manage.Application.AExternalPlatform.Service.CheckService.Services
{
    public class CheckServices : ICheckServices, ITransient
    {
        /// <summary>
        ///检查Sign
        /// </summary>
        public async Task CheckSign(string key,string signStr,string signMd5)
        {

          
            //检查key
            await CheckStoreKey(key);

            var thisSign = signStr + "&key=" + key;
            var vvv = MD5Encryption.Encrypt(thisSign.ToUpper()).ToUpper();

            var vvvvv = thisSign.ToUpper();

            //if (MD5Encryption.Encrypt(thisSign.ToUpper()).ToUpper() != signMd5.ToUpper())
            //{
            //    throw Oops.Oh(ErrorCodeEnum.Code1005);
            //}
        }

        
        /// <summary>
        /// 检查平key验证
        /// </summary>
        /// <param name="key"></param>
        private async Task CheckStoreKey(string key)
        {
            var data = await SqlSugarDb.Db.Queryable<BPA_StoreAuthorization>()
                .ClearFilter()
                .FirstAsync(x => x.Key == key);
            if (data == null)
            {
                throw Oops.Oh(ErrorCodeEnum.Code1004);
            }

            if (data.PeriodValidity != null)
            {
                if (data.PeriodValidity.Value.Date < DateTime.Now.Date)
                {
                    throw Oops.Oh(ErrorCodeEnum.Code10014);
                }
            }
            await CheckTenant(data.GroupId);

        }

        /// <summary>
        /// 检查租户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private async Task CheckTenant(string tenantId)
        {
            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                var url = App.Configuration["GroupMealBaseUrl"] + "api/authorization/GetCompanyByIdNew?id=" + tenantId;
                var jsonData = HttpHelper.HttpGet(url, null, dic, "application/json");
                var data = JsonConvert.DeserializeObject<ResultSAASManageDto<CompanyDto>>(jsonData);
                if (data.statusCode != 200) throw Oops.Oh(ErrorCodeEnum.Code10016);
                if (data.data == null) throw Oops.Oh(ErrorCodeEnum.Code10016);
                CurrentUser.TenantId = tenantId;
            }
            catch (Exception e)
            {
                throw Oops.Oh("请求错误");
            }

        }
    }
}
