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.

4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
4 miesięcy temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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.Text.RegularExpressions;
  13. using System.Windows.Forms;
  14. using System.Xml.Linq;
  15. using System.Xml.Serialization;
  16. namespace CNAS_DBSync
  17. {
  18. public delegate void ReferenceChangeSyncParamHanlder(SyncInstrumentItemInfo syncInstrumentItem);
  19. public partial class frmSyncParams : Form
  20. {
  21. public List<SyncInstrumentItemInfo> lstSyncInstrument = new List<SyncInstrumentItemInfo>(); //本地存储的仪器数据源
  22. public SyncInstrumentItemInfo currentSyncItem = new SyncInstrumentItemInfo(); //当前正在操作的仪器项
  23. public Dictionary<string, DataTable> dictInstruTables = new Dictionary<string, DataTable>(); //当前仪器项的来源表结构
  24. private SyncInstrumentItemInfo syncInstrumentItem; //其他调用传递的仪器项
  25. public ReferenceChangeSyncParamHanlder ParamsChangedDelegate; //传递仪器参数委托
  26. private string strMode = "";
  27. private string strTableInfoMode = ""; //获取所有表信息的方式;0为自动获取,1为手动输入
  28. public frmSyncParams(SyncInstrumentItemInfo syncInstrumentItem = null)
  29. {
  30. InitializeComponent();
  31. dgvInstrument.AutoGenerateColumns = false;
  32. dgvInstrument.RowHeadersVisible = false;
  33. dgvInstruDS.AutoGenerateColumns = false;
  34. dgvInstruDS.RowHeadersVisible = false;
  35. dgvCnas.AutoGenerateColumns = false;
  36. dgvCnas.RowHeadersVisible = false;
  37. dgvMapping.AutoGenerateColumns = false;
  38. dgvMapping.RowHeadersVisible = false;
  39. if (syncInstrumentItem != null)
  40. {
  41. strMode = "Reference";
  42. this.syncInstrumentItem = syncInstrumentItem;
  43. this.btnAdd.Visible = false;
  44. this.btnDel.Visible = false;
  45. }
  46. }
  47. private void frmSyncParams_Load(object sender, EventArgs e)
  48. {
  49. if (syncInstrumentItem == null)
  50. {
  51. lstSyncInstrument = FileOperation.GetLocalSyncInStrumentData();
  52. }
  53. else
  54. {
  55. lstSyncInstrument = new List<SyncInstrumentItemInfo>() { syncInstrumentItem };
  56. }
  57. //绑定数据源,填写相关内容
  58. if (lstSyncInstrument.Count != 0)
  59. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  60. }
  61. /// <summary>
  62. /// 保存当前设置到本地
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. private void btnSave_Click(object sender, EventArgs e)
  67. {
  68. //将配置好的信息存储到本地文件中
  69. try
  70. {
  71. bool bIfSaveSuccess = true;
  72. if (strMode == "Reference")
  73. {
  74. if (!CheckIfHaveDateField(new List<SyncInstrumentItemInfo>() { currentSyncItem }))
  75. {
  76. MessageBox.Show("日期字段不允许为空!");
  77. return;
  78. }
  79. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  80. {
  81. MessageBox.Show("关键字段不允许为空!");
  82. return;
  83. }
  84. //1.先加载所有数据 2.替换当前数据 3.重新保存
  85. List<SyncInstrumentItemInfo> lstDB = FileOperation.GetLocalSyncInStrumentData();
  86. var item = lstDB.Where(p => p.GUID == currentSyncItem.GUID).SingleOrDefault();
  87. if (item != null)
  88. {
  89. lstDB.Remove(item);
  90. lstDB.Add(currentSyncItem);
  91. }
  92. else
  93. {
  94. lstDB.Add(currentSyncItem);
  95. }
  96. //重新保存信息
  97. bIfSaveSuccess = FileOperation.SaveLocalSyncInStrumentData(lstDB);
  98. //委托发送参数
  99. this.ParamsChangedDelegate(syncInstrumentItem);
  100. }
  101. else
  102. {
  103. if (!CheckIfHaveDateField(lstSyncInstrument))
  104. {
  105. MessageBox.Show("日期字段不允许为空!");
  106. return;
  107. }
  108. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  109. {
  110. MessageBox.Show("关键字段不允许为空!");
  111. return;
  112. }
  113. bIfSaveSuccess = FileOperation.SaveLocalSyncInStrumentData(lstSyncInstrument);
  114. }
  115. if (bIfSaveSuccess)
  116. MessageBox.Show("保存成功!");
  117. else
  118. MessageBox.Show("保存失败!");
  119. }
  120. catch (Exception ex)
  121. {
  122. MessageBox.Show("保存失败!错误信息为:" + ex.Message.ToString());
  123. AppLog.Error(ex.Message);
  124. }
  125. }
  126. private bool CheckIfHaveDateField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  127. {
  128. bool bIfHave = true;
  129. foreach (var item in lstSyncInstrument)
  130. {
  131. if (item.LstSyncPramas == null) continue;
  132. if (item.LstSyncPramas.Count <= 0) continue;
  133. if (item.LstSyncPramas.Where(s => s.IfDateField == true).Count() != 1)
  134. {
  135. bIfHave = false;
  136. break;
  137. }
  138. }
  139. return bIfHave;
  140. }
  141. private bool CheckIfHaveKeyPrimaryField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  142. {
  143. bool bIfHave = true;
  144. foreach (var item in lstSyncInstrument)
  145. {
  146. if (item.LstSyncPramas == null) continue;
  147. if (item.LstSyncPramas.Count <= 0) continue;
  148. if (item.LstSyncPramas.Where(s => s.IfPrimaryKey == true).Count() <= 0)
  149. {
  150. bIfHave = false;
  151. break;
  152. }
  153. }
  154. return bIfHave;
  155. }
  156. /// <summary>
  157. /// 新增仪器
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. private void btnAdd_Click(object sender, EventArgs e)
  162. {
  163. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument);
  164. frmInstrumentCode frmCode = new frmInstrumentCode(lstSyncInstrument);
  165. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  166. {
  167. lstSyncInstrument.Add(Instrumentitem);
  168. //绑定数据
  169. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  170. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  171. dgvInstrument_SelectionChanged(null, null);
  172. };
  173. frmSetting.ShowDialog();
  174. }
  175. /// <summary>
  176. /// 删除仪器
  177. /// </summary>
  178. /// <param name="sender"></param>
  179. /// <param name="e"></param>
  180. private void btnDel_Click(object sender, EventArgs e)
  181. {
  182. if (dgvInstrument.Rows.Count <= 0) return;
  183. //当前选中行
  184. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  185. //找到数据源中数据,删除
  186. var lstitem = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  187. if (lstitem == null)
  188. return;
  189. else if (lstitem.Count >= 1)
  190. {
  191. foreach (var item in lstitem)
  192. {
  193. lstSyncInstrument.Remove(item);
  194. }
  195. }
  196. //绑定数据
  197. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>();
  198. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  199. //dgvInstrument.dgvInstrument_SelectionChanged()
  200. if (lstSyncInstrument.Count > 0)
  201. dgvInstrument_SelectionChanged(null, null);
  202. else
  203. {
  204. currentSyncItem = new SyncInstrumentItemInfo();
  205. dictInstruTables.Clear();
  206. cbxCnas.DataSource = null;
  207. cbxCnas.Items.Clear();
  208. cbxInstrument.DataSource = null;
  209. dgvInstruDS.DataSource = null;
  210. dgvCnas.DataSource = null;
  211. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  212. }
  213. }
  214. private void dgvInstrument_SelectionChanged(object sender, EventArgs e)
  215. {
  216. if (dgvInstrument.Rows.Count <= 0 || dgvInstrument.Rows[dgvInstrument.CurrentRow.Index].Cells[0].Value == null)
  217. {
  218. currentSyncItem = new SyncInstrumentItemInfo();
  219. return;
  220. }
  221. //清空绑定
  222. cbxCnas.DataSource = null;
  223. cbxInstrument.DataSource = null;
  224. dgvInstruDS.DataSource = null;
  225. dgvCnas.DataSource = null;
  226. //当前选中单元格
  227. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  228. var lstInstrument = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  229. if (lstInstrument.Count == 1)
  230. {
  231. currentSyncItem = lstInstrument[0];
  232. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn))
  233. txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  234. else
  235. txtInstrumentColumn.Text = "";
  236. if (currentSyncItem.SyncInstrumentDSInfo != null && currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType != DataSourceType.None)
  237. {
  238. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  239. if (currentSyncItem.SyncInstrumentDSInfo.ServerName != "")
  240. btnLoadDBData_Click(sender, e);
  241. }
  242. else
  243. {
  244. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  245. }
  246. }
  247. else
  248. {
  249. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  250. }
  251. }
  252. /// <summary>
  253. /// 切换选中表时发生
  254. /// </summary>
  255. /// <param name="sender"></param>
  256. /// <param name="e"></param>
  257. private void cbxInstrument_SelectedIndexChanged(object sender, EventArgs e)
  258. {
  259. if (cbxInstrument.Text == null) return;
  260. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  261. DataTable dtTableType = null;
  262. string strTableName_Instru = cbxInstrument.Text.ToString();
  263. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  264. {
  265. case DataSourceType.MySQL:
  266. dtTableType = SelectTableType.MySqlsec(strTableName_Instru);
  267. break;
  268. case DataSourceType.Dm:
  269. dtTableType = SelectTableType.DmSql(strTableName_Instru);
  270. break;
  271. case DataSourceType.Oracle:
  272. dtTableType = SelectTableType.OrcSql(strTableName_Instru, currentSyncItem);
  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. default:
  281. break;
  282. }
  283. DataTable dtInstruShow = new DataTable();
  284. dtInstruShow.Columns.Add("InstruFieldName");
  285. dtInstruShow.Columns.Add("InstruDataType");
  286. dtInstruShow.Columns.Add("remark");
  287. for (int i = 0; i < dtTableType.Rows.Count; i++)
  288. {
  289. dtInstruShow.Rows.Add(new object[] { dtTableType.Rows[i]["ColumnName"], dtTableType.Rows[i]["DataType"], dtTableType.Rows[i]["remark"] });
  290. }
  291. dgvInstruDS.DataSource = dtInstruShow;
  292. }
  293. /// <summary>
  294. /// 切换表时发生
  295. /// </summary>
  296. /// <param name="sender"></param>
  297. /// <param name="e"></param>
  298. private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
  299. {
  300. if (cbxCnas.Text == null) return;
  301. string strTableName_Cnas = cbxCnas.Text.ToString();
  302. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Cnas, currentSyncItem.SyncTargetDBInfo);
  303. //从数据库中加载数据表结构
  304. //DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
  305. if (dtTableStruct != null)
  306. {
  307. DataTable dtCnasShow = new DataTable();
  308. dtCnasShow.Columns.Add("CnasFieldName");
  309. dtCnasShow.Columns.Add("CnasDataType");
  310. dtCnasShow.Columns.Add("remark");
  311. for (int i = 0; i < dtTableStruct.Rows.Count; i++)
  312. {
  313. dtCnasShow.Rows.Add(new object[] { dtTableStruct.Rows[i]["ColumnName"], dtTableStruct.Rows[i]["DataType"], dtTableStruct.Rows[i]["remark"] });
  314. }
  315. dgvCnas.DataSource = dtCnasShow;
  316. //绑定数据
  317. cbxCNASColumn.DataSource = dtCnasShow.Copy();
  318. cbxCNASColumn.DisplayMember = "CnasFieldName";
  319. cbxCNASColumn.ValueMember = "CnasFieldName";
  320. if (dtTableStruct.Columns.Count > 0)
  321. {
  322. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn) && dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
  323. {
  324. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  325. }
  326. else
  327. {
  328. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
  329. }
  330. }
  331. }
  332. }
  333. //增加映射
  334. private void btnAddMapping_Click(object sender, EventArgs e)
  335. {
  336. if (cbxInstrument.Text == null) return;
  337. if (cbxCnas.Text == null) return;
  338. if (dgvInstruDS.Rows.Count <= 0) return;
  339. if (dgvCnas.Rows.Count <= 0) return;
  340. SyncParamasInfo syncParamas = new SyncParamasInfo();
  341. if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
  342. if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
  343. {
  344. MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
  345. return;
  346. }
  347. //仪器表名和选中行
  348. syncParamas.SourceTable = cbxInstrument.Text.ToString();
  349. syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString();
  350. syncParamas.DataType = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[1].Value.ToString().ToUpper();
  351. //CNAS表名和选中行
  352. syncParamas.TargetTable = cbxCnas.Text.ToString();
  353. syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString();
  354. //验证数据合法性
  355. SyncParamsOperation paramsOperation = new SyncParamsOperation();
  356. if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
  357. {
  358. MessageBox.Show("已存在不同表单映射数据,无法添加!");
  359. return;
  360. }
  361. //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
  362. //{
  363. // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
  364. // return;
  365. //}
  366. if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
  367. {
  368. MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
  369. return;
  370. }
  371. if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
  372. {
  373. MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
  374. return;
  375. }
  376. //绑定数据
  377. currentSyncItem.LstSyncPramas.Add(syncParamas);
  378. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  379. //选中最后一行
  380. dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count - 1].Cells[0];
  381. }
  382. /// <summary>
  383. /// 删除映射
  384. /// </summary>
  385. /// <param name="sender"></param>
  386. /// <param name="e"></param>
  387. private void btnDelMap_Click(object sender, EventArgs e)
  388. {
  389. //当前选中项
  390. if (dgvMapping.CurrentCell == null) return;
  391. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  392. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  393. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  394. if (lstDelItems.Count > 0)
  395. {
  396. foreach (var item in lstDelItems)
  397. {
  398. currentSyncItem.LstSyncPramas.Remove(item);
  399. }
  400. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  401. }
  402. }
  403. /// <summary>
  404. /// 配置数据库界面
  405. /// </summary>
  406. /// <param name="sender"></param>
  407. /// <param name="e"></param>
  408. private void btnDatabaseConfig_Click(object sender, EventArgs e)
  409. {
  410. frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
  411. frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  412. {
  413. this.currentSyncItem = Instrumentitem;
  414. };
  415. frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
  416. {
  417. //this.dictInstruTables = dict;
  418. };
  419. frmDatabase.ShowDialog();
  420. //加载数据
  421. if ((currentSyncItem.SyncInstrumentDSInfo.Host != null && currentSyncItem.SyncInstrumentDSInfo.Host.Length > 0) || (currentSyncItem.SyncInstrumentDSInfo.Path != null && currentSyncItem.SyncInstrumentDSInfo.Path.Length > 0))
  422. btnLoadDBData_Click(null, null);
  423. }
  424. private void btnLoadDBData_Click(object sender, EventArgs e)
  425. {
  426. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  427. strTableInfoMode = FileOperation.GetSystemFormatConfigData(strInstrumentCode).TableInfoMode;
  428. if (strTableInfoMode == "1")
  429. {
  430. cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  431. //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  432. }
  433. int iReturn = 0;
  434. if (strTableInfoMode == "0")
  435. iReturn = LoadSourceAndTargetData(true);
  436. else
  437. iReturn = LoadSourceAndTargetData();
  438. switch (iReturn)
  439. {
  440. case -1:
  441. MessageBox.Show("未能成功获取设备数据库配置信息,请配置后重试!");
  442. break;
  443. case -2:
  444. MessageBox.Show("未能成功获取CNAS数据库配置信息,请配置后重试!");
  445. break;
  446. case -3:
  447. MessageBox.Show("未能成功获取仪器数据信息,请配置后重试!");
  448. break;
  449. case -4:
  450. MessageBox.Show("未能成功获取CNAS数据信息,请配置后重试!");
  451. break;
  452. case -5:
  453. DialogResult dr = MessageBox.Show("检测到数据连接配置已经修改,是否全部删除已经分配的字段映射?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  454. if (dr == DialogResult.Yes)
  455. {
  456. //此时将全部删除已经分配的映射字段
  457. currentSyncItem.LstSyncPramas.Clear();
  458. currentSyncItem.lstFixedValue.Clear();
  459. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  460. }
  461. break;
  462. case -6:
  463. MessageBox.Show("请先手动输入表名");
  464. break;
  465. default:
  466. break;
  467. }
  468. }
  469. /// <summary>
  470. /// 加载来源和目标数据的数据结构
  471. /// </summary>
  472. /// <param name="bIfLoading"></param>
  473. /// <returns></returns>
  474. public int LoadSourceAndTargetData(bool bIfLoading = false)
  475. {
  476. //检查配置信息
  477. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  478. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  479. bool bIfSameTable = true;
  480. //是否需要重新加载来源库
  481. if (bIfLoading)
  482. {
  483. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  484. dictInstruTables = instrumentData.GetInstrumentData();
  485. }
  486. if (dictInstruTables.Count <= 0) return -3;
  487. //绑定ComboBox
  488. List<string> lstTableName = new List<string>();
  489. foreach (var item in dictInstruTables)
  490. {
  491. lstTableName.Add(item.Key);
  492. }
  493. cbxInstrument.DataSource = lstTableName;
  494. cbxInstrument.ValueMember = "";
  495. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  496. {
  497. if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
  498. cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
  499. else
  500. bIfSameTable = false;
  501. }
  502. //获取CNAS配置的数据库连接信息
  503. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  504. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  505. {
  506. List<string> lstCnasTables = new List<string>();
  507. foreach (DataRow dr in dtCNAS.Rows)
  508. {
  509. if (dtCNAS.Columns.Contains("TABNAME"))
  510. lstCnasTables.Add(dr["TABNAME"].ToString());
  511. else if (dtCNAS.Columns.Contains("table_name"))
  512. lstCnasTables.Add(dr["table_name"].ToString());
  513. }
  514. cbxCnas.DataSource = lstCnasTables;
  515. cbxCnas.ValueMember = "";
  516. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  517. {
  518. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  519. {
  520. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  521. //cbxCnas_SelectedIndexChanged(null, null);
  522. }
  523. else
  524. bIfSameTable = false;
  525. }
  526. }
  527. else
  528. return -4;
  529. if (!bIfSameTable)
  530. {
  531. return -5;
  532. }
  533. return 1;
  534. }
  535. public AccessFormatConfig AccessFormat { get; set; }
  536. public ExcelFormatConfig ExcelFormat { get; set; }
  537. public FoxProFormatConfig FoxProFormat { get; set; }
  538. public SqliteFormatConfig SqliteFormat { get; set; }
  539. public SqlServerFormatConfig SqlServerFormat { get; set; }
  540. public OracleFormatConfig OracleFormat { get; set; }
  541. public MySqlFormatConfig MySqlFormat { get; set; }
  542. public NormalFileFormatConfig NormalFileFormat { get; set; }
  543. public PostgreSqlFormatConfig PostgreSqlFormat { get; set; }
  544. public KingbaseFormatConfig KingbaseFormat { get; set; }
  545. public DmFormatConfig DmFormat { get; set; }
  546. /// <summary>
  547. /// 手动输入表名时,获取该表的表信息
  548. /// </summary>
  549. /// <returns></returns>
  550. public int LoadSourceAndTargetData()
  551. {
  552. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  553. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  554. string sql = "";
  555. ExcelFormat = FileOperation.GetFormatConfigData<ExcelFormatConfig>("ExcelFormatConfig.xml");
  556. AccessFormat = FileOperation.GetFormatConfigData<AccessFormatConfig>("AccessFormatConfig.xml");
  557. FoxProFormat = FileOperation.GetFormatConfigData<FoxProFormatConfig>("FoxProFormatConfig.xml");
  558. SqliteFormat = FileOperation.GetFormatConfigData<SqliteFormatConfig>("SqliteFormatConfig.xml");
  559. SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  560. OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
  561. NormalFileFormat = FileOperation.GetFormatConfigData<NormalFileFormatConfig>("NormalFileFormatConfig.xml");
  562. KingbaseFormat = FileOperation.GetFormatConfigData<KingbaseFormatConfig>("KingbaseFormatConfig.xml");
  563. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  564. {
  565. case DataSourceType.MySQL:
  566. MySqlFormat = FileOperation.GetFormatConfigData<MySqlFormatConfig>("MySqlFormatConfig.xml");
  567. sql = MySqlFormat.AutoSql.MySqlViewSql;
  568. break;
  569. case DataSourceType.Dm:
  570. DmFormat = FileOperation.GetFormatConfigData<DmFormatConfig>("DmFormatConfig.xml");
  571. sql = DmFormat.AutoSql.DmViewSql;
  572. break;
  573. case DataSourceType.Oracle:
  574. OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
  575. sql = OracleFormat.AutoSql.OracleViewSql;
  576. break;
  577. case DataSourceType.PostgreSQL:
  578. PostgreSqlFormat = FileOperation.GetFormatConfigData<PostgreSqlFormatConfig>("PostgreSqlFormatConfig.xml");
  579. sql = PostgreSqlFormat.AutoSql.PostgreSqlViewSql;
  580. break;
  581. case DataSourceType.SQL:
  582. SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  583. sql = SqlServerFormat.AutoSql.SqlServerViewSql;
  584. break;
  585. default:
  586. break;
  587. }
  588. cbxInstrument.Text = ExtractTableNames(sql);
  589. if (cbxInstrument.Text == "") return -6;
  590. int returnValue = 1;
  591. //根据手动输入来源库的表名加载字段
  592. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  593. //dictInstruTables = instrumentData.GetInstrumentData();
  594. //dictInstruTables =
  595. DataTable dataTableStruct = null;
  596. string strTableName_Instru = cbxInstrument.Text.ToString();
  597. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  598. {
  599. case DataSourceType.MySQL:
  600. dataTableStruct = SelectTableType.MySqlsec(cbxInstrument.Text);
  601. break;
  602. case DataSourceType.Dm:
  603. dataTableStruct = SelectTableType.DmSql(cbxInstrument.Text);
  604. break;
  605. case DataSourceType.Oracle:
  606. dataTableStruct = SelectTableType.OrcSql(cbxInstrument.Text, currentSyncItem);
  607. break;
  608. case DataSourceType.PostgreSQL:
  609. dataTableStruct = SelectTableType.PostgreSql(cbxInstrument.Text);
  610. break;
  611. case DataSourceType.SQL:
  612. dataTableStruct = SelectTableType.Sqlserversec(cbxInstrument.Text, currentSyncItem);
  613. break;
  614. default:
  615. break;
  616. }
  617. DataTable dtInstruShow = new DataTable();
  618. dtInstruShow.Columns.Add("InstruFieldName");
  619. dtInstruShow.Columns.Add("InstruDataType");
  620. dtInstruShow.Columns.Add("remark");
  621. if (dataTableStruct != null)
  622. {
  623. for (int i = 0; i < dataTableStruct.Rows.Count; i++)
  624. {
  625. dtInstruShow.Rows.Add(new object[] { dataTableStruct.Rows[i]["ColumnName"], dataTableStruct.Rows[i]["DataType"], dataTableStruct.Rows[i]["remark"] });
  626. }
  627. }
  628. dgvInstruDS.DataSource = dtInstruShow;
  629. if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
  630. {
  631. dictInstruTables.Clear();
  632. dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
  633. //return 1;
  634. }
  635. else
  636. {
  637. returnValue = -3;
  638. return returnValue;
  639. }
  640. //获取CNAS配置的数据库连接信息
  641. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  642. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  643. {
  644. List<string> lstCnasTables = new List<string>();
  645. foreach (DataRow dr in dtCNAS.Rows)
  646. {
  647. lstCnasTables.Add(dr["TABNAME"].ToString());
  648. }
  649. cbxCnas.DataSource = lstCnasTables;
  650. cbxCnas.ValueMember = "";
  651. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  652. {
  653. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  654. {
  655. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  656. //cbxCnas_SelectedIndexChanged(null, null);
  657. }
  658. else
  659. returnValue = -5;
  660. }
  661. }
  662. else
  663. returnValue = -4;
  664. return returnValue;
  665. }
  666. public string ExtractTableNames(string sql)
  667. {
  668. // 预处理:去除注释、统一空格
  669. sql = Regex.Replace(sql, @"(--.*)|(\/\*[\s\S]*?\*\/)", "", RegexOptions.Multiline);
  670. sql = Regex.Replace(sql, @"\s+", " ");
  671. // 定义匹配模式
  672. var patterns = new Dictionary<string, string>
  673. {
  674. { "SELECT", @"(?:FROM|JOIN)\s+([\w\.]+)(?:\s+AS\s+\w+)?" },
  675. { "INSERT", @"INSERT\s+INTO\s+([\w\.]+)" },
  676. { "UPDATE", @"UPDATE\s+([\w\.]+)" },
  677. { "DELETE", @"DELETE\s+FROM\s+([\w\.]+)" }
  678. };
  679. var tables = new HashSet<string>();
  680. foreach (var pattern in patterns.Values)
  681. {
  682. var matches = Regex.Matches(sql, pattern, RegexOptions.IgnoreCase);
  683. foreach (Match match in matches)
  684. {
  685. if (match.Groups[1].Success)
  686. tables.Add(match.Groups[1].Value.Trim());
  687. }
  688. }
  689. if (tables.Count == 0)
  690. return "";
  691. else
  692. return tables.ToList()[0];
  693. }
  694. private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  695. {
  696. if (dgvMapping.CurrentCell == null) return;
  697. if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
  698. {
  699. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  700. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  701. string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
  702. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  703. if (lstDelItems.Count > 0)
  704. {
  705. foreach (var item in lstDelItems)
  706. {
  707. item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
  708. }
  709. }
  710. }
  711. if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
  712. {
  713. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  714. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  715. string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
  716. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  717. if (lstDelItems.Count == 1)
  718. {
  719. if (strDateKey.ToLower() == "true")
  720. {
  721. //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
  722. foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
  723. {
  724. if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
  725. dgvRow.Cells["DateKey"].Value = false;
  726. }
  727. //内存数据源
  728. foreach (var item in currentSyncItem.LstSyncPramas)
  729. {
  730. if (item.IfDateField)
  731. item.IfDateField = false;
  732. }
  733. lstDelItems[0].IfDateField = true;
  734. }
  735. else
  736. {
  737. lstDelItems[0].IfDateField = false;
  738. }
  739. }
  740. }
  741. }
  742. private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  743. {
  744. if (dgvMapping.IsCurrentCellDirty)
  745. {
  746. dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
  747. }
  748. }
  749. private void btnCNASFieldConfig_Click(object sender, EventArgs e)
  750. {
  751. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  752. {
  753. MessageBox.Show("请先指定至少一个映射字段。");
  754. return;
  755. }
  756. frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
  757. if (frmCNAS.ShowDialog() == DialogResult.OK)
  758. {
  759. this.currentSyncItem = frmCNAS.syncInstrument;
  760. }
  761. }
  762. private void btnSourceFilter_Click(object sender, EventArgs e)
  763. {
  764. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  765. {
  766. MessageBox.Show("请先指定至少一个映射字段。");
  767. return;
  768. }
  769. frmSourceFilter frm = new frmSourceFilter(currentSyncItem, strTableInfoMode);
  770. frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
  771. {
  772. currentSyncItem.SourceFilter = dataFilter;
  773. };
  774. frm.ShowDialog();
  775. }
  776. private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  777. {
  778. if (dgvInstrument.Rows.Count <= 0) return;
  779. //当前选中单元格
  780. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  781. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  782. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  783. {
  784. lstSyncInstrument.Add(Instrumentitem);
  785. //绑定数据
  786. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  787. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  788. dgvInstrument_SelectionChanged(null, null);
  789. };
  790. frmSetting.ShowDialog();
  791. }
  792. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  793. {
  794. if (cbxCNASColumn.Visible)
  795. {
  796. cbxCNASColumn.Visible = false;
  797. txtInstrumentColumn.Visible = true;
  798. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  799. }
  800. }
  801. /// <summary>
  802. /// 当双击textBox时,显示ComboBOX用于选择
  803. /// </summary>
  804. /// <param name="sender"></param>
  805. /// <param name="e"></param>
  806. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  807. {
  808. txtInstrumentColumn.Visible = false;
  809. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  810. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  811. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  812. cbxCNASColumn.Location = txtInstrumentColumn.Location;
  813. cbxCNASColumn.Visible = true;
  814. }
  815. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  816. {
  817. if (cbxCNASColumn.Visible)
  818. {
  819. txtInstrumentColumn.Visible = true;
  820. cbxCNASColumn.Visible = false;
  821. }
  822. }
  823. private void tsmServiceSetting_Click(object sender, EventArgs e)
  824. {
  825. frmServiceConfig frmServiceConfig = new frmServiceConfig();
  826. frmServiceConfig.ShowDialog();
  827. }
  828. private void tsmSystemSetting_Click(object sender, EventArgs e)
  829. {
  830. //frmSystemSetting frmSystem = new frmSystemSetting();
  831. //frmSystem.ShowDialog();
  832. //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
  833. //if (strTableInfoMode == "1")
  834. //{
  835. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  836. // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  837. //}
  838. //else
  839. //{
  840. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
  841. //}
  842. }
  843. private void tsmSourceSetting_Click(object sender, EventArgs e)
  844. {
  845. frmSourceSetting frmSource = new frmSourceSetting();
  846. frmSource.ShowDialog();
  847. }
  848. private void tsmHelper_Click(object sender, EventArgs e)
  849. {
  850. string strHelpFilePath = FileHelper.getBasePath() + @"\Helper.CHM";
  851. //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
  852. //Help.ShowHelpIndex(this, strHelpFilePath);
  853. System.Diagnostics.Process.Start(strHelpFilePath);
  854. }
  855. }
  856. }