CNAS取数仪器端升级
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

991 lines
42KB

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