using OpenAuth.App.BaseApp.Base;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository;
using System.Text;
using OpenAuth.App.Interface;
using SqlSugar;
using Infrastructure;
using Infrastructure.Helpers;
using OpenAuth.App.ServiceApp.Response;
using Microsoft.Extensions.Configuration;
using OpenAuth.App.ServiceApp.DroneDocking.Request;
using DocumentFormat.OpenXml.Office.CustomUI;
using DocumentFormat.OpenXml.EMMA;
using OpenAuth.App.ServiceApp.DroneDocking.Response;
using DocumentFormat.OpenXml.Math;
using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;
using DocumentFormat.OpenXml.Drawing.Charts;
using Org.BouncyCastle.Ocsp;
using System.Net.Http.Headers;
using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
using System.Data.SQLite;
using System.Dynamic;
using Infrastructure.CloudSdk.minio;
using Infrastructure.Extensions;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json.Nodes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenAuth.WebApi;
using JsonSerializer = System.Text.Json.JsonSerializer;


namespace OpenAuth.App.ServiceApp.DroneDocking
{
    public class DroneDockApp : SqlSugarBaseApp<DroneDocktask, SugarDbContext>
    {
        private EncryptionHelper _helper;
        private IConfiguration configuration;
        private readonly ISqlSugarClient _client;
        private readonly MinioService _minioService;
        private readonly MqttClientManager _mqttClientManager;

        public DroneDockApp(EncryptionHelper helper, IConfiguration configuration, ISqlSugarClient client,
            MinioService minioService, MqttClientManager mqttClientManager, ISugarUnitOfWork<SugarDbContext> unitWork,
            ISimpleClient<DroneDocktask> repository, IAuth auth) : base(unitWork, repository, auth)
        {
            _helper = helper;
            this.configuration = configuration;
            _client = client;
            _minioService = minioService;
            _mqttClientManager = mqttClientManager;
        }

        /// <summary>
        /// 无人机机场接口注册更新
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<string>> RegistService(AirPortRegistReq req)
        {
            Response<string> Response = new Response<string>();

            var handler = new HttpClientHandler();
            // 如果需要忽略服务器证书错误（仅测试环境）
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            using (var client = new HttpClient())
            {
                try
                {
                    string url = configuration.GetSection("DroneDocking:Url").Value + "/droneAirport/RegistService";
                    string x_lc_secret = _helper.getxseret();
                    string centercode = "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ";
                    string x_token = _helper.GetToken(centercode);

                    // 序列化为 JSON 字符串
                    string json = JsonSerializer.Serialize(req);

                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(json);

                    var encryptedData = _helper.Encrypt(bytes);
                    // 创建请求体
                    var httpContent = new StringContent(encryptedData, Encoding.UTF8, "application/json");

                    // 添加请求头
                    client.DefaultRequestHeaders.Add("x-lc-secret", x_lc_secret);
                    client.DefaultRequestHeaders.Add("x-lc-token", x_token);

                    //发送请求
                    HttpResponseMessage response = await client.PostAsync(url, httpContent);
                    response.EnsureSuccessStatusCode();

                    // 获取响应头中的 x_cl_screte 参数
                    string resx_cl_screte = GetHeaderValue(response, "x-lc-secret");

                    // 读取响应内容并反序列化
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var result = JsonSerializer.Deserialize<ReciveData<string>>(responseBody);

                    //解密数据
                    byte[] resbytesx = Convert.FromBase64String(resx_cl_screte);
                    byte[] resdatabytes = Convert.FromBase64String(result?.data);
                    string data = _helper.Decrypt(resbytesx, resdatabytes);

                    Response.Result = data;
                    Response.Message = result.message;
                    Response.Code = result.code;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                    Response.Result = "连接错误";
                }
            }

            return Response;
        }

        /// <summary>
        /// 无人机机场设备注册/更新
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<string>> AddDevice(AddDeviceReq req)
        {
            Response<string> Response = new Response<string>();

            var handler = new HttpClientHandler();
            // 如果需要忽略服务器证书错误（仅测试环境）
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            using (var client = new HttpClient())
            {
                try
                {
                    string url = configuration.GetSection("DroneDocking:Url").Value + "/droneAirport/addDevice";
                    string x_lc_secret = _helper.getxseret();
                    string centercode = "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ";
                    string x_token = _helper.GetToken(centercode);

                    // 序列化为 JSON 字符串
                    string json = JsonSerializer.Serialize(req);

                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(json);

                    var encryptedData = _helper.Encrypt(bytes);
                    // 创建请求体
                    var httpContent = new StringContent(encryptedData, Encoding.UTF8, "application/json");

                    // 添加请求头
                    client.DefaultRequestHeaders.Add("x-lc-secret", x_lc_secret);
                    client.DefaultRequestHeaders.Add("x-lc-token", x_token);

                    //发送请求
                    HttpResponseMessage response = await client.PostAsync(url, httpContent);
                    response.EnsureSuccessStatusCode();

                    // 获取响应头中的 x_cl_screte 参数
                    string resx_cl_screte = GetHeaderValue(response, "x-lc-secret");

                    // 读取响应内容并反序列化
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var result = JsonSerializer.Deserialize<ReciveData<string>>(responseBody);

                    //解密数据
                    byte[] resbytesx = Convert.FromBase64String(resx_cl_screte);
                    byte[] resdatabytes = Convert.FromBase64String(result?.data);
                    string data = _helper.Decrypt(resbytesx, resdatabytes);

                    Response.Result = data;
                    Response.Message = result.message;
                    Response.Code = result.code;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                    Response.Result = "连接错误";
                }
            }

            return Response;
        }

