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.

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