森林草原湿地荒漠调查
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

520 lines
23 KiB

using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using KGIS.Framework.AE;
using KGIS.Framework.Commands;
using KGIS.Framework.DBOperator;
using KGIS.Framework.Maps;
using KGIS.Framework.Platform;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using KGIS.Framework.Views;
using Kingo.PluginServiceInterface;
using KUI.Windows;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using UIShell.OSGi;
namespace Kingo.Plugin.MapView.Commands
{
/// <summary>
/// 同步模板数据
/// </summary>
public class CmdCopyFileData : BaseMenuCommand
{
private IEngineEditor m_Editor;
public IHookHelper m_hookHelper { get; set; }
private ProjectInfo projectInfo = null;
public override void OnClick()
{
projectInfo = MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo;
CopyFileData();
}
public override void OnCreate(object Hook)
{
try
{
if (m_hookHelper == null)
{
m_hookHelper = new HookHelper
{
Hook = Hook
};
}
if (m_Editor == null)
m_Editor = new EngineEditorClass();
}
catch (Exception ex)
{
LogAPI.Debug("初始化 同步模板数据 时异常,异常信息如下:");
LogAPI.Debug(ex);
LogAPI.Debug("初始化 同步模板数据 时异常信息结束");
}
}
public override bool Enabled
{
get
{
//验证是否打开工程
object ProInfo = KGIS.Framework.Maps.MapsManager.Instance.MapService.GetProjectInfo();
if (ProInfo == null)
return false;
else
return true;
}
}
private void CopyFileData()
{
try
{
this.ShowLoading("正在进行同步模板数据.......", 0, 0);
//GDB 1、BGDB.gdb 2、NMDB.gdb 3、Scheme0.gdb 4、Scheme-YCL.gdb 5、ZLDB.gdb
CopyGDB("BGDB");
CopyGDB("ZLDB");
CopyGDB("NMDB");
CopyGDB("Scheme0");
CopyGDB("Scheme-YCL");
//BGTJ.sqlite 里面的表格
CopySqlite();
//变更成果模板
CopyBGCG();
//dic.mdb ReportData.db 变更成果检查.xls 检查数据.xls 土地变更一览表.xls
CopyFile();
//DataCheckrResult.db
CopyDataCheckrResultDB();
//QualityCheckResult.db
CopyQualityCheckResultDB();
this.CloseLoading();
MessageHelper.ShowTips("同步模板数据完成!");
}
catch (Exception ex)
{
this.CloseLoading();
LogAPI.Debug("同步工程模板数据异常:" + ex.Message);
LogAPI.Debug("同步工程模板数据异常:" + ex.StackTrace);
MessageHelper.ShowTips("同步工程模板数据异常:" + ex.Message);
}
}
private void CopyGDB(string GDBFileName)
{
IWorkspaceAPI templatewsAPI = null;
IWorkspaceAPI projectwsAPI = null;
try
{
string templateBGDBPath = SysAppPath.GetCurrentAppPath() + string.Format("工作空间\\模板\\新建变更工程\\{0}\\{1}.gdb", (int)Math.Floor(Math.Round(10 / projectInfo.XYResolution)), GDBFileName);
if (GDBFileName.Contains("Scheme0") || GDBFileName.Contains("Scheme-YCL"))
templateBGDBPath = SysAppPath.GetCurrentAppPath() + string.Format("工作空间DTBJK\\DatabaseTemplate\\{0}.gdb", GDBFileName);
string projectBGDBPath = Path.Combine(projectInfo.ProjDir, $"{GDBFileName}.gdb");
//先判定工程中GDB是否有数据
//若没有数据 则直接copy
//若有数据 则遍历模板GDB中字段 再遍历工程中GDB字段 判断是否一样
if (!Directory.Exists(templateBGDBPath)) return;
templatewsAPI = new WorkspaceAPI(templateBGDBPath, KGIS.Framework.AE.Enum.WorkspaceTypeEnum.GDBFile, true);
List<IFeatureClass> templatefeatureClasses = templatewsAPI.GetAllFeatureClass(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
if (templatefeatureClasses != null)
{
if (!Directory.Exists(projectBGDBPath))
{
PluginServiceInterface.CommonHelper.DirectoryCopy(templateBGDBPath, projectInfo.ProjDir);
return;
}
projectwsAPI = new WorkspaceAPI(projectBGDBPath, KGIS.Framework.AE.Enum.WorkspaceTypeEnum.GDBFile, true);
List<IFeatureClass> projectfeatureClasses = projectwsAPI.GetAllFeatureClass(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
if (projectfeatureClasses != null)
{
bool isEmpty = true;
foreach (IFeatureClass projectfeatureClass in projectfeatureClasses)
{
if (projectfeatureClass.FeatureCount(null) > 0)
{
isEmpty = false;
break;
}
}
if (isEmpty)
{
PluginServiceInterface.CommonHelper.DirectoryCopy(templateBGDBPath, projectInfo.ProjDir);
}
else
{
//判断增删IFeatureClass
foreach (IFeatureClass projectfeatureClass in projectfeatureClasses)
{
if (templatefeatureClasses.FirstOrDefault(a => (a as FeatureClass).BrowseName == (projectfeatureClass as FeatureClass).BrowseName) == null)
{
//需删除
projectwsAPI.DeleteFeatureClass((projectfeatureClass as FeatureClass).BrowseName);
projectfeatureClasses.Remove(projectfeatureClass);
}
}
foreach (IFeatureClass templatefeatureClass in templatefeatureClasses)
{
if (projectfeatureClasses.FirstOrDefault(a => (a as FeatureClass).BrowseName == (templatefeatureClass as FeatureClass).BrowseName) == null)
{
//需添加
IFeatureClass newFeatureClass = projectwsAPI.CreateFeatureClass((templatefeatureClass as FeatureClass).BrowseName, projectfeatureClasses[0].FeatureDataset, (projectfeatureClasses[0] as IGeoDataset).SpatialReference, ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon, templatefeatureClass.Fields).FeatureClass;
Marshal.ReleaseComObject(newFeatureClass);
}
}
//判断每一个IFeatureClass中的字段
foreach (IFeatureClass projectfeatureClass in projectfeatureClasses)
{
string featureName = (projectfeatureClass as FeatureClass).BrowseName;
IFeatureClass currentTempFeatureClass = templatefeatureClasses.FirstOrDefault(a => (a as FeatureClass).BrowseName == featureName);
if (currentTempFeatureClass == null) currentTempFeatureClass = templatefeatureClasses.FirstOrDefault(a => a.AliasName == projectfeatureClass.AliasName);
for (int i = 0; i < currentTempFeatureClass.Fields.FieldCount; i++)
{
IField field = currentTempFeatureClass.Fields.get_Field(i);
if (projectfeatureClass.FindField(field.Name) == -1)
{
projectfeatureClass.AddField(field);
}
}
for (int i = projectfeatureClass.Fields.FieldCount - 1; i >= 0; i--)
{
IField field = projectfeatureClass.Fields.get_Field(i);
if (currentTempFeatureClass.FindField(field.Name) == -1)
{
projectfeatureClass.DeleteField(field);
}
}
}
}
}
}
}
catch (Exception ex)
{
LogAPI.Debug("同步工程GDB模板数据异常:" + ex.Message);
LogAPI.Debug("同步工程GDB模板数据异常:" + ex.StackTrace);
}
finally
{
if (templatewsAPI != null)
templatewsAPI.CloseWorkspace();
if (projectwsAPI != null)
projectwsAPI.CloseWorkspace();
}
}
private void CopyTable(string sourceDatabase, string targetDatabase, string tableName)
{
SQLiteConnection sourceConnection = new SQLiteConnection($"Data Source={sourceDatabase}");
SQLiteConnection targetConnection = new SQLiteConnection($"Data Source={targetDatabase}");
try
{
// 打开源数据库连接
sourceConnection.Open();
// 打开目标数据库连接
targetConnection.Open();
// 创建SELECT语句查询源表的数据
string selectQuery = $"SELECT * FROM {tableName}";
SQLiteCommand selectCommand = new SQLiteCommand(selectQuery, sourceConnection);
// 执行SELECT语句并读取数据
SQLiteDataReader reader = selectCommand.ExecuteReader();
// 获取源表的列名
List<string> columnNames = new List<string>();
for (int i = 0; i < reader.FieldCount; i++)
{
columnNames.Add(reader.GetName(i));
}
// 构建INSERT语句和参数
StringBuilder insertQuery = new StringBuilder();
insertQuery.Append($"INSERT INTO {tableName} (");
StringBuilder columninsertQuery = new StringBuilder();
foreach (string columnName in columnNames)
{
columninsertQuery.Append($"{columnName}, ");
}
columninsertQuery.Length -= 2; // 移除最后一个逗号和空格
insertQuery.Append(columninsertQuery);
insertQuery.Append(") VALUES (");
foreach (string columnName in columnNames)
{
insertQuery.Append($"@{columnName}, ");
}
insertQuery.Length -= 2; // 移除最后一个逗号和空格
insertQuery.Append(")");
SQLiteCommand insertCommand = new SQLiteCommand(insertQuery.ToString(), targetConnection);
//insertCommand.ExecuteNonQuery();
// 根据源表的列名设置参数
foreach (string columnName in columnNames)
{
insertCommand.Parameters.AddWithValue($"@{columnName}", null);
}
// 创建CREATE TABLE语句查询源表的结构
string createTableQuery = "CREATE TABLE if not exists " + tableName + "(" + columninsertQuery + ");";
SQLiteCommand createTableCommand = new SQLiteCommand(createTableQuery, targetConnection);
// 执行CREATE TABLE语句
createTableCommand.ExecuteNonQuery();
// 遍历源表数据并插入到目标表
while (reader.Read())
{
foreach (string columnName in columnNames)
{
insertCommand.Parameters[$"@{columnName}"].Value = reader[columnName];
}
int aaa = insertCommand.ExecuteNonQuery();
}
// 关闭数据读取器和连接对象
reader.Close();
sourceConnection.Close();
targetConnection.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void CopySqlite()
{
IRDBHelper rdbHelper = null;
List<string> tableNames = new List<string>() { "AttributeCheckRule", "CheckingRule", "Sys_DicDetail", "Sys_DicManage" };
try
{
string templateFilePath = SysAppPath.GetCurrentAppPath() + "工作空间\\模板\\新建变更工程\\BGTJ.sqlite";
string projectFilePath = projectInfo.ProjDir + "\\BGTJ.sqlite";
if (File.Exists(templateFilePath))
{
if (File.Exists(projectFilePath))
{
rdbHelper = RDBFactory.CreateDbHelper(projectFilePath, DatabaseType.SQLite);
foreach (string tableName in tableNames)
{
if (rdbHelper.TableIsExist(tableName))
rdbHelper.ExecuteSQL($" drop TABLE {tableName} ");
CopyTable(templateFilePath, projectFilePath, tableName);
}
}
else
File.Copy(templateFilePath, projectFilePath, true);
}
}
catch (Exception ex)
{
LogAPI.Debug("同步工程模板CopySqlite异常:" + ex.Message);
LogAPI.Debug("同步工程模板CopySqlite异常:" + ex.StackTrace);
}
finally
{
if (rdbHelper != null && rdbHelper.Connect())
rdbHelper.DisConnect();
}
}
private void CopyBGCG()
{
try
{
string templateFilePath = SysAppPath.GetCurrentAppPath() + "工作空间\\模板\\变更成果模板";
string projectFilePath = projectInfo.ProjDir + "\\变更成果模板";
if (Directory.Exists(templateFilePath))
{
PluginServiceInterface.CommonHelper.DirectoryCopy(templateFilePath, projectFilePath);
}
}
catch (Exception ex)
{
LogAPI.Debug("同步工程模板CopyBGCG异常:" + ex.Message);
LogAPI.Debug("同步工程模板CopyBGCG异常:" + ex.StackTrace);
}
}
private void CopyFile()
{
string templateFilePath = string.Empty;
string projectFilePath = string.Empty;
try
{
List<string> fileNames = new List<string>() { "dic.mdb", "ReportData.db", "变更成果检查.xls", "检查数据.xls", "土地变更一览表.xls" };
foreach (string fileName in fileNames)
{
templateFilePath = SysAppPath.GetCurrentAppPath() + $"工作空间\\模板\\新建变更工程\\{fileName}";
projectFilePath = projectInfo.ProjDir + $"\\{fileName}";
if (!File.Exists(templateFilePath)) continue;
File.Copy(templateFilePath, projectFilePath, true);
}
}
catch (Exception ex)
{
LogAPI.Debug("同步工程模板CopyFile异常:" + ex.Message);
LogAPI.Debug("同步工程模板CopyFile异常:" + ex.StackTrace);
}
}
private void CopyDataCheckrResultDB()
{
string templateFilePath = string.Empty;
string projectFilePath = string.Empty;
IRDBHelper rdbHelper = null;
IRDBHelper rdbHelperTemplate = null;
try
{
List<string> tableNames = new List<string>() { "DelectFJ", "ErrorInfo" };
templateFilePath = SysAppPath.GetCurrentAppPath() + "工作空间\\模板\\新建变更工程\\DataCheckrResult.db";
projectFilePath = projectInfo.ProjDir + "\\DataCheckrResult.db";
if (File.Exists(templateFilePath))
{
if (File.Exists(projectFilePath))
{
rdbHelperTemplate = RDBFactory.CreateDbHelper(templateFilePath, DatabaseType.SQLite);
rdbHelper = RDBFactory.CreateDbHelper(projectFilePath, DatabaseType.SQLite);
foreach (string tableName in tableNames)
{
if (!rdbHelper.TableIsExist(tableName))
{
CopyTable(templateFilePath, projectFilePath, tableName);
continue;
}
DataTable templatedataTable = rdbHelperTemplate.ExecuteDatatable(tableName + "Temp", $"select * from {tableName}", true);
DataTable dataTable = rdbHelper.ExecuteDatatable(tableName, $"select * from {tableName}", true);
if (dataTable != null)
{
for (int i = 0; i < templatedataTable.Columns.Count; i++)
{
if (!dataTable.Columns.Contains(templatedataTable.Columns[i].ColumnName))
{
dataTable.Columns.Add(templatedataTable.Columns[i]);
}
}
for (int i = dataTable.Columns.Count - 1; i >= 0; i--)
{
if (!templatedataTable.Columns.Contains(dataTable.Columns[i].ColumnName))
{
dataTable.Columns.Remove(dataTable.Columns[i]);
}
}
}
else
CopyTable(templateFilePath, projectFilePath, tableName);
}
}
else
File.Copy(templateFilePath, projectFilePath, true);
}
}
catch (Exception ex)
{
LogAPI.Debug("同步工程模板CopyDataCheckrResultDB异常:" + ex.Message);
LogAPI.Debug("同步工程模板CopyDataCheckrResultDB异常:" + ex.StackTrace);
}
finally
{
if (rdbHelper != null && rdbHelper.Connect())
rdbHelper.DisConnect();
if (rdbHelperTemplate != null && rdbHelperTemplate.Connect())
rdbHelperTemplate.DisConnect();
}
}
private void CopyQualityCheckResultDB()
{
string templateFilePath = string.Empty;
string projectFilePath = string.Empty;
IRDBHelper rdbHelper = null;
IRDBHelper rdbHelperTemplate = null;
try
{
List<string> tableNames = new List<string>() { "ErrorBGYLB", "ErrorBGYLBSM", "ErrorBasic", "ErrorReport", "ErrorTC" };
templateFilePath = SysAppPath.GetCurrentAppPath() + "工作空间\\模板\\新建变更工程\\QualityCheckResult.db";
projectFilePath = projectInfo.ProjDir + "\\QualityCheckResult.db";
if (File.Exists(templateFilePath))
{
if (File.Exists(projectFilePath))
{
rdbHelperTemplate = RDBFactory.CreateDbHelper(templateFilePath, DatabaseType.SQLite);
rdbHelper = RDBFactory.CreateDbHelper(projectFilePath, DatabaseType.SQLite);
foreach (string tableName in tableNames)
{
if (!rdbHelper.TableIsExist(tableName))
{
CopyTable(templateFilePath, projectFilePath, tableName);
continue;
}
DataTable templatedataTable = rdbHelperTemplate.ExecuteDatatable(tableName + "Temp", $"select * from {tableName}", true);
DataTable dataTable = rdbHelper.ExecuteDatatable(tableName, $"select * from {tableName}", true);
if (dataTable != null)
{
for (int i = 0; i < templatedataTable.Columns.Count; i++)
{
if (!dataTable.Columns.Contains(templatedataTable.Columns[i].ColumnName))
{
dataTable.Columns.Add(templatedataTable.Columns[i]);
}
}
for (int i = dataTable.Columns.Count - 1; i >= 0; i--)
{
if (!templatedataTable.Columns.Contains(dataTable.Columns[i].ColumnName))
{
dataTable.Columns.Remove(dataTable.Columns[i]);
}
}
}
else
CopyTable(templateFilePath, projectFilePath, tableName);
}
}
else
File.Copy(templateFilePath, projectFilePath, true);
}
}
catch (Exception ex)
{
LogAPI.Debug("同步工程模板CopyQualityCheckResultDB异常:" + ex.Message);
LogAPI.Debug("同步工程模板CopyQualityCheckResultDB异常:" + ex.StackTrace);
}
finally
{
if (rdbHelper != null && rdbHelper.Connect())
rdbHelper.DisConnect();
if (rdbHelperTemplate != null && rdbHelperTemplate.Connect())
rdbHelperTemplate.DisConnect();
}
}
}
}