CNAS取数仪器端升级
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

831 行
34KB

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