        /// <summary>
        /// 无人机机场授权
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<string>> Authorization(AuthorizationReq req)
        {
            Response<string> Response = new Response<string>();

            var handler = new HttpClientHandler();
            // 如果需要忽略服务器证书错误（仅测试环境）
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            using (var client = new HttpClient())
            {
                try
                {
                    string url = configuration.GetSection("DroneDocking:Url").Value + "/droneAirport/authorization";
                    string x_lc_secret = _helper.getxseret();
                    string centercode = "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ";
                    string x_token = _helper.GetToken(centercode);

                    // 序列化为 JSON 字符串
                    string json = JsonSerializer.Serialize(req);

                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(json);

                    var encryptedData = _helper.Encrypt(bytes);
                    // 创建请求体
                    var httpContent = new StringContent(encryptedData, Encoding.UTF8, "application/json");

                    // 添加请求头
                    client.DefaultRequestHeaders.Add("x-lc-secret", x_lc_secret);
                    client.DefaultRequestHeaders.Add("x-lc-token", x_token);

                    //发送请求
                    HttpResponseMessage response = await client.PostAsync(url, httpContent);
                    response.EnsureSuccessStatusCode();

                    // 获取响应头中的 x_cl_screte 参数
                    string resx_cl_screte = GetHeaderValue(response, "x-lc-secret");

                    // 读取响应内容并反序列化
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var result = JsonSerializer.Deserialize<ReciveData<string>>(responseBody);

                    //解密数据
                    byte[] resbytesx = Convert.FromBase64String(resx_cl_screte);
                    byte[] resdatabytes = Convert.FromBase64String(result?.data);
                    string data = _helper.Decrypt(resbytesx, resdatabytes);

                    Response.Result = data;
                    Response.Message = result.message;
                    Response.Code = result.code;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                    Response.Result = "连接错误";
                }
            }

            return Response;
        }

        /// <summary>
        /// 无人机机场授权
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<string>> Test(AirPortTaskReq req)
        {
            Response<string> Response = new Response<string>();

            var handler = new HttpClientHandler();
            // 如果需要忽略服务器证书错误（仅测试环境）
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            using (var client = new HttpClient())
            {
                try
                {
                    string url = "http://localhost:10042/zhcfzx/droneAirport/AddTask";
                    string x_lc_secret = _helper.getxseret();
                    string centercode = "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ";
                    string x_token =
                        "API32_HENJOZMPBYKEXNVLFMY3Y5W5SQ.1751622229582.fmCjIucQYyq4YZe4CnSStN/rHcwjZTxUsDuXeXJfrYn0bwoaV1/IW8mcFwtLw8JHjowvMJrmPyy/QZAhssxQCQ==";

                    // 序列化为 JSON 字符串
                    string json = JsonSerializer.Serialize(req);

                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(json);

                    var encryptedData = _helper.Encrypt(bytes);
                    // 创建请求体
                    var httpContent = new StringContent(encryptedData, Encoding.UTF8, "application/json");

                    // 添加请求头
                    client.DefaultRequestHeaders.Add("x-lc-secret", x_lc_secret);
                    client.DefaultRequestHeaders.Add("x-lc-token", x_token);

                    //发送请求
                    HttpResponseMessage response = await client.PostAsync(url, httpContent);
                    response.EnsureSuccessStatusCode();

                    // 获取响应头中的 x_cl_screte 参数
                    string resx_cl_screte = GetHeaderValue(response, "x-lc-secret");

                    // 读取响应内容并反序列化
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var result = JsonSerializer.Deserialize<ReciveData<string>>(responseBody);

                    //解密数据
                    byte[] resbytesx = Convert.FromBase64String(resx_cl_screte);
                    byte[] resdatabytes = Convert.FromBase64String(result?.data);
                    string data = _helper.Decrypt(resbytesx, resdatabytes);

                    Response.Result = data;
                    Response.Message = result.message;
                    Response.Code = result.code;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                    Response.Result = "连接错误";
                }
            }

            return Response;
        }

        public async Task<Response<string>> Test1(string taskid)
        {
            Response<string> Response = new Response<string>();

            var handler = new HttpClientHandler();
            // 如果需要忽略服务器证书错误（仅测试环境）
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            using (var client = new HttpClient())
            {
                try
                {
                    string url = $"http://localhost:10042/zhcfzx/droneAirport/cancelTask?taskid={taskid}";
                    //string x_lc_secret = _helper.getxseret();
                    string centercode = "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ";
                    string x_token =
                        "API32_HENJOZMPBYKEXNVLFMY3Y5W5SQ.1751622229582.fmCjIucQYyq4YZe4CnSStN/rHcwjZTxUsDuXeXJfrYn0bwoaV1/IW8mcFwtLw8JHjowvMJrmPyy/QZAhssxQCQ==";

                    // 添加请求头
                    client.DefaultRequestHeaders.Add("x-lc-token", x_token);

                    //发送请求
                    HttpResponseMessage response = await client.GetAsync(url);
                    response.EnsureSuccessStatusCode();

                    // 获取响应头中的 x_cl_screte 参数
                    string resx_cl_screte = GetHeaderValue(response, "x-lc-secret");

                    // 读取响应内容并反序列化
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var result = JsonSerializer.Deserialize<ReciveData<string>>(responseBody);

                    //解密数据
                    byte[] resbytesx = Convert.FromBase64String(resx_cl_screte);
                    byte[] resdatabytes = Convert.FromBase64String(result?.data);
                    string data = _helper.Decrypt(resbytesx, resdatabytes);

                    Response.Result = data;
                    Response.Message = result.message;
                    Response.Code = result.code;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                    Response.Result = "连接错误";
                }
            }

            return Response;
        }

