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.

982 lignes
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. default:
  473. break;
  474. }
  475. }
  476. /// <summary>
  477. /// 加载来源和目标数据的数据结构
  478. /// </summary>
  479. /// <param name="bIfLoading"></param>
  480. /// <returns></returns>
  481. public int LoadSourceAndTargetData(bool bIfLoading = false)
  482. {
  483. //检查配置信息
  484. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  485. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  486. bool bIfSameTable = true;
  487. //是否需要重新加载来源库
  488. if (bIfLoading)
  489. {
  490. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  491. dictInstruTables = instrumentData.GetInstrumentData();
  492. }
  493. if (dictInstruTables.Count <= 0) return -3;
  494. //绑定ComboBox
  495. List<string> lstTableName = new List<string>();
  496. foreach (var item in dictInstruTables)
  497. {
  498. lstTableName.Add(item.Key);
  499. }
  500. cbxInstrument.DataSource = lstTableName;
  501. cbxInstrument.ValueMember = "";
  502. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  503. {
  504. if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
  505. cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
  506. else
  507. bIfSameTable = false;
  508. }
  509. //获取CNAS配置的数据库连接信息
  510. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  511. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  512. {
  513. List<string> lstCnasTables = new List<string>();
  514. foreach (DataRow dr in dtCNAS.Rows)
  515. {
  516. if (dtCNAS.Columns.Contains("TABNAME"))
  517. lstCnasTables.Add(dr["TABNAME"].ToString());
  518. else if (dtCNAS.Columns.Contains("table_name"))
  519. lstCnasTables.Add(dr["table_name"].ToString());
  520. }
  521. cbxCnas.DataSource = lstCnasTables;
  522. cbxCnas.ValueMember = "";
  523. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  524. {
  525. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  526. {
  527. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  528. //cbxCnas_SelectedIndexChanged(null, null);
  529. }
  530. else
  531. bIfSameTable = false;
  532. }
  533. }
  534. else
  535. return -4;
  536. if (!bIfSameTable)
  537. {
  538. return -5;
  539. }
  540. return 1;
  541. }
  542. public AccessFormatConfig AccessFormat { get; set; }
  543. public ExcelFormatConfig ExcelFormat { get; set; }
  544. public FoxProFormatConfig FoxProFormat { get; set; }
  545. public SqliteFormatConfig SqliteFormat { get; set; }
  546. public SqlServerFormatConfig SqlServerFormat { get; set; }
  547. public OracleFormatConfig OracleFormat { get; set; }
  548. public MySqlFormatConfig MySqlFormat { get; set; }
  549. public NormalFileFormatConfig NormalFileFormat { get; set; }
  550. public PostgreSqlFormatConfig PostgreSqlFormat { get; set; }
  551. public KingbaseFormatConfig KingbaseFormat { get; set; }
  552. public DmFormatConfig DmFormat { get; set; }
  553. /// <summary>
  554. /// 手动输入表名时,获取该表的表信息
  555. /// </summary>
  556. /// <returns></returns>
  557. public int LoadSourceAndTargetData()
  558. {
  559. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  560. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  561. string sql = "";
  562. ExcelFormat = FileOperation.GetFormatConfigData<ExcelFormatConfig>("ExcelFormatConfig.xml");
  563. AccessFormat = FileOperation.GetFormatConfigData<AccessFormatConfig>("AccessFormatConfig.xml");
  564. FoxProFormat = FileOperation.GetFormatConfigData<FoxProFormatConfig>("FoxProFormatConfig.xml");
  565. SqliteFormat = FileOperation.GetFormatConfigData<SqliteFormatConfig>("SqliteFormatConfig.xml");
  566. SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  567. OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
  568. NormalFileFormat = FileOperation.GetFormatConfigData<NormalFileFormatConfig>("NormalFileFormatConfig.xml");
  569. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  570. {
  571. case DataSourceType.MySQL:
  572. MySqlFormat = FileOperation.GetFormatConfigData<MySqlFormatConfig>("MySqlFormatConfig.xml");
  573. sql = MySqlFormat.AutoSql.MySqlViewSql;
  574. break;
  575. case DataSourceType.Dm:
  576. DmFormat = FileOperation.GetFormatConfigData<DmFormatConfig>("DmFormatConfig.xml");
  577. sql = DmFormat.AutoSql.DmViewName;
  578. break;
  579. case DataSourceType.Oracle:
  580. OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
  581. sql = OracleFormat.AutoSql.OracleViewSql;
  582. break;
  583. case DataSourceType.PostgreSQL:
  584. PostgreSqlFormat = FileOperation.GetFormatConfigData<PostgreSqlFormatConfig>("PostgreSqlFormatConfig.xml");
  585. sql = PostgreSqlFormat.AutoSql.PostgreSqlViewSql;
  586. break;
  587. case DataSourceType.SQL:
  588. SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  589. sql = SqlServerFormat.AutoSql.SqlServerViewName;
  590. break;
  591. case DataSourceType.Kingbase:
  592. KingbaseFormat = FileOperation.GetFormatConfigData<KingbaseFormatConfig>("KingbaseFormatConfig.xml");
  593. sql = KingbaseFormat.AutoSql.KingbaseViewName;
  594. break;
  595. default:
  596. break;
  597. }
  598. //cbxInstrument.Text = ExtractTableNames(sql);
  599. cbxInstrument.Text = sql;
  600. if (cbxInstrument.Text == "") return -6;
  601. int returnValue = 1;
  602. //根据手动输入来源库的表名加载字段
  603. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  604. //dictInstruTables = instrumentData.GetInstrumentData();
  605. //dictInstruTables =
  606. DataTable dataTableStruct = null;
  607. string strTableName_Instru = cbxInstrument.Text.ToString();
  608. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  609. {
  610. case DataSourceType.MySQL:
  611. dataTableStruct = SelectTableType.MySqlsec(cbxInstrument.Text);
  612. break;
  613. case DataSourceType.Dm:
  614. dataTableStruct = SelectTableType.DmSql(cbxInstrument.Text);
  615. break;
  616. case DataSourceType.Oracle:
  617. dataTableStruct = SelectTableType.OrcSql(cbxInstrument.Text, currentSyncItem);
  618. break;
  619. case DataSourceType.PostgreSQL:
  620. dataTableStruct = SelectTableType.PostgreSql(cbxInstrument.Text);
  621. break;
  622. case DataSourceType.SQL:
  623. dataTableStruct = SelectTableType.Sqlserversec(cbxInstrument.Text, currentSyncItem);
  624. break;
  625. case DataSourceType.Kingbase:
  626. if(cbxInstrument.Text.Contains('.'))
  627. cbxInstrument.Text = cbxInstrument.Text.Split('.')[1];
  628. dataTableStruct = SelectTableType.KingSql(cbxInstrument.Text);
  629. break;
  630. default:
  631. break;
  632. }
  633. DataTable dtInstruShow = new DataTable();
  634. dtInstruShow.Columns.Add("InstruFieldName");
  635. dtInstruShow.Columns.Add("InstruDataType");
  636. dtInstruShow.Columns.Add("remark");
  637. if (dataTableStruct != null)
  638. {
  639. for (int i = 0; i < dataTableStruct.Rows.Count; i++)
  640. {
  641. dtInstruShow.Rows.Add(new object[] { dataTableStruct.Rows[i]["ColumnName"], dataTableStruct.Rows[i]["DataType"], dataTableStruct.Rows[i]["remark"] });
  642. }
  643. }
  644. dgvInstruDS.DataSource = dtInstruShow;
  645. if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
  646. {
  647. dictInstruTables.Clear();
  648. dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
  649. //return 1;
  650. }
  651. else
  652. {
  653. returnValue = -3;
  654. return returnValue;
  655. }
  656. //获取CNAS配置的数据库连接信息
  657. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  658. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  659. {
  660. List<string> lstCnasTables = new List<string>();
  661. foreach (DataRow dr in dtCNAS.Rows)
  662. {
  663. lstCnasTables.Add(dr["TABNAME"].ToString());
  664. }
  665. cbxCnas.DataSource = lstCnasTables;
  666. cbxCnas.ValueMember = "";
  667. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  668. {
  669. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  670. {
  671. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  672. //cbxCnas_SelectedIndexChanged(null, null);
  673. }
  674. else
  675. returnValue = -5;
  676. }
  677. }
  678. else
  679. returnValue = -4;
  680. return returnValue;
  681. }
  682. public string ExtractTableNames(string sql)
  683. {
  684. // 预处理:去除注释、统一空格
  685. sql = Regex.Replace(sql, @"(--.*)|(\/\*[\s\S]*?\*\/)", "", RegexOptions.Multiline);
  686. sql = Regex.Replace(sql, @"\s+", " ");
  687. // 定义匹配模式
  688. var patterns = new Dictionary<string, string>
  689. {
  690. { "SELECT", @"(?:FROM|JOIN)\s+([\w\.]+)(?:\s+AS\s+\w+)?" },
  691. { "INSERT", @"INSERT\s+INTO\s+([\w\.]+)" },
  692. { "UPDATE", @"UPDATE\s+([\w\.]+)" },
  693. { "DELETE", @"DELETE\s+FROM\s+([\w\.]+)" }
  694. };
  695. var tables = new HashSet<string>();
  696. foreach (var pattern in patterns.Values)
  697. {
  698. var matches = Regex.Matches(sql, pattern, RegexOptions.IgnoreCase);
  699. foreach (Match match in matches)
  700. {
  701. if (match.Groups[1].Success)
  702. tables.Add(match.Groups[1].Value.Trim());
  703. }
  704. }
  705. if (tables.Count == 0)
  706. return "";
  707. else
  708. return tables.ToList()[0];
  709. }
  710. private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  711. {
  712. if (dgvMapping.CurrentCell == null) return;
  713. if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
  714. {
  715. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  716. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  717. string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
  718. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  719. if (lstDelItems.Count > 0)
  720. {
  721. foreach (var item in lstDelItems)
  722. {
  723. item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
  724. }
  725. }
  726. }
  727. if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
  728. {
  729. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  730. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  731. string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
  732. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  733. if (lstDelItems.Count == 1)
  734. {
  735. if (strDateKey.ToLower() == "true")
  736. {
  737. //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
  738. foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
  739. {
  740. if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
  741. dgvRow.Cells["DateKey"].Value = false;
  742. }
  743. //内存数据源
  744. foreach (var item in currentSyncItem.LstSyncPramas)
  745. {
  746. if (item.IfDateField)
  747. item.IfDateField = false;
  748. }
  749. lstDelItems[0].IfDateField = true;
  750. }
  751. else
  752. {
  753. lstDelItems[0].IfDateField = false;
  754. }
  755. }
  756. }
  757. }
  758. private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  759. {
  760. if (dgvMapping.IsCurrentCellDirty)
  761. {
  762. dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
  763. }
  764. }
  765. private void btnCNASFieldConfig_Click(object sender, EventArgs e)
  766. {
  767. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  768. {
  769. MessageBox.Show("请先指定至少一个映射字段。");
  770. return;
  771. }
  772. frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
  773. if (frmCNAS.ShowDialog() == DialogResult.OK)
  774. {
  775. this.currentSyncItem = frmCNAS.syncInstrument;
  776. }
  777. }
  778. private void btnSourceFilter_Click(object sender, EventArgs e)
  779. {
  780. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  781. {
  782. MessageBox.Show("请先指定至少一个映射字段。");
  783. return;
  784. }
  785. frmSourceFilter frm = new frmSourceFilter(currentSyncItem, strTableInfoMode);
  786. frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
  787. {
  788. currentSyncItem.SourceFilter = dataFilter;
  789. };
  790. frm.ShowDialog();
  791. }
  792. private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  793. {
  794. if (dgvInstrument.Rows.Count <= 0) return;
  795. //当前选中单元格
  796. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  797. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  798. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  799. {
  800. lstSyncInstrument.Add(Instrumentitem);
  801. //绑定数据
  802. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  803. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  804. dgvInstrument_SelectionChanged(null, null);
  805. };
  806. frmSetting.ShowDialog();
  807. }
  808. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  809. {
  810. if (cbxCNASColumn.Visible)
  811. {
  812. cbxCNASColumn.Visible = false;
  813. txtInstrumentColumn.Visible = true;
  814. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  815. }
  816. }
  817. /// <summary>
  818. /// 当双击textBox时,显示ComboBOX用于选择
  819. /// </summary>
  820. /// <param name="sender"></param>
  821. /// <param name="e"></param>
  822. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  823. {
  824. txtInstrumentColumn.Visible = false;
  825. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  826. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  827. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  828. cbxCNASColumn.Location = txtInstrumentColumn.Location;
  829. cbxCNASColumn.Visible = true;
  830. }
  831. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  832. {
  833. if (cbxCNASColumn.Visible)
  834. {
  835. txtInstrumentColumn.Visible = true;
  836. cbxCNASColumn.Visible = false;
  837. }
  838. }
  839. private void tsmServiceSetting_Click(object sender, EventArgs e)
  840. {
  841. frmServiceConfig frmServiceConfig = new frmServiceConfig();
  842. frmServiceConfig.ShowDialog();
  843. }
  844. private void tsmSystemSetting_Click(object sender, EventArgs e)
  845. {
  846. //frmSystemSetting frmSystem = new frmSystemSetting();
  847. //frmSystem.ShowDialog();
  848. //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
  849. //if (strTableInfoMode == "1")
  850. //{
  851. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  852. // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  853. //}
  854. //else
  855. //{
  856. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
  857. //}
  858. }
  859. private void tsmSourceSetting_Click(object sender, EventArgs e)
  860. {
  861. frmSourceSetting frmSource = new frmSourceSetting();
  862. frmSource.ShowDialog();
  863. }
  864. private void tsmHelper_Click(object sender, EventArgs e)
  865. {
  866. string strHelpFilePath = FileHelper.getBasePath() + @"\Helper.CHM";
  867. //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
  868. //Help.ShowHelpIndex(this, strHelpFilePath);
  869. System.Diagnostics.Process.Start(strHelpFilePath);
  870. }
  871. }
  872. }