using BPA.SAAS.Manage.Application.AExternalPlatform.BaseDto;
using BPA.SAAS.Manage.Application.AExternalPlatform.Enum;
using BPA.SAAS.Manage.Core.DataBase;
using BPA.SAAS.Manage.Core.Org;
using BPA.SAAS.Manage.Core;
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.SAAS.KitChenManage.Core;
using BPA.KitChen.GroupMeal.SqlSugar;

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 CheckKey(key);

            var thisSign = signStr + "&" + key;


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



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

        }

        /// <summary>
        /// 检查租户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private async Task CheckTenant(string tenantId)
        {
            var data = await SqlSugarDb.Db.Queryable<BPA_Company>().FirstAsync(x => x.Id == tenantId);

            if (data == null)
            {
                throw Oops.Oh(ErrorCodeEnum.Code1000);
            }

        }
    }
}