        /// <summary>
        /// 无人机任务添加
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<ResData> AddTask(string req, string secret)
        {
            ResData Response = new ResData();

            //解密数据
            byte[] secretbyte = Convert.FromBase64String(secret); //x-lc-secret
            byte[] reqbyte = Convert.FromBase64String(req); //返回数据
            string data = _helper.Decrypt(secretbyte, reqbyte);

            //序列化返回数据并处理
            var task = JsonSerializer.Deserialize<AirPortTaskReq>(data);

            string ids = "";
            foreach (var item in task.deviceid)
            {
            }

            //task数据生成
            DroneDocktask dt = new DroneDocktask();
            dt.id = Guid.NewGuid().ToString();
            dt.deviceid = string.Join(",", task.deviceid);
            dt.bizidname = task.bizidname.ToString();
            dt.taskid = task.taskid;
            dt.taskname = task.taskname;
            dt.datacode = task.datacode;
            dt.state = 0;
            dt.createtime = DateTime.Now;

            //taskdetail数据生成
            List<DroneDocktaskdetail> dalist = new List<DroneDocktaskdetail>();
            if (task.tasklist.Count > 0)
            {
                foreach (var item in task.tasklist)
                {
                    DroneDocktaskdetail da = new DroneDocktaskdetail();
                    da.id = Guid.NewGuid().ToString();
                    da.taskid = dt.id;
                    da.bz = item.bz;
                    da.dkbh = item.dkbh;
                    da.dkfw = item.dkfw;
                    da.dkmj = item.dkmj;
                    da.dkmc = item.dkmc;
                    da.xzqdm = item.xzqdm;
                    da.zdkbh = item.zdkbh;
                    da.bsm = item.bsm;
                    da.dklx = item.dklx;
                    dalist.Add(da);
                }
            }

            using (var uow = base.UnitWork.CreateContext())
            {
                //查询数据  检查是否有重复数据
                var info = uow.DroneDocktask.AsQueryable().Where(r => r.taskid == task.taskid).First();
                if (info == null)
                {
                    //本地数据库数据添加
                    await uow.DroneDocktask.InsertAsync(dt);
                    await uow.DroneDocktaskdetail.InsertRangeAsync(dalist);
                    var flag = uow.Commit();

                    //返回数据
                    if (flag)
                    {
                        string x_lc_secret = _helper.getxseret();
                        // 转换为字节数组
                        byte[] bytes = Encoding.UTF8.GetBytes(dt.taskid);
                        var encryptedResData = _helper.Encrypt(bytes);
                        Response.Result = encryptedResData;
                        Response.Message = "无人机机场任务添加成功";
                        Response.Code = 200;
                        Response.Secret = x_lc_secret;
                        return Response;
                    }
                    else
                    {
                        string x_lc_secret = _helper.getxseret();
                        // 转换为字节数组
                        byte[] bytes = Encoding.UTF8.GetBytes(" ");

                        var encryptedResData = _helper.Encrypt(bytes);
                        Response.Result = encryptedResData;
                        Response.Message = "无人机机场任务添加失败";
                        Response.Code = 500;
                        Response.Secret = x_lc_secret;
                        return Response;
                    }
                }
                else
                {
                    string x_lc_secret = _helper.getxseret();
                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(" ");

                    var encryptedResData = _helper.Encrypt(bytes);
                    Response.Result = encryptedResData;
                    Response.Message = "已存在相同taskid任务";
                    Response.Code = 500;
                    Response.Secret = x_lc_secret;
                    return Response;
                }
            }
        }


        /// <summary>
        /// 无人机任务更新
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<ResData> UpdateTask(string req, string secret)
        {
            ResData Response = new ResData();

            //解密数据
            byte[] secretbyte = Convert.FromBase64String(secret); //x-lc-secret
            byte[] reqbyte = Convert.FromBase64String(req); //返回数据
            string data = _helper.Decrypt(secretbyte, reqbyte);

            //序列化返回数据并处理
            var task = JsonSerializer.Deserialize<AirPortUpdateTaskReq>(data);

            using (var uow = base.UnitWork.CreateContext())
            {
                //查询数据
                var info = uow.DroneDocktask.AsQueryable().Where(r => r.taskid == task.taskid).First();
                if (info != null)
                {
                    if (info.state == 0)
                    {
                        //taskdetail数据生成
                        List<DroneDocktaskdetail> dalist = new List<DroneDocktaskdetail>();
                        if (task.tasklist.Count > 0)
                        {
                            foreach (var item in task.tasklist)
                            {
                                DroneDocktaskdetail da = new DroneDocktaskdetail();
                                da.id = Guid.NewGuid().ToString();
                                da.taskid = info.id;
                                da.bz = item.bz;
                                da.dkbh = item.dkbh;
                                da.dkfw = item.dkfw;
                                da.dkmj = item.dkmj;
                                da.dkmc = item.dkmc;
                                da.xzqdm = item.xzqdm;
                                da.zdkbh = item.zdkbh;
                                da.bsm = item.bsm;
                                da.dklx = item.dklx;
                                dalist.Add(da);
                            }
                        }

                        await uow.DroneDocktaskdetail.DeleteAsync(r => r.taskid == info.taskid);
                        await uow.DroneDocktaskdetail.InsertRangeAsync(dalist);
                        var flag = uow.Commit();

                        //返回数据
                        if (flag)
                        {
                            string x_lc_secret = _helper.getxseret();
                            // 转换为字节数组
                            byte[] bytes = Encoding.UTF8.GetBytes(info.taskid);
                            var encryptedResData = _helper.Encrypt(bytes);
                            Response.Result = encryptedResData;
                            Response.Message = "无人机机场任务更新成功";
                            Response.Code = 200;
                            Response.Secret = x_lc_secret;
                            return Response;
                        }
                        else
                        {
                            string x_lc_secret = _helper.getxseret();
                            // 转换为字节数组
                            byte[] bytes = Encoding.UTF8.GetBytes(" ");

                            var encryptedResData = _helper.Encrypt(bytes);
                            Response.Result = encryptedResData;
                            Response.Message = "无人机机场任务更新失败";
                            Response.Code = 500;
                            Response.Secret = x_lc_secret;
                            return Response;
                        }
                    }
                    else
                    {
                        string x_lc_secret = _helper.getxseret();
                        // 转换为字节数组
                        byte[] bytes = Encoding.UTF8.GetBytes(" ");

                        var encryptedResData = _helper.Encrypt(bytes);
                        Response.Result = encryptedResData;
                        Response.Message = "任务已开始执行，无法更新，请重新添加任务";
                        Response.Code = 500;
                        Response.Secret = x_lc_secret;
                        return Response;
                    }
                }
                else
                {
                    string x_lc_secret = _helper.getxseret();
                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(" ");

                    var encryptedResData = _helper.Encrypt(bytes);
                    Response.Result = encryptedResData;
                    Response.Message = "未找到任务数据";
                    Response.Code = 500;
                    Response.Secret = x_lc_secret;
                    return Response;
                }
            }
        }

