using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.NApi.Application.WXInfo
{
    /// <summary>
    /// 微信开放平台openticket配置
    /// </summary>
    public class WXService : IWXService, ITransient
    {
        private readonly ISqlSugarRepository<YB_OpenWXConfig> repository;
        private readonly SqlSugarClient dbClient;
        public WXService(ISqlSugarRepository<YB_OpenWXConfig> sqlSugarRepository)

        {
            repository = sqlSugarRepository;
            dbClient = repository.Context;
        }
        /// <summary>
        /// 获取openticket
        /// </summary>
        /// <param name="componentAppId"></param>
        /// <returns></returns>
        public async Task<string> GetOpenTicketAsync(string componentAppId)
        {
            var component = await dbClient.Queryable<YB_OpenWXConfig>().Where(x => x.ComponentAppId == componentAppId).FirstAsync();
            return component?.OpenTicket;
        }

        /// <summary>
        /// 获取公众号的刷新token
        /// </summary>
        /// <param name="authorizerId">公众号appid</param>
        /// <param name="componentAppId">开放平台appid</param>
        /// <returns></returns>
        public async Task<string> GetRefreshToken(string authorizerId, string componentAppId)
        {
            var data = await dbClient.Queryable<YB_OfficlaAccount>().FirstAsync(x => x.authorizer_appid == authorizerId && x.componentappid == componentAppId);
            if (data != null)
            {
                return data.authorizer_refresh_token;
            }
            return "";
        }
    }
}
