using System.Data;
using System.IO.Compression;
using System.Text;
using DocumentFormat.OpenXml.InkML;
using Infrastructure;
using Infrastructure.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NetTopologySuite;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.FormScheme.FormHelpers;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.ServiceApp.LayerManagerApp.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;
using Shapefile = NetTopologySuite.IO.Esri.Shapefile;

namespace OpenAuth.App.ServiceApp.LayerManagerApp;

public class LayerApp : SqlSugarBaseApp<DmLayer, SugarDbContext>
{
    private readonly string _filePath;
    private readonly ISqlSugarClient _client;

    public LayerApp(ISugarUnitOfWork<SugarDbContext> unitWork,
        ISimpleClient<DmLayer> repository, IAuth auth,
        IOptions<AppSetting> setOptions, ISqlSugarClient client) : base(unitWork, repository, auth)
    {
        _client = client;
        _filePath = setOptions.Value.UploadPath;
        if (string.IsNullOrEmpty(_filePath))
        {
            _filePath = AppContext.BaseDirectory;
        }
    }

    public async Task<Response<bool>> UploadExcel(LayerReq req)
    {
        using var db = Repository.AsSugarClient();
        var excelPath = Path.Combine(_filePath, req.FilePath);
        // excel名称
        var excelFileName = Path.GetFileNameWithoutExtension(excelPath);
        // 使用NPOI读取Excel文件
        using (var fileStream = new FileStream(excelPath, FileMode.Open, FileAccess.Read))
        {
            IWorkbook workbook;
            if (Path.GetExtension(excelPath).Equals(".xls"))
            {
                workbook = new HSSFWorkbook(fileStream);
            }
            else
            {
                workbook = new XSSFWorkbook(fileStream);
            }

            // 
            var sheet = workbook.GetSheetAt(0);
            // 获取总行数
            var rowCount = sheet.LastRowNum;
            if (rowCount < 3)
            {
                throw new Exception("只有两行表头");
            }

            // 获取总列数
            var columnCount = sheet.GetRow(0).LastCellNum;
            var headers = new List<DataTableAttr>();
            // 遍历表头
            var chineseRow = sheet.GetRow(0);
            var englishRow = sheet.GetRow(1);

            for (var j = 0; j < columnCount; j++)
            {
                var header = new DataTableAttr
                {
                    InitName = chineseRow.GetCell(j).ToString(), // 中文表头
                    RefName = englishRow.GetCell(j).ToString()?.ToLower(), // 英文表头
                    // 字段类型
                    Type = "string",
                    // 原始字段名称
                    Name = englishRow.GetCell(j).ToString(),
                    // 字段长度
                    Length = 64
                };
                headers.Add(header);
            }

            var keys = headers.Select(a => a.RefName).ToList();
            if (!keys.Contains("lng") || !keys.Contains("lat"))
            {
                throw new Exception("缺少经纬度字段");
            }

            var typeBuilder = db.DynamicBuilder().CreateClass(req.DataTable,
                new SugarTable() { TableName = req.DataTable, TableDescription = req.ServerName + "图斑" });
            //添加主键
            typeBuilder.CreateProperty("id", typeof(string), new SugarColumn()
            {
                IsPrimaryKey = true,
                IsIdentity = false,
                ColumnDataType = "varchar",
                Length = 36,
                ColumnDescription = "主键",
            });
            //添加主键
            typeBuilder.CreateProperty("geometry", typeof(string), new SugarColumn()
            {
                IsPrimaryKey = false,
                IsIdentity = false,
                // ColumnDataType = "geometry(GEOMETRY)",
                ColumnDataType = string.Concat("geometry(GEOMETRY,",
                    req.SpatialRef.AsSpan(req.SpatialRef.LastIndexOf(":", StringComparison.Ordinal) + 1),
                    ")"),
                ColumnDescription = "图斑",
            });

            headers.ForEach(u =>
            {
                if (!u.RefName.Equals("lng") && !u.RefName.Equals("lat"))
                {
                    typeBuilder.CreateProperty(u.RefName, typeof(string), new SugarColumn()
                    {
                        IsPrimaryKey = false,
                        IsIdentity = false,
                        IsNullable = true,
                        Length = u.Length,
                        ColumnDescription = u.InitName,
                    });
                }
            });
            // 开启事务
            await db.Ado.BeginTranAsync(IsolationLevel.ReadCommitted);
            // 创建表
            //db.CodeFirst.InitTables(typeBuilder.BuilderType());

            var cols = new string[headers.Count];
            for (var i = 0; i < headers.Count; i++)
            {
                if (headers[i].RefName.Equals("lng"))
                {
                    cols[i] = "geometry";
                }
                else if (headers[i].RefName.Equals("lat"))
                {
                    cols[i] = "id";
                }
                else
                {
                    cols[i] = headers[i].RefName;
                }
            }

            var objs = new List<Dictionary<string, object>>(1001);
            var num = 0;
            // 遍历数据行
            for (var i = 2; i < rowCount; i++)
            {
                var row = sheet.GetRow(i);
                if (row == null)
                {
                    continue;
                }

                var data = new Dictionary<string, object>();
                // 遍历列
                string lat = null, lng = null;
                for (var j = 0; j < columnCount; j++)
                {
                    var cell = row.GetCell(j);
                    if (cell == null)
                    {
                        continue;
                    }

                    var cellValue = cell.ToString();
                    switch (headers[j].RefName)
                    {
                        case "lat":
                            lat = cellValue;
                            continue;
                        case "lng":
                            lng = cellValue;
                            continue;
                        default:
                            data[headers[j].RefName] = cellValue;
                            break;
                    }
                }

                if (lat != null && lng != null)
                {
                    var geography =
                        NtsGeometryServices.Instance.CreateGeometryFactory(
                            int.Parse(req.SpatialRef.Substring(
                                req.SpatialRef.IndexOf(":", StringComparison.Ordinal) + 1)));
                    var point = geography.CreatePoint(new Coordinate(double.Parse(lng), double.Parse(lat)));
                    data["geometry"] = point.AsText();
                }

                data["id"] = Guid.NewGuid().ToString();
                objs.Add(data);
                if (num++ == 999)
                {
                    await db.Insertable(objs)
                        .AS(req.DataTable) // 指定目标表名
                        .InsertColumns(cols) // 指定要插入的列
                        .ExecuteCommandAsync();
                    num = 0;
                    objs.Clear();
                }
            }

            await db.Insertable(objs)
                .AS(req.DataTable) // 指定目标表名
                .InsertColumns(cols) // 指定要插入的列
                .ExecuteCommandAsync();
            await db.Ado.CommitTranAsync();
            workbook.Close();
            // 关于图层发布失败后，如何处理？
            // excel 矢量点发布图层 
            var response = await GeoUtil.CreateStoreAndLayer("", req.SpatialRef, req.DataTable, "");
            // todo  保存图层信息
            var dmLayer = new DmLayer()
            {
                Id = Guid.NewGuid().ToString(),
                SeverName = req.ServerName,
                SpatialRef = req.SpatialRef,
                SeverType = req.SeverType, // 服务类型
                StoreType = "", //存储类型
                DataSourceType = req.DataSourceType,
                TableRef = req.DataTable,
                ShapeType = req.ShapeType,
                StyleName = req.StyleName, // 样式名称
                Description = req.Description, // 描述
                Screenshot = req.Screenshot // 截图
            };
            await Repository.AsSugarClient().Insertable(dmLayer).ExecuteCommandAsync();
        }

        return new Response<bool>
        {
            Result = true
        };
    }

