using OpenAuth.App.BaseApp.Base;
using OpenAuth.Repository.Domain.DataMaintenance;
using OpenAuth.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.App.Interface;
using SqlSugar;
using System.Data;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp.FireManagement.Request;

namespace OpenAuth.App.ServiceApp.FireManagement
{
    public class FireResourcesApp : SqlSugarBaseApp<ApplicationData, SugarDbContext>
    {
        public FireResourcesApp(ISugarUnitOfWork<SugarDbContext> unitWork,
        ISimpleClient<ApplicationData> repository, IAuth auth) : base(unitWork,
        repository,
        auth)
        {
            _auth = auth;
        }
        /// <summary>
        /// 根据sql获取数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<Response<DataTable>> GetDataBySql(SqlReq sql)
        {
            // 检查 SQL 是否为查询操作
            if (string.IsNullOrWhiteSpace(sql.sql) || !sql.sql.Trim().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
            {
                throw new Exception("只允许查询操作");
            } 
            try
            {
                using (SugarDbContext db = base.UnitWork.CreateContext())
                {
                    // 执行查询操作，返回 DataTable
                    DataTable dt = await db.Db.SqlQueryable<object>(sql.sql).ToDataTableAsync();
                    return new Response<DataTable>
                    {
                        Result = dt
                    };
                }
            }
            catch (Exception ex)
            {
                throw new Exception("sql格式不正确");
            }
        }
    }
}