        /// <summary>
        /// 取消任务
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<ResData> CancelTask(string taskid)
        {
            ResData Response = new ResData();

            using (var uow = base.UnitWork.CreateContext())
            {
                //查询数据
                var info = uow.DroneDocktask.AsQueryable().Where(r => r.taskid == taskid).First();
                if (info != null)
                {
                    var flag = await uow.DroneDocktask.UpdateAsync(it => new DroneDocktask()
                    {
                        state = 3
                    }, it => it.id == info.id);

                    //返回数据
                    if (uow.Commit())
                    {
                        string x_lc_secret = _helper.getxseret();
                        // 转换为字节数组
                        byte[] bytes = Encoding.UTF8.GetBytes(info.taskid);
                        var encryptedResData = _helper.Encrypt(bytes);
                        Response.Result = encryptedResData;
                        Response.Message = "无人机机场任务取消成功";
                        Response.Code = 200;
                        Response.Secret = x_lc_secret;
                        return Response;
                    }
                    else
                    {
                        string x_lc_secret = _helper.getxseret();
                        // 转换为字节数组
                        byte[] bytes = Encoding.UTF8.GetBytes(" ");

                        var encryptedResData = _helper.Encrypt(bytes);
                        Response.Result = encryptedResData;
                        Response.Message = "无人机机场任务取消失败";
                        Response.Code = 500;
                        Response.Secret = x_lc_secret;
                        return Response;
                    }
                }
                else
                {
                    string x_lc_secret = _helper.getxseret();
                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(" ");

                    var encryptedResData = _helper.Encrypt(bytes);
                    Response.Result = encryptedResData;
                    Response.Message = "未找到任务数据";
                    Response.Code = 500;
                    Response.Secret = x_lc_secret;
                    return Response;
                }
            }
        }

        /// <summary>
        /// 无人机状态获取
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<ResData> GetDroneStatus(string deviceid)
        {
            /*string data = @"
{
""code"": 200,
""data"": {
""mode_code"": 0,
""longitude"": 122.45,
""latitude"": 30.45,
""height"": 80,
""elevation"": 50,
""gimbal_pitch"": -45,
""gimbal_yaw"": 23,
""battery_capacity_percent"": 35,
""gps_state"": 2
},
""message"": ""获取无人机机场状态成功！"",
""traceid"": ""xxx""
}
";*/
            dynamic data = new ExpandoObject();
            var dock = await Repository
                .ChangeRepository<SugarRepositiry<LasaDronePort>>()
                .AsQueryable()
                .LeftJoin<LasaUav>((a, b) => a.Id == b.PId)
                .Where((a, b) => b.Sn == deviceid).FirstAsync();
            if (dock == null) // 无人机不在线
            {
                data.code = 200;
                dynamic inData = new ExpandoObject();
                inData.mode_code = 1; // 不在线
                data.message = "获取无人机状态成功";
            }
            else
            {
                data.code = 200;
                var dockOsdTopic = $"thing/product/{dock.Sn}/osd";
                var now = DateTime.Now;
                var time = now.AddSeconds(-5);
                var log = await Repository
                    .ChangeRepository<SugarRepositiry<LasaLog>>()
                    .AsQueryable()
                    .Where(r => r.Topic == dockOsdTopic)
                    .Where(r => r.CreateTime > time) // 查询5秒以内数据
                    .Where(r => SqlFunc.JsonLike(r.Data, "drone_in_dock"))
                    .OrderByDescending(r => r.CreateTime)
                    .FirstAsync();
                if (log != null)
                {
                    // 当前时间
                    var dockOsdData =
                        JsonConvert.DeserializeObject<JObject>(JsonConvert.DeserializeObject<string>(log.Data));
                    //drone_in_dock 0":"舱外","1":"舱内"
                    var droneInDock = dockOsdData["data"]?["drone_in_dock"]?.Value<int>();
                    if (droneInDock == 0) // 飞行任务状态
                    {
                        var droneOsdTopic = $"thing/product/{deviceid}/osd";
                        log = await Repository
                            .ChangeRepository<SugarRepositiry<LasaLog>>()
                            .AsQueryable()
                            .Where(r => r.Topic == droneOsdTopic)
                            .Where(r => r.CreateTime > time) // 查询5秒以内数据
                            .OrderByDescending(r => r.CreateTime)
                            .FirstAsync();
                        if (log != null)
                        {
                            var droneOsdData =
                                JsonConvert.DeserializeObject<JObject>(JsonConvert.DeserializeObject<string>(log.Data));

                            var longitude = Math.Round(droneOsdData["data"]?["longitude"]?.Value<decimal>() ?? 0m, 7);
                            var latitude = Math.Round(droneOsdData["data"]?["latitude"]?.Value<decimal>() ?? 0m, 7);
                            var height = Math.Round(droneOsdData["data"]?["height"]?.Value<decimal>() ?? 0m, 2);
                            var elevation = Math.Round(droneOsdData["data"]?["elevation"]?.Value<decimal>() ?? 0m, 2);
                            // 云台信息
                            var payload99 = droneOsdData["data"]?["99-0-0"];
                            var gimbalPitch = payload99?["gimbal_pitch"]?.Value<double>();
                            var gimbalYaw = payload99?["gimbal_yaw"]?.Value<double>();
                            //var gimbalRoll = payload99?["gimbal_roll"]?.Value<double>();
                            var batteryCapacityPercent =
                                droneOsdData["data"]?["battery"]?["capacity_percent"]?.Value<int>();
                            var quality = droneOsdData["data"]?["position_state"]?["quality"]?.Value<int>();

                            dynamic inData = new ExpandoObject();
                            // mode_code  0:在线；1:不在线；2:任务执行中
                            inData.mode_code = 2;
                            inData.longitude = longitude;
                            inData.latitude = latitude;
                            inData.height = height;
                            inData.elevation = elevation;
                            inData.gimbal_pitch = gimbalPitch;
                            inData.gimbal_yaw = gimbalYaw;
                            //data.gimbal_roll = gimbalRoll;
                            inData.battery_capacity_percent = batteryCapacityPercent;

                            inData.gps_state = quality switch
                            {
                                < 2 => 1, // 低精度
                                < 10 => 2, // 高精度
                                _ => 3 // rtk定位
                            };
                            data.data = inData;
                            data.message = "获取无人机状态成功！";
                            data.traceid = Guid.NewGuid().ToString();
                        }
                        else
                        {
                            data.code = 502;
                            data.message = "获取无人机状态失败";
                        }
                    }
                    else
                    {
                        // 舱内 关机或者开机状态
                        // 这里取机场osd日志信息
                        dynamic inData = new ExpandoObject();
                        inData.battery_capacity_percent =
                            dockOsdData["data"]?["drone_charge_state"]?["capacity_percent"]?.Value<int>();
                        inData.elevation = 0;
                        inData.mode_code = 0;
                        var longitude = Math.Round(dockOsdData["data"]?["longitude"]?.Value<decimal>() ?? 0m, 7);
                        var latitude = Math.Round(dockOsdData["data"]?["latitude"]?.Value<decimal>() ?? 0m, 7);
                        var height = Math.Round(dockOsdData["data"]?["height"]?.Value<decimal>() ?? 0m, 2);
                        inData.longitude = longitude;
                        inData.latitude = latitude;
                        inData.height = height;
                        var quality = dockOsdData["data"]?["position_state"]?["quality"]?.Value<int>();
                        inData.gps_state = quality switch
                        {
                            < 2 => 1,
                            < 10 => 2,
                            _ => 3
                        };
                        data.data = inData;
                        data.traceid = Guid.NewGuid().ToString();
                        data.code = 200;
                        data.message = "获取无人机状态成功";
                    }
                }
            }

            var response = new ResData();
            if (data.code == 200)
            {
                string xLcSecret = _helper.getxseret();
                // 转换为字节数组
                byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
                var encryptedResData = _helper.Encrypt(bytes);
                response.Result = encryptedResData;
                response.Message = "获取无人机状态成功！";
                response.Code = 200;
                response.Secret = xLcSecret;
                return response;
            }
            else
            {
                string xLcSecret = _helper.getxseret();
                // 转换为字节数组
                byte[] bytes = Encoding.UTF8.GetBytes(" ");
                var encryptedResData = _helper.Encrypt(bytes);
                response.Result = encryptedResData;
                response.Message = "获取无人机状态失败！";
                response.Code = 500;
                response.Secret = xLcSecret;
                return response;
            }
        }

