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.

848 line
36KB

  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.Windows.Forms;
  13. using System.Xml.Linq;
  14. using System.Xml.Serialization;
  15. namespace CNAS_DBSync
  16. {
  17. public delegate void ReferenceChangeSyncParamHanlder(SyncInstrumentItemInfo syncInstrumentItem);
  18. public partial class frmSyncParams : Form
  19. {
  20. public List<SyncInstrumentItemInfo> lstSyncInstrument = new List<SyncInstrumentItemInfo>(); //本地存储的仪器数据源
  21. public SyncInstrumentItemInfo currentSyncItem = new SyncInstrumentItemInfo(); //当前正在操作的仪器项
  22. public Dictionary<string, DataTable> dictInstruTables = new Dictionary<string, DataTable>(); //当前仪器项的来源表结构
  23. private SyncInstrumentItemInfo syncInstrumentItem; //其他调用传递的仪器项
  24. public ReferenceChangeSyncParamHanlder ParamsChangedDelegate; //传递仪器参数委托
  25. private string strMode = "";
  26. private string strTableInfoMode = ""; //获取所有表信息的方式;0为自动获取,1为手动输入
  27. public frmSyncParams(SyncInstrumentItemInfo syncInstrumentItem=null)
  28. {
  29. InitializeComponent();
  30. dgvInstrument.AutoGenerateColumns = false;
  31. dgvInstrument.RowHeadersVisible = false;
  32. dgvInstruDS.AutoGenerateColumns = false;
  33. dgvInstruDS.RowHeadersVisible = false;
  34. dgvCnas.AutoGenerateColumns = false;
  35. dgvCnas.RowHeadersVisible = false;
  36. dgvMapping.AutoGenerateColumns = false;
  37. dgvMapping.RowHeadersVisible = false;
  38. if (syncInstrumentItem != null)
  39. {
  40. strMode = "Reference";
  41. this.syncInstrumentItem = syncInstrumentItem;
  42. this.btnAdd.Visible = false;
  43. this.btnDel.Visible = false;
  44. }
  45. }
  46. private void frmSyncParams_Load(object sender, EventArgs e)
  47. {
  48. if (syncInstrumentItem == null)
  49. {
  50. lstSyncInstrument = FileOperation.GetLocalSyncInStrumentData();
  51. }
  52. else
  53. {
  54. lstSyncInstrument = new List<SyncInstrumentItemInfo>() { syncInstrumentItem };
  55. }
  56. //绑定数据源,填写相关内容
  57. if(lstSyncInstrument.Count!=0)
  58. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  59. }
  60. /// <summary>
  61. /// 保存当前设置到本地
  62. /// </summary>
  63. /// <param name="sender"></param>
  64. /// <param name="e"></param>
  65. private void btnSave_Click(object sender, EventArgs e)
  66. {
  67. //将配置好的信息存储到本地文件中
  68. try
  69. {
  70. bool bIfSaveSuccess = true;
  71. if (strMode == "Reference")
  72. {
  73. if (!CheckIfHaveDateField(new List<SyncInstrumentItemInfo>() { currentSyncItem }))
  74. {
  75. MessageBox.Show("日期字段不允许为空!");
  76. return;
  77. }
  78. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  79. {
  80. MessageBox.Show("关键字段不允许为空!");
  81. return;
  82. }
  83. //1.先加载所有数据 2.替换当前数据 3.重新保存
  84. List<SyncInstrumentItemInfo> lstDB = FileOperation.GetLocalSyncInStrumentData();
  85. var item = lstDB.Where(p => p.GUID == currentSyncItem.GUID).SingleOrDefault();
  86. if (item != null)
  87. {
  88. lstDB.Remove(item);
  89. lstDB.Add(currentSyncItem);
  90. }
  91. else
  92. {
  93. lstDB.Add(currentSyncItem);
  94. }
  95. //重新保存信息
  96. bIfSaveSuccess=FileOperation.SaveLocalSyncInStrumentData(lstDB);
  97. //委托发送参数
  98. this.ParamsChangedDelegate(syncInstrumentItem);
  99. }
  100. else
  101. {
  102. if (!CheckIfHaveDateField(lstSyncInstrument))
  103. {
  104. MessageBox.Show("日期字段不允许为空!");
  105. return;
  106. }
  107. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  108. {
  109. MessageBox.Show("关键字段不允许为空!");
  110. return;
  111. }
  112. bIfSaveSuccess =FileOperation.SaveLocalSyncInStrumentData(lstSyncInstrument);
  113. }
  114. if(bIfSaveSuccess)
  115. MessageBox.Show("保存成功!");
  116. else
  117. MessageBox.Show("保存失败!");
  118. }
  119. catch (Exception ex)
  120. {
  121. MessageBox.Show("保存失败!错误信息为:"+ex.Message.ToString());
  122. AppLog.Error(ex.Message);
  123. }
  124. }
  125. private bool CheckIfHaveDateField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  126. {
  127. bool bIfHave = true;
  128. foreach (var item in lstSyncInstrument)
  129. {
  130. if (item.LstSyncPramas == null) continue;
  131. if (item.LstSyncPramas.Count <=0) continue;
  132. if (item.LstSyncPramas.Where(s => s.IfDateField == true).Count() != 1)
  133. {
  134. bIfHave = false;
  135. break;
  136. }
  137. }
  138. return bIfHave;
  139. }
  140. private bool CheckIfHaveKeyPrimaryField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  141. {
  142. bool bIfHave = true;
  143. foreach (var item in lstSyncInstrument)
  144. {
  145. if (item.LstSyncPramas == null) continue;
  146. if (item.LstSyncPramas.Count <= 0) continue;
  147. if (item.LstSyncPramas.Where(s => s.IfPrimaryKey == true).Count() <= 0)
  148. {
  149. bIfHave = false;
  150. break;
  151. }
  152. }
  153. return bIfHave;
  154. }
  155. /// <summary>
  156. /// 新增仪器
  157. /// </summary>
  158. /// <param name="sender"></param>
  159. /// <param name="e"></param>
  160. private void btnAdd_Click(object sender, EventArgs e)
  161. {
  162. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument);
  163. frmInstrumentCode frmCode = new frmInstrumentCode(lstSyncInstrument);
  164. frmSetting.InstrumentDelegate = delegate(SyncInstrumentItemInfo Instrumentitem)
  165. {
  166. lstSyncInstrument.Add(Instrumentitem);
  167. //绑定数据
  168. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  169. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  170. dgvInstrument_SelectionChanged(null, null);
  171. };
  172. frmSetting.ShowDialog();
  173. }
  174. /// <summary>
  175. /// 删除仪器
  176. /// </summary>
  177. /// <param name="sender"></param>
  178. /// <param name="e"></param>
  179. private void btnDel_Click(object sender, EventArgs e)
  180. {
  181. if (dgvInstrument.Rows.Count <= 0) return;
  182. //当前选中行
  183. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  184. //找到数据源中数据,删除
  185. var lstitem = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  186. if (lstitem == null)
  187. return;
  188. else if (lstitem.Count >= 1)
  189. {
  190. foreach (var item in lstitem)
  191. {
  192. lstSyncInstrument.Remove(item);
  193. }
  194. }
  195. //绑定数据
  196. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>();
  197. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  198. //dgvInstrument.dgvInstrument_SelectionChanged()
  199. if (lstSyncInstrument.Count > 0)
  200. dgvInstrument_SelectionChanged(null, null);
  201. else
  202. {
  203. currentSyncItem = new SyncInstrumentItemInfo();
  204. dictInstruTables.Clear();
  205. cbxCnas.DataSource = null;
  206. cbxCnas.Items.Clear();
  207. cbxInstrument.DataSource = null;
  208. dgvInstruDS.DataSource = null;
  209. dgvCnas.DataSource = null;
  210. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  211. }
  212. }
  213. private void dgvInstrument_SelectionChanged(object sender, EventArgs e)
  214. {
  215. if (dgvInstrument.Rows.Count <= 0|| dgvInstrument.Rows[dgvInstrument.CurrentRow.Index].Cells[0].Value == null)
  216. {
  217. currentSyncItem = new SyncInstrumentItemInfo();
  218. return;
  219. }
  220. //清空绑定
  221. cbxCnas.DataSource = null;
  222. cbxInstrument.DataSource = null;
  223. dgvInstruDS.DataSource = null;
  224. dgvCnas.DataSource = null;
  225. //当前选中单元格
  226. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  227. var lstInstrument = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  228. if (lstInstrument.Count == 1)
  229. {
  230. currentSyncItem = lstInstrument[0];
  231. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn))
  232. txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  233. else
  234. txtInstrumentColumn.Text = "";
  235. if (currentSyncItem.SyncInstrumentDSInfo != null&& currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType!=DataSourceType.None)
  236. {
  237. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  238. if(currentSyncItem.SyncInstrumentDSInfo.ServerName!="")
  239. btnLoadDBData_Click(sender, e);
  240. }
  241. else
  242. {
  243. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  244. }
  245. }
  246. else
  247. {
  248. dgvMapping.DataSource =new BindingList<SyncParamasInfo>();
  249. }
  250. }
  251. /// <summary>
  252. /// 切换选中表时发生
  253. /// </summary>
  254. /// <param name="sender"></param>
  255. /// <param name="e"></param>
  256. private void cbxInstrument_SelectedIndexChanged(object sender, EventArgs e)
  257. {
  258. if (cbxInstrument.Text == null) return;
  259. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  260. //dictInstruTables = instrumentData.GetInstrumentData();
  261. //dictInstruTables =
  262. DataTable dtTableType = null;
  263. string strTableName_Instru = cbxInstrument.Text.ToString();
  264. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  265. {
  266. case DataSourceType.MySQL:
  267. dtTableType = SelectTableType.MySqlsec(strTableName_Instru);
  268. break;
  269. case DataSourceType.Dm:
  270. dtTableType = SelectTableType.DmSql(strTableName_Instru);
  271. break;
  272. case DataSourceType.Oracle:
  273. dtTableType = SelectTableType.OrcSql(strTableName_Instru, currentSyncItem);
  274. break;
  275. case DataSourceType.PostgreSQL:
  276. dtTableType = SelectTableType.PostgreSql(strTableName_Instru);
  277. break;
  278. case DataSourceType.SQL:
  279. dtTableType = SelectTableType.Sqlserversec(strTableName_Instru, currentSyncItem);
  280. break;
  281. default:
  282. break;
  283. }
  284. DataTable dtInstruShow = new DataTable();
  285. dtInstruShow.Columns.Add("InstruFieldName");
  286. dtInstruShow.Columns.Add("InstruDataType");
  287. dtInstruShow.Columns.Add("remark");
  288. for (int i = 0; i < dtTableType.Rows.Count; i++)
  289. {
  290. dtInstruShow.Rows.Add(new object[] { dtTableType.Rows[i]["ColumnName"], dtTableType.Rows[i]["DataType"], dtTableType.Rows[i]["remark"] });
  291. }
  292. dgvInstruDS.DataSource = dtInstruShow;
  293. }
  294. /// <summary>
  295. /// 切换表时发生
  296. /// </summary>
  297. /// <param name="sender"></param>
  298. /// <param name="e"></param>
  299. private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
  300. {
  301. if (cbxCnas.Text == null) return;
  302. string strTableName_Cnas = cbxCnas.Text.ToString();
  303. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Cnas, currentSyncItem.SyncTargetDBInfo);
  304. //从数据库中加载数据表结构
  305. //DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
  306. if (dtTableStruct != null)
  307. {
  308. DataTable dtCnasShow = new DataTable();
  309. dtCnasShow.Columns.Add("CnasFieldName");
  310. dtCnasShow.Columns.Add("CnasDataType");
  311. dtCnasShow.Columns.Add("remark");
  312. for (int i = 0; i < dtTableStruct.Rows.Count; i++)
  313. {
  314. dtCnasShow.Rows.Add(new object[] { dtTableStruct.Rows[i]["ColumnName"], dtTableStruct.Rows[i]["DataType"], dtTableStruct.Rows[i]["remark"] });
  315. }
  316. dgvCnas.DataSource = dtCnasShow;
  317. //绑定数据
  318. cbxCNASColumn.DataSource = dtCnasShow.Copy();
  319. cbxCNASColumn.DisplayMember = "CnasFieldName";
  320. cbxCNASColumn.ValueMember = "CnasFieldName";
  321. if (dtTableStruct.Columns.Count > 0)
  322. {
  323. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn)&&dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
  324. {
  325. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  326. }
  327. else
  328. {
  329. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
  330. }
  331. }
  332. }
  333. }
  334. //增加映射
  335. private void btnAddMapping_Click(object sender, EventArgs e)
  336. {
  337. if (cbxInstrument.Text == null) return;
  338. if (cbxCnas.Text == null) return;
  339. if (dgvInstruDS.Rows.Count <= 0) return;
  340. if (dgvCnas.Rows.Count <= 0) return;
  341. SyncParamasInfo syncParamas = new SyncParamasInfo();
  342. if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
  343. if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
  344. {
  345. MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
  346. return;
  347. }
  348. //仪器表名和选中行
  349. syncParamas.SourceTable = cbxInstrument.Text.ToString();
  350. syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString();
  351. syncParamas.DataType = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[1].Value.ToString().ToUpper();
  352. //CNAS表名和选中行
  353. syncParamas.TargetTable = cbxCnas.Text.ToString();
  354. syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString();
  355. //验证数据合法性
  356. SyncParamsOperation paramsOperation = new SyncParamsOperation();
  357. if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
  358. {
  359. MessageBox.Show("已存在不同表单映射数据,无法添加!");
  360. return;
  361. }
  362. //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
  363. //{
  364. // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
  365. // return;
  366. //}
  367. if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
  368. {
  369. MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
  370. return;
  371. }
  372. if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
  373. {
  374. MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
  375. return;
  376. }
  377. //绑定数据
  378. currentSyncItem.LstSyncPramas.Add(syncParamas);
  379. dgvMapping.DataSource= new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  380. //选中最后一行
  381. dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count-1].Cells[0];
  382. }
  383. /// <summary>
  384. /// 删除映射
  385. /// </summary>
  386. /// <param name="sender"></param>
  387. /// <param name="e"></param>
  388. private void btnDelMap_Click(object sender, EventArgs e)
  389. {
  390. //当前选中项
  391. if (dgvMapping.CurrentCell == null) return;
  392. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  393. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  394. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  395. if (lstDelItems.Count > 0)
  396. {
  397. foreach (var item in lstDelItems)
  398. {
  399. currentSyncItem.LstSyncPramas.Remove(item);
  400. }
  401. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  402. }
  403. }
  404. /// <summary>
  405. /// 配置数据库界面
  406. /// </summary>
  407. /// <param name="sender"></param>
  408. /// <param name="e"></param>
  409. private void btnDatabaseConfig_Click(object sender, EventArgs e)
  410. {
  411. frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
  412. frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  413. {
  414. this.currentSyncItem = Instrumentitem;
  415. };
  416. frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
  417. {
  418. //this.dictInstruTables = dict;
  419. };
  420. frmDatabase.ShowDialog();
  421. //加载数据
  422. if ((currentSyncItem.SyncInstrumentDSInfo.Host!=null&& currentSyncItem.SyncInstrumentDSInfo.Host.Length>0) || (currentSyncItem.SyncInstrumentDSInfo.Path!=null&& currentSyncItem.SyncInstrumentDSInfo.Path.Length>0))
  423. btnLoadDBData_Click(null, null);
  424. }
  425. private void btnLoadDBData_Click(object sender, EventArgs e)
  426. {
  427. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  428. strTableInfoMode = FileOperation.GetSystemFormatConfigData(strInstrumentCode).TableInfoMode;
  429. if (strTableInfoMode == "1")
  430. {
  431. cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  432. //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  433. }
  434. int iReturn = 0;
  435. if (strTableInfoMode=="0")
  436. iReturn=LoadSourceAndTargetData(true);
  437. else
  438. iReturn= LoadSourceAndTargetData();
  439. switch (iReturn)
  440. {
  441. case -1:
  442. MessageBox.Show("未能成功获取设备数据库配置信息,请配置后重试!");
  443. break;
  444. case -2:
  445. MessageBox.Show("未能成功获取CNAS数据库配置信息,请配置后重试!");
  446. break;
  447. case -3:
  448. MessageBox.Show("未能成功获取仪器数据信息,请配置后重试!");
  449. break;
  450. case -4:
  451. MessageBox.Show("未能成功获取CNAS数据信息,请配置后重试!");
  452. break;
  453. case -5:
  454. DialogResult dr = MessageBox.Show("检测到数据连接配置已经修改,是否全部删除已经分配的字段映射?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  455. if (dr == DialogResult.Yes)
  456. {
  457. //此时将全部删除已经分配的映射字段
  458. currentSyncItem.LstSyncPramas.Clear();
  459. currentSyncItem.lstFixedValue.Clear();
  460. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  461. }
  462. break;
  463. case -6:
  464. MessageBox.Show("请先手动输入表名");
  465. break;
  466. default:
  467. break;
  468. }
  469. }
  470. /// <summary>
  471. /// 加载来源和目标数据的数据结构
  472. /// </summary>
  473. /// <param name="bIfLoading"></param>
  474. /// <returns></returns>
  475. public int LoadSourceAndTargetData(bool bIfLoading=false)
  476. {
  477. //检查配置信息
  478. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  479. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  480. bool bIfSameTable = true;
  481. //是否需要重新加载来源库
  482. if (bIfLoading)
  483. {
  484. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  485. dictInstruTables = instrumentData.GetInstrumentData();
  486. }
  487. if (dictInstruTables.Count <= 0) return -3;
  488. //绑定ComboBox
  489. List<string> lstTableName = new List<string>();
  490. foreach (var item in dictInstruTables)
  491. {
  492. lstTableName.Add(item.Key);
  493. }
  494. cbxInstrument.DataSource = lstTableName;
  495. cbxInstrument.ValueMember = "";
  496. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  497. {
  498. if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
  499. cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
  500. else
  501. bIfSameTable = false;
  502. }
  503. //获取CNAS配置的数据库连接信息
  504. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  505. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  506. {
  507. List<string> lstCnasTables = new List<string>();
  508. foreach (DataRow dr in dtCNAS.Rows)
  509. {
  510. if (dtCNAS.Columns.Contains("TABNAME"))
  511. lstCnasTables.Add(dr["TABNAME"].ToString());
  512. else if (dtCNAS.Columns.Contains("table_name"))
  513. lstCnasTables.Add(dr["table_name"].ToString());
  514. }
  515. cbxCnas.DataSource = lstCnasTables;
  516. cbxCnas.ValueMember = "";
  517. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  518. {
  519. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  520. {
  521. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  522. //cbxCnas_SelectedIndexChanged(null, null);
  523. }
  524. else
  525. bIfSameTable = false;
  526. }
  527. }
  528. else
  529. return -4;
  530. if (!bIfSameTable)
  531. {
  532. return -5;
  533. }
  534. return 1;
  535. }
  536. /// <summary>
  537. /// 手动输入表名时,获取该表的表信息
  538. /// </summary>
  539. /// <returns></returns>
  540. public int LoadSourceAndTargetData()
  541. {
  542. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  543. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  544. if (cbxInstrument.Text == "") return -6;
  545. int returnValue = 1;
  546. //根据手动输入来源库的表名加载字段
  547. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { cbxInstrument.Text, "", "" });
  548. DataTable dataTableStruct = instrumentData.GetInstrumentDataStruct();
  549. if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
  550. {
  551. dictInstruTables.Clear();
  552. dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
  553. cbxInstrument_SelectedIndexChanged(null, null);
  554. //return 1;
  555. }
  556. else
  557. {
  558. returnValue = -3;
  559. return returnValue;
  560. }
  561. //获取CNAS配置的数据库连接信息
  562. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  563. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  564. {
  565. List<string> lstCnasTables = new List<string>();
  566. foreach (DataRow dr in dtCNAS.Rows)
  567. {
  568. lstCnasTables.Add(dr["TABNAME"].ToString());
  569. }
  570. cbxCnas.DataSource = lstCnasTables;
  571. cbxCnas.ValueMember = "";
  572. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  573. {
  574. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  575. {
  576. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  577. //cbxCnas_SelectedIndexChanged(null, null);
  578. }
  579. else
  580. returnValue = -5;
  581. }
  582. }
  583. else
  584. returnValue = -4;
  585. return returnValue;
  586. }
  587. private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  588. {
  589. if (dgvMapping.CurrentCell == null) return;
  590. if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
  591. {
  592. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  593. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  594. string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
  595. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  596. if (lstDelItems.Count > 0)
  597. {
  598. foreach (var item in lstDelItems)
  599. {
  600. item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
  601. }
  602. }
  603. }
  604. if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
  605. {
  606. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  607. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  608. string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
  609. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  610. if (lstDelItems.Count == 1)
  611. {
  612. if (strDateKey.ToLower() == "true")
  613. {
  614. //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
  615. foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
  616. {
  617. if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
  618. dgvRow.Cells["DateKey"].Value = false;
  619. }
  620. //内存数据源
  621. foreach (var item in currentSyncItem.LstSyncPramas)
  622. {
  623. if (item.IfDateField)
  624. item.IfDateField = false;
  625. }
  626. lstDelItems[0].IfDateField = true;
  627. }
  628. else
  629. {
  630. lstDelItems[0].IfDateField = false;
  631. }
  632. }
  633. }
  634. }
  635. private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  636. {
  637. if (dgvMapping.IsCurrentCellDirty)
  638. {
  639. dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
  640. }
  641. }
  642. private void btnCNASFieldConfig_Click(object sender, EventArgs e)
  643. {
  644. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  645. {
  646. MessageBox.Show("请先指定至少一个映射字段。");
  647. return;
  648. }
  649. frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
  650. if (frmCNAS.ShowDialog() == DialogResult.OK)
  651. {
  652. this.currentSyncItem = frmCNAS.syncInstrument;
  653. }
  654. }
  655. private void btnSourceFilter_Click(object sender, EventArgs e)
  656. {
  657. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  658. {
  659. MessageBox.Show("请先指定至少一个映射字段。");
  660. return;
  661. }
  662. frmSourceFilter frm = new frmSourceFilter(currentSyncItem,strTableInfoMode);
  663. frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
  664. {
  665. currentSyncItem.SourceFilter = dataFilter;
  666. };
  667. frm.ShowDialog();
  668. }
  669. private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  670. {
  671. if (dgvInstrument.Rows.Count <= 0) return;
  672. //当前选中单元格
  673. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  674. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  675. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  676. {
  677. lstSyncInstrument.Add(Instrumentitem);
  678. //绑定数据
  679. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  680. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  681. dgvInstrument_SelectionChanged(null, null);
  682. };
  683. frmSetting.ShowDialog();
  684. }
  685. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  686. {
  687. if (cbxCNASColumn.Visible)
  688. {
  689. cbxCNASColumn.Visible = false;
  690. txtInstrumentColumn.Visible = true;
  691. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  692. }
  693. }
  694. /// <summary>
  695. /// 当双击textBox时,显示ComboBOX用于选择
  696. /// </summary>
  697. /// <param name="sender"></param>
  698. /// <param name="e"></param>
  699. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  700. {
  701. txtInstrumentColumn.Visible = false;
  702. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  703. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  704. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  705. cbxCNASColumn.Location= txtInstrumentColumn.Location;
  706. cbxCNASColumn.Visible = true;
  707. }
  708. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  709. {
  710. if (cbxCNASColumn.Visible)
  711. {
  712. txtInstrumentColumn.Visible = true;
  713. cbxCNASColumn.Visible = false;
  714. }
  715. }
  716. private void tsmServiceSetting_Click(object sender, EventArgs e)
  717. {
  718. frmServiceConfig frmServiceConfig = new frmServiceConfig();
  719. frmServiceConfig.ShowDialog();
  720. }
  721. private void tsmSystemSetting_Click(object sender, EventArgs e)
  722. {
  723. //frmSystemSetting frmSystem = new frmSystemSetting();
  724. //frmSystem.ShowDialog();
  725. //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
  726. //if (strTableInfoMode == "1")
  727. //{
  728. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  729. // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  730. //}
  731. //else
  732. //{
  733. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
  734. //}
  735. }
  736. private void tsmSourceSetting_Click(object sender, EventArgs e)
  737. {
  738. frmSourceSetting frmSource = new frmSourceSetting();
  739. frmSource.ShowDialog();
  740. }
  741. private void tsmHelper_Click(object sender, EventArgs e)
  742. {
  743. string strHelpFilePath = FileHelper.getBasePath()+ @"\Helper.CHM";
  744. //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
  745. //Help.ShowHelpIndex(this, strHelpFilePath);
  746. System.Diagnostics.Process.Start(strHelpFilePath);
  747. }
  748. }
  749. }