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

831 lines
35KB

  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. string strTableName_Instru = cbxInstrument.Text.ToString();
  260. if (dictInstruTables.ContainsKey(strTableName_Instru))
  261. {
  262. DataTable dt = dictInstruTables[strTableName_Instru];
  263. if (dt != null)
  264. {
  265. DataTable dtInstruShow = new DataTable();
  266. dtInstruShow.Columns.Add("InstruFieldName");
  267. dtInstruShow.Columns.Add("InstruDataType");
  268. foreach (DataColumn dc in dt.Columns)
  269. {
  270. dtInstruShow.Rows.Add(new object[] { dc.ColumnName,dc.DataType});
  271. }
  272. dgvInstruDS.DataSource = dtInstruShow;
  273. }
  274. }
  275. }
  276. /// <summary>
  277. /// 切换表时发生
  278. /// </summary>
  279. /// <param name="sender"></param>
  280. /// <param name="e"></param>
  281. private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
  282. {
  283. if (cbxCnas.Text == null) return;
  284. string strTableName_Cnas = cbxCnas.Text.ToString();
  285. //if (currentSyncItem.CnasInstrumentColumn != null && currentSyncItem.CnasInstrumentColumn != "")
  286. //{
  287. // DialogResult dr = MessageBox.Show("已存在配置完成的CNAS仪器对应列,切换表将导致该配置失效,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  288. // if (dr == DialogResult.No)
  289. // {
  290. // return;
  291. // }
  292. //}
  293. //从数据库中加载数据表结构
  294. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
  295. if (dtTableStruct != null)
  296. {
  297. DataTable dtCnasShow = new DataTable();
  298. dtCnasShow.Columns.Add("CnasFieldName");
  299. dtCnasShow.Columns.Add("CnasDataType");
  300. foreach (DataColumn dc in dtTableStruct.Columns)
  301. {
  302. dtCnasShow.Rows.Add(new object[] { dc.ColumnName ,dc.DataType});
  303. }
  304. dgvCnas.DataSource = dtCnasShow;
  305. //绑定数据
  306. cbxCNASColumn.DataSource = dtCnasShow.Copy();
  307. cbxCNASColumn.DisplayMember = "CnasFieldName";
  308. cbxCNASColumn.ValueMember = "CnasFieldName";
  309. if (dtTableStruct.Columns.Count > 0)
  310. {
  311. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn)&&dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
  312. {
  313. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  314. }
  315. else
  316. {
  317. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
  318. }
  319. }
  320. }
  321. }
  322. //增加映射
  323. private void btnAddMapping_Click(object sender, EventArgs e)
  324. {
  325. if (cbxInstrument.Text == null) return;
  326. if (cbxCnas.Text == null) return;
  327. if (dgvInstruDS.Rows.Count <= 0) return;
  328. if (dgvCnas.Rows.Count <= 0) return;
  329. SyncParamasInfo syncParamas = new SyncParamasInfo();
  330. if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
  331. if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
  332. {
  333. MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
  334. return;
  335. }
  336. //仪器表名和选中行
  337. syncParamas.SourceTable = cbxInstrument.Text.ToString().ToUpper();
  338. syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper();
  339. syncParamas.DataType = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[1].Value.ToString().ToUpper();
  340. //CNAS表名和选中行
  341. syncParamas.TargetTable = cbxCnas.Text.ToString().ToUpper();
  342. syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper();
  343. //验证数据合法性
  344. SyncParamsOperation paramsOperation = new SyncParamsOperation();
  345. if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
  346. {
  347. MessageBox.Show("已存在不同表单映射数据,无法添加!");
  348. return;
  349. }
  350. //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
  351. //{
  352. // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
  353. // return;
  354. //}
  355. if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
  356. {
  357. MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
  358. return;
  359. }
  360. if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
  361. {
  362. MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
  363. return;
  364. }
  365. //绑定数据
  366. currentSyncItem.LstSyncPramas.Add(syncParamas);
  367. dgvMapping.DataSource= new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  368. //选中最后一行
  369. dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count-1].Cells[0];
  370. }
  371. /// <summary>
  372. /// 删除映射
  373. /// </summary>
  374. /// <param name="sender"></param>
  375. /// <param name="e"></param>
  376. private void btnDelMap_Click(object sender, EventArgs e)
  377. {
  378. //当前选中项
  379. if (dgvMapping.CurrentCell == null) return;
  380. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  381. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  382. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  383. if (lstDelItems.Count > 0)
  384. {
  385. foreach (var item in lstDelItems)
  386. {
  387. currentSyncItem.LstSyncPramas.Remove(item);
  388. }
  389. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  390. }
  391. }
  392. /// <summary>
  393. /// 配置数据库界面
  394. /// </summary>
  395. /// <param name="sender"></param>
  396. /// <param name="e"></param>
  397. private void btnDatabaseConfig_Click(object sender, EventArgs e)
  398. {
  399. frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
  400. frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  401. {
  402. this.currentSyncItem = Instrumentitem;
  403. };
  404. frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
  405. {
  406. //this.dictInstruTables = dict;
  407. };
  408. frmDatabase.ShowDialog();
  409. //加载数据
  410. if ((currentSyncItem.SyncInstrumentDSInfo.Host!=null&& currentSyncItem.SyncInstrumentDSInfo.Host.Length>0) || (currentSyncItem.SyncInstrumentDSInfo.Path!=null&& currentSyncItem.SyncInstrumentDSInfo.Path.Length>0))
  411. btnLoadDBData_Click(null, null);
  412. }
  413. private void btnLoadDBData_Click(object sender, EventArgs e)
  414. {
  415. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  416. strTableInfoMode = FileOperation.GetSystemFormatConfigData(strInstrumentCode).TableInfoMode;
  417. if (strTableInfoMode == "1")
  418. {
  419. cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  420. //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  421. }
  422. int iReturn = 0;
  423. if (strTableInfoMode=="0")
  424. iReturn=LoadSourceAndTargetData(true);
  425. else
  426. iReturn= LoadSourceAndTargetData();
  427. switch (iReturn)
  428. {
  429. case -1:
  430. MessageBox.Show("未能成功获取设备数据库配置信息,请配置后重试!");
  431. break;
  432. case -2:
  433. MessageBox.Show("未能成功获取CNAS数据库配置信息,请配置后重试!");
  434. break;
  435. case -3:
  436. MessageBox.Show("未能成功获取仪器数据信息,请配置后重试!");
  437. break;
  438. case -4:
  439. MessageBox.Show("未能成功获取CNAS数据信息,请配置后重试!");
  440. break;
  441. case -5:
  442. DialogResult dr = MessageBox.Show("检测到数据连接配置已经修改,是否全部删除已经分配的字段映射?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  443. if (dr == DialogResult.Yes)
  444. {
  445. //此时将全部删除已经分配的映射字段
  446. currentSyncItem.LstSyncPramas.Clear();
  447. currentSyncItem.lstFixedValue.Clear();
  448. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  449. }
  450. break;
  451. case -6:
  452. MessageBox.Show("请先手动输入表名");
  453. break;
  454. default:
  455. break;
  456. }
  457. }
  458. /// <summary>
  459. /// 加载来源和目标数据的数据结构
  460. /// </summary>
  461. /// <param name="bIfLoading"></param>
  462. /// <returns></returns>
  463. public int LoadSourceAndTargetData(bool bIfLoading=false)
  464. {
  465. //检查配置信息
  466. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  467. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  468. bool bIfSameTable = true;
  469. //是否需要重新加载来源库
  470. if (bIfLoading)
  471. {
  472. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  473. dictInstruTables = instrumentData.GetInstrumentData();
  474. }
  475. if (dictInstruTables.Count <= 0) return -3;
  476. //绑定ComboBox
  477. List<string> lstTableName = new List<string>();
  478. foreach (var item in dictInstruTables)
  479. {
  480. lstTableName.Add(item.Key);
  481. }
  482. cbxInstrument.DataSource = lstTableName;
  483. cbxInstrument.ValueMember = "";
  484. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  485. {
  486. if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
  487. cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
  488. else
  489. bIfSameTable = false;
  490. }
  491. //获取CNAS配置的数据库连接信息
  492. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  493. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  494. {
  495. List<string> lstCnasTables = new List<string>();
  496. foreach (DataRow dr in dtCNAS.Rows)
  497. {
  498. if (dtCNAS.Columns.Contains("TABNAME"))
  499. lstCnasTables.Add(dr["TABNAME"].ToString());
  500. else if (dtCNAS.Columns.Contains("table_name"))
  501. lstCnasTables.Add(dr["table_name"].ToString());
  502. }
  503. cbxCnas.DataSource = lstCnasTables;
  504. cbxCnas.ValueMember = "";
  505. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  506. {
  507. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  508. {
  509. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  510. //cbxCnas_SelectedIndexChanged(null, null);
  511. }
  512. else
  513. bIfSameTable = false;
  514. }
  515. }
  516. else
  517. return -4;
  518. if (!bIfSameTable)
  519. {
  520. return -5;
  521. }
  522. return 1;
  523. }
  524. /// <summary>
  525. /// 手动输入表名时,获取该表的表信息
  526. /// </summary>
  527. /// <returns></returns>
  528. public int LoadSourceAndTargetData()
  529. {
  530. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  531. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  532. if (cbxInstrument.Text == "") return -6;
  533. int returnValue = 1;
  534. //根据手动输入来源库的表名加载字段
  535. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { cbxInstrument.Text, "", "" });
  536. DataTable dataTableStruct = instrumentData.GetInstrumentDataStruct();
  537. if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
  538. {
  539. dictInstruTables.Clear();
  540. dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
  541. cbxInstrument_SelectedIndexChanged(null, null);
  542. //return 1;
  543. }
  544. else
  545. {
  546. returnValue = -3;
  547. return returnValue;
  548. }
  549. //获取CNAS配置的数据库连接信息
  550. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  551. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  552. {
  553. List<string> lstCnasTables = new List<string>();
  554. foreach (DataRow dr in dtCNAS.Rows)
  555. {
  556. lstCnasTables.Add(dr["TABNAME"].ToString());
  557. }
  558. cbxCnas.DataSource = lstCnasTables;
  559. cbxCnas.ValueMember = "";
  560. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  561. {
  562. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  563. {
  564. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  565. //cbxCnas_SelectedIndexChanged(null, null);
  566. }
  567. else
  568. returnValue = -5;
  569. }
  570. }
  571. else
  572. returnValue = -4;
  573. return returnValue;
  574. }
  575. private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  576. {
  577. if (dgvMapping.CurrentCell == null) return;
  578. if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
  579. {
  580. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  581. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  582. string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
  583. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  584. if (lstDelItems.Count > 0)
  585. {
  586. foreach (var item in lstDelItems)
  587. {
  588. item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
  589. }
  590. }
  591. }
  592. if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
  593. {
  594. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  595. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  596. string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
  597. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  598. if (lstDelItems.Count == 1)
  599. {
  600. if (strDateKey.ToLower() == "true")
  601. {
  602. //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
  603. foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
  604. {
  605. if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
  606. dgvRow.Cells["DateKey"].Value = false;
  607. }
  608. //内存数据源
  609. foreach (var item in currentSyncItem.LstSyncPramas)
  610. {
  611. if (item.IfDateField)
  612. item.IfDateField = false;
  613. }
  614. lstDelItems[0].IfDateField = true;
  615. }
  616. else
  617. {
  618. lstDelItems[0].IfDateField = false;
  619. }
  620. }
  621. }
  622. }
  623. private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  624. {
  625. if (dgvMapping.IsCurrentCellDirty)
  626. {
  627. dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
  628. }
  629. }
  630. private void btnCNASFieldConfig_Click(object sender, EventArgs e)
  631. {
  632. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  633. {
  634. MessageBox.Show("请先指定至少一个映射字段。");
  635. return;
  636. }
  637. frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
  638. if (frmCNAS.ShowDialog() == DialogResult.OK)
  639. {
  640. this.currentSyncItem = frmCNAS.syncInstrument;
  641. }
  642. }
  643. private void btnSourceFilter_Click(object sender, EventArgs e)
  644. {
  645. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  646. {
  647. MessageBox.Show("请先指定至少一个映射字段。");
  648. return;
  649. }
  650. frmSourceFilter frm = new frmSourceFilter(currentSyncItem,strTableInfoMode);
  651. frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
  652. {
  653. currentSyncItem.SourceFilter = dataFilter;
  654. };
  655. frm.ShowDialog();
  656. }
  657. private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  658. {
  659. if (dgvInstrument.Rows.Count <= 0) return;
  660. //当前选中单元格
  661. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  662. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  663. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  664. {
  665. lstSyncInstrument.Add(Instrumentitem);
  666. //绑定数据
  667. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  668. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  669. dgvInstrument_SelectionChanged(null, null);
  670. };
  671. frmSetting.ShowDialog();
  672. }
  673. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  674. {
  675. if (cbxCNASColumn.Visible)
  676. {
  677. cbxCNASColumn.Visible = false;
  678. txtInstrumentColumn.Visible = true;
  679. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  680. }
  681. }
  682. /// <summary>
  683. /// 当双击textBox时,显示ComboBOX用于选择
  684. /// </summary>
  685. /// <param name="sender"></param>
  686. /// <param name="e"></param>
  687. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  688. {
  689. txtInstrumentColumn.Visible = false;
  690. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  691. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  692. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  693. cbxCNASColumn.Location= txtInstrumentColumn.Location;
  694. cbxCNASColumn.Visible = true;
  695. }
  696. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  697. {
  698. if (cbxCNASColumn.Visible)
  699. {
  700. txtInstrumentColumn.Visible = true;
  701. cbxCNASColumn.Visible = false;
  702. }
  703. }
  704. private void tsmServiceSetting_Click(object sender, EventArgs e)
  705. {
  706. frmServiceConfig frmServiceConfig = new frmServiceConfig();
  707. frmServiceConfig.ShowDialog();
  708. }
  709. private void tsmSystemSetting_Click(object sender, EventArgs e)
  710. {
  711. //frmSystemSetting frmSystem = new frmSystemSetting();
  712. //frmSystem.ShowDialog();
  713. //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
  714. //if (strTableInfoMode == "1")
  715. //{
  716. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  717. // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  718. //}
  719. //else
  720. //{
  721. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
  722. //}
  723. }
  724. private void tsmSourceSetting_Click(object sender, EventArgs e)
  725. {
  726. frmSourceSetting frmSource = new frmSourceSetting();
  727. frmSource.ShowDialog();
  728. }
  729. private void tsmHelper_Click(object sender, EventArgs e)
  730. {
  731. string strHelpFilePath = FileHelper.getBasePath()+ @"\Helper.CHM";
  732. //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
  733. //Help.ShowHelpIndex(this, strHelpFilePath);
  734. System.Diagnostics.Process.Start(strHelpFilePath);
  735. }
  736. }
  737. }