CNAS取数仪器端升级
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

887 wiersze
37KB

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