CNAS取数仪器端升级
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

922 рядки
38KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusClient;
  3. using CnasSynchrousModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Windows.Forms;
  14. using System.Xml.Linq;
  15. using System.Xml.Serialization;
  16. namespace CNAS_DBSync
  17. {
  18. public delegate void ReferenceChangeSyncParamHanlder(SyncInstrumentItemInfo syncInstrumentItem);
  19. public partial class frmSyncParams : Form
  20. {
  21. public List<SyncInstrumentItemInfo> lstSyncInstrument = new List<SyncInstrumentItemInfo>(); //本地存储的仪器数据源
  22. public SyncInstrumentItemInfo currentSyncItem = new SyncInstrumentItemInfo(); //当前正在操作的仪器项
  23. public Dictionary<string, DataTable> dictInstruTables = new Dictionary<string, DataTable>(); //当前仪器项的来源表结构
  24. private SyncInstrumentItemInfo syncInstrumentItem; //其他调用传递的仪器项
  25. public ReferenceChangeSyncParamHanlder ParamsChangedDelegate; //传递仪器参数委托
  26. private string strMode = "";
  27. private string strTableInfoMode = ""; //获取所有表信息的方式;0为自动获取,1为手动输入
  28. public frmSyncParams(SyncInstrumentItemInfo syncInstrumentItem = null)
  29. {
  30. InitializeComponent();
  31. dgvInstrument.AutoGenerateColumns = false;
  32. dgvInstrument.RowHeadersVisible = false;
  33. dgvInstruDS.AutoGenerateColumns = false;
  34. dgvInstruDS.RowHeadersVisible = false;
  35. dgvCnas.AutoGenerateColumns = false;
  36. dgvCnas.RowHeadersVisible = false;
  37. dgvMapping.AutoGenerateColumns = false;
  38. dgvMapping.RowHeadersVisible = false;
  39. if (syncInstrumentItem != null)
  40. {
  41. strMode = "Reference";
  42. this.syncInstrumentItem = syncInstrumentItem;
  43. this.btnAdd.Visible = false;
  44. this.btnDel.Visible = false;
  45. }
  46. }
  47. private void frmSyncParams_Load(object sender, EventArgs e)
  48. {
  49. if (syncInstrumentItem == null)
  50. {
  51. lstSyncInstrument = FileOperation.GetLocalSyncInStrumentData();
  52. }
  53. else
  54. {
  55. lstSyncInstrument = new List<SyncInstrumentItemInfo>() { syncInstrumentItem };
  56. }
  57. //绑定数据源,填写相关内容
  58. if (lstSyncInstrument.Count != 0)
  59. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  60. }
  61. /// <summary>
  62. /// 保存当前设置到本地
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. private void btnSave_Click(object sender, EventArgs e)
  67. {
  68. //将配置好的信息存储到本地文件中
  69. try
  70. {
  71. bool bIfSaveSuccess = true;
  72. if (strMode == "Reference")
  73. {
  74. if (!CheckIfHaveDateField(new List<SyncInstrumentItemInfo>() { currentSyncItem }))
  75. {
  76. MessageBox.Show("日期字段不允许为空!");
  77. return;
  78. }
  79. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  80. {
  81. MessageBox.Show("关键字段不允许为空!");
  82. return;
  83. }
  84. //1.先加载所有数据 2.替换当前数据 3.重新保存
  85. List<SyncInstrumentItemInfo> lstDB = FileOperation.GetLocalSyncInStrumentData();
  86. var item = lstDB.Where(p => p.GUID == currentSyncItem.GUID).SingleOrDefault();
  87. if (item != null)
  88. {
  89. lstDB.Remove(item);
  90. lstDB.Add(currentSyncItem);
  91. }
  92. else
  93. {
  94. lstDB.Add(currentSyncItem);
  95. }
  96. //重新保存信息
  97. bIfSaveSuccess = FileOperation.SaveLocalSyncInStrumentData(lstDB);
  98. //委托发送参数
  99. this.ParamsChangedDelegate(syncInstrumentItem);
  100. }
  101. else
  102. {
  103. if (!CheckIfHaveDateField(lstSyncInstrument))
  104. {
  105. MessageBox.Show("日期字段不允许为空!");
  106. return;
  107. }
  108. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  109. {
  110. MessageBox.Show("关键字段不允许为空!");
  111. return;
  112. }
  113. bIfSaveSuccess = FileOperation.SaveLocalSyncInStrumentData(lstSyncInstrument);
  114. }
  115. if (bIfSaveSuccess)
  116. MessageBox.Show("保存成功!");
  117. else
  118. MessageBox.Show("保存失败!");
  119. }
  120. catch (Exception ex)
  121. {
  122. MessageBox.Show("保存失败!错误信息为:" + ex.Message.ToString());
  123. AppLog.Error(ex.Message);
  124. }
  125. }
  126. private bool CheckIfHaveDateField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  127. {
  128. bool bIfHave = true;
  129. foreach (var item in lstSyncInstrument)
  130. {
  131. if (item.LstSyncPramas == null) continue;
  132. if (item.LstSyncPramas.Count <= 0) continue;
  133. if (item.LstSyncPramas.Where(s => s.IfDateField == true).Count() != 1)
  134. {
  135. bIfHave = false;
  136. break;
  137. }
  138. }
  139. return bIfHave;
  140. }
  141. private bool CheckIfHaveKeyPrimaryField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  142. {
  143. bool bIfHave = true;
  144. foreach (var item in lstSyncInstrument)
  145. {
  146. if (item.LstSyncPramas == null) continue;
  147. if (item.LstSyncPramas.Count <= 0) continue;
  148. if (item.LstSyncPramas.Where(s => s.IfPrimaryKey == true).Count() <= 0)
  149. {
  150. bIfHave = false;
  151. break;
  152. }
  153. }
  154. return bIfHave;
  155. }
  156. /// <summary>
  157. /// 新增仪器
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. private void btnAdd_Click(object sender, EventArgs e)
  162. {
  163. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument);
  164. frmInstrumentCode frmCode = new frmInstrumentCode(lstSyncInstrument);
  165. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  166. {
  167. lstSyncInstrument.Add(Instrumentitem);
  168. //绑定数据
  169. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  170. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  171. dgvInstrument_SelectionChanged(null, null);
  172. };
  173. frmSetting.ShowDialog();
  174. }
  175. /// <summary>
  176. /// 删除仪器
  177. /// </summary>
  178. /// <param name="sender"></param>
  179. /// <param name="e"></param>
  180. private void btnDel_Click(object sender, EventArgs e)
  181. {
  182. if (dgvInstrument.Rows.Count <= 0) return;
  183. //当前选中行
  184. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  185. //找到数据源中数据,删除
  186. var lstitem = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  187. if (lstitem == null)
  188. return;
  189. else if (lstitem.Count >= 1)
  190. {
  191. foreach (var item in lstitem)
  192. {
  193. lstSyncInstrument.Remove(item);
  194. }
  195. }
  196. //绑定数据
  197. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>();
  198. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  199. //dgvInstrument.dgvInstrument_SelectionChanged()
  200. if (lstSyncInstrument.Count > 0)
  201. dgvInstrument_SelectionChanged(null, null);
  202. else
  203. {
  204. currentSyncItem = new SyncInstrumentItemInfo();
  205. dictInstruTables.Clear();
  206. cbxCnas.DataSource = null;
  207. cbxCnas.Items.Clear();
  208. cbxInstrument.DataSource = null;
  209. dgvInstruDS.DataSource = null;
  210. dgvCnas.DataSource = null;
  211. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  212. }
  213. }
  214. private void dgvInstrument_SelectionChanged(object sender, EventArgs e)
  215. {
  216. if (dgvInstrument.Rows.Count <= 0 || dgvInstrument.Rows[dgvInstrument.CurrentRow.Index].Cells[0].Value == null)
  217. {
  218. currentSyncItem = new SyncInstrumentItemInfo();
  219. return;
  220. }
  221. //清空绑定
  222. cbxCnas.DataSource = null;
  223. cbxInstrument.DataSource = null;
  224. dgvInstruDS.DataSource = null;
  225. dgvCnas.DataSource = null;
  226. //当前选中单元格
  227. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  228. var lstInstrument = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  229. if (lstInstrument.Count == 1)
  230. {
  231. currentSyncItem = lstInstrument[0];
  232. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn))
  233. txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  234. else
  235. txtInstrumentColumn.Text = "";
  236. if (currentSyncItem.SyncInstrumentDSInfo != null && currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType != DataSourceType.None)
  237. {
  238. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  239. if (currentSyncItem.SyncInstrumentDSInfo.ServerName != "")
  240. btnLoadDBData_Click(sender, e);
  241. }
  242. else
  243. {
  244. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  245. }
  246. }
  247. else
  248. {
  249. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  250. }
  251. }
  252. /// <summary>
  253. /// 切换选中表时发生
  254. /// </summary>
  255. /// <param name="sender"></param>
  256. /// <param name="e"></param>
  257. private void cbxInstrument_SelectedIndexChanged(object sender, EventArgs e)
  258. {
  259. if (cbxInstrument.Text == null) return;
  260. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  261. //dictInstruTables = instrumentData.GetInstrumentData();
  262. //dictInstruTables =
  263. DataTable dtTableType = null;
  264. string strTableName_Instru = cbxInstrument.Text.ToString();
  265. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  266. {
  267. case DataSourceType.MySQL:
  268. dtTableType = SelectTableType.MySqlsec(strTableName_Instru);
  269. break;
  270. case DataSourceType.Dm:
  271. dtTableType = SelectTableType.DmSql(strTableName_Instru);
  272. break;
  273. case DataSourceType.Oracle:
  274. dtTableType = SelectTableType.OrcSql(strTableName_Instru, currentSyncItem);
  275. break;
  276. case DataSourceType.PostgreSQL:
  277. dtTableType = SelectTableType.PostgreSql(strTableName_Instru);
  278. break;
  279. case DataSourceType.SQL:
  280. dtTableType = SelectTableType.Sqlserversec(strTableName_Instru, currentSyncItem);
  281. break;
  282. default:
  283. break;
  284. }
  285. DataTable dtInstruShow = new DataTable();
  286. dtInstruShow.Columns.Add("InstruFieldName");
  287. dtInstruShow.Columns.Add("InstruDataType");
  288. dtInstruShow.Columns.Add("remark");
  289. for (int i = 0; i < dtTableType.Rows.Count; i++)
  290. {
  291. dtInstruShow.Rows.Add(new object[] { dtTableType.Rows[i]["ColumnName"], dtTableType.Rows[i]["DataType"], dtTableType.Rows[i]["remark"] });
  292. }
  293. dgvInstruDS.DataSource = dtInstruShow;
  294. }
  295. /// <summary>
  296. /// 切换表时发生
  297. /// </summary>
  298. /// <param name="sender"></param>
  299. /// <param name="e"></param>
  300. private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
  301. {
  302. if (cbxCnas.Text == null) return;
  303. string strTableName_Cnas = cbxCnas.Text.ToString();
  304. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Cnas, currentSyncItem.SyncTargetDBInfo);
  305. //从数据库中加载数据表结构
  306. //DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
  307. if (dtTableStruct != null)
  308. {
  309. DataTable dtCnasShow = new DataTable();
  310. dtCnasShow.Columns.Add("CnasFieldName");
  311. dtCnasShow.Columns.Add("CnasDataType");
  312. dtCnasShow.Columns.Add("remark");
  313. for (int i = 0; i < dtTableStruct.Rows.Count; i++)
  314. {
  315. dtCnasShow.Rows.Add(new object[] { dtTableStruct.Rows[i]["ColumnName"], dtTableStruct.Rows[i]["DataType"], dtTableStruct.Rows[i]["remark"] });
  316. }
  317. dgvCnas.DataSource = dtCnasShow;
  318. //绑定数据
  319. cbxCNASColumn.DataSource = dtCnasShow.Copy();
  320. cbxCNASColumn.DisplayMember = "CnasFieldName";
  321. cbxCNASColumn.ValueMember = "CnasFieldName";
  322. if (dtTableStruct.Columns.Count > 0)
  323. {
  324. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn) && dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
  325. {
  326. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  327. }
  328. else
  329. {
  330. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
  331. }
  332. }
  333. }
  334. }
  335. //增加映射
  336. private void btnAddMapping_Click(object sender, EventArgs e)
  337. {
  338. if (cbxInstrument.Text == null) return;
  339. if (cbxCnas.Text == null) return;
  340. if (dgvInstruDS.Rows.Count <= 0) return;
  341. if (dgvCnas.Rows.Count <= 0) return;
  342. SyncParamasInfo syncParamas = new SyncParamasInfo();
  343. if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
  344. if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
  345. {
  346. MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
  347. return;
  348. }
  349. //仪器表名和选中行
  350. syncParamas.SourceTable = cbxInstrument.Text.ToString();
  351. syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString();
  352. syncParamas.DataType = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[1].Value.ToString().ToUpper();
  353. //CNAS表名和选中行
  354. syncParamas.TargetTable = cbxCnas.Text.ToString();
  355. syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString();
  356. //验证数据合法性
  357. SyncParamsOperation paramsOperation = new SyncParamsOperation();
  358. if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
  359. {
  360. MessageBox.Show("已存在不同表单映射数据,无法添加!");
  361. return;
  362. }
  363. //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
  364. //{
  365. // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
  366. // return;
  367. //}
  368. if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
  369. {
  370. MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
  371. return;
  372. }
  373. if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
  374. {
  375. MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
  376. return;
  377. }
  378. //绑定数据
  379. currentSyncItem.LstSyncPramas.Add(syncParamas);
  380. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  381. //选中最后一行
  382. dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count - 1].Cells[0];
  383. }
  384. /// <summary>
  385. /// 删除映射
  386. /// </summary>
  387. /// <param name="sender"></param>
  388. /// <param name="e"></param>
  389. private void btnDelMap_Click(object sender, EventArgs e)
  390. {
  391. //当前选中项
  392. if (dgvMapping.CurrentCell == null) return;
  393. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  394. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  395. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  396. if (lstDelItems.Count > 0)
  397. {
  398. foreach (var item in lstDelItems)
  399. {
  400. currentSyncItem.LstSyncPramas.Remove(item);
  401. }
  402. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  403. }
  404. }
  405. /// <summary>
  406. /// 配置数据库界面
  407. /// </summary>
  408. /// <param name="sender"></param>
  409. /// <param name="e"></param>
  410. private void btnDatabaseConfig_Click(object sender, EventArgs e)
  411. {
  412. frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
  413. frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  414. {
  415. this.currentSyncItem = Instrumentitem;
  416. };
  417. frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
  418. {
  419. //this.dictInstruTables = dict;
  420. };
  421. frmDatabase.ShowDialog();
  422. //加载数据
  423. if ((currentSyncItem.SyncInstrumentDSInfo.Host != null && currentSyncItem.SyncInstrumentDSInfo.Host.Length > 0) || (currentSyncItem.SyncInstrumentDSInfo.Path != null && currentSyncItem.SyncInstrumentDSInfo.Path.Length > 0))
  424. btnLoadDBData_Click(null, null);
  425. }
  426. private void btnLoadDBData_Click(object sender, EventArgs e)
  427. {
  428. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  429. strTableInfoMode = FileOperation.GetSystemFormatConfigData(strInstrumentCode).TableInfoMode;
  430. if (strTableInfoMode == "1")
  431. {
  432. cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  433. //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  434. }
  435. int iReturn = 0;
  436. if (strTableInfoMode == "0")
  437. iReturn = LoadSourceAndTargetData(true);
  438. else
  439. iReturn = LoadSourceAndTargetData();
  440. switch (iReturn)
  441. {
  442. case -1:
  443. MessageBox.Show("未能成功获取设备数据库配置信息,请配置后重试!");
  444. break;
  445. case -2:
  446. MessageBox.Show("未能成功获取CNAS数据库配置信息,请配置后重试!");
  447. break;
  448. case -3:
  449. MessageBox.Show("未能成功获取仪器数据信息,请配置后重试!");
  450. break;
  451. case -4:
  452. MessageBox.Show("未能成功获取CNAS数据信息,请配置后重试!");
  453. break;
  454. case -5:
  455. DialogResult dr = MessageBox.Show("检测到数据连接配置已经修改,是否全部删除已经分配的字段映射?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  456. if (dr == DialogResult.Yes)
  457. {
  458. //此时将全部删除已经分配的映射字段
  459. currentSyncItem.LstSyncPramas.Clear();
  460. currentSyncItem.lstFixedValue.Clear();
  461. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  462. }
  463. break;
  464. case -6:
  465. MessageBox.Show("请先手动输入表名");
  466. break;
  467. default:
  468. break;
  469. }
  470. }
  471. /// <summary>
  472. /// 加载来源和目标数据的数据结构
  473. /// </summary>
  474. /// <param name="bIfLoading"></param>
  475. /// <returns></returns>
  476. public int LoadSourceAndTargetData(bool bIfLoading = false)
  477. {
  478. //检查配置信息
  479. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  480. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  481. bool bIfSameTable = true;
  482. //是否需要重新加载来源库
  483. if (bIfLoading)
  484. {
  485. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  486. dictInstruTables = instrumentData.GetInstrumentData();
  487. }
  488. if (dictInstruTables.Count <= 0) return -3;
  489. //绑定ComboBox
  490. List<string> lstTableName = new List<string>();
  491. foreach (var item in dictInstruTables)
  492. {
  493. lstTableName.Add(item.Key);
  494. }
  495. cbxInstrument.DataSource = lstTableName;
  496. cbxInstrument.ValueMember = "";
  497. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  498. {
  499. if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
  500. cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
  501. else
  502. bIfSameTable = false;
  503. }
  504. //获取CNAS配置的数据库连接信息
  505. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  506. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  507. {
  508. List<string> lstCnasTables = new List<string>();
  509. foreach (DataRow dr in dtCNAS.Rows)
  510. {
  511. if (dtCNAS.Columns.Contains("TABNAME"))
  512. lstCnasTables.Add(dr["TABNAME"].ToString());
  513. else if (dtCNAS.Columns.Contains("table_name"))
  514. lstCnasTables.Add(dr["table_name"].ToString());
  515. }
  516. cbxCnas.DataSource = lstCnasTables;
  517. cbxCnas.ValueMember = "";
  518. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  519. {
  520. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  521. {
  522. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  523. //cbxCnas_SelectedIndexChanged(null, null);
  524. }
  525. else
  526. bIfSameTable = false;
  527. }
  528. }
  529. else
  530. return -4;
  531. if (!bIfSameTable)
  532. {
  533. return -5;
  534. }
  535. return 1;
  536. }
  537. /// <summary>
  538. /// 手动输入表名时,获取该表的表信息
  539. /// </summary>
  540. /// <returns></returns>
  541. public int LoadSourceAndTargetData()
  542. {
  543. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  544. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  545. SqlServerFormatConfig SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  546. string sql = SqlServerFormat.AutoSql.SqlServerViewSql;
  547. cbxInstrument.Text = ExtractTableNames(sql);
  548. if (cbxInstrument.Text == "") return -6;
  549. int returnValue = 1;
  550. //根据手动输入来源库的表名加载字段
  551. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  552. //dictInstruTables = instrumentData.GetInstrumentData();
  553. //dictInstruTables =
  554. DataTable dataTableStruct = null;
  555. string strTableName_Instru = cbxInstrument.Text.ToString();
  556. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  557. {
  558. case DataSourceType.MySQL:
  559. dataTableStruct = SelectTableType.MySqlsec(cbxInstrument.Text);
  560. break;
  561. case DataSourceType.Dm:
  562. dataTableStruct = SelectTableType.DmSql(cbxInstrument.Text);
  563. break;
  564. case DataSourceType.Oracle:
  565. dataTableStruct = SelectTableType.OrcSql(cbxInstrument.Text, currentSyncItem);
  566. break;
  567. case DataSourceType.PostgreSQL:
  568. dataTableStruct = SelectTableType.PostgreSql(cbxInstrument.Text);
  569. break;
  570. case DataSourceType.SQL:
  571. dataTableStruct = SelectTableType.Sqlserversec(cbxInstrument.Text, currentSyncItem);
  572. break;
  573. default:
  574. break;
  575. }
  576. DataTable dtInstruShow = new DataTable();
  577. dtInstruShow.Columns.Add("InstruFieldName");
  578. dtInstruShow.Columns.Add("InstruDataType");
  579. dtInstruShow.Columns.Add("remark");
  580. if (dataTableStruct != null)
  581. {
  582. for (int i = 0; i < dataTableStruct.Rows.Count; i++)
  583. {
  584. dtInstruShow.Rows.Add(new object[] { dataTableStruct.Rows[i]["ColumnName"], dataTableStruct.Rows[i]["DataType"], dataTableStruct.Rows[i]["remark"] });
  585. }
  586. }
  587. dgvInstruDS.DataSource = dtInstruShow;
  588. if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
  589. {
  590. dictInstruTables.Clear();
  591. dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
  592. //return 1;
  593. }
  594. else
  595. {
  596. returnValue = -3;
  597. return returnValue;
  598. }
  599. //获取CNAS配置的数据库连接信息
  600. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  601. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  602. {
  603. List<string> lstCnasTables = new List<string>();
  604. foreach (DataRow dr in dtCNAS.Rows)
  605. {
  606. lstCnasTables.Add(dr["TABNAME"].ToString());
  607. }
  608. cbxCnas.DataSource = lstCnasTables;
  609. cbxCnas.ValueMember = "";
  610. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  611. {
  612. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  613. {
  614. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  615. //cbxCnas_SelectedIndexChanged(null, null);
  616. }
  617. else
  618. returnValue = -5;
  619. }
  620. }
  621. else
  622. returnValue = -4;
  623. return returnValue;
  624. }
  625. public string ExtractTableNames(string sql)
  626. {
  627. // 预处理:去除注释、统一空格
  628. sql = Regex.Replace(sql, @"(--.*)|(\/\*[\s\S]*?\*\/)", "", RegexOptions.Multiline);
  629. sql = Regex.Replace(sql, @"\s+", " ");
  630. // 定义匹配模式
  631. var patterns = new Dictionary<string, string>
  632. {
  633. { "SELECT", @"(?:FROM|JOIN)\s+([\w\.]+)(?:\s+AS\s+\w+)?" },
  634. { "INSERT", @"INSERT\s+INTO\s+([\w\.]+)" },
  635. { "UPDATE", @"UPDATE\s+([\w\.]+)" },
  636. { "DELETE", @"DELETE\s+FROM\s+([\w\.]+)" }
  637. };
  638. var tables = new HashSet<string>();
  639. foreach (var pattern in patterns.Values)
  640. {
  641. var matches = Regex.Matches(sql, pattern, RegexOptions.IgnoreCase);
  642. foreach (Match match in matches)
  643. {
  644. if (match.Groups[1].Success)
  645. tables.Add(match.Groups[1].Value.Trim());
  646. }
  647. }
  648. if (tables.Count == 0)
  649. return "";
  650. else
  651. return tables.ToList()[0];
  652. }
  653. private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  654. {
  655. if (dgvMapping.CurrentCell == null) return;
  656. if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
  657. {
  658. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  659. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  660. string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
  661. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  662. if (lstDelItems.Count > 0)
  663. {
  664. foreach (var item in lstDelItems)
  665. {
  666. item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
  667. }
  668. }
  669. }
  670. if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
  671. {
  672. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  673. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  674. string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
  675. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  676. if (lstDelItems.Count == 1)
  677. {
  678. if (strDateKey.ToLower() == "true")
  679. {
  680. //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
  681. foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
  682. {
  683. if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
  684. dgvRow.Cells["DateKey"].Value = false;
  685. }
  686. //内存数据源
  687. foreach (var item in currentSyncItem.LstSyncPramas)
  688. {
  689. if (item.IfDateField)
  690. item.IfDateField = false;
  691. }
  692. lstDelItems[0].IfDateField = true;
  693. }
  694. else
  695. {
  696. lstDelItems[0].IfDateField = false;
  697. }
  698. }
  699. }
  700. }
  701. private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  702. {
  703. if (dgvMapping.IsCurrentCellDirty)
  704. {
  705. dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
  706. }
  707. }
  708. private void btnCNASFieldConfig_Click(object sender, EventArgs e)
  709. {
  710. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  711. {
  712. MessageBox.Show("请先指定至少一个映射字段。");
  713. return;
  714. }
  715. frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
  716. if (frmCNAS.ShowDialog() == DialogResult.OK)
  717. {
  718. this.currentSyncItem = frmCNAS.syncInstrument;
  719. }
  720. }
  721. private void btnSourceFilter_Click(object sender, EventArgs e)
  722. {
  723. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  724. {
  725. MessageBox.Show("请先指定至少一个映射字段。");
  726. return;
  727. }
  728. frmSourceFilter frm = new frmSourceFilter(currentSyncItem, strTableInfoMode);
  729. frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
  730. {
  731. currentSyncItem.SourceFilter = dataFilter;
  732. };
  733. frm.ShowDialog();
  734. }
  735. private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  736. {
  737. if (dgvInstrument.Rows.Count <= 0) return;
  738. //当前选中单元格
  739. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  740. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  741. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  742. {
  743. lstSyncInstrument.Add(Instrumentitem);
  744. //绑定数据
  745. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  746. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  747. dgvInstrument_SelectionChanged(null, null);
  748. };
  749. frmSetting.ShowDialog();
  750. }
  751. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  752. {
  753. if (cbxCNASColumn.Visible)
  754. {
  755. cbxCNASColumn.Visible = false;
  756. txtInstrumentColumn.Visible = true;
  757. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  758. }
  759. }
  760. /// <summary>
  761. /// 当双击textBox时,显示ComboBOX用于选择
  762. /// </summary>
  763. /// <param name="sender"></param>
  764. /// <param name="e"></param>
  765. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  766. {
  767. txtInstrumentColumn.Visible = false;
  768. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  769. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  770. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  771. cbxCNASColumn.Location = txtInstrumentColumn.Location;
  772. cbxCNASColumn.Visible = true;
  773. }
  774. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  775. {
  776. if (cbxCNASColumn.Visible)
  777. {
  778. txtInstrumentColumn.Visible = true;
  779. cbxCNASColumn.Visible = false;
  780. }
  781. }
  782. private void tsmServiceSetting_Click(object sender, EventArgs e)
  783. {
  784. frmServiceConfig frmServiceConfig = new frmServiceConfig();
  785. frmServiceConfig.ShowDialog();
  786. }
  787. private void tsmSystemSetting_Click(object sender, EventArgs e)
  788. {
  789. //frmSystemSetting frmSystem = new frmSystemSetting();
  790. //frmSystem.ShowDialog();
  791. //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
  792. //if (strTableInfoMode == "1")
  793. //{
  794. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  795. // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  796. //}
  797. //else
  798. //{
  799. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
  800. //}
  801. }
  802. private void tsmSourceSetting_Click(object sender, EventArgs e)
  803. {
  804. frmSourceSetting frmSource = new frmSourceSetting();
  805. frmSource.ShowDialog();
  806. }
  807. private void tsmHelper_Click(object sender, EventArgs e)
  808. {
  809. string strHelpFilePath = FileHelper.getBasePath() + @"\Helper.CHM";
  810. //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
  811. //Help.ShowHelpIndex(this, strHelpFilePath);
  812. System.Diagnostics.Process.Start(strHelpFilePath);
  813. }
  814. }
  815. }