        /// <summary>
        /// 无人机任务查询
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<ResData> queryTaskStatus(string req, string secret)
        {
            ResData Response = new ResData();

            //解密数据
            byte[] secretbyte = Convert.FromBase64String(secret); //x-lc-secret
            byte[] reqbyte = Convert.FromBase64String(req); //返回数据
            string data = _helper.Decrypt(secretbyte, reqbyte);

            //序列化返回数据并处理
            var task = JsonSerializer.Deserialize<AirPortTaskReq>(data);
            string sql = "select * from drone_docktask where taskid=" + data;


            string x_lc_secret = _helper.getxseret();
            // 转换为字节数组
            byte[] bytes = Encoding.UTF8.GetBytes(sql);
            var encryptedResData = _helper.Encrypt(bytes);
            Response.Result = encryptedResData;
            Response.Message = "查询无人机机场任务状态成功！";
            Response.Code = 200;
            Response.Secret = x_lc_secret;
            return Response;
        }

        /// <summary>
        /// 无人机机场设备注册/更新
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<string>> reportDroneStatus(List<AirPortStatusApply> req)
        {
            Response<string> Response = new Response<string>();

            var handler = new HttpClientHandler();
            // 如果需要忽略服务器证书错误（仅测试环境）
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            using (var client = new HttpClient())
            {
                try
                {
                    string url = configuration.GetSection("DroneDocking:Url").Value + "/droneAirport/reportDroneStatus";
                    string x_lc_secret = _helper.getxseret();
                    string centercode = "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ";
                    string x_token = _helper.GetToken(centercode);

                    // 序列化为 JSON 字符串
                    string json = JsonSerializer.Serialize(req);

                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(json);

                    var encryptedData = _helper.Encrypt(bytes);
                    // 创建请求体
                    var httpContent = new StringContent(encryptedData, Encoding.UTF8, "application/json");

                    // 添加请求头
                    client.DefaultRequestHeaders.Add("x-lc-secret", x_lc_secret);
                    client.DefaultRequestHeaders.Add("x-lc-token", x_token);

                    //发送请求
                    HttpResponseMessage response = await client.PostAsync(url, httpContent);
                    response.EnsureSuccessStatusCode();

                    // 获取响应头中的 x_cl_screte 参数
                    string resx_cl_screte = GetHeaderValue(response, "x-lc-secret");

                    // 读取响应内容并反序列化
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var result = JsonSerializer.Deserialize<ReciveData<string>>(responseBody);

                    //解密数据
                    byte[] resbytesx = Convert.FromBase64String(resx_cl_screte);
                    byte[] resdatabytes = Convert.FromBase64String(result?.data);
                    string data = _helper.Decrypt(resbytesx, resdatabytes);

                    Response.Result = data;
                    Response.Message = result.message;
                    Response.Code = result.code;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                    Response.Result = "连接错误";
                }
            }

            return Response;
        }


        /// <summary>
        /// 获取临时上传地址
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response<string>> getUploadFilePath(AirPortUploadReq req)
        {
            Response<string> Response = new Response<string>();

            var handler = new HttpClientHandler();
            // 如果需要忽略服务器证书错误（仅测试环境）
            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            using (var client = new HttpClient())
            {
                try
                {
                    string url = configuration.GetSection("DroneDocking:Url").Value + "/DataExchange/getUploadFilePath";
                    string x_lc_secret = _helper.getxseret();
                    string centercode = "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ";
                    string x_token = _helper.GetToken(centercode);

                    // 序列化为 JSON 字符串
                    string json = JsonSerializer.Serialize(req);

                    // 转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(json);

                    var encryptedData = _helper.Encrypt(bytes);
                    // 创建请求体
                    var httpContent = new StringContent(encryptedData, Encoding.UTF8, "application/json");

                    // 添加请求头
                    client.DefaultRequestHeaders.Add("x-lc-secret", x_lc_secret);
                    client.DefaultRequestHeaders.Add("x-lc-token", x_token);
                    client.DefaultRequestHeaders.Add("x-lc-datacode", "AirportData");

                    //发送请求
                    HttpResponseMessage response = await client.PostAsync(url, httpContent);
                    response.EnsureSuccessStatusCode();

                    // 获取响应头中的 x_cl_screte 参数
                    string resx_cl_screte = GetHeaderValue(response, "x-lc-secret");

                    // 读取响应内容并反序列化
                    string responseBody = await response.Content.ReadAsStringAsync();
                    var result = JsonSerializer.Deserialize<ReciveData<string>>(responseBody);

                    //解密数据
                    byte[] resbytesx = Convert.FromBase64String(resx_cl_screte);
                    byte[] resdatabytes = Convert.FromBase64String(result?.data);
                    string data = _helper.Decrypt(resbytesx, resdatabytes);

                    Response.Result = data;
                    Response.Message = result.message;
                    Response.Code = result.code;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                    Response.Result = "连接错误";
                }
            }

            return Response;
        }

