CNAS取数仪器端升级
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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