CNAS取数仪器端升级
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

860 行
36KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusClient;
  3. using CnasSynchrousModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. using System.Xml.Linq;
  14. using System.Xml.Serialization;
  15. namespace CNAS_DBSync
  16. {
  17. public delegate void ReferenceChangeSyncParamHanlder(SyncInstrumentItemInfo syncInstrumentItem);
  18. public partial class frmSyncParams : Form
  19. {
  20. public List<SyncInstrumentItemInfo> lstSyncInstrument = new List<SyncInstrumentItemInfo>(); //本地存储的仪器数据源
  21. public SyncInstrumentItemInfo currentSyncItem = new SyncInstrumentItemInfo(); //当前正在操作的仪器项
  22. public Dictionary<string, DataTable> dictInstruTables = new Dictionary<string, DataTable>(); //当前仪器项的来源表结构
  23. private SyncInstrumentItemInfo syncInstrumentItem; //其他调用传递的仪器项
  24. public ReferenceChangeSyncParamHanlder ParamsChangedDelegate; //传递仪器参数委托
  25. private string strMode = "";
  26. private string strTableInfoMode = ""; //获取所有表信息的方式;0为自动获取,1为手动输入
  27. public frmSyncParams(SyncInstrumentItemInfo syncInstrumentItem=null)
  28. {
  29. InitializeComponent();
  30. dgvInstrument.AutoGenerateColumns = false;
  31. dgvInstrument.RowHeadersVisible = false;
  32. dgvInstruDS.AutoGenerateColumns = false;
  33. dgvInstruDS.RowHeadersVisible = false;
  34. dgvCnas.AutoGenerateColumns = false;
  35. dgvCnas.RowHeadersVisible = false;
  36. dgvMapping.AutoGenerateColumns = false;
  37. dgvMapping.RowHeadersVisible = false;
  38. if (syncInstrumentItem != null)
  39. {
  40. strMode = "Reference";
  41. this.syncInstrumentItem = syncInstrumentItem;
  42. this.btnAdd.Visible = false;
  43. this.btnDel.Visible = false;
  44. }
  45. }
  46. private void frmSyncParams_Load(object sender, EventArgs e)
  47. {
  48. if (syncInstrumentItem == null)
  49. {
  50. lstSyncInstrument = FileOperation.GetLocalSyncInStrumentData();
  51. }
  52. else
  53. {
  54. lstSyncInstrument = new List<SyncInstrumentItemInfo>() { syncInstrumentItem };
  55. }
  56. //绑定数据源,填写相关内容
  57. if(lstSyncInstrument.Count!=0)
  58. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  59. }
  60. /// <summary>
  61. /// 保存当前设置到本地
  62. /// </summary>
  63. /// <param name="sender"></param>
  64. /// <param name="e"></param>
  65. private void btnSave_Click(object sender, EventArgs e)
  66. {
  67. //将配置好的信息存储到本地文件中
  68. try
  69. {
  70. bool bIfSaveSuccess = true;
  71. if (strMode == "Reference")
  72. {
  73. if (!CheckIfHaveDateField(new List<SyncInstrumentItemInfo>() { currentSyncItem }))
  74. {
  75. MessageBox.Show("日期字段不允许为空!");
  76. return;
  77. }
  78. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  79. {
  80. MessageBox.Show("关键字段不允许为空!");
  81. return;
  82. }
  83. //1.先加载所有数据 2.替换当前数据 3.重新保存
  84. List<SyncInstrumentItemInfo> lstDB = FileOperation.GetLocalSyncInStrumentData();
  85. var item = lstDB.Where(p => p.GUID == currentSyncItem.GUID).SingleOrDefault();
  86. if (item != null)
  87. {
  88. lstDB.Remove(item);
  89. lstDB.Add(currentSyncItem);
  90. }
  91. else
  92. {
  93. lstDB.Add(currentSyncItem);
  94. }
  95. //重新保存信息
  96. bIfSaveSuccess=FileOperation.SaveLocalSyncInStrumentData(lstDB);
  97. //委托发送参数
  98. this.ParamsChangedDelegate(syncInstrumentItem);
  99. }
  100. else
  101. {
  102. if (!CheckIfHaveDateField(lstSyncInstrument))
  103. {
  104. MessageBox.Show("日期字段不允许为空!");
  105. return;
  106. }
  107. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  108. {
  109. MessageBox.Show("关键字段不允许为空!");
  110. return;
  111. }
  112. bIfSaveSuccess =FileOperation.SaveLocalSyncInStrumentData(lstSyncInstrument);
  113. }
  114. if(bIfSaveSuccess)
  115. MessageBox.Show("保存成功!");
  116. else
  117. MessageBox.Show("保存失败!");
  118. }
  119. catch (Exception ex)
  120. {
  121. MessageBox.Show("保存失败!错误信息为:"+ex.Message.ToString());
  122. AppLog.Error(ex.Message);
  123. }
  124. }
  125. private bool CheckIfHaveDateField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  126. {
  127. bool bIfHave = true;
  128. foreach (var item in lstSyncInstrument)
  129. {
  130. if (item.LstSyncPramas == null) continue;
  131. if (item.LstSyncPramas.Count <=0) continue;
  132. if (item.LstSyncPramas.Where(s => s.IfDateField == true).Count() != 1)
  133. {
  134. bIfHave = false;
  135. break;
  136. }
  137. }
  138. return bIfHave;
  139. }
  140. private bool CheckIfHaveKeyPrimaryField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  141. {
  142. bool bIfHave = true;
  143. foreach (var item in lstSyncInstrument)
  144. {
  145. if (item.LstSyncPramas == null) continue;
  146. if (item.LstSyncPramas.Count <= 0) continue;
  147. if (item.LstSyncPramas.Where(s => s.IfPrimaryKey == true).Count() <= 0)
  148. {
  149. bIfHave = false;
  150. break;
  151. }
  152. }
  153. return bIfHave;
  154. }
  155. /// <summary>
  156. /// 新增仪器
  157. /// </summary>
  158. /// <param name="sender"></param>
  159. /// <param name="e"></param>
  160. private void btnAdd_Click(object sender, EventArgs e)
  161. {
  162. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument);
  163. frmInstrumentCode frmCode = new frmInstrumentCode(lstSyncInstrument);
  164. frmSetting.InstrumentDelegate = delegate(SyncInstrumentItemInfo Instrumentitem)
  165. {
  166. lstSyncInstrument.Add(Instrumentitem);
  167. //绑定数据
  168. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  169. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  170. dgvInstrument_SelectionChanged(null, null);
  171. };
  172. frmSetting.ShowDialog();
  173. }
  174. /// <summary>
  175. /// 删除仪器
  176. /// </summary>
  177. /// <param name="sender"></param>
  178. /// <param name="e"></param>
  179. private void btnDel_Click(object sender, EventArgs e)
  180. {
  181. if (dgvInstrument.Rows.Count <= 0) return;
  182. //当前选中行
  183. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  184. //找到数据源中数据,删除
  185. var lstitem = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  186. if (lstitem == null)
  187. return;
  188. else if (lstitem.Count >= 1)
  189. {
  190. foreach (var item in lstitem)
  191. {
  192. lstSyncInstrument.Remove(item);
  193. }
  194. }
  195. //绑定数据
  196. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>();
  197. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  198. //dgvInstrument.dgvInstrument_SelectionChanged()
  199. if (lstSyncInstrument.Count > 0)
  200. dgvInstrument_SelectionChanged(null, null);
  201. else
  202. {
  203. currentSyncItem = new SyncInstrumentItemInfo();
  204. dictInstruTables.Clear();
  205. cbxCnas.DataSource = null;
  206. cbxCnas.Items.Clear();
  207. cbxInstrument.DataSource = null;
  208. dgvInstruDS.DataSource = null;
  209. dgvCnas.DataSource = null;
  210. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  211. }
  212. }
  213. private void dgvInstrument_SelectionChanged(object sender, EventArgs e)
  214. {
  215. if (dgvInstrument.Rows.Count <= 0|| dgvInstrument.Rows[dgvInstrument.CurrentRow.Index].Cells[0].Value == null)
  216. {
  217. currentSyncItem = new SyncInstrumentItemInfo();
  218. return;
  219. }
  220. //清空绑定
  221. cbxCnas.DataSource = null;
  222. cbxInstrument.DataSource = null;
  223. dgvInstruDS.DataSource = null;
  224. dgvCnas.DataSource = null;
  225. //当前选中单元格
  226. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  227. var lstInstrument = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  228. if (lstInstrument.Count == 1)
  229. {
  230. currentSyncItem = lstInstrument[0];
  231. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn))
  232. txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  233. else
  234. txtInstrumentColumn.Text = "";
  235. if (currentSyncItem.SyncInstrumentDSInfo != null&& currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType!=DataSourceType.None)
  236. {
  237. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  238. if(currentSyncItem.SyncInstrumentDSInfo.ServerName!="")
  239. btnLoadDBData_Click(sender, e);
  240. }
  241. else
  242. {
  243. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  244. }
  245. }
  246. else
  247. {
  248. dgvMapping.DataSource =new BindingList<SyncParamasInfo>();
  249. }
  250. }
  251. /// <summary>
  252. /// 切换选中表时发生
  253. /// </summary>
  254. /// <param name="sender"></param>
  255. /// <param name="e"></param>
  256. private void cbxInstrument_SelectedIndexChanged(object sender, EventArgs e)
  257. {
  258. if (cbxInstrument.Text == null) return;
  259. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  260. //dictInstruTables = instrumentData.GetInstrumentData();
  261. //dictInstruTables =
  262. string strTableName_Instru = cbxInstrument.Text.ToString();
  263. DataTable dtTableType = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Instru, currentSyncItem.SyncTargetDBInfo);
  264. //string strTableName_Instru = cbxInstrument.Text.ToString();
  265. DataTable dtInstruShow = new DataTable();
  266. dtInstruShow.Columns.Add("InstruFieldName");
  267. dtInstruShow.Columns.Add("InstruDataType");
  268. dtInstruShow.Columns.Add("remark");
  269. for (int i = 0; i < dtTableType.Rows.Count; i++)
  270. {
  271. dtInstruShow.Rows.Add(new object[] { dtTableType.Rows[i]["ColumnName"], dtTableType.Rows[i]["DataType"], dtTableType.Rows[i]["remark"] });
  272. }
  273. dgvInstruDS.DataSource = dtInstruShow;
  274. //if (dictInstruTables.ContainsKey(strTableName_Instru))
  275. //{
  276. // DataTable dt = dictInstruTables[strTableName_Instru];
  277. // if (dt != null)
  278. // {
  279. // DataTable dtInstruShow = new DataTable();
  280. // dtInstruShow.Columns.Add("InstruFieldName");
  281. // dtInstruShow.Columns.Add("InstruDataType");
  282. // foreach (DataColumn dc in dt.Columns)
  283. // {
  284. // dtInstruShow.Rows.Add(new object[] { dc.ColumnName,dc.DataType});
  285. // }
  286. // }
  287. //}
  288. }
  289. /// <summary>
  290. /// 切换表时发生
  291. /// </summary>
  292. /// <param name="sender"></param>
  293. /// <param name="e"></param>
  294. private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
  295. {
  296. if (cbxCnas.Text == null) return;
  297. string strTableName_Cnas = cbxCnas.Text.ToString();
  298. //if (currentSyncItem.CnasInstrumentColumn != null && currentSyncItem.CnasInstrumentColumn != "")
  299. //{
  300. // DialogResult dr = MessageBox.Show("已存在配置完成的CNAS仪器对应列,切换表将导致该配置失效,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  301. // if (dr == DialogResult.No)
  302. // {
  303. // return;
  304. // }
  305. //}
  306. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Cnas, currentSyncItem.SyncTargetDBInfo);
  307. //从数据库中加载数据表结构
  308. //DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
  309. if (dtTableStruct != null)
  310. {
  311. //DataTable dtTableType = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Instru, currentSyncItem.SyncTargetDBInfo);
  312. //string strTableName_Instru = cbxInstrument.Text.ToString();
  313. DataTable dtCnasShow = new DataTable();
  314. dtCnasShow.Columns.Add("CnasFieldName");
  315. dtCnasShow.Columns.Add("CnasDataType");
  316. dtCnasShow.Columns.Add("remark");
  317. for (int i = 0; i < dtTableStruct.Rows.Count; i++)
  318. {
  319. dtCnasShow.Rows.Add(new object[] { dtTableStruct.Rows[i]["ColumnName"], dtTableStruct.Rows[i]["DataType"], dtTableStruct.Rows[i]["remark"] });
  320. }
  321. dgvCnas.DataSource = dtCnasShow;
  322. //绑定数据
  323. cbxCNASColumn.DataSource = dtCnasShow.Copy();
  324. cbxCNASColumn.DisplayMember = "CnasFieldName";
  325. cbxCNASColumn.ValueMember = "CnasFieldName";
  326. if (dtTableStruct.Columns.Count > 0)
  327. {
  328. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn)&&dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
  329. {
  330. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  331. }
  332. else
  333. {
  334. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
  335. }
  336. }
  337. }
  338. }
  339. //增加映射
  340. private void btnAddMapping_Click(object sender, EventArgs e)
  341. {
  342. if (cbxInstrument.Text == null) return;
  343. if (cbxCnas.Text == null) return;
  344. if (dgvInstruDS.Rows.Count <= 0) return;
  345. if (dgvCnas.Rows.Count <= 0) return;
  346. SyncParamasInfo syncParamas = new SyncParamasInfo();
  347. if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
  348. if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
  349. {
  350. MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
  351. return;
  352. }
  353. //仪器表名和选中行
  354. syncParamas.SourceTable = cbxInstrument.Text.ToString();
  355. syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString();
  356. syncParamas.DataType = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[1].Value.ToString().ToUpper();
  357. //CNAS表名和选中行
  358. syncParamas.TargetTable = cbxCnas.Text.ToString();
  359. syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString();
  360. //验证数据合法性
  361. SyncParamsOperation paramsOperation = new SyncParamsOperation();
  362. if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
  363. {
  364. MessageBox.Show("已存在不同表单映射数据,无法添加!");
  365. return;
  366. }
  367. //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
  368. //{
  369. // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
  370. // return;
  371. //}
  372. if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
  373. {
  374. MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
  375. return;
  376. }
  377. if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
  378. {
  379. MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
  380. return;
  381. }
  382. //绑定数据
  383. currentSyncItem.LstSyncPramas.Add(syncParamas);
  384. dgvMapping.DataSource= new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  385. //选中最后一行
  386. dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count-1].Cells[0];
  387. }
  388. /// <summary>
  389. /// 删除映射
  390. /// </summary>
  391. /// <param name="sender"></param>
  392. /// <param name="e"></param>
  393. private void btnDelMap_Click(object sender, EventArgs e)
  394. {
  395. //当前选中项
  396. if (dgvMapping.CurrentCell == null) return;
  397. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  398. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  399. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  400. if (lstDelItems.Count > 0)
  401. {
  402. foreach (var item in lstDelItems)
  403. {
  404. currentSyncItem.LstSyncPramas.Remove(item);
  405. }
  406. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  407. }
  408. }
  409. /// <summary>
  410. /// 配置数据库界面
  411. /// </summary>
  412. /// <param name="sender"></param>
  413. /// <param name="e"></param>
  414. private void btnDatabaseConfig_Click(object sender, EventArgs e)
  415. {
  416. frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
  417. frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  418. {
  419. this.currentSyncItem = Instrumentitem;
  420. };
  421. frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
  422. {
  423. //this.dictInstruTables = dict;
  424. };
  425. frmDatabase.ShowDialog();
  426. //加载数据
  427. if ((currentSyncItem.SyncInstrumentDSInfo.Host!=null&& currentSyncItem.SyncInstrumentDSInfo.Host.Length>0) || (currentSyncItem.SyncInstrumentDSInfo.Path!=null&& currentSyncItem.SyncInstrumentDSInfo.Path.Length>0))
  428. btnLoadDBData_Click(null, null);
  429. }
  430. private void btnLoadDBData_Click(object sender, EventArgs e)
  431. {
  432. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  433. strTableInfoMode = FileOperation.GetSystemFormatConfigData(strInstrumentCode).TableInfoMode;
  434. if (strTableInfoMode == "1")
  435. {
  436. cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  437. //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  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. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  680. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  681. {
  682. lstSyncInstrument.Add(Instrumentitem);
  683. //绑定数据
  684. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  685. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  686. dgvInstrument_SelectionChanged(null, null);
  687. };
  688. frmSetting.ShowDialog();
  689. }
  690. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  691. {
  692. if (cbxCNASColumn.Visible)
  693. {
  694. cbxCNASColumn.Visible = false;
  695. txtInstrumentColumn.Visible = true;
  696. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  697. }
  698. }
  699. /// <summary>
  700. /// 当双击textBox时,显示ComboBOX用于选择
  701. /// </summary>
  702. /// <param name="sender"></param>
  703. /// <param name="e"></param>
  704. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  705. {
  706. txtInstrumentColumn.Visible = false;
  707. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  708. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  709. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  710. cbxCNASColumn.Location= txtInstrumentColumn.Location;
  711. cbxCNASColumn.Visible = true;
  712. }
  713. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  714. {
  715. if (cbxCNASColumn.Visible)
  716. {
  717. txtInstrumentColumn.Visible = true;
  718. cbxCNASColumn.Visible = false;
  719. }
  720. }
  721. private void tsmServiceSetting_Click(object sender, EventArgs e)
  722. {
  723. frmServiceConfig frmServiceConfig = new frmServiceConfig();
  724. frmServiceConfig.ShowDialog();
  725. }
  726. private void tsmSystemSetting_Click(object sender, EventArgs e)
  727. {
  728. //frmSystemSetting frmSystem = new frmSystemSetting();
  729. //frmSystem.ShowDialog();
  730. //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
  731. //if (strTableInfoMode == "1")
  732. //{
  733. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  734. // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  735. //}
  736. //else
  737. //{
  738. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
  739. //}
  740. }
  741. private void tsmSourceSetting_Click(object sender, EventArgs e)
  742. {
  743. frmSourceSetting frmSource = new frmSourceSetting();
  744. frmSource.ShowDialog();
  745. }
  746. private void tsmHelper_Click(object sender, EventArgs e)
  747. {
  748. string strHelpFilePath = FileHelper.getBasePath()+ @"\Helper.CHM";
  749. //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
  750. //Help.ShowHelpIndex(this, strHelpFilePath);
  751. System.Diagnostics.Process.Start(strHelpFilePath);
  752. }
  753. }
  754. }