        public async Task<Response<string>> UploadFile(AirPortUploadDbReq req)
        {
            Response<string> Response = new Response<string>();
            var filePath = req.filePath;
            var uploadUrl = req.fileUrl;
            var fileName = req.filename;
            byte[] fileBuffer = null;
            HttpClient client = null;
            MultipartFormDataContent formData = null;
            try
            {
                //文件过大，建议使用 大文件上传接口，分段读取，进行上传
                fileBuffer = File.ReadAllBytes(filePath);
                client = new HttpClient();
                client.Timeout = new TimeSpan(0, 0, 0, 5);
                formData = new MultipartFormDataContent();
                var fileContent = new ByteArrayContent(fileBuffer);
                fileContent.Headers.ContentDisposition = new
                    ContentDispositionHeaderValue("attachment");
                fileContent.Headers.ContentDisposition.FileName = fileName;
                //注意：务必 根据 文件扩展名，这里指定 ContentType
                fileContent.Headers.ContentType = new
                    MediaTypeHeaderValue("application/octet-stream");
                formData.Add(fileContent);
                var request = new HttpRequestMessage
                {
                    //注意：这里是 PUT
                    Method = HttpMethod.Put,
                    RequestUri = new Uri(uploadUrl),
                    Content = formData
                };
                var result = client.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
                if (string.IsNullOrWhiteSpace(result))
                {
                    Response.Result = result;
                }
            }
            catch (Exception ex)
            {
                Response.Result = "上传失败";
            }
            finally
            {
                fileBuffer = null;
                formData?.Dispose();
                client?.Dispose();
            }

            return Response;
        }

        /// <summary>
        /// 无人机状态获取
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<ResData> getResult(string taskid)
        {
            ResData Response = new ResData();
            using (var uow = base.UnitWork.CreateContext())
            {
                //查询数据  检查是否有重复数据
                var info = uow.DbfineInfo.AsQueryable().Where(r => r.taskid == taskid).First();

                string json = JsonSerializer.Serialize(info);
                string x_lc_secret = _helper.getxseret();
                // 转换为字节数组
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                var encryptedResData = _helper.Encrypt(bytes);
                Response.Result = encryptedResData;
                Response.Message = "获取无人机成果成功！";
                Response.Code = 200;
                Response.Secret = x_lc_secret;
                return Response;
            }
        }

