|
- using CnasSynchronousCommon;
- using CnasSynchronusClient;
- using CnasSynchrousModel;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using System.Xml.Linq;
- using System.Xml.Serialization;
- using static System.Runtime.CompilerServices.RuntimeHelpers;
-
- namespace CNAS_DBSync
- {
- public delegate void ReferenceChangeSyncParamHanlder(SyncInstrumentItemInfo syncInstrumentItem);
- public partial class frmSyncParams : Form
- {
- public List<SyncInstrumentItemInfo> lstSyncInstrument = new List<SyncInstrumentItemInfo>(); //本地存储的仪器数据源
- public SyncInstrumentItemInfo currentSyncItem = new SyncInstrumentItemInfo(); //当前正在操作的仪器项
- public Dictionary<string, DataTable> dictInstruTables = new Dictionary<string, DataTable>(); //当前仪器项的来源表结构
- private SyncInstrumentItemInfo syncInstrumentItem; //其他调用传递的仪器项
- public ReferenceChangeSyncParamHanlder ParamsChangedDelegate; //传递仪器参数委托
- private bool isLocked = false; // Track lock state
-
- private string strMode = "";
- private string strTableInfoMode = ""; //获取所有表信息的方式;0为自动获取,1为手动输入
-
- public frmSyncParams(SyncInstrumentItemInfo syncInstrumentItem = null)
- {
- InitializeComponent();
-
- dgvInstrument.AutoGenerateColumns = false;
- dgvInstrument.RowHeadersVisible = false;
-
- dgvInstruDS.AutoGenerateColumns = false;
- dgvInstruDS.RowHeadersVisible = false;
-
- dgvCnas.AutoGenerateColumns = false;
- dgvCnas.RowHeadersVisible = false;
-
- dgvMapping.AutoGenerateColumns = false;
- dgvMapping.RowHeadersVisible = false;
-
- if (syncInstrumentItem != null)
- {
- strMode = "Reference";
- this.syncInstrumentItem = syncInstrumentItem;
-
- this.btnAdd.Visible = false;
- this.btnDel.Visible = false;
- }
-
- if (LockStateOperation.GetLockState())
- {
- isLocked = true;
- btnLock.Text = "解锁";
- btnAdd.Enabled = false;
- btnDel.Enabled = false;
- }
- }
-
- private void btnLock_Click(object sender, EventArgs e)
- {
- if (isLocked)
- {
- // Try to unlock
- if (new frmLockPwd(false).ShowDialog() == DialogResult.OK)
- {
- isLocked = false;
- btnLock.Text = "锁定";
- btnAdd.Enabled = true;
- btnDel.Enabled = true;
- }
- }
- else
- {
- // Try to lock
- if (new frmLockPwd(true).ShowDialog() == DialogResult.OK)
- {
- isLocked = true;
- btnLock.Text = "解锁";
- btnAdd.Enabled = false;
- btnDel.Enabled = false;
- }
- }
- }
-
- private void Option1_Click(object sender, EventArgs e)
- {
- string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
- frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
- frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
- {
- var lstInstrument = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
- if (lstInstrument.Count == 1)
- {
- lstInstrument[0].Code = Instrumentitem.Code;
- lstInstrument[0].InstruType = Instrumentitem.InstruType;
- }
- };
- if (frmSetting.ShowDialog() == DialogResult.OK)
- {
- //绑定数据
- dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
- }
- //frmSetting.ShowDialog();
- }
-
- private void Option2_Click(object sender, EventArgs e)
- {
- // 处理选项2的点击事件
- MessageBox.Show("选项2被点击");
- }
- private void frmSyncParams_Load(object sender, EventArgs e)
- {
- if (syncInstrumentItem == null)
- {
- lstSyncInstrument = FileOperation.GetLocalSyncInStrumentData();
- }
- else
- {
- lstSyncInstrument = new List<SyncInstrumentItemInfo>() { syncInstrumentItem };
- }
-
- //绑定数据源,填写相关内容
- if (lstSyncInstrument.Count != 0)
- dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
- }
-
- /// <summary>
- /// 保存当前设置到本地
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSave_Click(object sender, EventArgs e)
- {
- //将配置好的信息存储到本地文件中
- try
- {
- bool bIfSaveSuccess = true;
- if (strMode == "Reference")
- {
- if (!CheckIfHaveDateField(new List<SyncInstrumentItemInfo>() { currentSyncItem }))
- {
- MessageBox.Show("日期字段不允许为空!");
- return;
- }
- if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
- {
- MessageBox.Show("关键字段不允许为空!");
- return;
- }
- //1.先加载所有数据 2.替换当前数据 3.重新保存
- List<SyncInstrumentItemInfo> lstDB = FileOperation.GetLocalSyncInStrumentData();
- var item = lstDB.Where(p => p.GUID == currentSyncItem.GUID).SingleOrDefault();
- if (item != null)
- {
- lstDB.Remove(item);
- lstDB.Add(currentSyncItem);
- }
- else
- {
- lstDB.Add(currentSyncItem);
- }
- //重新保存信息
- bIfSaveSuccess = FileOperation.SaveLocalSyncInStrumentData(lstDB);
- //委托发送参数
- this.ParamsChangedDelegate(syncInstrumentItem);
- }
- else
- {
- if (!CheckIfHaveDateField(lstSyncInstrument))
- {
- MessageBox.Show("日期字段不允许为空!");
- return;
- }
- if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
- {
- MessageBox.Show("关键字段不允许为空!");
- return;
- }
- bIfSaveSuccess = FileOperation.SaveLocalSyncInStrumentData(lstSyncInstrument);
- }
- if (bIfSaveSuccess)
- MessageBox.Show("保存成功!");
- else
- MessageBox.Show("保存失败!");
- }
- catch (Exception ex)
- {
- MessageBox.Show("保存失败!错误信息为:" + ex.Message.ToString());
- AppLog.Error(ex.Message);
- }
- }
- private bool CheckIfHaveDateField(List<SyncInstrumentItemInfo> lstSyncInstrument)
- {
- bool bIfHave = true;
- foreach (var item in lstSyncInstrument)
- {
- if (item.LstSyncPramas == null) continue;
- if (item.LstSyncPramas.Count <= 0) continue;
- if (item.LstSyncPramas.Where(s => s.IfDateField == true).Count() != 1)
- {
- bIfHave = false;
- break;
- }
- }
- return bIfHave;
- }
- private bool CheckIfHaveKeyPrimaryField(List<SyncInstrumentItemInfo> lstSyncInstrument)
- {
- bool bIfHave = true;
- foreach (var item in lstSyncInstrument)
- {
- if (item.LstSyncPramas == null) continue;
- if (item.LstSyncPramas.Count <= 0) continue;
- if (item.LstSyncPramas.Where(s => s.IfPrimaryKey == true).Count() <= 0)
- {
- bIfHave = false;
- break;
- }
- }
- return bIfHave;
- }
- /// <summary>
- /// 新增仪器
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnAdd_Click(object sender, EventArgs e)
- {
- frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument);
-
- frmInstrumentCode frmCode = new frmInstrumentCode(lstSyncInstrument);
- frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
- {
- lstSyncInstrument.Add(Instrumentitem);
-
- //绑定数据
- dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
- dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
-
- dgvInstrument_SelectionChanged(null, null);
- };
- frmSetting.ShowDialog();
- }
-
-
- /// <summary>
- /// 删除仪器
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnDel_Click(object sender, EventArgs e)
- {
- if (dgvInstrument.Rows.Count <= 0) return;
-
- //当前选中行
- string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
-
- //找到数据源中数据,删除
- var lstitem = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
- if (lstitem == null)
- return;
- else if (lstitem.Count >= 1)
- {
- foreach (var item in lstitem)
- {
- lstSyncInstrument.Remove(item);
- }
- }
-
- //绑定数据
- dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>();
- dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
-
- //dgvInstrument.dgvInstrument_SelectionChanged()
- if (lstSyncInstrument.Count > 0)
- dgvInstrument_SelectionChanged(null, null);
- else
- {
- currentSyncItem = new SyncInstrumentItemInfo();
- dictInstruTables.Clear();
-
- cbxCnas.DataSource = null;
- cbxCnas.Items.Clear();
- cbxInstrument.DataSource = null;
- dgvInstruDS.DataSource = null;
- dgvCnas.DataSource = null;
- dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
- }
- }
-
-
-
- private void dgvInstrument_SelectionChanged(object sender, EventArgs e)
- {
- if (dgvInstrument.Rows.Count <= 0 || dgvInstrument.Rows[dgvInstrument.CurrentRow.Index].Cells[0].Value == null)
- {
- currentSyncItem = new SyncInstrumentItemInfo();
- return;
- }
-
- //清空绑定
- cbxCnas.DataSource = null;
- cbxInstrument.DataSource = null;
- dgvInstruDS.DataSource = null;
- dgvCnas.DataSource = null;
-
- //当前选中单元格
- string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
- var lstInstrument = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
- if (lstInstrument.Count == 1)
- {
- currentSyncItem = lstInstrument[0];
-
- if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn))
- txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
- else
- txtInstrumentColumn.Text = "";
-
- if (currentSyncItem.SyncInstrumentDSInfo != null && currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType != DataSourceType.None)
- {
-
- switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
- {
- case DataSourceType.MySQL:
- case DataSourceType.Dm:
- case DataSourceType.Oracle:
- case DataSourceType.PostgreSQL:
- case DataSourceType.SQL:
- case DataSourceType.Kingbase:
- if (currentSyncItem.LstSyncPramas != null)
- dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
- //if (currentSyncItem.SyncInstrumentDSInfo.ServerName != "")
- // btnLoadDBData_Click(sender, e);
- break;
- default:
- if (currentSyncItem.LstSyncPramas != null)
- dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
- // btnLoadDBData_Click(sender, e);
- break;
- }
-
-
-
-
- }
- else
- {
- dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
- }
- }
- else
- {
- dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
- }
- }
- /// <summary>
- /// 切换选中表时发生
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void cbxInstrument_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (cbxInstrument.Text == null) return;
- string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
-
- // 使用LINQ查找匹配的数据
- SyncInstrumentItemInfo matchedInstrument = lstSyncInstrument.FirstOrDefault(x => x.Code == strInstrumentCode);
-
- InstrumentData instrumentDatas = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
-
- DataTable dtTableType = null;
- string strTableName_Instru = cbxInstrument.Text.ToString();
-
- if (matchedInstrument.SyncInstrumentDSInfo != null)
- {
- switch (matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType)
- {
- case DataSourceType.MySQL:
- dtTableType = SelectTableType.MySqlsec(strTableName_Instru);
- break;
- case DataSourceType.Dm:
- dtTableType = SelectTableType.DmSql(strTableName_Instru);
- break;
- case DataSourceType.Oracle:
- dtTableType = SelectTableType.OrcSql(strTableName_Instru, currentSyncItem);
- break;
- case DataSourceType.PostgreSQL:
- dtTableType = SelectTableType.PostgreSql(strTableName_Instru);
- break;
- case DataSourceType.SQL:
- dtTableType = SelectTableType.Sqlserversec(strTableName_Instru, currentSyncItem);
- break;
- case DataSourceType.Kingbase:
- dtTableType = SelectTableType.KingSql(strTableName_Instru);
- break;
-
- default:
- strTableName_Instru = matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType.ToString();
-
-
- strTableName_Instru = currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType.ToString();
-
- InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(matchedInstrument.SyncInstrumentDSInfo, new object[] { "", "", "" });
- dictInstruTables = instrumentData.GetInstrumentData();
- dictInstruTables = instrumentDatas.GetInstrumentData();
- if (dictInstruTables.ContainsKey(strTableName_Instru))
- dtTableType = dictInstruTables[strTableName_Instru];
-
- if (dictInstruTables.ContainsKey(cbxInstrument.Text))
- dtTableType = dictInstruTables[cbxInstrument.Text];
- break;
- }
- DataTable dtInstruShow = new DataTable();
- dtInstruShow.Columns.Add("InstruFieldName");
- dtInstruShow.Columns.Add("InstruDataType");
- dtInstruShow.Columns.Add("remark");
- if (dtTableType != null)
- {
- switch (matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType)
- {
- case DataSourceType.MySQL:
- case DataSourceType.Dm:
- case DataSourceType.PostgreSQL:
- case DataSourceType.SQL:
- case DataSourceType.Kingbase:
- for (int i = 0; i < dtTableType.Rows.Count; i++)
- {
- dtInstruShow.Rows.Add(new object[] { dtTableType.Rows[i]["ColumnName"], dtTableType.Rows[i]["DataType"], dtTableType.Rows[i]["remark"] });
- }
- break;
- case DataSourceType.Oracle:
- strTableName_Instru = cbxInstrument.Text.ToString();
- if (dictInstruTables.ContainsKey(strTableName_Instru))
- {
- DataTable dt = dictInstruTables[strTableName_Instru];
- if (dt != null)
- {
- dtInstruShow = new DataTable();
- dtInstruShow.Columns.Add("InstruFieldName");
- dtInstruShow.Columns.Add("InstruDataType");
-
- foreach (DataColumn dc in dt.Columns)
- {
- dtInstruShow.Rows.Add(new object[] { dc.ColumnName, dc.DataType });
- }
-
- dgvInstruDS.DataSource = dtInstruShow;
- }
- }
- break;
- default:
- foreach (DataColumn dc in dtTableType.Columns)
- {
- dtInstruShow.Rows.Add(new object[] { dc.ColumnName, dc.DataType, "" });
- }
- break;
- }
- }
-
- dgvInstruDS.DataSource = dtInstruShow;
- }
- }
-
- /// <summary>
- /// 切换表时发生
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (cbxCnas.Text == null) return;
-
- string strTableName_Cnas = cbxCnas.Text.ToString();
-
- DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Cnas, currentSyncItem.SyncTargetDBInfo);
-
- //从数据库中加载数据表结构
- //DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
- if (dtTableStruct != null)
- {
- DataTable dtCnasShow = new DataTable();
- dtCnasShow.Columns.Add("CnasFieldName");
- dtCnasShow.Columns.Add("CnasDataType");
- dtCnasShow.Columns.Add("remark");
-
- for (int i = 0; i < dtTableStruct.Rows.Count; i++)
- {
- dtCnasShow.Rows.Add(new object[] { dtTableStruct.Rows[i]["ColumnName"], dtTableStruct.Rows[i]["DataType"], dtTableStruct.Rows[i]["remark"] });
- }
- dgvCnas.DataSource = null;
- dgvCnas.DataSource = dtCnasShow;
-
-
- //绑定数据
- cbxCNASColumn.DataSource = dtCnasShow.Copy();
- cbxCNASColumn.DisplayMember = "CnasFieldName";
- cbxCNASColumn.ValueMember = "CnasFieldName";
-
- if (dtTableStruct.Columns.Count > 0)
- {
- if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn) && dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
- {
- cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
- }
- //else
- //{
- // cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
- //}
- }
- }
- }
-
- //增加映射
- private void btnAddMapping_Click(object sender, EventArgs e)
- {
- if (cbxInstrument.Text == null) return;
- if (cbxCnas.Text == null) return;
- if (dgvInstruDS.Rows.Count <= 0) return;
- if (dgvCnas.Rows.Count <= 0) return;
-
- SyncParamasInfo syncParamas = new SyncParamasInfo();
- if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
-
- if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
- {
- MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
- return;
- }
-
- //仪器表名和选中行
- syncParamas.SourceTable = cbxInstrument.Text.ToString();
- syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString();
- syncParamas.DataType = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[1].Value.ToString().ToUpper();
- //CNAS表名和选中行
- syncParamas.TargetTable = cbxCnas.Text.ToString();
- syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString();
-
- //验证数据合法性
- SyncParamsOperation paramsOperation = new SyncParamsOperation();
- if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
- {
- MessageBox.Show("已存在不同表单映射数据,无法添加!");
- return;
- }
- //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
- //{
- // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
- // return;
- //}
- if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
- {
- MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
- return;
- }
- if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
- {
- MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
- return;
- }
-
- //绑定数据
- currentSyncItem.LstSyncPramas.Add(syncParamas);
- dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
- //选中最后一行
- dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count - 1].Cells[0];
- }
-
- /// <summary>
- /// 删除映射
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnDelMap_Click(object sender, EventArgs e)
- {
- //当前选中项
- if (dgvMapping.CurrentCell == null) return;
- string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
- string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
-
- var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
- if (lstDelItems.Count > 0)
- {
- foreach (var item in lstDelItems)
- {
- currentSyncItem.LstSyncPramas.Remove(item);
- }
-
- dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
- }
- }
- /// <summary>
- /// 配置数据库界面
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnDatabaseConfig_Click(object sender, EventArgs e)
- {
- frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
- frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
- {
- this.currentSyncItem = Instrumentitem;
- };
- frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
- {
- //this.dictInstruTables = dict;
- };
- frmDatabase.ShowDialog();
-
- //加载数据
- if ((currentSyncItem.SyncInstrumentDSInfo.Host != null && currentSyncItem.SyncInstrumentDSInfo.Host.Length > 0) || (currentSyncItem.SyncInstrumentDSInfo.Path != null && currentSyncItem.SyncInstrumentDSInfo.Path.Length > 0))
- btnLoadDBData_Click(null, null);
- }
-
- private void btnLoadDBData_Click(object sender, EventArgs e)
- {
- string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
-
- strTableInfoMode = FileOperation.GetSystemFormatConfigData(strInstrumentCode).TableInfoMode;
- if (strTableInfoMode == "1")
- {
- cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
- //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
- }
- int iReturn = 0;
- if (strTableInfoMode == "0")
- iReturn = LoadSourceAndTargetData(true);
- else
- iReturn = LoadSourceAndTargetData();
- switch (iReturn)
- {
- case -1:
- MessageBox.Show("未能成功获取设备数据库配置信息,请配置后重试!");
- break;
- case -2:
- MessageBox.Show("未能成功获取CNAS数据库配置信息,请配置后重试!");
- break;
- case -3:
- MessageBox.Show("未能成功获取仪器数据信息,请配置后重试!");
- break;
- case -4:
- MessageBox.Show("未能成功获取CNAS数据信息,请配置后重试!");
- break;
- case -5:
- DialogResult dr = MessageBox.Show("检测到数据连接配置已经修改,是否全部删除已经分配的字段映射?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
- if (dr == DialogResult.Yes)
- {
- //此时将全部删除已经分配的映射字段
- currentSyncItem.LstSyncPramas.Clear();
- currentSyncItem.lstFixedValue.Clear();
-
- dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
- }
- break;
- case -6:
- MessageBox.Show("请先手动输入表名");
- break;
- case -7:
- MessageBox.Show("配置SQL查询没有结果,请检查SQL是否正确");
- break;
- default:
- break;
- }
- }
-
- /// <summary>
- /// 加载来源和目标数据的数据结构
- /// </summary>
- /// <param name="bIfLoading"></param>
- /// <returns></returns>
- public int LoadSourceAndTargetData(bool bIfLoading = false)
- {
- //检查配置信息
- if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
- if (currentSyncItem.SyncTargetDBInfo == null) return -2;
- bool bIfSameTable = true;
-
- //是否需要重新加载来源库
- if (bIfLoading)
- {
- InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
- dictInstruTables = instrumentData.GetInstrumentData();
- }
- if (dictInstruTables.Count <= 0) return -3;
-
- //绑定ComboBox
- List<string> lstTableName = new List<string>();
- foreach (var item in dictInstruTables)
- {
- lstTableName.Add(item.Key);
- }
-
- cbxInstrument.DataSource = lstTableName;
- cbxInstrument.ValueMember = "";
-
- if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
- {
- if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
- cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
- else
- bIfSameTable = false;
- }
-
- //获取CNAS配置的数据库连接信息
- DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
- if (dtCNAS != null && dtCNAS.Rows.Count > 0)
- {
- List<string> lstCnasTables = new List<string>();
- foreach (DataRow dr in dtCNAS.Rows)
- {
- if (dtCNAS.Columns.Contains("TABNAME"))
- lstCnasTables.Add(dr["TABNAME"].ToString());
- else if (dtCNAS.Columns.Contains("table_name"))
- lstCnasTables.Add(dr["table_name"].ToString());
- }
-
- cbxCnas.DataSource = lstCnasTables;
- cbxCnas.ValueMember = "";
-
- if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
- {
- if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
- {
- cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
- //cbxCnas_SelectedIndexChanged(null, null);
- }
- else
- bIfSameTable = false;
- }
- }
- else
- return -4;
-
- if (!bIfSameTable)
- {
- return -5;
- }
-
- return 1;
- }
-
- public AccessFormatConfig AccessFormat { get; set; }
- public ExcelFormatConfig ExcelFormat { get; set; }
- public FoxProFormatConfig FoxProFormat { get; set; }
- public SqliteFormatConfig SqliteFormat { get; set; }
- public SqlServerFormatConfig SqlServerFormat { get; set; }
- public OracleFormatConfig OracleFormat { get; set; }
- public MySqlFormatConfig MySqlFormat { get; set; }
- public NormalFileFormatConfig NormalFileFormat { get; set; }
- public PostgreSqlFormatConfig PostgreSqlFormat { get; set; }
- public KingbaseFormatConfig KingbaseFormat { get; set; }
- public DmFormatConfig DmFormat { get; set; }
- /// <summary>
- /// 手动输入表名时,获取该表的表信息
- /// </summary>
- /// <returns></returns>
- public int LoadSourceAndTargetData()
- {
- if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
- if (currentSyncItem.SyncTargetDBInfo == null) return -2;
- string sqlName = "";
- string sql = "";
- ExcelFormat = FileOperation.GetFormatConfigData<ExcelFormatConfig>("ExcelFormatConfig.xml");
- AccessFormat = FileOperation.GetFormatConfigData<AccessFormatConfig>("AccessFormatConfig.xml");
- FoxProFormat = FileOperation.GetFormatConfigData<FoxProFormatConfig>("FoxProFormatConfig.xml");
- SqliteFormat = FileOperation.GetFormatConfigData<SqliteFormatConfig>("SqliteFormatConfig.xml");
- SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
- OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
- NormalFileFormat = FileOperation.GetFormatConfigData<NormalFileFormatConfig>("NormalFileFormatConfig.xml");
-
- switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
- {
- case DataSourceType.MySQL:
- MySqlFormat = FileOperation.GetFormatConfigData<MySqlFormatConfig>("MySqlFormatConfig.xml");
- sqlName = MySqlFormat.AutoSql.MySqlViewName;
- sql = MySqlFormat.AutoSql.MySqlViewSql;
- break;
- case DataSourceType.Dm:
- DmFormat = FileOperation.GetFormatConfigData<DmFormatConfig>("DmFormatConfig.xml");
- sqlName = DmFormat.AutoSql.DmViewName;
- sql = DmFormat.AutoSql.DmViewSql;
- break;
- case DataSourceType.Oracle:
- OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
- sqlName = OracleFormat.AutoSql.OracleViewName;
- sql = OracleFormat.AutoSql.OracleViewSql;
- break;
- case DataSourceType.PostgreSQL:
- PostgreSqlFormat = FileOperation.GetFormatConfigData<PostgreSqlFormatConfig>("PostgreSqlFormatConfig.xml");
- sqlName = PostgreSqlFormat.AutoSql.PostgreSqlViewName;
- sql = PostgreSqlFormat.AutoSql.PostgreSqlViewSql;
- break;
- case DataSourceType.SQL:
- SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
- sqlName = SqlServerFormat.AutoSql.SqlServerViewName;
- sql = SqlServerFormat.AutoSql.SqlServerViewSql;
- break;
- case DataSourceType.Kingbase:
- KingbaseFormat = FileOperation.GetFormatConfigData<KingbaseFormatConfig>("KingbaseFormatConfig.xml");
- sqlName = KingbaseFormat.AutoSql.KingbaseViewName;
- sql = KingbaseFormat.AutoSql.KingbaseViewSql;
- break;
- case DataSourceType.Access:
- AccessFormat = FileOperation.GetFormatConfigData<AccessFormatConfig>("AccessFormatConfig.xml");
- sqlName = AccessFormat.AutoSql.AccessViewName;
- sql = AccessFormat.AutoSql.AccessViewSql;
- break;
- default:
- break;
- }
-
-
- //cbxInstrument.Text = ExtractTableNames(sql);
- cbxInstrument.Text = sqlName;
- if (cbxInstrument.Text == "") return -6;
-
- int returnValue = 1;
-
- //根据手动输入来源库的表名加载字段
-
-
-
- InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
- //dictInstruTables = instrumentData.GetInstrumentData();
- //dictInstruTables =
- DataTable dataTableStruct = null;
- string strTableName_Instru = cbxInstrument.Text.ToString();
-
- switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
- {
- case DataSourceType.MySQL:
- dataTableStruct = SelectTableType.MySqlsecSD(sql);
- break;
- case DataSourceType.Dm:
- dataTableStruct = SelectTableType.DmSqlSD(sql);
- break;
- case DataSourceType.Oracle:
- dataTableStruct = SelectTableType.OrcSqlSD(sql, currentSyncItem);
- break;
- case DataSourceType.PostgreSQL:
- dataTableStruct = SelectTableType.PostgreSqlSD(sql);
- break;
- case DataSourceType.SQL:
- dataTableStruct = SelectTableType.SqlserversecSD(sql, currentSyncItem);
- break;
- case DataSourceType.Kingbase:
- if (cbxInstrument.Text.Contains('.'))
- cbxInstrument.Text = cbxInstrument.Text.Split('.')[1];
-
- dataTableStruct = SelectTableType.KingSql(sql);
- break;
-
- case DataSourceType.Access:
- //根据手动输入来源库的表名加载字段
- dataTableStruct = SelectTableType.AccSql(currentSyncItem.SyncInstrumentDSInfo, sql);
- break;
- default:
-
- break;
- }
- DataTable dtInstruShow = new DataTable();
- dtInstruShow.Columns.Add("InstruFieldName");
- dtInstruShow.Columns.Add("InstruDataType");
- dtInstruShow.Columns.Add("remark");
- if (dataTableStruct != null)
- {
- for (int i = 0; i < dataTableStruct.Columns.Count; i++)
- {
- dtInstruShow.Rows.Add(new object[] { dataTableStruct.Columns[i].ColumnName, dataTableStruct.Columns[i].DataType });
- }
- }
- dgvInstruDS.DataSource = dtInstruShow;
-
-
- if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
- {
- dictInstruTables.Clear();
- dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
- //return 1;
- }
- else
- {
- returnValue = -7;
- return returnValue;
- }
-
- //获取CNAS配置的数据库连接信息
- DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
- if (dtCNAS != null && dtCNAS.Rows.Count > 0)
- {
- List<string> lstCnasTables = new List<string>();
- foreach (DataRow dr in dtCNAS.Rows)
- {
- lstCnasTables.Add(dr["TABNAME"].ToString());
- }
-
- cbxCnas.DataSource = lstCnasTables;
- cbxCnas.ValueMember = "";
-
- if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
- {
- if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
- {
- cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
- //cbxCnas_SelectedIndexChanged(null, null);
- }
- else
- returnValue = -5;
- }
- }
- else
- returnValue = -4;
-
- return returnValue;
- }
-
-
- public string ExtractTableNames(string sql)
- {
- // 预处理:去除注释、统一空格
- sql = Regex.Replace(sql, @"(--.*)|(\/\*[\s\S]*?\*\/)", "", RegexOptions.Multiline);
- sql = Regex.Replace(sql, @"\s+", " ");
-
- // 定义匹配模式
- var patterns = new Dictionary<string, string>
- {
- { "SELECT", @"(?:FROM|JOIN)\s+([\w\.]+)(?:\s+AS\s+\w+)?" },
- { "INSERT", @"INSERT\s+INTO\s+([\w\.]+)" },
- { "UPDATE", @"UPDATE\s+([\w\.]+)" },
- { "DELETE", @"DELETE\s+FROM\s+([\w\.]+)" }
- };
-
- var tables = new HashSet<string>();
- foreach (var pattern in patterns.Values)
- {
- var matches = Regex.Matches(sql, pattern, RegexOptions.IgnoreCase);
- foreach (Match match in matches)
- {
- if (match.Groups[1].Success)
- tables.Add(match.Groups[1].Value.Trim());
- }
- }
- if (tables.Count == 0)
- return "";
- else
- return tables.ToList()[0];
- }
- private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
- {
- if (dgvMapping.CurrentCell == null) return;
- if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
- {
- string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
- string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
- string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
-
- var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
- if (lstDelItems.Count > 0)
- {
- foreach (var item in lstDelItems)
- {
- item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
- }
- }
- }
- if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
- {
- string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
- string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
- string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
-
- var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
- if (lstDelItems.Count == 1)
- {
- if (strDateKey.ToLower() == "true")
- {
- //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
- foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
- {
- if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
- dgvRow.Cells["DateKey"].Value = false;
- }
- //内存数据源
- foreach (var item in currentSyncItem.LstSyncPramas)
- {
- if (item.IfDateField)
- item.IfDateField = false;
- }
- lstDelItems[0].IfDateField = true;
- }
- else
- {
- lstDelItems[0].IfDateField = false;
- }
- }
- }
- }
-
- private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
- {
- if (dgvMapping.IsCurrentCellDirty)
- {
- dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
- }
- }
-
- private void btnCNASFieldConfig_Click(object sender, EventArgs e)
- {
- if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
- {
- MessageBox.Show("请先指定至少一个映射字段。");
- return;
- }
- frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
- if (frmCNAS.ShowDialog() == DialogResult.OK)
- {
- this.currentSyncItem = frmCNAS.syncInstrument;
- }
- }
-
- private void btnSourceFilter_Click(object sender, EventArgs e)
- {
- if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
- {
- MessageBox.Show("请先指定至少一个映射字段。");
- return;
- }
- frmSourceFilter frm = new frmSourceFilter(currentSyncItem, strTableInfoMode);
- frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
- {
- currentSyncItem.SourceFilter = dataFilter;
- };
- frm.ShowDialog();
- }
-
- private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
- {
- if (dgvInstrument.Rows.Count <= 0) return;
- //当前选中单元格
- string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
-
- frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
- frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
- {
- lstSyncInstrument.Add(Instrumentitem);
- //绑定数据
- dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
- dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
- dgvInstrument_SelectionChanged(null, null);
- };
- frmSetting.ShowDialog();
- }
-
- private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (cbxCNASColumn.Visible)
- {
- cbxCNASColumn.Visible = false;
- txtInstrumentColumn.Visible = true;
-
- currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
- }
- }
-
- /// <summary>
- /// 当双击textBox时,显示ComboBOX用于选择
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
- {
- txtInstrumentColumn.Visible = false;
-
- cbxCNASColumn.Top = txtInstrumentColumn.Top;
- cbxCNASColumn.Height = txtInstrumentColumn.Height;
- cbxCNASColumn.Width = txtInstrumentColumn.Width;
- cbxCNASColumn.Location = txtInstrumentColumn.Location;
-
- cbxCNASColumn.Visible = true;
- }
-
- private void cbxCNASColumn_Leave(object sender, EventArgs e)
- {
- if (cbxCNASColumn.Visible)
- {
- txtInstrumentColumn.Visible = true;
- cbxCNASColumn.Visible = false;
- }
- }
-
- private void tsmServiceSetting_Click(object sender, EventArgs e)
- {
- frmServiceConfig frmServiceConfig = new frmServiceConfig();
- frmServiceConfig.ShowDialog();
- }
-
- private void tsmSystemSetting_Click(object sender, EventArgs e)
- {
- //frmSystemSetting frmSystem = new frmSystemSetting();
- //frmSystem.ShowDialog();
-
- //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
- //if (strTableInfoMode == "1")
- //{
- // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
- // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
- //}
- //else
- //{
- // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
- //}
- }
-
- private void tsmSourceSetting_Click(object sender, EventArgs e)
- {
- frmSourceSetting frmSource = new frmSourceSetting();
- frmSource.ShowDialog();
- }
-
- private void tsmHelper_Click(object sender, EventArgs e)
- {
- string strHelpFilePath = FileHelper.getBasePath() + @"\Helper.CHM";
- //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
- //Help.ShowHelpIndex(this, strHelpFilePath);
- System.Diagnostics.Process.Start(strHelpFilePath);
- }
-
- /// <summary>
- /// 右键事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvInstrument_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
- {
- if (e.Button == MouseButtons.Right)
- {
- string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
- if (dgvInstrument.Rows.Count <= 0) return;
- //当前选中单元格
-
- if (strInstrumentCode != "")
- {
- // 显示上下文菜单
- ContextMenuStrip contextMenu = new ContextMenuStrip();
- contextMenu.Items.Add("编辑", null, new EventHandler(Option1_Click));
- contextMenu.Show(dgvInstrument, new Point(e.X, e.Y));
- }
-
- }
- }
- }
- }
|