CNAS取数仪器端升级
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frmSyncParams.cs 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. }
  244. else
  245. {
  246. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  247. }
  248. }
  249. else
  250. {
  251. dgvMapping.DataSource =new BindingList<SyncParamasInfo>();
  252. }
  253. }
  254. /// <summary>
  255. /// 切换选中表时发生
  256. /// </summary>
  257. /// <param name="sender"></param>
  258. /// <param name="e"></param>
  259. private void cbxInstrument_SelectedIndexChanged(object sender, EventArgs e)
  260. {
  261. if (cbxInstrument.Text == null) return;
  262. string strTableName_Instru = cbxInstrument.Text.ToString();
  263. if (dictInstruTables.ContainsKey(strTableName_Instru))
  264. {
  265. DataTable dt = dictInstruTables[strTableName_Instru];
  266. if (dt != null)
  267. {
  268. DataTable dtInstruShow = new DataTable();
  269. dtInstruShow.Columns.Add("InstruFieldName");
  270. dtInstruShow.Columns.Add("InstruDataType");
  271. foreach (DataColumn dc in dt.Columns)
  272. {
  273. dtInstruShow.Rows.Add(new object[] { dc.ColumnName,dc.DataType});
  274. }
  275. dgvInstruDS.DataSource = dtInstruShow;
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// 切换表时发生
  281. /// </summary>
  282. /// <param name="sender"></param>
  283. /// <param name="e"></param>
  284. private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
  285. {
  286. if (cbxCnas.Text == null) return;
  287. string strTableName_Cnas = cbxCnas.Text.ToString();
  288. //if (currentSyncItem.CnasInstrumentColumn != null && currentSyncItem.CnasInstrumentColumn != "")
  289. //{
  290. // DialogResult dr = MessageBox.Show("已存在配置完成的CNAS仪器对应列,切换表将导致该配置失效,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  291. // if (dr == DialogResult.No)
  292. // {
  293. // return;
  294. // }
  295. //}
  296. //从数据库中加载数据表结构
  297. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
  298. if (dtTableStruct != null)
  299. {
  300. DataTable dtCnasShow = new DataTable();
  301. dtCnasShow.Columns.Add("CnasFieldName");
  302. dtCnasShow.Columns.Add("CnasDataType");
  303. foreach (DataColumn dc in dtTableStruct.Columns)
  304. {
  305. dtCnasShow.Rows.Add(new object[] { dc.ColumnName ,dc.DataType});
  306. }
  307. dgvCnas.DataSource = dtCnasShow;
  308. //绑定数据
  309. cbxCNASColumn.DataSource = dtCnasShow.Copy();
  310. cbxCNASColumn.DisplayMember = "CnasFieldName";
  311. cbxCNASColumn.ValueMember = "CnasFieldName";
  312. if (dtTableStruct.Columns.Count > 0)
  313. {
  314. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn)&&dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
  315. {
  316. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  317. }
  318. else
  319. {
  320. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
  321. }
  322. }
  323. }
  324. }
  325. //增加映射
  326. private void btnAddMapping_Click(object sender, EventArgs e)
  327. {
  328. if (cbxInstrument.Text == null) return;
  329. if (cbxCnas.Text == null) return;
  330. if (dgvInstruDS.Rows.Count <= 0) return;
  331. if (dgvCnas.Rows.Count <= 0) return;
  332. SyncParamasInfo syncParamas = new SyncParamasInfo();
  333. if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
  334. if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
  335. {
  336. MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
  337. return;
  338. }
  339. //仪器表名和选中行
  340. syncParamas.SourceTable = cbxInstrument.Text.ToString().ToUpper();
  341. syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper();
  342. //CNAS表名和选中行
  343. syncParamas.TargetTable = cbxCnas.Text.ToString().ToUpper();
  344. syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper();
  345. //验证数据合法性
  346. SyncParamsOperation paramsOperation = new SyncParamsOperation();
  347. if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
  348. {
  349. MessageBox.Show("已存在不同表单映射数据,无法添加!");
  350. return;
  351. }
  352. //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
  353. //{
  354. // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
  355. // return;
  356. //}
  357. if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
  358. {
  359. MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
  360. return;
  361. }
  362. if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
  363. {
  364. MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
  365. return;
  366. }
  367. //绑定数据
  368. currentSyncItem.LstSyncPramas.Add(syncParamas);
  369. dgvMapping.DataSource= new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  370. //选中最后一行
  371. dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count-1].Cells[0];
  372. }
  373. /// <summary>
  374. /// 删除映射
  375. /// </summary>
  376. /// <param name="sender"></param>
  377. /// <param name="e"></param>
  378. private void btnDelMap_Click(object sender, EventArgs e)
  379. {
  380. //当前选中项
  381. if (dgvMapping.CurrentCell == null) return;
  382. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  383. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  384. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  385. if (lstDelItems.Count > 0)
  386. {
  387. foreach (var item in lstDelItems)
  388. {
  389. currentSyncItem.LstSyncPramas.Remove(item);
  390. }
  391. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  392. }
  393. }
  394. /// <summary>
  395. /// 配置数据库界面
  396. /// </summary>
  397. /// <param name="sender"></param>
  398. /// <param name="e"></param>
  399. private void btnDatabaseConfig_Click(object sender, EventArgs e)
  400. {
  401. frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
  402. frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  403. {
  404. this.currentSyncItem = Instrumentitem;
  405. };
  406. frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
  407. {
  408. //this.dictInstruTables = dict;
  409. };
  410. frmDatabase.ShowDialog();
  411. //加载数据
  412. btnLoadDBData_Click(null, null);
  413. }
  414. private void btnLoadDBData_Click(object sender, EventArgs e)
  415. {
  416. int iReturn = 0;
  417. if (strTableInfoMode=="0")
  418. iReturn=LoadSourceAndTargetData(true);
  419. else
  420. iReturn= LoadSourceAndTargetData();
  421. switch (iReturn)
  422. {
  423. case -1:
  424. MessageBox.Show("未能成功获取设备数据库配置信息,请配置后重试!");
  425. break;
  426. case -2:
  427. MessageBox.Show("未能成功获取CNAS数据库配置信息,请配置后重试!");
  428. break;
  429. case -3:
  430. MessageBox.Show("未能成功获取仪器数据信息,请配置后重试!");
  431. break;
  432. case -4:
  433. MessageBox.Show("未能成功获取CNAS数据信息,请配置后重试!");
  434. break;
  435. case -5:
  436. DialogResult dr = MessageBox.Show("检测到数据连接配置已经修改,是否全部删除已经分配的字段映射?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  437. if (dr == DialogResult.Yes)
  438. {
  439. //此时将全部删除已经分配的映射字段
  440. currentSyncItem.LstSyncPramas.Clear();
  441. currentSyncItem.lstFixedValue.Clear();
  442. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  443. }
  444. break;
  445. case -6:
  446. MessageBox.Show("请先手动输入表名");
  447. break;
  448. default:
  449. break;
  450. }
  451. }
  452. /// <summary>
  453. /// 加载来源和目标数据的数据结构
  454. /// </summary>
  455. /// <param name="bIfLoading"></param>
  456. /// <returns></returns>
  457. public int LoadSourceAndTargetData(bool bIfLoading=false)
  458. {
  459. //检查配置信息
  460. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  461. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  462. bool bIfSameTable = true;
  463. //是否需要重新加载来源库
  464. if (bIfLoading)
  465. {
  466. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  467. dictInstruTables = instrumentData.GetInstrumentData();
  468. }
  469. if (dictInstruTables.Count <= 0) return -3;
  470. //绑定ComboBox
  471. List<string> lstTableName = new List<string>();
  472. foreach (var item in dictInstruTables)
  473. {
  474. lstTableName.Add(item.Key);
  475. }
  476. cbxInstrument.DataSource = lstTableName;
  477. cbxInstrument.ValueMember = "";
  478. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  479. {
  480. if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
  481. cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
  482. else
  483. bIfSameTable = false;
  484. }
  485. //获取CNAS配置的数据库连接信息
  486. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  487. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  488. {
  489. List<string> lstCnasTables = new List<string>();
  490. foreach (DataRow dr in dtCNAS.Rows)
  491. {
  492. lstCnasTables.Add(dr["TABNAME"].ToString());
  493. }
  494. cbxCnas.DataSource = lstCnasTables;
  495. cbxCnas.ValueMember = "";
  496. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  497. {
  498. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  499. {
  500. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  501. //cbxCnas_SelectedIndexChanged(null, null);
  502. }
  503. else
  504. bIfSameTable = false;
  505. }
  506. }
  507. else
  508. return -4;
  509. if (!bIfSameTable)
  510. {
  511. return -5;
  512. }
  513. return 1;
  514. }
  515. /// <summary>
  516. /// 手动输入表名时,获取该表的表信息
  517. /// </summary>
  518. /// <returns></returns>
  519. public int LoadSourceAndTargetData()
  520. {
  521. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  522. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  523. if (cbxInstrument.Text == ""||cbxCnas.Text=="") return -6;
  524. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { cbxInstrument.Text, "", "" });
  525. DataTable dataTableStruct = instrumentData.GetInstrumentDataStruct();
  526. if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
  527. {
  528. dictInstruTables.Clear();
  529. dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
  530. cbxInstrument_SelectedIndexChanged(null, null);
  531. if (CnasDataOperationFact.CnasDataOperation().TestConnect(currentSyncItem.SyncTargetDBInfo.DBHost, currentSyncItem.SyncTargetDBInfo.DBName, currentSyncItem.SyncTargetDBInfo.DBUser, currentSyncItem.SyncTargetDBInfo.DBPwd, currentSyncItem.SyncTargetDBInfo.DBPort))
  532. cbxCnas_SelectedIndexChanged(null, null);
  533. else
  534. return -4;
  535. return 1;
  536. }
  537. else
  538. {
  539. return -3;
  540. }
  541. }
  542. private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  543. {
  544. if (dgvMapping.CurrentCell == null) return;
  545. if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
  546. {
  547. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  548. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  549. string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
  550. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  551. if (lstDelItems.Count > 0)
  552. {
  553. foreach (var item in lstDelItems)
  554. {
  555. item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
  556. }
  557. }
  558. }
  559. if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
  560. {
  561. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  562. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  563. string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
  564. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  565. if (lstDelItems.Count == 1)
  566. {
  567. if (strDateKey.ToLower() == "true")
  568. {
  569. //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
  570. foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
  571. {
  572. if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
  573. dgvRow.Cells["DateKey"].Value = false;
  574. }
  575. //内存数据源
  576. foreach (var item in currentSyncItem.LstSyncPramas)
  577. {
  578. if (item.IfDateField)
  579. item.IfDateField = false;
  580. }
  581. lstDelItems[0].IfDateField = true;
  582. }
  583. else
  584. {
  585. lstDelItems[0].IfDateField = false;
  586. }
  587. }
  588. }
  589. }
  590. private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  591. {
  592. if (dgvMapping.IsCurrentCellDirty)
  593. {
  594. dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
  595. }
  596. }
  597. private void btnCNASFieldConfig_Click(object sender, EventArgs e)
  598. {
  599. frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
  600. if (frmCNAS.ShowDialog() == DialogResult.OK)
  601. {
  602. this.currentSyncItem = frmCNAS.syncInstrument;
  603. }
  604. }
  605. private void btnSourceFilter_Click(object sender, EventArgs e)
  606. {
  607. frmSourceFilter frm = new frmSourceFilter(currentSyncItem);
  608. frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
  609. {
  610. currentSyncItem.SourceFilter = dataFilter;
  611. };
  612. frm.ShowDialog();
  613. }
  614. private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  615. {
  616. if (dgvInstrument.Rows.Count <= 0) return;
  617. //当前选中单元格
  618. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  619. frmInstrumentCode frmCode = new frmInstrumentCode(lstSyncInstrument, strInstrumentCode);
  620. frmCode.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  621. {
  622. var lstInstrument = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  623. if (lstInstrument.Count == 1)
  624. {
  625. lstInstrument[0].Code = Instrumentitem.Code;
  626. lstInstrument[0].InstruType = Instrumentitem.InstruType;
  627. }
  628. };
  629. frmCode.ShowDialog();
  630. //绑定数据
  631. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  632. }
  633. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  634. {
  635. if (cbxCNASColumn.Visible)
  636. {
  637. cbxCNASColumn.Visible = false;
  638. txtInstrumentColumn.Visible = true;
  639. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  640. }
  641. }
  642. /// <summary>
  643. /// 当双击textBox时,显示ComboBOX用于选择
  644. /// </summary>
  645. /// <param name="sender"></param>
  646. /// <param name="e"></param>
  647. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  648. {
  649. txtInstrumentColumn.Visible = false;
  650. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  651. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  652. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  653. cbxCNASColumn.Location= txtInstrumentColumn.Location;
  654. cbxCNASColumn.Visible = true;
  655. }
  656. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  657. {
  658. if (cbxCNASColumn.Visible)
  659. {
  660. txtInstrumentColumn.Visible = true;
  661. cbxCNASColumn.Visible = false;
  662. }
  663. }
  664. }
  665. }