        public async void InsertDb(string taskid)
        {
            var sqliteconnect = configuration.GetSection("DroneDocking:DbFile").Value;
            /*// 确保数据库文件存在
            if (!File.Exists(sqliteconnect))
            {
                var directory = Path.GetDirectoryName(sqliteconnect);
                if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                SQLiteConnection.CreateFile(sqliteconnect);
            }*/
            string _sqliteConnection = "DataSource = " + sqliteconnect + "; Version = 3; ";
            string sql = "select * from drone_docktaskdetail where taskid='" + taskid + "'";
            var filigtlist = _client.Ado.SqlQuery<DroneDocktaskdetail>(sql);
            using (var sqliteConn = new SQLiteConnection(_sqliteConnection))
            {
                sqliteConn.Open();
                string deleteDbinfo1 = "delete from JMXX";
                using (var cmd2 = new SQLiteCommand(deleteDbinfo1, sqliteConn))
                {
                    cmd2.ExecuteNonQuery();
                }

                string sqlInsert =
                    "INSERT INTO JMXX (ZSDM, SZZS,ZSBFJGDM,ZSBFJGGY) VALUES (@ZSDM, @SZZS,@ZSBFJGDM,@ZSBFJGGY)";
                using (var cmd1 = new SQLiteCommand(sqlInsert, sqliteConn))
                {
                    cmd1.Parameters.AddWithValue("@ZSDM", "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ");
                    cmd1.Parameters.AddWithValue("@SZZS",
                        "eyJvcmdjb2RlIjoiOTEzNzEzMDAzMjgzOTg1NzdRIiwib3JnbmFtZSI6IuWxseS4nOaFp+WIm+S/oeaBr+enkeaKgOaciemZkOWFrOWPuCIsImNlcnRjb2RlIjoiVUFWMzJfTEpZMkZQTVlERTZVREVTM1AzWkQ3VjNJS1EiLCJjZXJ0b3JnY29kZSI6IjEyMTAwMDAwNDAwMDEwMzk4UCIsImNlcnRvcmduYW1lIjoi5Lit5Zu95Zu95Zyf5YuY5rWL6KeE5YiS6Zmi5omn5rOV5qih5Z2XIiwiZXhwaXJhdGlvbiI6IjIwMjYtMDctMDEgMTU6NDY6MjAiLCJwdWJsaWNrZXkiOiIwNEYxQkYxQkVGRDYzRjE2RTVEQjNCQ0QzMkNBNUQ5QThFOERGN0FERDY3RDZDNEYyOTNGQjIzRjdCREE3NEY1MkQzMUI4RERBRDAzRDk1NUQ1NjY1MDVBMTVEOEI3REJFODNCNDIzNzhCOEU4NzQ5QzBFQjZFMkZCQzg2Nz" +
                        "U4MzhCIiwic2lnbiI6IjJBQTIyNDkwQzYxNkI0NjEzNUY2MDRBNDczNjFCQjJGRDcxMEJGOTdBQjcyNDdCMzYxQzc2N0NGQTNGNzcyMTQ0MkExQzhDMUVBRTIxRkEwQUZEMzJGRDg1NjdDMDg1MThGRUY4NTg2RUVCNkE4OTE1MDVBQjUxRjZDNDUyRUM2IiwiY2VydHR5cGUiOiLml6DkurrmnLrkuL7or4Hor4HkuaYiLCJyZWdpb25jb2RlIjoiMzcxMzAwIn0=");
                    cmd1.Parameters.AddWithValue("@ZSBFJGDM", "91371300328398577Q");
                    cmd1.Parameters.AddWithValue("@ZSBFJGGY",
                        "04F1BF1BEFD63F16E5DB3BCD32CA5D9A8E8DF7ADD67D6C4F293FB23F7BDA74F52D31B8DDAD03D955D566505A15D8B7DBE83B42378B8E8749C0EB6E2FBC8675838B");
                    cmd1.ExecuteNonQuery();
                }


                string deleteDbinfo = "delete from DB_INFO";
                using (var cmd = new SQLiteCommand(deleteDbinfo, sqliteConn))
                {
                    cmd.ExecuteNonQuery();
                }

                DateTime dt = DateTime.Now;
                string dt24 = dt.ToString("yyyy-MM-dd HH:mm:ss");
                string sqlInsertDbinfo =
                    "INSERT INTO DB_INFO (VERSION, CREATETIME,UPDATETIME,CREATEUSER,DESCRIPTION) VALUES (@VERSION, @CREATETIME,@UPDATETIME,@CREATEUSER,@DESCRIPTION)";
                using (var cmd = new SQLiteCommand(sqlInsertDbinfo, sqliteConn))
                {
                    cmd.Parameters.AddWithValue("@VERSION", 2.0);
                    cmd.Parameters.AddWithValue("@CREATETIME", dt24);
                    cmd.Parameters.AddWithValue("@UPDATETIME", dt24);
                    cmd.Parameters.AddWithValue("@CREATEUSER", "system");
                    cmd.Parameters.AddWithValue("@DESCRIPTION", "DESCRIPTION");
                    cmd.ExecuteNonQuery();
                }

                string deleteFj = "delete from FJ";
                using (var cmd = new SQLiteCommand(deleteFj, sqliteConn))
                {
                    cmd.ExecuteNonQuery();
                }

                for (int i = 0; i < filigtlist.Count; i++)
                {
                    var flightid = filigtlist[i];
                    string fligthpicsql = "select * from lasa_mediafile where \"FlightId\"='" + flightid.flighttaskid +
                                          "'";

                    var pics = _client.Ado.SqlQuery<LasaMediaFile>(fligthpicsql);
                    for (int j = 0; j < pics.Count; j++)
                    {
                        var pic = pics[j];
                        var imageStream = _minioService.GetObjectAsStream("", pic.ObjectKey + "");
                        if (imageStream.Result == null)
                        {
                            continue;
                        }

                        byte[] bytes;
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            imageStream.Result.CopyTo(memoryStream); // 将原始流复制到MemoryStream中
                            bytes = memoryStream.ToArray(); // 获取MemoryStream的字节数组表示
                        }

                        string fileHash;
                        using (var sha256 = SHA256.Create())
                        {
                            var hashBytes = sha256.ComputeHash(bytes);
                            fileHash = BitConverter.ToString(hashBytes).Replace("-",
                                "").ToLower();
                        }

                        string sqlintoFj =
                            "INSERT INTO FJ (BSM, DKLX,DKBSM,ZDKBH,XZQDM,FJMC,FJLX,PSTZ,FJ,FJHXZ,PSSJ,XDGD,JDGD,Longitude,Latitude,PSFYJ,PSJD,PSHGJ,PSJJ,FJYSKD,FJYSGD,PSDXZXX,PSRY,SPKZXX,ZSDM,JYM) " +
                            "VALUES (@BSM, @DKLX,@DKBSM,@ZDKBH,@XZQDM,@FJMC,@FJLX,@PSTZ,@FJ,@FJHXZ,@PSSJ,@XDGD,@JDGD,@Longitude,@Latitude,@PSFYJ,@PSJD,@PSHGJ,@PSJJ,@FJYSKD,@FJYSGD,@PSDXZXX,@PSRY,@SPKZXX,@ZSDM,@JYM)";
                        using (var cmd = new SQLiteCommand(sqlintoFj, sqliteConn))
                        {
                            cmd.Parameters.AddWithValue("@BSM", pic.Id);
                            cmd.Parameters.AddWithValue("@DKLX", flightid.dklx);
                            cmd.Parameters.AddWithValue("@DKBSM", flightid.dkbh);
                            cmd.Parameters.AddWithValue("@ZDKBH", flightid.zdkbh);
                            cmd.Parameters.AddWithValue("@XZQDM", flightid.xzqdm);
                            cmd.Parameters.AddWithValue("@FJMC", pic.Name);
                            cmd.Parameters.AddWithValue("@FJLX", 1);
                            cmd.Parameters.AddWithValue("@PSTZ", "Y");
                            cmd.Parameters.AddWithValue("@FJ", bytes);
                            cmd.Parameters.AddWithValue("@FJHXZ", fileHash);
                            cmd.Parameters.AddWithValue("@PSSJ", pic.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                            cmd.Parameters.AddWithValue("@XDGD", pic.RelativeAltitude);
                            cmd.Parameters.AddWithValue("@JDGD", pic.AbsoluteAltitude);
                            cmd.Parameters.AddWithValue("@Longitude", pic.Lat);
                            cmd.Parameters.AddWithValue("@Latitude", pic.Lng);
                            cmd.Parameters.AddWithValue("@PSFYJ", pic.GimbalYawDegree);
                            cmd.Parameters.AddWithValue("@PSJD", pic.GimbalYawDegree);
                            cmd.Parameters.AddWithValue("@PSHGJ", pic.GimbalYawDegree);
                            cmd.Parameters.AddWithValue("@PSJJ", pic.FocalLength);
                            cmd.Parameters.AddWithValue("@FJYSKD", pic.Width);
                            cmd.Parameters.AddWithValue("@FJYSGD", pic.Height);
                            cmd.Parameters.AddWithValue("@PSDXZXX", "");
                            cmd.Parameters.AddWithValue("@PSRY", "system");
                            cmd.Parameters.AddWithValue("@SPKZXX", "wu");
                            cmd.Parameters.AddWithValue("@ZSDM", "UAV32_LJY2FPMYDE6UDES3P3ZD7V3IKQ");
                            cmd.Parameters.AddWithValue("@JYM", "DESCRIPTION");
                            cmd.ExecuteNonQuery();
                        }
                    }
                }

                sqliteConn.Close();
            }

            AirPortUploadReq airPortUploadReq = new AirPortUploadReq();
            airPortUploadReq.code = "AirportData";
            airPortUploadReq.regioncode = filigtlist[0].xzqdm;
            List<string> strings = new List<string>();
            strings.Add(filigtlist[0].xzqdm + ".db");
            airPortUploadReq.filenames = strings;
            var result = await getUploadFilePath(airPortUploadReq);
            var jsonstr = result.Result;
            JArray jObject = JArray.Parse(jsonstr);
            var liststr = jObject[0].Value<JObject>();
            var write = liststr["writeurl"].ToString();
            var read = liststr["readurl"].ToString();
            AirPortUploadDbReq req = new AirPortUploadDbReq();
            req.fileUrl = write;
            req.filePath = configuration.GetSection("DroneDocking:DbFile").Value;
            req.filename = filigtlist[0].xzqdm + ".db";
            using (var uow = base.UnitWork.CreateContext())
            {
                DbfineInfo dbfine = new DbfineInfo();
                dbfine.id = Guid.NewGuid().ToString();
                dbfine.taskid = taskid;
                dbfine.regioncode = filigtlist[0].xzqdm;
                dbfine.extension = ".db";
                dbfine.attachment_count = 1;
                dbfine.size = 1000;
                dbfine.count = 50;
                dbfine.url = read;
                dbfine.hashcode = "adewfdafdaf";
                var info = await uow.DbfineInfo.InsertAsync(dbfine);

                uow.Commit();
                Console.WriteLine(info);
            }

            await UploadFile(req);
        }

