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>> GetDataBySql1(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();
                    DataTable dt1 = await db.Db.Ado.GetDataTableAsync(sql.sql).ConfigureAwait(false);
                    return new Response<DataTable>
                    {
                        Result = dt
                    };
                }
            }
            catch (Exception ex)
            {
                throw new Exception("sql格式不正确");
            }
        }
        public async Task<Response<DataTable>> GetDataBySql(SqlReq sql)
        {
            if (string.IsNullOrWhiteSpace(sql.sql) ||
                !sql.sql.Trim().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
            {
                throw new Exception("只允许查询操作");
            }
            using (var db = base.UnitWork.CreateContext())
            {
                db.Db.Ado.CommandTimeOut = 60;
                var dt = await db.Db.Ado.GetDataTableAsync(sql.sql).ConfigureAwait(false);
                return new Response<DataTable> { Result = dt };
            }

        }
        /// <summary>
        /// 寻路
        /// </summary>
        /// <param name="startlng"></param>
        /// <param name="startlat"></param>
        /// <param name="endlng"></param>
        /// <param name="endlat"></param>
        /// <param name="areaname"></param>
        /// <returns></returns>
        public async Task<Response<DataTable>> LoadRoad(double startlng, double startlat, double endlng, double endlat, string areaname)
        { 
            using (var db = base.UnitWork.CreateContext())
            {
                string sql = "select ST_astext(ST_Union(geom)) as route from pgr_fromAtoB('" + areaname + "'::text," + startlng + "," + startlat + "," + endlng + "," + endlat + ") ;";
                db.Db.Ado.CommandTimeOut = 60;
                var dt = await db.Db.Ado.GetDataTableAsync(sql).ConfigureAwait(false);
                return new Response<DataTable> { Result = dt };
            }

        }
    }
}