    public async Task<Response<bool>> UploadShape(LayerReq req)
    {
        try
        {
        }
        catch (Exception ex)
        {
            throw new CommonException(ex.Message, 500);
        }

        return new Response<bool>
        {
            Result = true
        };
    }
    public Response<Dictionary<string, object>> TableDataByTableName(string tablename, string type, string keyword, string keyvalue, int page, int pagesize)
    {
        Dictionary<string, object> result = new Dictionary<string, object>();
        // string sql = $"select column_name as name from  INFORMATION_SCHEMA.COLUMNS WHERE  table_schema = 'public'   AND table_name   ='" + tablename + "'";
        string sqlcom = $"SELECT \r\n\r\n\r\n  c.column_name AS \"columnName\",   pgd.description AS \"comment\" \r\nFROM \r\n    information_schema.columns c\r\nLEFT JOIN \r\n    " +
            $"pg_catalog.pg_description pgd ON pgd.objoid = c.table_name::regclass::oid \r\n    AND pgd.objsubid = c.ordinal_position\r\nWHERE \r\n c.table_name ='" + tablename + "';";
        //var cloumnlist = _client.Ado.SqlQuery<string>(sql);
        var coment = _client.Ado.SqlQuery<dynamic>(sqlcom);
        //result.Add("head", cloumnlist);
        result.Add("headName", coment);
        string names = "";
        string geoms = "";
        for (int i = 0; i < coment.Count; i++)
        {
            var name = coment[i].columnName;
            if (name.Contains("geom"))
            {
                geoms = name;

            }
            else
            {

                names = names + "\"" + name + "\",";
            }
        }
        string sql1 = $"select " + names + "ST_AsText(" + geoms + ") as " + geoms + $" from " + tablename + " where 1=1 ";
        string sql2 = "";
        if (string.IsNullOrEmpty(keyword) || string.IsNullOrEmpty(keyvalue))
        {
            sql1 = sql1 + "limit " + pagesize + " offset " + (page - 1) * pagesize;
        }
        else
        {
            sql1 = sql1 + " and " + keyword + " like '%" + keyvalue + "%'" + "limit " + pagesize + " offset " + (page - 1) * pagesize;
            sql2 = " and " + keyword + " like '%" + keyvalue + "%'";
        }
        string sqlcount = $"select count(*) from " + tablename + " where 1=1 ";
        var count = _client.Ado.GetInt(sqlcount + sql2);
        var result1 = _client.Ado.SqlQuery<dynamic>(sql1);
        result.Add("data", result1);
        result.Add("total", count);
        return new Response<Dictionary<string, object>>
        {
            Result = result
        };
    }
    public Response<MemoryStream> TempeleteByTableName(string tablename, int type, string shpFilePath, string shpFilePathzip)
    {
        Response<MemoryStream> response = new Response<MemoryStream>();
        string sql = $"select column_name as name from  INFORMATION_SCHEMA.COLUMNS WHERE  table_schema = 'public'   AND table_name   ='" + tablename + "'";
        var cloumnlist = _client.Ado.SqlQuery<string>(sql);
        if (type == 0)
        {
            try
            {
                HSSFWorkbook workbook = new HSSFWorkbook();

                IFont font = workbook.CreateFont(); //创建一个字体样式对象
                font.FontName = "Microsoft YaHei"; //和excel里面的字体对应
                font.Boldweight = (short)FontBoldWeight.Bold; //字体加粗
                font.FontHeightInPoints = 12; //字体大小
                ICellStyle style1 = workbook.CreateCellStyle(); //创建样式对象
                style1.BorderBottom = BorderStyle.Thin;
                style1.BorderLeft = BorderStyle.Thin;
                style1.BorderRight = BorderStyle.Thin;
                style1.BorderTop = BorderStyle.Thin;
                style1.Alignment = HorizontalAlignment.Center;
                style1.VerticalAlignment = VerticalAlignment.Center;
                style1.SetFont(font); //将字体样式赋给样式对象 

                // 创建一个数值格式的CellStyle
                ICellStyle numericStyle = workbook.CreateCellStyle();

                // 创建一个数据格式对象
                IDataFormat dataFormat = workbook.CreateDataFormat();
                var font11 = workbook.CreateFont();
                font11.FontName = "Microsoft YaHei";
                font11.FontHeightInPoints = 12;
                // 设置单元格格式为数值型
                numericStyle.DataFormat = dataFormat.GetFormat("0.00");
                numericStyle.BorderBottom = BorderStyle.Thin;
                numericStyle.BorderLeft = BorderStyle.Thin;
                numericStyle.BorderRight = BorderStyle.Thin;
                numericStyle.BorderTop = BorderStyle.Thin;
                numericStyle.Alignment = HorizontalAlignment.Center;
                numericStyle.VerticalAlignment = VerticalAlignment.Center;
                numericStyle.SetFont(font11);
                ISheet sheet = workbook.CreateSheet("Sheet0");
                IRow rowHeader = sheet.CreateRow(0);
                rowHeader.Height = 20 * 30;
                for (int i = 0; i < cloumnlist.Count + 1; i++)
                {
                    if (i == cloumnlist.Count)
                    {
                        rowHeader.CreateCell(i);
                        rowHeader.Cells[i].CellStyle = style1;
                        rowHeader.Cells[i].SetCellValue("lng");
                    }
                    else
                    {
                        var header = cloumnlist[i];
                        if (header.Contains("geom"))
                        {
                            rowHeader.CreateCell(i);
                            rowHeader.Cells[i].CellStyle = style1;
                            rowHeader.Cells[i].SetCellValue("lat");
                        }
                        else
                        {
                            rowHeader.CreateCell(i);
                            rowHeader.Cells[i].CellStyle = style1;
                            rowHeader.Cells[i].SetCellValue(header);
                        }

                    }

                }
                response.Result = new MemoryStream();
                workbook.Write(response.Result);
                workbook = null;
                response.Result.Close();
                response.Result.Dispose();
                response.Code = 200;
                response.Message = "获取成功";

            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.Message;
            }
        }

        if (type == 1)
        {
            List<IFeature> features = new List<IFeature>();
            var attributes = new AttributesTable();
            string wkt = "POINT(117.968055555555 35.4761111111111)";
            var geometry = ParseGeometry(wkt);
            for (int i = 0; i < cloumnlist.Count; i++)
            {

                var header = cloumnlist[i];
                if (header.Contains("geom"))
                {
                    continue;
                }
                attributes.Add(header, 11);
            }

            IFeature feature = new Feature(geometry, attributes);
            features.Add(feature);
            // 导出 SHP 文件及其关联文件 
            ExportToShapefileFour(shpFilePath, features);
            // 将文件打包成 ZIP
            CreateZipFromShapefiles(shpFilePath, shpFilePathzip);
        }

        return response;
    }
    public Geometry ParseGeometry(string wkt)
    {
        if (string.IsNullOrEmpty(wkt))
        {
            return null;
        }

        var reader = new WKTReader();
        return reader.Read(wkt);
    }
    public void ExportToShapefileFour(string shpPath, List<IFeature> features)
    {
        //var geometryFactory = new GeometryFactory();

        //// 写入 CPG 文件（定义字符编码）
        //var cpgPath = System.IO.Path.ChangeExtension(shpPath, "cpg");
        //File.WriteAllText(cpgPath, "UTF-8", Encoding.UTF8);
        //// 获取 SHP 文件头
        //var header = ShapefileDataWriter.GetHeader(features[0], features.Count);

        //// 创建 ShapefileDataWriter
        //var shapeFileWriter = new ShapefileDataWriter(shpPath, geometryFactory)
        //{
        //    Header = header
        //};

        //// 写入 SHP 文件
        //shapeFileWriter.Write(features);

        // 写入 CPG 文件（定义字符编码）
        string prjStr =
            @"GEOGCS[""GCS_China_Geodetic_Coordinate_System_2000"",DATUM[""D_China_2000"",SPHEROID[""CGCS2000"",6378137.0,298.257222101]],PRIMEM[""Greenwich"",0.0],UNIT[""Degree"",0.0174532925199433]]";
        var cpgPath = Path.ChangeExtension(shpPath, "prj");
        File.WriteAllText(cpgPath, prjStr, Encoding.UTF8);

        Shapefile.WriteAllFeatures(features, shpPath, encoding: Encoding.UTF8);
    }