        // 安全获取响应头值的方法
        private static string GetHeaderValue(HttpResponseMessage response, string headerName)
        {
            if (response.Headers.TryGetValues(headerName, out IEnumerable<string> values))
            {
                return string.Join(", ", values); // 如果多个值则合并
            }

            // 尝试忽略大小写再次查找
            foreach (var header in response.Headers)
            {
                if (header.Key.Equals(headerName, StringComparison.OrdinalIgnoreCase))
                {
                    return string.Join(", ", header.Value);
                }
            }

            return null; // 未找到
        }

        //获取省对接任务信息
        public async Task<Response<PageInfo<List<DroneDockRes>>>> GetDroneDockInfos(string keyWord, int page, int limit)
        {
            RefAsync<int> totalCount = 0;
            var info = await base.Repository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(keyWord), t => t.taskname.Contains(keyWord)||t.bizidname.Contains(keyWord))
            .Select(t=>new DroneDockRes
            {
                id= t.id,
                bizidname= t.bizidname,
                taskname= t.taskname,
                createtime= t.createtime,
                state= t.state,
                datacode= t.datacode,
                deviceid= t.deviceid,
                taskid= t.taskid,
                tasklist = SqlFunc.Subqueryable<DroneDocktaskdetail>().Where(r => r.taskid == t.id).LeftJoin<LasaTask>((r, s) => r.flighttaskid == s.FlightId).ToList((r,s)=> new DetailRes
                {
                    id = r.id,
                    bsm=r.bsm,
                    Status=s.Status,
                    bz=r.bz,
                    dkbh=r.dkbh,
                    dkfw=r.dkfw,
                    dklx=r.dklx,
                    dkmc=r.dkmc,
                    dkmj=r.dkmj,
                    flighttaskid=r.flighttaskid,
                    taskid=r.taskid,
                    xzqdm=r.xzqdm,
                    zdkbh=r.zdkbh
                })
            })
            .ToPageListAsync(page, limit, totalCount);
            return new Response<PageInfo<List<DroneDockRes>>>
            {
                Result = new PageInfo<List<DroneDockRes>>
                {
                    Items = info,
                    Total = totalCount
                }
            };
        }

        //获取飞行数据
        public async Task<Response<DroneDockflightRes>> GetDroneDockflightInfos(string taskid)
        {
            DroneDockflightRes res = new DroneDockflightRes();
            var detailinfo = await Repository.ChangeRepository<SugarRepositiry<DroneDocktaskdetail>>()
                .AsQueryable()
                .Where(r => r.flighttaskid == taskid)
                .ToListAsync();
            if (detailinfo.Count > 0)
            {
                var taskId = detailinfo[0].taskid;
                var taskinfo = await Repository.AsQueryable().Where(r => r.id == taskId).FirstAsync();
                var flighttaskinfo = await Repository.ChangeRepository<SugarRepositiry<LasaTask>>()
                    .AsQueryable()
                    .Where(r => r.FlightId == taskid)
                    .FirstAsync();
                if (taskinfo != null)
                {
                    res.bizidname = taskinfo.bizidname;
                    res.taskname = taskinfo.taskname;
                }

                res.tasklist = detailinfo;
                if (flighttaskinfo != null)
                {
                    res.taskExecuteTime =
                        Convert.ToDateTime(flighttaskinfo.ExecuteTime).ToString("yyyy-MM-dd HH:mm:ss");
                    res.waylineName = flighttaskinfo.TaskAirLineName;
                    res.flightTaskType = Convert.ToInt32(flighttaskinfo.FlightTaskType);
                    switch (flighttaskinfo.Status)
                    {
                        case 0:
                            res.flightTaskState = 1;
                            break;

                        case 1:
                            res.flightTaskState = 2;
                            break;

                        case 2:
                            res.flightTaskState = 3;
                            break;

                        case 5:
                            res.flightTaskState = 0;
                            break;
                        case null: //交集
                            res.flightTaskState = 4;
                            break;
                    }
                }
            }

            return new Response<DroneDockflightRes>
            {
                Result = res
            };
        }
    }
}