CNAS取数仪器端升级
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1037 Zeilen
45KB

  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. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  239. {
  240. case DataSourceType.MySQL:
  241. case DataSourceType.Dm:
  242. case DataSourceType.Oracle:
  243. case DataSourceType.PostgreSQL:
  244. case DataSourceType.SQL:
  245. case DataSourceType.Kingbase:
  246. if (currentSyncItem.LstSyncPramas != null)
  247. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  248. if (currentSyncItem.SyncInstrumentDSInfo.ServerName != "")
  249. btnLoadDBData_Click(sender, e);
  250. break;
  251. default:
  252. btnLoadDBData_Click(sender, e);
  253. break;
  254. }
  255. }
  256. else
  257. {
  258. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  259. }
  260. }
  261. else
  262. {
  263. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  264. }
  265. }
  266. /// <summary>
  267. /// 切换选中表时发生
  268. /// </summary>
  269. /// <param name="sender"></param>
  270. /// <param name="e"></param>
  271. private void cbxInstrument_SelectedIndexChanged(object sender, EventArgs e)
  272. {
  273. if (cbxInstrument.Text == null) return;
  274. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  275. // 使用LINQ查找匹配的数据
  276. SyncInstrumentItemInfo matchedInstrument = lstSyncInstrument.FirstOrDefault(x => x.Code == strInstrumentCode);
  277. // InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  278. DataTable dtTableType = null;
  279. string strTableName_Instru = cbxInstrument.Text.ToString();
  280. if (matchedInstrument.SyncInstrumentDSInfo != null)
  281. {
  282. switch (matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType)
  283. {
  284. case DataSourceType.MySQL:
  285. dtTableType = SelectTableType.MySqlsec(strTableName_Instru);
  286. break;
  287. case DataSourceType.Dm:
  288. dtTableType = SelectTableType.DmSql(strTableName_Instru);
  289. break;
  290. case DataSourceType.Oracle:
  291. dtTableType = SelectTableType.OrcSql(strTableName_Instru, currentSyncItem);
  292. break;
  293. case DataSourceType.PostgreSQL:
  294. dtTableType = SelectTableType.PostgreSql(strTableName_Instru);
  295. break;
  296. case DataSourceType.SQL:
  297. dtTableType = SelectTableType.Sqlserversec(strTableName_Instru, currentSyncItem);
  298. break;
  299. case DataSourceType.Kingbase:
  300. dtTableType = SelectTableType.KingSql(strTableName_Instru);
  301. break;
  302. default:
  303. strTableName_Instru = matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType.ToString();
  304. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(matchedInstrument.SyncInstrumentDSInfo, new object[] { "", "", "" });
  305. dictInstruTables = instrumentData.GetInstrumentData();
  306. if (dictInstruTables.ContainsKey(strTableName_Instru))
  307. {
  308. dtTableType = dictInstruTables[strTableName_Instru];
  309. }
  310. break;
  311. }
  312. DataTable dtInstruShow = new DataTable();
  313. dtInstruShow.Columns.Add("InstruFieldName");
  314. dtInstruShow.Columns.Add("InstruDataType");
  315. dtInstruShow.Columns.Add("remark");
  316. if (dtTableType != null)
  317. {
  318. switch (matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType)
  319. {
  320. case DataSourceType.MySQL:
  321. case DataSourceType.Dm:
  322. case DataSourceType.Oracle:
  323. case DataSourceType.PostgreSQL:
  324. case DataSourceType.SQL:
  325. case DataSourceType.Kingbase:
  326. for (int i = 0; i < dtTableType.Rows.Count; i++)
  327. {
  328. dtInstruShow.Rows.Add(new object[] { dtTableType.Rows[i]["ColumnName"], dtTableType.Rows[i]["DataType"], dtTableType.Rows[i]["remark"] });
  329. }
  330. break;
  331. default:
  332. foreach (DataColumn dc in dtTableType.Columns)
  333. {
  334. dtInstruShow.Rows.Add(new object[] { dc.ColumnName, dc.DataType, "" });
  335. }
  336. break;
  337. }
  338. }
  339. dgvInstruDS.DataSource = dtInstruShow;
  340. }
  341. }
  342. /// <summary>
  343. /// 切换表时发生
  344. /// </summary>
  345. /// <param name="sender"></param>
  346. /// <param name="e"></param>
  347. private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
  348. {
  349. if (cbxCnas.Text == null) return;
  350. string strTableName_Cnas = cbxCnas.Text.ToString();
  351. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Cnas, currentSyncItem.SyncTargetDBInfo);
  352. //从数据库中加载数据表结构
  353. //DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
  354. if (dtTableStruct != null)
  355. {
  356. DataTable dtCnasShow = new DataTable();
  357. dtCnasShow.Columns.Add("CnasFieldName");
  358. dtCnasShow.Columns.Add("CnasDataType");
  359. dtCnasShow.Columns.Add("remark");
  360. for (int i = 0; i < dtTableStruct.Rows.Count; i++)
  361. {
  362. dtCnasShow.Rows.Add(new object[] { dtTableStruct.Rows[i]["ColumnName"], dtTableStruct.Rows[i]["DataType"], dtTableStruct.Rows[i]["remark"] });
  363. }
  364. dgvCnas.DataSource = null;
  365. dgvCnas.DataSource = dtCnasShow;
  366. //绑定数据
  367. cbxCNASColumn.DataSource = dtCnasShow.Copy();
  368. cbxCNASColumn.DisplayMember = "CnasFieldName";
  369. cbxCNASColumn.ValueMember = "CnasFieldName";
  370. if (dtTableStruct.Columns.Count > 0)
  371. {
  372. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn) && dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
  373. {
  374. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  375. }
  376. else
  377. {
  378. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
  379. }
  380. }
  381. }
  382. }
  383. //增加映射
  384. private void btnAddMapping_Click(object sender, EventArgs e)
  385. {
  386. if (cbxInstrument.Text == null) return;
  387. if (cbxCnas.Text == null) return;
  388. if (dgvInstruDS.Rows.Count <= 0) return;
  389. if (dgvCnas.Rows.Count <= 0) return;
  390. SyncParamasInfo syncParamas = new SyncParamasInfo();
  391. if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
  392. if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
  393. {
  394. MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
  395. return;
  396. }
  397. //仪器表名和选中行
  398. syncParamas.SourceTable = cbxInstrument.Text.ToString();
  399. syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString();
  400. syncParamas.DataType = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[1].Value.ToString().ToUpper();
  401. //CNAS表名和选中行
  402. syncParamas.TargetTable = cbxCnas.Text.ToString();
  403. syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString();
  404. //验证数据合法性
  405. SyncParamsOperation paramsOperation = new SyncParamsOperation();
  406. if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
  407. {
  408. MessageBox.Show("已存在不同表单映射数据,无法添加!");
  409. return;
  410. }
  411. //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
  412. //{
  413. // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
  414. // return;
  415. //}
  416. if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
  417. {
  418. MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
  419. return;
  420. }
  421. if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
  422. {
  423. MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
  424. return;
  425. }
  426. //绑定数据
  427. currentSyncItem.LstSyncPramas.Add(syncParamas);
  428. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  429. //选中最后一行
  430. dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count - 1].Cells[0];
  431. }
  432. /// <summary>
  433. /// 删除映射
  434. /// </summary>
  435. /// <param name="sender"></param>
  436. /// <param name="e"></param>
  437. private void btnDelMap_Click(object sender, EventArgs e)
  438. {
  439. //当前选中项
  440. if (dgvMapping.CurrentCell == null) return;
  441. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  442. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  443. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  444. if (lstDelItems.Count > 0)
  445. {
  446. foreach (var item in lstDelItems)
  447. {
  448. currentSyncItem.LstSyncPramas.Remove(item);
  449. }
  450. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  451. }
  452. }
  453. /// <summary>
  454. /// 配置数据库界面
  455. /// </summary>
  456. /// <param name="sender"></param>
  457. /// <param name="e"></param>
  458. private void btnDatabaseConfig_Click(object sender, EventArgs e)
  459. {
  460. frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
  461. frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  462. {
  463. this.currentSyncItem = Instrumentitem;
  464. };
  465. frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
  466. {
  467. //this.dictInstruTables = dict;
  468. };
  469. frmDatabase.ShowDialog();
  470. //加载数据
  471. if ((currentSyncItem.SyncInstrumentDSInfo.Host != null && currentSyncItem.SyncInstrumentDSInfo.Host.Length > 0) || (currentSyncItem.SyncInstrumentDSInfo.Path != null && currentSyncItem.SyncInstrumentDSInfo.Path.Length > 0))
  472. btnLoadDBData_Click(null, null);
  473. }
  474. private void btnLoadDBData_Click(object sender, EventArgs e)
  475. {
  476. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  477. strTableInfoMode = FileOperation.GetSystemFormatConfigData(strInstrumentCode).TableInfoMode;
  478. if (strTableInfoMode == "1")
  479. {
  480. cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  481. //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  482. }
  483. int iReturn = 0;
  484. if (strTableInfoMode == "0")
  485. iReturn = LoadSourceAndTargetData(true);
  486. else
  487. iReturn = LoadSourceAndTargetData();
  488. switch (iReturn)
  489. {
  490. case -1:
  491. MessageBox.Show("未能成功获取设备数据库配置信息,请配置后重试!");
  492. break;
  493. case -2:
  494. MessageBox.Show("未能成功获取CNAS数据库配置信息,请配置后重试!");
  495. break;
  496. case -3:
  497. MessageBox.Show("未能成功获取仪器数据信息,请配置后重试!");
  498. break;
  499. case -4:
  500. MessageBox.Show("未能成功获取CNAS数据信息,请配置后重试!");
  501. break;
  502. case -5:
  503. DialogResult dr = MessageBox.Show("检测到数据连接配置已经修改,是否全部删除已经分配的字段映射?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  504. if (dr == DialogResult.Yes)
  505. {
  506. //此时将全部删除已经分配的映射字段
  507. currentSyncItem.LstSyncPramas.Clear();
  508. currentSyncItem.lstFixedValue.Clear();
  509. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  510. }
  511. break;
  512. case -6:
  513. MessageBox.Show("请先手动输入表名");
  514. break;
  515. case -7:
  516. MessageBox.Show("配置SQL查询没有结果,请检查SQL是否正确");
  517. break;
  518. default:
  519. break;
  520. }
  521. }
  522. /// <summary>
  523. /// 加载来源和目标数据的数据结构
  524. /// </summary>
  525. /// <param name="bIfLoading"></param>
  526. /// <returns></returns>
  527. public int LoadSourceAndTargetData(bool bIfLoading = false)
  528. {
  529. //检查配置信息
  530. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  531. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  532. bool bIfSameTable = true;
  533. //是否需要重新加载来源库
  534. if (bIfLoading)
  535. {
  536. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  537. dictInstruTables = instrumentData.GetInstrumentData();
  538. }
  539. if (dictInstruTables.Count <= 0) return -3;
  540. //绑定ComboBox
  541. List<string> lstTableName = new List<string>();
  542. foreach (var item in dictInstruTables)
  543. {
  544. lstTableName.Add(item.Key);
  545. }
  546. cbxInstrument.DataSource = lstTableName;
  547. cbxInstrument.ValueMember = "";
  548. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  549. {
  550. if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
  551. cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
  552. else
  553. bIfSameTable = false;
  554. }
  555. //获取CNAS配置的数据库连接信息
  556. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  557. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  558. {
  559. List<string> lstCnasTables = new List<string>();
  560. foreach (DataRow dr in dtCNAS.Rows)
  561. {
  562. if (dtCNAS.Columns.Contains("TABNAME"))
  563. lstCnasTables.Add(dr["TABNAME"].ToString());
  564. else if (dtCNAS.Columns.Contains("table_name"))
  565. lstCnasTables.Add(dr["table_name"].ToString());
  566. }
  567. cbxCnas.DataSource = lstCnasTables;
  568. cbxCnas.ValueMember = "";
  569. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  570. {
  571. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  572. {
  573. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  574. //cbxCnas_SelectedIndexChanged(null, null);
  575. }
  576. else
  577. bIfSameTable = false;
  578. }
  579. }
  580. else
  581. return -4;
  582. if (!bIfSameTable)
  583. {
  584. return -5;
  585. }
  586. return 1;
  587. }
  588. public AccessFormatConfig AccessFormat { get; set; }
  589. public ExcelFormatConfig ExcelFormat { get; set; }
  590. public FoxProFormatConfig FoxProFormat { get; set; }
  591. public SqliteFormatConfig SqliteFormat { get; set; }
  592. public SqlServerFormatConfig SqlServerFormat { get; set; }
  593. public OracleFormatConfig OracleFormat { get; set; }
  594. public MySqlFormatConfig MySqlFormat { get; set; }
  595. public NormalFileFormatConfig NormalFileFormat { get; set; }
  596. public PostgreSqlFormatConfig PostgreSqlFormat { get; set; }
  597. public KingbaseFormatConfig KingbaseFormat { get; set; }
  598. public DmFormatConfig DmFormat { get; set; }
  599. /// <summary>
  600. /// 手动输入表名时,获取该表的表信息
  601. /// </summary>
  602. /// <returns></returns>
  603. public int LoadSourceAndTargetData()
  604. {
  605. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  606. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  607. string sqlName = "";
  608. string sql = "";
  609. ExcelFormat = FileOperation.GetFormatConfigData<ExcelFormatConfig>("ExcelFormatConfig.xml");
  610. AccessFormat = FileOperation.GetFormatConfigData<AccessFormatConfig>("AccessFormatConfig.xml");
  611. FoxProFormat = FileOperation.GetFormatConfigData<FoxProFormatConfig>("FoxProFormatConfig.xml");
  612. SqliteFormat = FileOperation.GetFormatConfigData<SqliteFormatConfig>("SqliteFormatConfig.xml");
  613. SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  614. OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
  615. NormalFileFormat = FileOperation.GetFormatConfigData<NormalFileFormatConfig>("NormalFileFormatConfig.xml");
  616. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  617. {
  618. case DataSourceType.MySQL:
  619. MySqlFormat = FileOperation.GetFormatConfigData<MySqlFormatConfig>("MySqlFormatConfig.xml");
  620. sqlName = MySqlFormat.AutoSql.MySqlViewName;
  621. sql = MySqlFormat.AutoSql.MySqlViewSql;
  622. break;
  623. case DataSourceType.Dm:
  624. DmFormat = FileOperation.GetFormatConfigData<DmFormatConfig>("DmFormatConfig.xml");
  625. sqlName = DmFormat.AutoSql.DmViewName;
  626. sql = DmFormat.AutoSql.DmViewSql;
  627. break;
  628. case DataSourceType.Oracle:
  629. OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
  630. sqlName = OracleFormat.AutoSql.OracleViewName;
  631. sql = OracleFormat.AutoSql.OracleViewSql;
  632. break;
  633. case DataSourceType.PostgreSQL:
  634. PostgreSqlFormat = FileOperation.GetFormatConfigData<PostgreSqlFormatConfig>("PostgreSqlFormatConfig.xml");
  635. sqlName = PostgreSqlFormat.AutoSql.PostgreSqlViewName;
  636. sql = PostgreSqlFormat.AutoSql.PostgreSqlViewSql;
  637. break;
  638. case DataSourceType.SQL:
  639. SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  640. sqlName = SqlServerFormat.AutoSql.SqlServerViewName;
  641. sql = SqlServerFormat.AutoSql.SqlServerViewSql;
  642. break;
  643. case DataSourceType.Kingbase:
  644. KingbaseFormat = FileOperation.GetFormatConfigData<KingbaseFormatConfig>("KingbaseFormatConfig.xml");
  645. sqlName = KingbaseFormat.AutoSql.KingbaseViewName;
  646. sql = KingbaseFormat.AutoSql.KingbaseViewSql;
  647. break;
  648. default:
  649. break;
  650. }
  651. //cbxInstrument.Text = ExtractTableNames(sql);
  652. cbxInstrument.Text = sqlName;
  653. if (cbxInstrument.Text == "") return -6;
  654. int returnValue = 1;
  655. //根据手动输入来源库的表名加载字段
  656. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  657. //dictInstruTables = instrumentData.GetInstrumentData();
  658. //dictInstruTables =
  659. DataTable dataTableStruct = null;
  660. string strTableName_Instru = cbxInstrument.Text.ToString();
  661. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  662. {
  663. case DataSourceType.MySQL:
  664. dataTableStruct = SelectTableType.MySqlsecSD(sql);
  665. break;
  666. case DataSourceType.Dm:
  667. dataTableStruct = SelectTableType.DmSqlSD(sql);
  668. break;
  669. case DataSourceType.Oracle:
  670. dataTableStruct = SelectTableType.OrcSqlSD(sql, currentSyncItem);
  671. break;
  672. case DataSourceType.PostgreSQL:
  673. dataTableStruct = SelectTableType.PostgreSqlSD(sql);
  674. break;
  675. case DataSourceType.SQL:
  676. dataTableStruct = SelectTableType.SqlserversecSD(sql, currentSyncItem);
  677. break;
  678. case DataSourceType.Kingbase:
  679. if (cbxInstrument.Text.Contains('.'))
  680. cbxInstrument.Text = cbxInstrument.Text.Split('.')[1];
  681. dataTableStruct = SelectTableType.KingSql(sql);
  682. break;
  683. default:
  684. break;
  685. }
  686. DataTable dtInstruShow = new DataTable();
  687. dtInstruShow.Columns.Add("InstruFieldName");
  688. dtInstruShow.Columns.Add("InstruDataType");
  689. dtInstruShow.Columns.Add("remark");
  690. if (dataTableStruct != null)
  691. {
  692. for (int i = 0; i < dataTableStruct.Columns.Count; i++)
  693. {
  694. dtInstruShow.Rows.Add(new object[] { dataTableStruct.Columns[i].ColumnName, dataTableStruct.Columns[i].DataType });
  695. }
  696. }
  697. dgvInstruDS.DataSource = dtInstruShow;
  698. if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
  699. {
  700. dictInstruTables.Clear();
  701. dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
  702. //return 1;
  703. }
  704. else
  705. {
  706. returnValue = -7;
  707. return returnValue;
  708. }
  709. //获取CNAS配置的数据库连接信息
  710. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  711. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  712. {
  713. List<string> lstCnasTables = new List<string>();
  714. foreach (DataRow dr in dtCNAS.Rows)
  715. {
  716. lstCnasTables.Add(dr["TABNAME"].ToString());
  717. }
  718. cbxCnas.DataSource = lstCnasTables;
  719. cbxCnas.ValueMember = "";
  720. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  721. {
  722. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  723. {
  724. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  725. //cbxCnas_SelectedIndexChanged(null, null);
  726. }
  727. else
  728. returnValue = -5;
  729. }
  730. }
  731. else
  732. returnValue = -4;
  733. return returnValue;
  734. }
  735. public string ExtractTableNames(string sql)
  736. {
  737. // 预处理:去除注释、统一空格
  738. sql = Regex.Replace(sql, @"(--.*)|(\/\*[\s\S]*?\*\/)", "", RegexOptions.Multiline);
  739. sql = Regex.Replace(sql, @"\s+", " ");
  740. // 定义匹配模式
  741. var patterns = new Dictionary<string, string>
  742. {
  743. { "SELECT", @"(?:FROM|JOIN)\s+([\w\.]+)(?:\s+AS\s+\w+)?" },
  744. { "INSERT", @"INSERT\s+INTO\s+([\w\.]+)" },
  745. { "UPDATE", @"UPDATE\s+([\w\.]+)" },
  746. { "DELETE", @"DELETE\s+FROM\s+([\w\.]+)" }
  747. };
  748. var tables = new HashSet<string>();
  749. foreach (var pattern in patterns.Values)
  750. {
  751. var matches = Regex.Matches(sql, pattern, RegexOptions.IgnoreCase);
  752. foreach (Match match in matches)
  753. {
  754. if (match.Groups[1].Success)
  755. tables.Add(match.Groups[1].Value.Trim());
  756. }
  757. }
  758. if (tables.Count == 0)
  759. return "";
  760. else
  761. return tables.ToList()[0];
  762. }
  763. private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  764. {
  765. if (dgvMapping.CurrentCell == null) return;
  766. if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
  767. {
  768. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  769. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  770. string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
  771. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  772. if (lstDelItems.Count > 0)
  773. {
  774. foreach (var item in lstDelItems)
  775. {
  776. item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
  777. }
  778. }
  779. }
  780. if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
  781. {
  782. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  783. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  784. string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
  785. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  786. if (lstDelItems.Count == 1)
  787. {
  788. if (strDateKey.ToLower() == "true")
  789. {
  790. //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
  791. foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
  792. {
  793. if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
  794. dgvRow.Cells["DateKey"].Value = false;
  795. }
  796. //内存数据源
  797. foreach (var item in currentSyncItem.LstSyncPramas)
  798. {
  799. if (item.IfDateField)
  800. item.IfDateField = false;
  801. }
  802. lstDelItems[0].IfDateField = true;
  803. }
  804. else
  805. {
  806. lstDelItems[0].IfDateField = false;
  807. }
  808. }
  809. }
  810. }
  811. private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  812. {
  813. if (dgvMapping.IsCurrentCellDirty)
  814. {
  815. dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
  816. }
  817. }
  818. private void btnCNASFieldConfig_Click(object sender, EventArgs e)
  819. {
  820. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  821. {
  822. MessageBox.Show("请先指定至少一个映射字段。");
  823. return;
  824. }
  825. frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
  826. if (frmCNAS.ShowDialog() == DialogResult.OK)
  827. {
  828. this.currentSyncItem = frmCNAS.syncInstrument;
  829. }
  830. }
  831. private void btnSourceFilter_Click(object sender, EventArgs e)
  832. {
  833. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  834. {
  835. MessageBox.Show("请先指定至少一个映射字段。");
  836. return;
  837. }
  838. frmSourceFilter frm = new frmSourceFilter(currentSyncItem, strTableInfoMode);
  839. frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
  840. {
  841. currentSyncItem.SourceFilter = dataFilter;
  842. };
  843. frm.ShowDialog();
  844. }
  845. private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  846. {
  847. if (dgvInstrument.Rows.Count <= 0) return;
  848. //当前选中单元格
  849. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  850. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  851. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  852. {
  853. lstSyncInstrument.Add(Instrumentitem);
  854. //绑定数据
  855. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  856. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  857. dgvInstrument_SelectionChanged(null, null);
  858. };
  859. frmSetting.ShowDialog();
  860. }
  861. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  862. {
  863. if (cbxCNASColumn.Visible)
  864. {
  865. cbxCNASColumn.Visible = false;
  866. txtInstrumentColumn.Visible = true;
  867. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  868. }
  869. }
  870. /// <summary>
  871. /// 当双击textBox时,显示ComboBOX用于选择
  872. /// </summary>
  873. /// <param name="sender"></param>
  874. /// <param name="e"></param>
  875. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  876. {
  877. txtInstrumentColumn.Visible = false;
  878. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  879. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  880. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  881. cbxCNASColumn.Location = txtInstrumentColumn.Location;
  882. cbxCNASColumn.Visible = true;
  883. }
  884. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  885. {
  886. if (cbxCNASColumn.Visible)
  887. {
  888. txtInstrumentColumn.Visible = true;
  889. cbxCNASColumn.Visible = false;
  890. }
  891. }
  892. private void tsmServiceSetting_Click(object sender, EventArgs e)
  893. {
  894. frmServiceConfig frmServiceConfig = new frmServiceConfig();
  895. frmServiceConfig.ShowDialog();
  896. }
  897. private void tsmSystemSetting_Click(object sender, EventArgs e)
  898. {
  899. //frmSystemSetting frmSystem = new frmSystemSetting();
  900. //frmSystem.ShowDialog();
  901. //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
  902. //if (strTableInfoMode == "1")
  903. //{
  904. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  905. // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  906. //}
  907. //else
  908. //{
  909. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
  910. //}
  911. }
  912. private void tsmSourceSetting_Click(object sender, EventArgs e)
  913. {
  914. frmSourceSetting frmSource = new frmSourceSetting();
  915. frmSource.ShowDialog();
  916. }
  917. private void tsmHelper_Click(object sender, EventArgs e)
  918. {
  919. string strHelpFilePath = FileHelper.getBasePath() + @"\Helper.CHM";
  920. //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
  921. //Help.ShowHelpIndex(this, strHelpFilePath);
  922. System.Diagnostics.Process.Start(strHelpFilePath);
  923. }
  924. }
  925. }