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

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