449 lines
17 KiB
C#
449 lines
17 KiB
C#
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Runtime.Serialization.Formatters.Binary;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Web.Script.Serialization;
|
||
using System.Windows.Forms;
|
||
|
||
namespace DTTest
|
||
{
|
||
public partial class E { }
|
||
public partial class ModelManagement
|
||
{
|
||
|
||
/// <summary>
|
||
/// 导出项目数据
|
||
/// 2020.10.29
|
||
/// </summary>
|
||
public void ExportProjectData_FixedTable_Dialog(bool isLightweight)
|
||
{
|
||
var exportOpenFileDialog = new SaveFileDialog();
|
||
//设置文件类型
|
||
exportOpenFileDialog.Filter = "JavaScript Object Notation(*.json)|*.json";
|
||
//exportOpenFileDialog.Filter = "JavaScript Object Notation(*.json)|*.json|Microsoft Excel(*.xls)|*.xls|Data File(*.dat)|*.dat";
|
||
//设置默认文件类型显示顺序
|
||
exportOpenFileDialog.FilterIndex = 1;
|
||
//保存对话框是否记忆上次打开的目录
|
||
exportOpenFileDialog.RestoreDirectory = true;
|
||
|
||
var exportDialogResult = exportOpenFileDialog.ShowDialog();
|
||
|
||
//点了保存按钮进入
|
||
if (exportDialogResult == DialogResult.OK)
|
||
{
|
||
var localFilePath = exportOpenFileDialog.FileName;//获得文件路径
|
||
var fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径
|
||
var fileExtension = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1); //获取文件扩展名
|
||
|
||
//获取文件路径,不带文件名
|
||
//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
|
||
|
||
//给文件名前加上时间
|
||
//newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;
|
||
|
||
//在文件名里加字符
|
||
//saveFileDialog1.FileName.Insert(1,"dameng");
|
||
|
||
//System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件
|
||
var fileExtensionToLower = fileExtension.ToLower();
|
||
|
||
ExportProjectData_FixedTable(localFilePath, fileExtensionToLower, isLightweight);
|
||
|
||
|
||
}
|
||
//else
|
||
//{
|
||
// MessageBox.Show("您已取消了导出数据操作!");
|
||
//}
|
||
|
||
}
|
||
|
||
public void ExportProjectData_FixedTable_Dialog(string projectName)
|
||
{
|
||
var exportOpenFileDialog = new SaveFileDialog();
|
||
//设置文件类型
|
||
exportOpenFileDialog.Filter = "导出文件(*.bak)|*.bak";
|
||
//exportOpenFileDialog.Filter = "JavaScript Object Notation(*.json)|*.json|Microsoft Excel(*.xls)|*.xls|Data File(*.dat)|*.dat";
|
||
//设置默认文件类型显示顺序
|
||
exportOpenFileDialog.FilterIndex = 1;
|
||
//保存对话框是否记忆上次打开的目录
|
||
exportOpenFileDialog.RestoreDirectory = true;
|
||
|
||
var exportDialogResult = exportOpenFileDialog.ShowDialog();
|
||
|
||
//点了保存按钮进入
|
||
if (exportDialogResult == DialogResult.OK)
|
||
{
|
||
var localFilePath = exportOpenFileDialog.FileName;//获得文件路径
|
||
//var fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径
|
||
//var fileExtension = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1); //获取文件扩展名
|
||
|
||
//获取文件路径,不带文件名
|
||
//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
|
||
|
||
//给文件名前加上时间
|
||
//newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;
|
||
|
||
//在文件名里加字符
|
||
//saveFileDialog1.FileName.Insert(1,"dameng");
|
||
|
||
//System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件
|
||
//var fileExtensionToLower = fileExtension.ToLower();
|
||
System.Threading.ThreadPool.QueueUserWorkItem((object obj) => {
|
||
ExportProjectData(localFilePath, projectName);
|
||
});
|
||
|
||
|
||
}
|
||
//else
|
||
//{
|
||
// MessageBox.Show("您已取消了导出数据操作!");
|
||
//}
|
||
|
||
}
|
||
|
||
public void ExportProjectData(string localFilePath,string projectName)
|
||
{
|
||
string projectDbDir = PP.currExecPath + "\\models\\GLTFmodel\\" + projectName;
|
||
string projectDbPath = projectDbDir + "\\db.json";
|
||
//1导出项目数据
|
||
ExportProjectData_FixedTable(projectDbPath, "json", false);
|
||
|
||
// Zip.ZipFile(projectDbDir, localFilePath, out string err);
|
||
|
||
}
|
||
|
||
public void ImportProjectData(string localFilePath, string projectName)
|
||
{
|
||
|
||
string projectDbDir = PP.currExecPath + "\\models\\GLTFmodel\\" + projectName;
|
||
string projectDbPath = projectDbDir + "\\db.json";
|
||
// Zip.UnZipFile(localFilePath, projectDbDir, out string err);
|
||
ImportProjectData_FixedTable(projectDbPath, projectName);
|
||
//1导出项目数据
|
||
//ExportProjectData_FixedTable(projectDbPath, "json", false);
|
||
|
||
|
||
}
|
||
/// <summary>
|
||
/// 导出项目数据
|
||
/// 2020.10.29
|
||
/// 2021.3.12
|
||
/// </summary>
|
||
public void ExportProjectData_FixedTable(string localFilePath, string fileExtensionToLower, bool isLightweight)
|
||
{
|
||
var outDataSet = new DataSet();
|
||
var tableDataList = new List<String>();
|
||
|
||
var dt_TableList = SqlOperation.TransferProject.Select_DB_Tables();
|
||
|
||
// PP.iGlobalVar2.ShowMainProgressSet("导出项目数据进度", dt_TableList.Rows.Count - 1);
|
||
|
||
for (int i = 0; i < dt_TableList.Rows.Count; i++)
|
||
{
|
||
var tableName = dt_TableList.Rows[i]["name"].ToString();
|
||
DataTable dt = null;
|
||
try
|
||
{
|
||
dt = SqlOperation.TransferProject.Select_Table(tableName);
|
||
}
|
||
catch (Exception err)
|
||
{
|
||
LogNet.LogisTrac.WriteLog(typeof(ModelManagement), err);
|
||
if(ModelBasic.ErrDef.iErrHandler!=null)
|
||
// ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("导出项目数据查询表【错误】在表名【" + tableName + "】:\n" + err.ToString());
|
||
continue;
|
||
}
|
||
|
||
if (dt.Columns.Contains("ID"))
|
||
{
|
||
dt.Columns.Remove("ID");
|
||
}
|
||
if (dt.Columns.Contains("OperationTime"))
|
||
{
|
||
dt.Columns.Remove("OperationTime");
|
||
}
|
||
if (dt.Columns.Contains("ProjectCode"))
|
||
{
|
||
dt.Columns.Remove("ProjectCode");
|
||
}
|
||
if (dt.Columns.Contains("ActData"))
|
||
{
|
||
dt.Columns.Remove("ActData");
|
||
}
|
||
|
||
|
||
|
||
if (isLightweight)//轻量级选项
|
||
{
|
||
if (dt.Columns.Contains("CraftPlayData"))
|
||
{
|
||
dt.Columns.Remove("CraftPlayData");
|
||
}
|
||
}
|
||
|
||
//if (dt.Columns.Contains("CraftPlayData_Joint"))
|
||
//{
|
||
// dt.Columns.Remove("CraftPlayData_Joint");
|
||
//}
|
||
//if (dt.Columns.Contains("CraftPlayData_Raw"))
|
||
//{
|
||
// dt.Columns.Remove("CraftPlayData_Raw");
|
||
//}
|
||
|
||
outDataSet.Tables.Add(dt.Copy());
|
||
if (dt.Rows.Count > 0)
|
||
tableDataList.Add("{\"" + dt.TableName + "\":" + JsonConvert.SerializeObject(dt) + "}");
|
||
// PP.iGlobalVar2.ShowMainProgress(i);
|
||
}
|
||
|
||
if (fileExtensionToLower == "json" | fileExtensionToLower == "dat")
|
||
{
|
||
var db_Project_Json = "[" + string.Join(",", tableDataList.ToArray()) + "]";
|
||
File.WriteAllText(localFilePath, db_Project_Json);
|
||
|
||
//string strobj = db_Project_Json;
|
||
//FileStream stream = new FileStream(localFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
|
||
//BinaryFormatter formatter = new BinaryFormatter();
|
||
//formatter.Serialize(stream, strobj);
|
||
//stream.Close();
|
||
|
||
}
|
||
if (fileExtensionToLower == "xls" | fileExtensionToLower == "xlsx")
|
||
{
|
||
var success = OExcel.ExeclHelper_Model.DataSetToExcel(outDataSet, localFilePath);
|
||
if (success == false)
|
||
{
|
||
// ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("项目数据导出Excel【错误】,原因是数据中存在大字段!请导出.json或.dat文件");
|
||
return;
|
||
}
|
||
}
|
||
// if(ModelBasic.ErrDef.iErrHandler!=null)
|
||
// ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("导出项目数据完成!输出路径:" + localFilePath);
|
||
}
|
||
|
||
public void ImportProjectData(string projectName)
|
||
{
|
||
//1:判断项目名称是否存在?
|
||
|
||
|
||
//2:项目名称如果存在,如果导入,删除原来项目
|
||
|
||
if (MessageBox.Show("确认导入项目?【" + projectName + "】", "导入确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
|
||
{
|
||
var importOpenFileDialog = new OpenFileDialog();
|
||
var importDialogResult = importOpenFileDialog.ShowDialog();
|
||
if (importDialogResult == DialogResult.OK)
|
||
{
|
||
var localFilePath = importOpenFileDialog.FileName;
|
||
ImportProjectData( localFilePath, projectName);
|
||
}
|
||
//else
|
||
//{
|
||
// MessageBox.Show("您已取消了导入项目操作!");
|
||
//}
|
||
|
||
}
|
||
//else
|
||
//{
|
||
// MessageBox.Show("您已取消了导入项目操作!");
|
||
//}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 导入项目数据
|
||
/// </summary>
|
||
public void ImportProjectData()
|
||
{
|
||
//1:判断项目名称是否存在?
|
||
|
||
|
||
//2:项目名称如果存在,如果导入,删除原来项目
|
||
|
||
if (MessageBox.Show("确认导入项目?【" + "projectName" + "】", "导入确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
|
||
{
|
||
var importOpenFileDialog = new OpenFileDialog();
|
||
var importDialogResult = importOpenFileDialog.ShowDialog();
|
||
if (importDialogResult == DialogResult.OK)
|
||
{
|
||
var localFilePath = importOpenFileDialog.FileName;
|
||
ImportProjectData_FixedTable(localFilePath);
|
||
}
|
||
//else
|
||
//{
|
||
// MessageBox.Show("您已取消了导入项目操作!");
|
||
//}
|
||
|
||
}
|
||
//else
|
||
//{
|
||
// MessageBox.Show("您已取消了导入项目操作!");
|
||
//}
|
||
}
|
||
|
||
|
||
public void ImportProjectData_FixedTable(string localFilePath, string restoredProjectName)
|
||
{
|
||
string PP_ProjectCode = Guid.NewGuid().ToString("D").ToUpper();
|
||
var t1 = DateTime.Now.ToFileTimeUtc();
|
||
var projectDataFile = File.ReadAllText(localFilePath);
|
||
|
||
var t2 = DateTime.Now.ToFileTimeUtc();
|
||
var D1 = ((t2 - t1) / 10000).ToString();
|
||
|
||
var jProjectData = (JArray)JsonConvert.DeserializeObject(projectDataFile);
|
||
|
||
var curr_Index_All = 1;
|
||
var t_ProgressBar_2 = new T_ProgressBar_2("导入项目数据进度", jProjectData.Count);
|
||
//t_ProgressBar_2.Show();
|
||
|
||
foreach (JToken jProjectData_P in jProjectData)
|
||
{
|
||
var jObj = JObject.Parse(jProjectData_P.ToString());
|
||
var tableName = jObj.First.Path;
|
||
var j_TableData = jObj[tableName].ToString();
|
||
|
||
var dt_TableData = JsonConvert.DeserializeObject<DataTable>(j_TableData);
|
||
|
||
var columnName = GetColumnsByDataTable(dt_TableData);
|
||
var columnNameRows = string.Join(",", columnName.ToList<String>().ToArray());
|
||
|
||
t_ProgressBar_2.ShowProgressSet(dt_TableData.Rows.Count);
|
||
|
||
//foreach(DataRow dr in dt_TableData.Rows)
|
||
for (int k = 0; k < dt_TableData.Rows.Count; k++)
|
||
{
|
||
var rowDataList = new List<string>();
|
||
for (int i = 0; i < columnName.Length; i++)
|
||
{
|
||
var currValue = dt_TableData.Rows[k][(columnName[i])].ToString();
|
||
if (!ModelBasic.Judge.IsIntOrDouble(currValue))
|
||
currValue = "'" + currValue + "'";
|
||
|
||
rowDataList.Add(currValue);
|
||
|
||
}
|
||
|
||
var rowData = string.Join(",", rowDataList.ToArray());
|
||
SqlOperation.TransferProject.Insert_Table(tableName, columnNameRows, rowData, PP_ProjectCode, restoredProjectName);
|
||
|
||
//t_ProgressBar_2.ShowProgress(k);
|
||
}
|
||
|
||
//t_ProgressBar_2.ShowProgress_All(curr_Index_All);
|
||
curr_Index_All = curr_Index_All + 1;
|
||
}
|
||
|
||
var t3 = DateTime.Now.ToFileTimeUtc();
|
||
var D2 = ((t3 - t2) / 10000).ToString();
|
||
MessageBox.Show("项目文件导入成功!\n"
|
||
+ "读取耗时:" + D1 + " ms\n"
|
||
+ "转换存储耗时:" + D2 + " ms\n");
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 导入项目数据
|
||
/// 2020.10.30
|
||
/// 固定格式文件方式
|
||
/// </summary>
|
||
public void ImportProjectData_FixedTable(string localFilePath)
|
||
{
|
||
var t1 = DateTime.Now.ToFileTimeUtc();
|
||
var projectDataFile = File.ReadAllText(localFilePath);
|
||
|
||
var t2 = DateTime.Now.ToFileTimeUtc();
|
||
var D1 = ((t2 - t1) / 10000).ToString();
|
||
|
||
var jProjectData = (JArray)JsonConvert.DeserializeObject(projectDataFile);
|
||
|
||
var curr_Index_All = 1;
|
||
var t_ProgressBar_2 = new T_ProgressBar_2("导入项目数据进度", jProjectData.Count);
|
||
t_ProgressBar_2.Show();
|
||
|
||
foreach (JToken jProjectData_P in jProjectData)
|
||
{
|
||
var jObj = JObject.Parse(jProjectData_P.ToString());
|
||
var tableName = jObj.First.Path;
|
||
var j_TableData = jObj[tableName].ToString();
|
||
|
||
var dt_TableData = JsonConvert.DeserializeObject<DataTable>(j_TableData);
|
||
|
||
var columnName = GetColumnsByDataTable(dt_TableData);
|
||
var columnNameRows = string.Join(",", columnName.ToList<String>().ToArray());
|
||
|
||
t_ProgressBar_2.ShowProgressSet(dt_TableData.Rows.Count);
|
||
|
||
//foreach(DataRow dr in dt_TableData.Rows)
|
||
for (int k = 0; k < dt_TableData.Rows.Count; k++)
|
||
{
|
||
var rowDataList = new List<string>();
|
||
for (int i = 0; i < columnName.Length; i++)
|
||
{
|
||
var currValue = dt_TableData.Rows[k][(columnName[i])].ToString();
|
||
if (!ModelBasic.Judge.IsIntOrDouble(currValue))
|
||
currValue = "'" + currValue + "'";
|
||
|
||
rowDataList.Add(currValue);
|
||
|
||
}
|
||
|
||
var rowData = string.Join(",", rowDataList.ToArray());
|
||
SqlOperation.TransferProject.Insert_Table(tableName, columnNameRows, rowData);
|
||
|
||
t_ProgressBar_2.ShowProgress(k);
|
||
}
|
||
|
||
t_ProgressBar_2.ShowProgress_All(curr_Index_All);
|
||
curr_Index_All = curr_Index_All + 1;
|
||
}
|
||
|
||
var t3 = DateTime.Now.ToFileTimeUtc();
|
||
var D2 = ((t3 - t2) / 10000).ToString();
|
||
MessageBox.Show("项目文件导入成功!\n"
|
||
+ "读取耗时:" + D1 + " ms\n"
|
||
+ "转换存储耗时:" + D2 + " ms\n");
|
||
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 根据datatable获得列名
|
||
/// </summary>
|
||
/// <param name="dt">表对象</param>
|
||
/// <returns>返回结果的数据列数组</returns>
|
||
public static string[] GetColumnsByDataTable(DataTable dt)
|
||
{
|
||
string[] strColumns = null;
|
||
|
||
if (dt.Columns.Count > 0)
|
||
{
|
||
var columnNum = 0;
|
||
columnNum = dt.Columns.Count;
|
||
strColumns = new string[columnNum];
|
||
for (int i = 0; i < dt.Columns.Count; i++)
|
||
{
|
||
strColumns[i] = dt.Columns[i].ColumnName;
|
||
}
|
||
}
|
||
|
||
|
||
return strColumns;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
}
|