CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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