CNAS取数仪器端升级
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1016 lines
44KB

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