    public void CreateZipFromShapefiles(string shpPath, string zipPath)
    {
        var files = new List<string>
            {
                Path.ChangeExtension(shpPath, "cpg"),
                shpPath,
                Path.ChangeExtension(shpPath, "shx"),
                Path.ChangeExtension(shpPath, "dbf"),
                Path.ChangeExtension(shpPath, "prj")
            };

        using (var zipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
        {
            foreach (var file in files)
            {
                if (File.Exists(file))
                {
                    zipArchive.CreateEntryFromFile(file, Path.GetFileName(file));
                }
            }
        }
    }

    public async Task<bool> UploadExcel1(string tableName, IFormFile file)
    {
        if (file == null || file.Length == 0)
        {
            return false;
        }


        try
        {
            using (var stream = new MemoryStream())
            {
                // 将文件复制到内存流
                file.CopyTo(stream);
                stream.Position = 0;

                IWorkbook workbook;
                if (Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
                {
                    workbook = new XSSFWorkbook(stream); // 处理 .xlsx 文件
                }
                else
                {
                    workbook = new HSSFWorkbook(stream);
                }

                // 
                var sheet = workbook.GetSheetAt(0);
                // 获取总行数
                var rowCount = sheet.LastRowNum;
                if (rowCount < 1)
                {
                    throw new Exception("没有数据");
                }
                var currentRow1 = sheet.GetRow(0);
                var cols = new string[currentRow1.LastCellNum];
                for (int i = 0; i < currentRow1.LastCellNum; i++)
                {
                    var cell1 = currentRow1.GetCell(i);
                    cols[i] = cell1.ToString();
                }
                var result = new List<string[]>();
                List<Dictionary<String, Object>> listmap = new List<Dictionary<String, Object>>();
                // 遍历每一行
                var maxid = nextid(tableName, "Id");
                for (int row = 1; row <= rowCount; row++)
                {
                    maxid++;
                    var currentRow = sheet.GetRow(row);
                    if (currentRow == null) continue;
                    Dictionary<String, Object> map = new Dictionary<String, Object>();
                    string lat = null, lng = null;

                    for (int col = 0; col < currentRow.LastCellNum; col++)
                    {

                        var cell = currentRow.GetCell(col);
                        if (cell == null)
                        {
                            continue;
                        }
                        string head = cols[col];
                        var cellValue = cell.ToString();
                        if ("id".Equals(head.ToLower()))
                        {
                            map.Add(head, maxid);
                        }

                        switch (head)
                        {
                            case "lat":
                                lat = cellValue;
                                continue;
                            case "lng":
                                lng = cellValue;
                                continue;
                            case "Id":
                                continue;
                            case "id":
                                continue;
                            case "ID":
                                continue;
                            default:
                                map.Add(head, cellValue);
                                break;
                        }



                    }
                    // 遍历列



                    if (lat != null && lng != null)
                    {
                        var geography =
                            NtsGeometryServices.Instance.CreateGeometryFactory(
                                int.Parse("4326"));
                        var point = geography.CreatePoint(new Coordinate(double.Parse(lng), double.Parse(lat)));
                        map["geom"] = point.AsText();
                    }
                    listmap.Add(map);
                }
                _client.Insertable(listmap).AS(tableName).ExecuteCommand();
                await GeoUtil.CreateStoreAndLayer("", "EPSG:4326", tableName, "");
                // 返回结果
                return true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }

    }



    public bool UpdateTableData(UpdateTableReq req)
    {

        string tableName = req.TableName;
        string id = "";
        var list = req.list;
        string sql = "update " + tableName + " set ";
        string sql1 = "";
        string num = "";

        for (int i = 0; i < list.Count; i++)
        {
            var map = list[i];
            if ("id".Equals(map["name"].ToString().ToLower()))
            {
                num = map["name"].ToString();
                id = map["value"].ToString();
                continue;
            }
            if (map["name"].ToString().ToLower().Contains("time"))
            {
                continue;
            }
            if (map["value"] != null)
            {
                sql1 = sql1 + "\"" + map["name"] + "\" = '" + map["value"] + "',";
            }
        }
        var sql2 = sql1.Substring(0, sql1.Length - 1) + " where 1=1 and \"" + num + "\"=" + id;
        _client.Ado.ExecuteCommand(sql + sql2);
        return true;
    }
    public bool AddTableData(UpdateTableReq req)
    {
        string tableName = req.TableName;
        string id = "";
        var list = req.list;
        string sql = "insert into " + tableName + " ( ";
        string sql1 = "";
        string sql2 = "";
        string num = "";
        for (int j = 0; j < list.Count; j++)
        {
            var map = list[j];
            if ("id".Equals(map["name"].ToString().ToLower()))
            {
                id = map["name"].ToString();
            }
        }
        var maxid = nextid(req.TableName, id);

        for (int i = 0; i < list.Count; i++)
        {
            maxid++;
            var map = list[i];
            if ("id".Equals(map["name"].ToString().ToLower()))
            {
                sql1 = sql1 + "\"" + map["name"] + "\",";
                sql2 = sql2 + maxid + ",";
                continue;
            }
            if (map["name"].ToString().ToLower().Contains("time"))
            {
                continue;
            }
            if (map["value"] != null)
            {
                sql1 = sql1 + "\"" + map["name"] + "\",";
                sql2 = sql2 + "'" + map["value"] + "'" + ",";
            }
        }
        var sql3 = sql1.Substring(0, sql1.Length - 1) + ") values(" + sql2.Substring(0, sql2.Length - 1) + ")";
        _client.Ado.ExecuteCommand(sql + sql3);
        return true;
    }
    public bool UpdateTableData1(UpdateTableReq req)
    {

        string tableName = req.TableName;
        string id = "";
        var list = req.list;
        string sql = "update " + tableName + " set ";

        string num = "";
        for (int i = 0; i < list.Count; i++)
        {
            var map = list[i];
            string sql1 = "";
            foreach (KeyValuePair<string, object> kvp in map)
            {
                string key = kvp.Key;
                object value = kvp.Value;
                if ("id".Equals(key.ToString().ToLower()))
                {
                    num = key;
                    id = value.ToString();
                    continue;
                }
                if (key.ToLower().Contains("time"))
                {
                    continue;
                }
                if (value != null)
                {
                    sql1 = sql1 + "\"" + key + "\" = '" + value + "',";
                }
                // 处理键值对
            }
            var sql2 = sql1.Substring(0, sql1.Length - 1) + " where 1=1 and \"" + num + "\"=" + id;
            _client.Ado.ExecuteCommand(sql + sql2);
        }


        return true;
    }


    public async Task<Response<Dictionary<string, object>>> UploadExcel2(string tableName, IFormFile file)
    {
        if (file == null || file.Length == 0)
        {
            return null;
        }


        try
        {
            using (var stream = new MemoryStream())
            {

                List<dynamic> dynamics = new List<dynamic>();
                List<Dictionary<string, object>> dicts = new List<Dictionary<string, object>>();
                List<Dictionary<string, object>> errordicts = new List<Dictionary<string, object>>();
                // 将文件复制到内存流
                file.CopyTo(stream);
                stream.Position = 0;

                IWorkbook workbook;
                if (Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
                {
                    workbook = new XSSFWorkbook(stream); // 处理 .xlsx 文件
                }
                else
                {
                    workbook = new HSSFWorkbook(stream);
                }

                // 
                var sheet = workbook.GetSheetAt(0);
                // 获取总行数
                var rowCount = sheet.LastRowNum;
                if (rowCount < 1)
                {
                    throw new Exception("没有数据");
                }


                var currentRow1 = sheet.GetRow(0);
                var cols = new string[currentRow1.LastCellNum];
                string ids = "";
                for (int i = 0; i < currentRow1.LastCellNum; i++)
                {
                    var cell1 = currentRow1.GetCell(i);
                    cols[i] = cell1.ToString();
                    if (cell1.ToString().ToLower().Equals("gid"))
                    {
                        ids = cell1.ToString();
                    }
                }
                var result = new List<string[]>();
                List<Dictionary<String, Object>> listmap = new List<Dictionary<String, Object>>();
                // 遍历每一行
                var maxid = nextid(tableName, ids);
                var tablelist = tablestruc(tableName);
                for (int row = 1; row <= rowCount; row++)
                {
                    maxid++;
                    var currentRow = sheet.GetRow(row);
                    if (currentRow == null) continue;
                    Dictionary<String, Object> map = new Dictionary<String, Object>();
                    Dictionary<string, object> dict = new Dictionary<string, object>();
                    Dictionary<string, object> errorData = new Dictionary<string, object>();
                    // 读取列
                    var flag = false;
                    string lat = null, lng = null;
                    var flag1 = false;
                    for (int col = 0; col < currentRow.LastCellNum; col++)
                    {

                        var cell = currentRow.GetCell(col);
                        if (cell == null)
                        {
                            continue;
                        }
                        string head = cols[col];
                        var cellValue = cell.ToString();
                        for (int i = 0; i < tablelist.Count; i++)
                        {
                            string cloName = tablelist[i].ColumnName;
                            string cloType = tablelist[i].Type;
                            if (head.ToLower().Equals(cloName.ToLower()) && "gid".Equals(head.ToLower()))
                            {
                                dict.Add(head, cellValue);
                                errorData.Add(head, cellValue);
                                if (cloType.Contains("int"))
                                {
                                    map.Add(head, maxid);
                                }
                                if (cloType.Contains("character"))
                                {
                                    map.Add(head, Guid.NewGuid().ToString());
                                }
                                break;

                            }
                            if (head.Contains("geom"))
                            {
                                break;
                            }
                            if (head.Contains("time"))
                            {
                                break;
                            }
                            if (head.Equals(cloName))
                            {
                                var dictres = dealType(cloName, cloType, head, cellValue);
                                if ((bool)dictres["flag"])
                                {
                                    map.Add(head, dictres["data"]);
                                    dict.Add(head, cellValue);
                                    errorData.Add(head, cellValue);
                                }
                                else
                                {
                                    errorData.Add(head, "error" + cellValue);
                                    dict.Add(head, cellValue);
                                    flag1 = true;
                                }
                            }

                        }




                        switch (head)
                        {
                            case "lat":
                                lat = cellValue;

                                dict.Add(head, cellValue);
                                errorData.Add(head, cellValue);
                                continue;
                            case "lng":
                                lng = cellValue;

                                dict.Add(head, cellValue);
                                errorData.Add(head, cellValue);
                                continue;

                            default:

                                break;
                        }


                    }
                    if (flag1)
                    {
                        errordicts.Add(errorData);
                        continue;
                    }
                    // 遍历列
                    if (lat != null && lng != null)
                    {
                        var geography =
                            NtsGeometryServices.Instance.CreateGeometryFactory(
                                int.Parse("4326"));
                        var point = geography.CreatePoint(new Coordinate(double.Parse(lng), double.Parse(lat)));
                        map.Add("geom", point.AsText());

                    }
                    var geometryForWgs84 = map["geom"];

                    string sql = $"select column_name as name from  INFORMATION_SCHEMA.COLUMNS WHERE  table_schema = 'public'   AND table_name   ='" + tableName + "'";

                    var cloumnlist = _client.Ado.SqlQuery<string>(sql);

                    string names = "";
                    string geoms = "";
                    for (int l = 0; l < cloumnlist.Count; l++)
                    {
                        var name = cloumnlist[l];
                        if (name.Contains("geom"))
                        {
                            geoms = name;

                        }
                        else
                        {

                            names = names + "\"" + name + "\",";
                        }
                    }
                    string sql1 = $"select " + names + "ST_AsText(" + geoms + ") as " + geoms + $" from " + tableName + " where  geom='" + geometryForWgs84 + "'";

                    var obj = _client.Ado.SqlQuerySingle<dynamic>(sql1);
                    if (obj != null)
                    {
                        dynamics.Add(obj);
                        dict.Add("geom", geometryForWgs84);
                        flag = true;
                    }
                    if (flag)
                    {
                        dicts.Add(dict);
                        continue;
                    }
                    listmap.Add(map);
                }
                _client.Insertable(listmap).AS(tableName).ExecuteCommand();

                // 返回结果
                Dictionary<string, object> res = new Dictionary<string, object>();
                res.Add("nowData", dicts);
                res.Add("originData", dynamics);
                res.Add("errorData", errordicts);
                return new Response<Dictionary<string, object>>
                {
                    Result = res
                };
            }
        }
        catch (Exception ex)
        {
            return new Response<Dictionary<string, object>>
            {
                Result = null,
                Message = ex.Message,
                Code = 500
            };
        }


    }

    [HttpPost]
    public async Task<Response<Dictionary<string, object>>> AddUploadExcel(UploadExcelReq req)
    {

        if (req.file == null || req.file.Length == 0)
        {
            return null;
        }


        try
        {
            using (var stream = new MemoryStream())
            {

                string sq2 = "delete from " + req.tableName;
                _client.Ado.ExecuteCommand(sq2);
                // 将文件复制到内存流
                req.file.CopyTo(stream);
                stream.Position = 0;
                List<Dictionary<string, object>> errordicts = new List<Dictionary<string, object>>();
                IWorkbook workbook;
                if (Path.GetExtension(req.file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
                {
                    workbook = new XSSFWorkbook(stream); // 处理 .xlsx 文件
                }
                else
                {
                    workbook = new HSSFWorkbook(stream);
                }

                // 
                var sheet = workbook.GetSheetAt(0);
                // 获取总行数
                var rowCount = sheet.LastRowNum;
                if (rowCount < 1)
                {
                    throw new Exception("没有数据");
                }
                var currentRow1 = sheet.GetRow(0);
                var cols = new string[currentRow1.LastCellNum];
                string ids = "";
                for (int i = 0; i < currentRow1.LastCellNum; i++)
                {
                    var cell1 = currentRow1.GetCell(i);
                    cols[i] = cell1.ToString();
                    if (cell1.ToString().ToLower().Equals("gid"))
                    {
                        ids = cell1.ToString();
                    }
                }
                var result = new List<string[]>();
                List<Dictionary<String, Object>> listmap = new List<Dictionary<String, Object>>();
                // 遍历每一行
                var maxid = nextid(req.tableName, ids);
                var tablelist = tablestruc(req.tableName);
                // 遍历每一行
                for (int row = 1; row <= rowCount; row++)
                {
                    maxid++;
                    var currentRow = sheet.GetRow(row);
                    if (currentRow == null) continue;
                    Dictionary<String, Object> map = new Dictionary<String, Object>();
                    Dictionary<string, object> errorData = new Dictionary<string, object>();
                    string lat = null, lng = null;
                    var flag1 = false;
                    for (int col = 0; col < currentRow.LastCellNum; col++)
                    {

                        var cell = currentRow.GetCell(col);
                        if (cell == null)
                        {
                            continue;
                        }
                        string head = cols[col];
                        var cellValue = cell.ToString();
                        for (int i = 0; i < tablelist.Count; i++)
                        {
                            string cloName = tablelist[i].ColumnName;
                            string cloType = tablelist[i].Type;
                            if (head.ToLower().Equals(cloName.ToLower()) && "gid".Equals(head.ToLower()))
                            {

                                errorData.Add(head, cellValue);
                                if (cloType.Contains("int"))
                                {
                                    map.Add(head, maxid);
                                }
                                if (cloType.Contains("character"))
                                {
                                    map.Add(head, Guid.NewGuid().ToString());
                                }
                                break;

                            }
                            if (head.Contains("geom"))
                            {
                                break;
                            }
                            if (head.Contains("time"))
                            {
                                break;
                            }
                            if (head.Equals(cloName))
                            {
                                var dictres = dealType(cloName, cloType, head, cellValue);
                                if ((bool)dictres["flag"])
                                {
                                    map.Add(head, dictres["data"]);

                                    errorData.Add(head, cellValue);
                                }
                                else
                                {
                                    errorData.Add(head, "error" + cellValue);
                                    flag1 = true;
                                }
                            }

                        }




                        switch (head)
                        {
                            case "lat":
                                lat = cellValue;


                                errorData.Add(head, cellValue);
                                continue;
                            case "lng":
                                lng = cellValue;


                                errorData.Add(head, cellValue);
                                continue;

                            default:

                                break;
                        }


                    }
                    if (flag1)
                    {
                        errordicts.Add(errorData);
                        continue;
                    }


                    if (lat != null && lng != null)
                    {
                        var geography =
                            NtsGeometryServices.Instance.CreateGeometryFactory(
                                int.Parse("4326"));
                        var point = geography.CreatePoint(new Coordinate(double.Parse(lng), double.Parse(lat)));
                        map["geom"] = point.AsText();
                    }
                    listmap.Add(map);
                }
                _client.Insertable(listmap).AS(req.tableName).ExecuteCommand();
                //await GeoUtil.CreateStoreAndLayer("", "EPSG:4326", req.tableName, "");
                // 返回结果
                Dictionary<string, object> res = new Dictionary<string, object>();

                res.Add("errorData", errordicts);
                return new Response<Dictionary<string, object>>
                {
                    Result = res
                };
            }
        }
        catch (Exception ex)
        {
            return new Response<Dictionary<string, object>>
            {
                Result = null,
                Message = ex.Message,
                Code = 500
            };
        }

    }

    public async Task<Response<dynamic>> GetSldFilePath(string tablename)
    {
        Response<dynamic> response = new Response<dynamic>();
        string sql = $"select * from sldfile_record where name='" + tablename + "'";
        var filepath = _client.Ado.SqlQuerySingle<dynamic>(sql);
        response.Result = filepath;
        return response;
    }
    public async Task<Response<bool>> uploadSldStyle(string styleName, string tablename, string filepath, string fileid)
    {
        Response<bool> response = new Response<bool>();

        var sldpath = Path.Combine(_filePath, filepath);
        var flag = await GeoUtil.ExistStyle("", tablename);
        var flag1 = false;
        if (flag)
        {
            flag1 = await GeoUtil.UpdateSldStyle(filepath, tablename);
            await GeoUtil.UpdateStyle("", tablename, tablename);
        }

        else
        {
            flag1 = await GeoUtil.PublishSldStyle(sldpath, tablename);
            await GeoUtil.UpdateStyle("", tablename, tablename);
        }

        string sql = $"select count(*) from sldfile_record where name='" + tablename + "'";
        var count = _client.Ado.GetInt(sql);
        if (count > 0)
        {
            string updatesql = $"update sldfile_record set filepath='" + filepath + "',styleName='" + tablename + "',fileid='" + fileid + "' where name='" + tablename + "'";
            _client.Ado.ExecuteCommand(updatesql);
        }
        else
        {
            string id = Guid.NewGuid().ToString();
            string insertsql = $"insert into sldfile_record(id,name,filepath,stylename,fileid) values('" + id + "','" + tablename + "','" + filepath + "','" + tablename + "'," + fileid + "')";
            _client.Ado.ExecuteCommand(insertsql);
        }
        if (flag1)
        {
            response.Message = "发布成功";
            response.Result = flag1;
            return response;
        }
        else
        {
            response.Message = "发布失败";
            response.Result = false;
            response.Code = 500;
            return response;
        }
    }
    public async Task<Response<Dictionary<string, object>>> UploadShape2(string zipFilePath, string tableName
        )
    {
        try
        {
            string sq2 = "delete from " + tableName;
            _client.Ado.ExecuteCommand(sq2);
            string srd = "";
            using var db = Repository.AsSugarClient();
            // 开启事务

            // 取得文件完全路径
            zipFilePath = Path.Combine(_filePath, zipFilePath);
            // 确保ZIP文件存在
            if (!File.Exists(zipFilePath))
            {
                throw new FileNotFoundException("ZIP文件未找到。");
            }


            // 打开ZIP存档 取除后缀之前的字符串
            var extractPath = zipFilePath.Substring(0, zipFilePath.LastIndexOf(".", StringComparison.Ordinal)) +
                              "extract";
            // 解压文件
            await UnZip(zipFilePath, extractPath);
            var searchPattern = "*.shp"; // 设置你想要遍历的文件后缀名
            var fileName = Directory.GetFiles(extractPath, searchPattern, SearchOption.AllDirectories);
            if (fileName.IsEmpty())
            {
                throw new Exception("压缩文件中无shp文件");
            }
            List<Dictionary<string, object>> errordicts = new List<Dictionary<string, object>>();
            var shpFileName = fileName[0];
            var shpFile = Path.Combine(extractPath, shpFileName);
            using (var dataReader = new ShapefileDataReader(shpFile, GeometryFactory.Default))
            {

                var dbfReader = dataReader.DbaseHeader;
                List<string> headlist = new List<string>();

                string ids = "";
                foreach (var dbaseFieldDescriptor in dbfReader.Fields)
                {
                    headlist.Add(dbaseFieldDescriptor.Name);
                    if ("gid".Equals(dbaseFieldDescriptor.Name.ToLower()))
                    {
                        ids = dbaseFieldDescriptor.Name;
                    }
                }

                Console.WriteLine("记录数" + dataReader.RecordCount);
                //throw new Exception("失败");
                var objs = new List<Dictionary<string, object>>(1001);
                var num = 0;
                int count = 0;

                var maxid = nextid(tableName, ids);
                var tablelist = tablestruc(tableName);
                while (dataReader.Read())
                {
                    maxid++;
                    count++;
                    Console.WriteLine("记录行：" + count);
                    var map = new Dictionary<string, object>();
                    var errorData = new Dictionary<string, object>();
                    var flag1 = false;
                    // 读取列
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        var colName = dataReader.GetName(i);
                        if (dataReader.GetValue(i) == null)
                        {
                            map.Add(colName, dataReader.GetValue(i));
                            errorData.Add(colName, dataReader.GetValue(i));
                            continue;
                        }
                        for (int l = 0; l < tablelist.Count; l++)
                        {
                            string cloName = tablelist[l].ColumnName;
                            string cloType = tablelist[l].Type;
                            if (colName.ToLower().Equals(cloName.ToLower()) && "gid".Equals(colName.ToLower()))
                            {

                                errorData.Add(colName, dataReader.GetValue(i));
                                if (cloType.Contains("int"))
                                {
                                    map.Add(colName, maxid);
                                }
                                if (cloType.Contains("character"))
                                {
                                    map.Add(colName, Guid.NewGuid().ToString());
                                }
                                break;

                            }
                            if (colName.ToLower().Contains("time"))
                            {
                                break;
                            }
                            if (colName.ToLower().Equals("geometry"))
                            {
                                var geometry = (Geometry)dataReader.GetValue(i);

                                var geometryForWgs84 = GeometryFactory.Default.WithSRID(4326)
                                    .CreateGeometry(geometry);
                                map.Add("geom", geometryForWgs84.AsText());

                                errorData.Add("geom", geometryForWgs84.AsText());
                                break;
                            }
                            if (colName.ToLower().Equals(cloName.ToLower()))
                            {
                                var dictres = dealType(cloName, cloType, "", dataReader.GetValue(i).ToString());
                                if ((bool)dictres["flag"])
                                {
                                    map.Add(colName, dictres["data"]);

                                    errorData.Add(colName, dataReader.GetValue(i));
                                }
                                else
                                {

                                    errorData.Add(colName, dataReader.GetValue(i));
                                    flag1 = true;
                                }
                            }

                        }

                    }

                    objs.Add(map);
                    if (num++ == 999)
                    {
                        await db.Insertable(objs)
                            .AS(tableName) // 指定目标表名
                            .ExecuteCommandAsync();
                        num = 0;
                        objs.Clear();
                    }
                }

                // 读取末尾
                await db.Insertable(objs)
                    .AS(tableName) // 指定目标表名
                    .ExecuteCommandAsync();
            }

            await db.Ado.CommitTranAsync();
            Dictionary<string, object> res = new Dictionary<string, object>();
            res.Add("errorData", errordicts);
            return new Response<Dictionary<string, object>>
            {
                Result = res
            };

        }
        catch (Exception ex)
        {
            throw new CommonException(ex.Message, 500);
        }
    }
    public async Task<Response<bool>> UploadShape3(string zipFilePath, string tableName
   )
    {
        try
        {

            string srd = "";
            using var db = Repository.AsSugarClient();
            // 开启事务

            // 取得文件完全路径
            zipFilePath = Path.Combine(_filePath, zipFilePath);
            // 确保ZIP文件存在
            if (!File.Exists(zipFilePath))
            {
                throw new FileNotFoundException("ZIP文件未找到。");
            }


            // 打开ZIP存档 取除后缀之前的字符串
            var extractPath = zipFilePath.Substring(0, zipFilePath.LastIndexOf(".", StringComparison.Ordinal)) +
                              "extract";
            // 解压文件
            await UnZip(zipFilePath, extractPath);
            var searchPattern = "*.shp"; // 设置你想要遍历的文件后缀名
            var fileName = Directory.GetFiles(extractPath, searchPattern, SearchOption.AllDirectories);
            if (fileName.IsEmpty())
            {
                throw new Exception("压缩文件中无shp文件");
            }

            var shpFileName = fileName[0];
            var shpFile = Path.Combine(extractPath, shpFileName);
            using (var dataReader = new ShapefileDataReader(shpFile, GeometryFactory.Default))
            {

                var dbfReader = dataReader.DbaseHeader;
                List<string> headlist = new List<string>();
                foreach (var dbaseFieldDescriptor in dbfReader.Fields)
                {
                    headlist.Add(dbaseFieldDescriptor.Name);

                }

                Console.WriteLine("记录数" + dataReader.RecordCount);
                //throw new Exception("失败");
                var objs = new List<Dictionary<string, object>>(1001);
                var num = 0;
                int count = 0;

                var maxid = nextid(tableName, "Id");
                while (dataReader.Read())
                {
                    count++;
                    maxid++;
                    Console.WriteLine("记录行：" + count);
                    var insertObj = new Dictionary<string, object>();
                    // 读取列
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        var colName = dataReader.GetName(i);

                        for (int k = 0; k < headlist.Count; k++)
                        {
                            var header = headlist[k];
                            if (colName.ToLower().Contains("time"))
                            {
                                break;
                            }
                            if (colName.ToLower().Equals("geometry"))
                            {
                                var geometry = (Geometry)dataReader.GetValue(i);

                                var geometryForWgs84 = GeometryFactory.Default.WithSRID(4326)
                                    .CreateGeometry(geometry);
                                insertObj.Add("geom", geometryForWgs84.AsText());
                                break;
                            }
                            if (header.Equals(colName))
                            {

                                if (header.Equals("Id"))
                                {
                                    insertObj.Add(header, maxid);
                                    break;
                                }
                                else
                                {
                                    insertObj.Add(header, dataReader.GetValue(i));
                                    break;
                                }

                            }

                        }

                    }

                    objs.Add(insertObj);
                    if (num++ == 999)
                    {
                        await db.Insertable(objs)
                            .AS(tableName) // 指定目标表名
                            .ExecuteCommandAsync();
                        num = 0;
                        objs.Clear();
                    }
                }

                // 读取末尾
                await db.Insertable(objs)
                    .AS(tableName) // 指定目标表名
                    .ExecuteCommandAsync();
            }

            await db.Ado.CommitTranAsync();
            await GeoUtil.CreateStoreAndLayer("", "EPSG:4326", tableName, "");
        }
        catch (Exception ex)
        {
            throw new CommonException(ex.Message, 500);
        }

        return new Response<bool>
        {
            Result = true
        };
    }
    private static async Task UnZip(string zipFilePath, string extractPath)
    {
        await Task.Run(() =>
        {
            Directory.CreateDirectory(extractPath);
            // 设置字符编码
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            var gbk = Encoding.GetEncoding("utf-8");
            using (var archive = ZipFile.OpenRead(zipFilePath))
            {
                // 列举ZIP文件中的条目
                foreach (var entry in archive.Entries)
                {
                    var xxx = gbk.GetString(Encoding.Default.GetBytes(entry.FullName));
                    Console.WriteLine(xxx);
                }

                // 提取ZIP文件中的所有文件到指定目录

                foreach (var entry in archive.Entries)
                {
                    if (entry.Name != string.Empty)
                    {
                        // 确保完整路径存在  entry.FullName 是否可以编码
                        var fixedEntryName = entry.FullName.Replace("/", "");
                        var destinationPath = Path.GetFullPath(Path.Combine(extractPath, fixedEntryName));
                        Console.WriteLine("解压文件路径：" + destinationPath);
                        if (!destinationPath.StartsWith(Path.GetFullPath(extractPath) + Path.DirectorySeparatorChar))
                        {
                            throw new UnauthorizedAccessException("试图提取的文件超出了目标文件夹的路径边界。");
                        }

                        // 提取条目到目标路径
                        entry.ExtractToFile(destinationPath, overwrite: true);
                    }
                }
            }

            // 遍历解压目录，是否有shp后缀的文件
            if (!Directory.Exists(extractPath))
            {
                throw new Exception("文件解压失败");
            }
        });
    }
    public async Task<Response<Dictionary<string, object>>> UploadShape1(string zipFilePath, string tableName
       )
    {
        try
        {

            string srd = "";
            using var db = Repository.AsSugarClient();
            // 开启事务

            // 取得文件完全路径
            zipFilePath = Path.Combine(_filePath, zipFilePath);
            // 确保ZIP文件存在
            if (!File.Exists(zipFilePath))
            {
                throw new FileNotFoundException("ZIP文件未找到。");
            }


            // 打开ZIP存档 取除后缀之前的字符串
            var extractPath = zipFilePath.Substring(0, zipFilePath.LastIndexOf(".", StringComparison.Ordinal)) +
                              "extract";
            // 解压文件
            await UnZip(zipFilePath, extractPath);
            var searchPattern = "*.shp"; // 设置你想要遍历的文件后缀名
            var fileName = Directory.GetFiles(extractPath, searchPattern, SearchOption.AllDirectories);
            if (fileName.IsEmpty())
            {
                throw new Exception("压缩文件中无shp文件");
            }

            var shpFileName = fileName[0];
            var shpFile = Path.Combine(extractPath, shpFileName);
            List<dynamic> dynamics = new List<dynamic>();
            List<Dictionary<string, object>> dicts = new List<Dictionary<string, object>>();
            List<Dictionary<string, object>> errordicts = new List<Dictionary<string, object>>();
            using (var dataReader = new ShapefileDataReader(shpFile, GeometryFactory.Default))
            {

                var dbfReader = dataReader.DbaseHeader;
                List<string> headlist = new List<string>();
                string ids = "";
                foreach (var dbaseFieldDescriptor in dbfReader.Fields)
                {
                    headlist.Add(dbaseFieldDescriptor.Name);
                    if ("gid".Equals(dbaseFieldDescriptor.Name.ToLower()))
                    {
                        ids = dbaseFieldDescriptor.Name;
                    }
                }

                Console.WriteLine("记录数" + dataReader.RecordCount);
                //throw new Exception("失败");
                var objs = new List<Dictionary<string, object>>(1001);
                var num = 0;
                int count = 0;

                var maxid = nextid(tableName, ids);
                var tablelist = tablestruc(tableName);
                while (dataReader.Read())
                {
                    count++;
                    maxid++;
                    Console.WriteLine("记录行：" + count);
                    var map = new Dictionary<string, object>();
                    var errorData = new Dictionary<string, object>();
                    var flag1 = false;
                    // 读取列
                    Dictionary<string, object> dict = new Dictionary<string, object>();
                    // 读取列
                    var flag = false;
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        var colName = dataReader.GetName(i);
                        if (dataReader.GetValue(i) == null)
                        {
                            map.Add(colName, dataReader.GetValue(i));
                            errorData.Add(colName, dataReader.GetValue(i));
                            continue;
                        }

                        for (int l = 0; l < tablelist.Count; l++)
                        {
                            string cloName = tablelist[l].ColumnName;
                            string cloType = tablelist[l].Type;
                            if (colName.ToLower().Equals(colName.ToLower()) && "gid".Equals(colName.ToLower()))
                            {

                                errorData.Add(colName, dataReader.GetValue(i));
                                if (cloType.Contains("int"))
                                {
                                    map.Add(colName, maxid);
                                }
                                if (cloType.Contains("character"))
                                {
                                    map.Add(colName, Guid.NewGuid().ToString());
                                }
                                break;

                            }
                            if (colName.ToLower().Contains("time"))
                            {
                                break;
                            }
                            if (colName.ToLower().Equals("geometry"))
                            {
                                var geometry = (Geometry)dataReader.GetValue(i);

                                var geometryForWgs84 = GeometryFactory.Default.WithSRID(4326)
                                    .CreateGeometry(geometry);
                                map.Add("geom", geometryForWgs84.AsText());

                                errorData.Add("geom", geometryForWgs84.AsText());
                                break;
                            }
                            if (colName.ToLower().Equals(colName.ToLower()))
                            {
                                var dictres = dealType(cloName, cloType, "", dataReader.GetValue(i).ToString());
                                if ((bool)dictres["flag"])
                                {
                                    map.Add(colName, dictres["data"]);

                                    errorData.Add(colName, dataReader.GetValue(i));
                                }
                                else
                                {

                                    errorData.Add(colName, dataReader.GetValue(i));
                                    flag1 = true;
                                }
                            }

                        }
                        if (colName.ToLower().Equals("geometry"))
                        {
                            var geometry = (Geometry)dataReader.GetValue(i);

                            var geometryForWgs84 = GeometryFactory.Default.WithSRID(4326)
                                .CreateGeometry(geometry);
                            string sql = $"select column_name as name from  INFORMATION_SCHEMA.COLUMNS WHERE  table_schema = 'public'   AND table_name   ='" + tableName + "'";

                            var cloumnlist = _client.Ado.SqlQuery<string>(sql);

                            string names = "";
                            string geoms = "";
                            for (int l = 0; l < cloumnlist.Count; l++)
                            {
                                var name = cloumnlist[l];
                                if (name.Contains("geom"))
                                {
                                    geoms = name;

                                }
                                else
                                {

                                    names = names + "\"" + name + "\",";
                                }
                            }
                            string sql1 = $"select " + names + "ST_AsText(" + geoms + ") as " + geoms + $" from " + tableName + " where  geom='" + geometryForWgs84.AsText() + "'";

                            var obj = _client.Ado.SqlQuerySingle<dynamic>(sql1);
                            if (obj != null)
                            {
                                dynamics.Add(obj);
                                dict.Add("geom", geometryForWgs84.AsText());
                                flag = true;
                            }

                        }
                        else
                        {
                            dict.Add(colName, dataReader.GetValue(i));
                        }


                    }
                    if (flag1)
                    {
                        errordicts.Add(errorData);
                        continue;
                    }
                    if (flag)
                    {
                        dicts.Add(dict);
                        continue;
                    }
                    objs.Add(map);
                    if (num++ == 999)
                    {
                        await db.Insertable(objs)
                            .AS(tableName) // 指定目标表名
                            .ExecuteCommandAsync();
                        num = 0;
                        objs.Clear();
                    }
                }

                // 读取末尾
                await db.Insertable(objs)
                    .AS(tableName) // 指定目标表名
                    .ExecuteCommandAsync();

            }

            await db.Ado.CommitTranAsync();
            Dictionary<string, object> res = new Dictionary<string, object>();
            res.Add("nowData", dicts);
            res.Add("originData", dynamics);
            res.Add("errorData", errordicts);
            return new Response<Dictionary<string, object>>
            {
                Result = res
            };

        }
        catch (Exception ex)
        {
            throw new CommonException(ex.Message, 500);
        }


    }
    public int nextid(string tablename, string id)
    {
        string sql = "select max(\"" + id + "\") from " + tablename;
        var maxid = _client.Ado.GetInt(sql);
        return maxid;
    }
    public List<dynamic> tablestruc(string tableName)
    {
        string tableStruc = $"\t\t\r\nSELECT \r\n    c.column_name AS \"ColumnName\",\r\n    c.data_type AS \"Type\"\r\n\r\nFROM \r\n  " +
                   $"  information_schema.columns c\r\nLEFT JOIN \r\n    pg_catalog.pg_description pgd ON pgd.objoid = c.table_name::regclass::oid \r\n " +
                   $"   AND pgd.objsubid = c.ordinal_position\r\nWHERE \r\n c.table_name = '" + tableName + "'; ";
        return _client.Ado.SqlQuery<dynamic>(tableStruc);
    }
    public Dictionary<string, object> dealType(string cloum, string type, string head, string data)
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        string str = "";
        bool flag = false;
        double d = 0.0;
        float f = 0.0f;
        int i = 0;
        Object obj = null;
        switch (type)
        {
            case "real":
                flag = float.TryParse(data, out f);
                obj = f;
                break;
            case "integer":
                flag = Int32.TryParse(data, out i);
                obj = i;
                break;

            case "numeric":
                flag = double.TryParse(data, out d);
                obj = d;
                break;
            case "bigint":
                flag = Int32.TryParse(data, out i);
                obj = i;
                break;
            case "double precision":
                flag = double.TryParse(data, out d);
                obj = d;
                break;
            case "character varying":
                flag = true;
                obj = data;
                break;
            case "date":
                flag = true;
                obj = new DateTime();
                break;
            case "timestamp without time zone":
                flag = true;
                obj = new Timestamp();
                break;

            default:
                flag = false;
                break;
        }
        dict.Add("flag", flag);
        dict.Add("data", obj);
        return dict;
    }
}