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

frmSyncParams.cs 34KB

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