CNAS取数仪器端升级
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1008 lignes
43KB

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