
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Interface;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenAuth.App
{
    /// <summary>
    /// 起火点网格及资源查询
    /// </summary>
    public class FireGrideResourceApp 
    {
        IConfiguration _configuration;
        private ILogger _logger;
        private readonly ISqlSugarClient _client;
        IAuth _auth;
        public FireGrideResourceApp(ISqlSugarClient client ,IConfiguration configuration, ILogger<FireGrideResourceApp> logger, IAuth auth)
        {
            _client = client;
            _configuration = configuration;
            _logger = logger;
            _auth = auth;
        }

        /// <summary>
        /// 根据起火点坐标，查询起火点所在的网格
        /// </summary>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <returns></returns>
        public dynamic GetGridInfoByLngLat(string lng,string lat)
        {
            string sql= @"SELECT ""Id"",""GridName"", st_astext(""Geom"") as Geom FROM ""wg_gridinfo""  where st_contains(""Geom"", st_geomfromtext('POINT(" + lng + @" " + lat + @")', 4326)) = TRUE  order by ""LayerLevel"" desc ";

            using (var db = _client)       
            {
                var list = db.SqlQueryable<dynamic>(sql).First();
                return list;
            }
        }

        /// <summary>
        /// 根据网格id获取水源地信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic GetWaterResource()
        {
            string sql = @"SELECT ""Id"",""Name"",""TypeName"",""Lng"",""Lat"",""Capacity"",""WaterForm"",""State"",""Address"" ,st_astext(""Geom"") as Geom FROM ""fm_waterresource""  order by ""Name"" ";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }
        /// <summary>
        /// 根据网格id获取水源地信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic GetWuziResource()
        {
            string sql = @"SELECT ""Id"",""Name"",""Info"",""Lng"",""Lat"",""State"",st_astext(""Geom"") as Geom FROM ""fm_materialpoint""  order by ""Name"" ";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }


        /// <summary>
        /// 根据网格id获取防火营房信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic GetWg_Barrack(long id)
        {
            string sql = @"SELECT ""Id"",""Name"",""GridId"", st_astext(""Geom"") as Geom FROM ""wg_barrack""  where ""GridId""=" + id + @"  order by ""Name"" ";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }

        /// <summary>
        /// 根据网格id获取水防火物资数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic GetMaterials(long id)
        {
            string sql = @"SELECT ""Id"",""Name"",""Type"",""Info"",""GridId"", st_astext(""Geom"") as Geom FROM ""wg_materials""  where ""GridId""=" + id + @"  order by ""Name"" ";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }

        /// <summary>
        /// 根据网格id获取道路信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic GetWgRoad(long id)
        {
            string sql = @"SELECT ""Id"",""Name"",""Type"",""GridId"", st_astext(""Geom"") as Geom FROM ""wg_road""  where ""GridId""=" + id + @"  order by ""Name"" ";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }
        /// <summary>
        /// 获取所有道路信息
        /// </summary> 
        /// <returns></returns>
        public dynamic GetAllWgRoad()
        {
            string sql = @"SELECT ""Id"",""Name"",""Type"",""GridId"", st_astext(""Geom"") as Geom FROM ""wg_road"" order by ""Name"" ";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }

        /// <summary>
        /// 根据网格id获取水网信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public dynamic GetWgWaterNetwork(long id)
        {
            string sql = @"SELECT ""Id"",""GridId"", st_astext(""Geom"") as Geom FROM ""wg_waternetwork""  where ""GridId""=" + id + @"";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }

        /// <summary>
        /// 获取全部道路信息
        /// </summary>
        /// <returns></returns>
        public dynamic GetAllRode()    
        {
            string sql = @"SELECT ""id"",""qufen"", st_astext(""geom"") as geom FROM ""beibudaolufenlei"" ";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }

        /// <summary>
        /// 获取全部营房信息
        /// </summary>
        /// <returns></returns>
        public dynamic GetAllYingFang()
        {
            string sql = @"SELECT ""Id"",""Name"",""Info"",""Lng"",""Lat"",""State"",st_astext(""geom"") as geom FROM ""fm_fireproofbarracks"" ";

            using (var db = _client)
            {
                var list = db.SqlQueryable<dynamic>(sql).ToList();
                return list;
            }
        }
    }
}
