RuleFormViewModel.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. namespace GDNXFD.Alert.Config.ViewModel
  2. {
  3. using GalaSoft.MvvmLight;
  4. using GalaSoft.MvvmLight.Command;
  5. using GalaSoft.MvvmLight.Messaging;
  6. using GDNXFD.Alert.Config.Model;
  7. using GDNXFD.Alert.Config.Resources.Strings;
  8. using GDNXFD.Alert.Config.Services.Navigation;
  9. using GDNXFD.Data;
  10. using GDNXFD.Alert.Interpreter;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Input;
  19. using Base;
  20. using System.ComponentModel;
  21. using Data.Repositories;
  22. using System.Windows.Controls;
  23. using Validations;
  24. using System.Text;
  25. /// <summary>
  26. /// MainWindow ViewModel
  27. /// </summary>
  28. public class RuleFormViewModel : VMBase
  29. {
  30. #region 初始化
  31. private readonly INavigationService navService;
  32. private FormMode fMode = FormMode.Create;
  33. private AlertRule oriAlertRule;
  34. public RuleFormViewModel(INavigationService navService)
  35. {
  36. this.navService = navService;
  37. SubscribeCommands();
  38. }
  39. public void InitializeData(AlertRule aRule, FormMode formMode)
  40. {
  41. Messenger.Default.Send(new LoadingMessage(true));
  42. try
  43. {
  44. IsIdEditable = true;
  45. IsFormEditable = true;
  46. isDirty = false;
  47. if (CategoryList == null)
  48. {
  49. var lstCategory = DataCache.Instance.GetDataDictByCategory("custom");
  50. CategoryList = new ObservableCollection<DictItem>(lstCategory);
  51. }
  52. if (LevelList == null)
  53. {
  54. var lstLevel = DataCache.Instance.GetDataDictByCategory("AlertLevel");
  55. LevelList = new ObservableCollection<DictItem>(lstLevel);
  56. }
  57. this.fMode = formMode;
  58. AlertRuleIDValidationRule.Enabled = fMode == FormMode.Create ? true : false;
  59. if (fMode == FormMode.Create)
  60. {
  61. FormTitle = StringProvider.GetString("NewRuleTitle");
  62. }
  63. else if (fMode == FormMode.Update)
  64. {
  65. FormTitle = StringProvider.GetString("EditRuleTitle");
  66. IsIdEditable = false;
  67. IsFormEditable = true;
  68. }
  69. else
  70. {
  71. FormTitle = StringProvider.GetString("ViewRuleTitle");
  72. IsFormEditable = false;
  73. IsIdEditable = false;
  74. }
  75. AlertRule = aRule;
  76. this.AlertRule.PropertyChanged += RuleFormViewModel_PropertyChanged;
  77. oriAlertRule = new AlertRule();
  78. oriAlertRule.Category = aRule.Category;
  79. oriAlertRule.Expression = aRule.Expression;
  80. oriAlertRule.Tag = aRule.Tag;
  81. if (SelectedItemsStation != null)
  82. SelectedItemsStation.Clear();
  83. if (SelectedItemsModel != null)
  84. SelectedItemsModel.Clear();
  85. if (SelectedItemsWindturbine != null)
  86. SelectedItemsWindturbine.Clear();
  87. if (SelectedItemsProject != null)
  88. SelectedItemsProject.Clear();
  89. if (SelectedItemsLine != null)
  90. SelectedItemsLine.Clear();
  91. InitStation();
  92. InitEquipmentModel();
  93. InitWindturbine();
  94. InitLine();
  95. InitProject();
  96. CategorySelectionChangedCommandExecute(null);
  97. }
  98. catch (Exception ex)
  99. {
  100. CustomDialogMessage message = new CustomDialogMessage(() => { }, StringResources.UnexpectedError + ex.Message + ex.StackTrace, Visibility.Collapsed);
  101. Messenger.Default.Send<CustomDialogMessage>(message);
  102. }
  103. finally
  104. {
  105. Messenger.Default.Send(new LoadingMessage(false));
  106. }
  107. }
  108. /// <summary>
  109. /// 初始化风场数据
  110. /// </summary>
  111. public void InitStation()
  112. {
  113. if (ItemsStation == null || ItemsStation.Count == 0)
  114. {
  115. ItemsStation = DataCache.Instance.DictStation;
  116. }
  117. if (SelectedItemsStation == null)
  118. {
  119. SelectedItemsStation = new Dictionary<string, object>();
  120. }
  121. if (SelectedItemsStation.Count == 0)
  122. {
  123. if (String.IsNullOrWhiteSpace(alertRule.Station))
  124. {
  125. foreach (var kv in ItemsStation)
  126. {
  127. SelectedItemsStation.Add(kv.Key, kv.Value);
  128. }
  129. }
  130. else
  131. {
  132. string[] arr = alertRule.Station.Split(',');
  133. foreach (var kv in DataCache.Instance.WindPowerStation)
  134. {
  135. if (arr.Contains(kv.Id))
  136. SelectedItemsStation.Add(kv.Name, kv);
  137. }
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// 初始化风机类型数据
  143. /// </summary>
  144. public void InitEquipmentModel()
  145. {
  146. Dictionary<string, object> dictModel = new Dictionary<string, object>();
  147. Dictionary<string, object> dictSelectedModel = new Dictionary<string, object>();
  148. if (SelectedItemsStation != null && SelectedItemsStation.Count > 0)
  149. {
  150. IList<string> lstModels = new List<string>();
  151. foreach(var kv in SelectedItemsStation)
  152. {
  153. var v = kv.Value as WindPowerStation;
  154. if (v != null)
  155. {
  156. string[] arr = v.Models.Split('/');
  157. foreach(string s in arr)
  158. {
  159. if (!string.IsNullOrWhiteSpace(s))
  160. {
  161. lstModels.Add(s.Trim());
  162. }
  163. }
  164. }
  165. }
  166. string[] arrModelId = null;
  167. if (fMode != FormMode.Create && !string.IsNullOrWhiteSpace(alertRule.ModelId))
  168. arrModelId = alertRule.ModelId.Split(',');
  169. foreach(var kv in DataCache.Instance.EquipmentModel)
  170. {
  171. if (lstModels.Contains(kv.Id))
  172. {
  173. dictModel.Add(kv.Id, kv);
  174. if (arrModelId != null)
  175. {
  176. if (arrModelId.Contains(kv.Id))
  177. dictSelectedModel.Add(kv.Id, kv);
  178. }
  179. else
  180. dictSelectedModel.Add(kv.Id, kv);
  181. }
  182. }
  183. }
  184. ItemsModel = dictModel;
  185. SelectedItemsModel = dictSelectedModel;
  186. }
  187. /// <summary>
  188. /// 初始化风机数据
  189. /// </summary>
  190. public void InitWindturbine()
  191. {
  192. Dictionary<string, object> dictWindturbine = new Dictionary<string, object>();
  193. Dictionary<string, object> dictSelectedWindturbine = new Dictionary<string, object>();
  194. string[] arrId = null;
  195. if (fMode != FormMode.Create && !string.IsNullOrWhiteSpace(alertRule.Windturbine))
  196. arrId = alertRule.Windturbine.Split(',');
  197. var lstStation = GetSelectedStationIds();
  198. var lstModel = GetSelectedModelIds();
  199. foreach (var kv in DataCache.Instance.WindTurbinies)
  200. {
  201. if (lstStation.Contains(kv.WindPowerStationId) &&
  202. lstModel.Contains(kv.ModelId))
  203. {
  204. dictWindturbine.Add(kv.Name, kv);
  205. if (arrId != null)
  206. {
  207. if (arrId.Contains(kv.Id))
  208. dictSelectedWindturbine.Add(kv.Name, kv);
  209. }
  210. else
  211. dictSelectedWindturbine.Add(kv.Name, kv);
  212. }
  213. }
  214. ItemsWindturbine = dictWindturbine;
  215. SelectedItemsWindturbine = dictSelectedWindturbine;
  216. }
  217. /// <summary>
  218. /// 初始化线路
  219. /// </summary>
  220. public void InitLine()
  221. {
  222. if (ItemsLine == null || ItemsLine.Count == 0)
  223. {
  224. ItemsLine = DataCache.Instance.DictLine;
  225. }
  226. if (SelectedItemsLine == null)
  227. {
  228. SelectedItemsLine = new Dictionary<string, object>();
  229. }
  230. if (SelectedItemsLine.Count == 0)
  231. {
  232. if (String.IsNullOrWhiteSpace(alertRule.Line))
  233. {
  234. foreach (var kv in ItemsLine)
  235. {
  236. SelectedItemsLine.Add(kv.Key, kv.Value);
  237. }
  238. }
  239. else
  240. {
  241. string[] arr = alertRule.Line.Split(',');
  242. foreach (var kv in DataCache.Instance.Lines)
  243. {
  244. if (arr.Contains(kv.Id))
  245. SelectedItemsLine.Add(kv.Name, kv);
  246. }
  247. }
  248. }
  249. }
  250. /// <summary>
  251. /// 初始化工程
  252. /// </summary>
  253. public void InitProject()
  254. {
  255. if (ItemsProject == null || ItemsProject.Count == 0)
  256. {
  257. ItemsProject = DataCache.Instance.DictProject;
  258. }
  259. if (SelectedItemsProject == null)
  260. {
  261. SelectedItemsProject = new Dictionary<string, object>();
  262. }
  263. if (SelectedItemsProject.Count == 0)
  264. {
  265. if (String.IsNullOrWhiteSpace(alertRule.Project))
  266. {
  267. foreach (var kv in ItemsProject)
  268. {
  269. SelectedItemsProject.Add(kv.Key, kv.Value);
  270. }
  271. }
  272. else
  273. {
  274. string[] arr = alertRule.Project.Split(',');
  275. foreach (var kv in DataCache.Instance.Projects)
  276. {
  277. if (arr.Contains(kv.Id))
  278. SelectedItemsProject.Add(kv.Name, kv);
  279. }
  280. }
  281. }
  282. }
  283. private bool isDirty;
  284. void RuleFormViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  285. {
  286. isDirty = true;
  287. }
  288. private void SubscribeCommands()
  289. {
  290. saveCommand = new RelayCommand(SaveCommandExecute);
  291. cancelCommand = new RelayCommand(CancelCommandExecute);
  292. checkExpressionCommand = new RelayCommand(CheckExpressionCommandExecute);
  293. showExpressionEditorCommand = new RelayCommand(ShowExpressionEditorCommandExecute);
  294. //equipmentSelectionChangedCommand = new RelayCommand<SelectionChangedEventArgs>(EquipmentSelectionChangedCommandExecute);
  295. categorySelectionChangedCommand = new RelayCommand<SelectionChangedEventArgs>(CategorySelectionChangedCommandExecute);
  296. stationSelectionChangedCommand = new RelayCommand<EventArgs>(StationSelectionChangedCommandExecute);
  297. modelSelectionChangedCommand = new RelayCommand<EventArgs>(ModelSelectionChangedCommandExecute);
  298. }
  299. private string formTitle;
  300. public string FormTitle
  301. {
  302. get { return formTitle; }
  303. set
  304. {
  305. formTitle = value;
  306. RaisePropertyChanged(() => FormTitle);
  307. }
  308. }
  309. private bool isIdEditable;
  310. public bool IsIdEditable
  311. {
  312. get { return isIdEditable; }
  313. set
  314. {
  315. isIdEditable = value;
  316. RaisePropertyChanged(() => IsIdEditable);
  317. }
  318. }
  319. private bool isFormEditable;
  320. public bool IsFormEditable
  321. {
  322. get { return isFormEditable; }
  323. set
  324. {
  325. isFormEditable = value;
  326. RaisePropertyChanged(() => IsFormEditable);
  327. }
  328. }
  329. #endregion
  330. #region 规则实体
  331. private AlertRule alertRule;
  332. public AlertRule AlertRule
  333. {
  334. get { return this.alertRule; }
  335. set
  336. {
  337. this.alertRule = value;
  338. RaisePropertyChanged(() => AlertRule);
  339. RaisePropertyChanged(() => SelectedAlertLevel);
  340. RaisePropertyChanged(() => SelectedAlertCategory);
  341. RaisePropertyChanged(() => AlertRuleDisabled);
  342. }
  343. }
  344. private bool alertRuleDisabled;
  345. public bool AlertRuleDisabled
  346. {
  347. get
  348. {
  349. if (alertRule != null)
  350. return !alertRule.Enabled;
  351. return true;
  352. }
  353. set
  354. {
  355. alertRuleDisabled = value;
  356. AlertRule.Enabled = !value;
  357. }
  358. }
  359. private bool alertRuleEnabled;
  360. public bool AlertRuleEnabled
  361. {
  362. get
  363. {
  364. if (alertRule != null)
  365. return alertRule.Enabled;
  366. return true;
  367. }
  368. set
  369. {
  370. alertRuleEnabled = value;
  371. if (alertRule != null)
  372. alertRule.Enabled = value;
  373. }
  374. }
  375. public DictItem SelectedAlertLevel
  376. {
  377. get
  378. {
  379. if (this.alertRule != null && levelList != null)
  380. return levelList.Where(a => a.Code == this.alertRule.Rank).FirstOrDefault();
  381. else
  382. return null;
  383. }
  384. set
  385. {
  386. if (this.alertRule != null && value != null)
  387. {
  388. this.alertRule.Rank = value.Code;
  389. RaisePropertyChanged(() => AlertRule);
  390. RaisePropertyChanged(() => SelectedAlertLevel);
  391. }
  392. }
  393. }
  394. private ObservableCollection<DictItem> levelList;
  395. public ObservableCollection<DictItem> LevelList
  396. {
  397. get { return levelList; }
  398. set
  399. {
  400. levelList = value;
  401. RaisePropertyChanged(() => LevelList);
  402. }
  403. }
  404. public DictItem SelectedAlertCategory
  405. {
  406. get
  407. {
  408. if (this.alertRule != null && categoryList != null)
  409. return categoryList.Where(a => a.Code == this.alertRule.Category).FirstOrDefault();
  410. else
  411. return null;
  412. }
  413. set
  414. {
  415. if (this.alertRule != null && value != null)
  416. {
  417. this.alertRule.Category = value.Code;
  418. RaisePropertyChanged(() => AlertRule);
  419. RaisePropertyChanged(() => SelectedAlertCategory);
  420. }
  421. }
  422. }
  423. private ObservableCollection<DictItem> categoryList;
  424. public ObservableCollection<DictItem> CategoryList
  425. {
  426. get { return categoryList; }
  427. set
  428. {
  429. categoryList = value;
  430. RaisePropertyChanged(() => CategoryList);
  431. }
  432. }
  433. #endregion
  434. #region 保存或取消
  435. private RelayCommand saveCommand;
  436. private RelayCommand cancelCommand;
  437. public ICommand SaveCommand
  438. {
  439. get { return saveCommand; }
  440. }
  441. public ICommand CancelCommand
  442. {
  443. get { return cancelCommand; }
  444. }
  445. private bool ValidateForm()
  446. {
  447. if (String.IsNullOrWhiteSpace(alertRule.Id) ||
  448. String.IsNullOrWhiteSpace(alertRule.Name) ||
  449. String.IsNullOrWhiteSpace(alertRule.Expression) )
  450. return false;
  451. char[] arr = alertRule.Id.ToCharArray();
  452. if (arr.Length < 5 || !IsLetter(arr[0]))
  453. {
  454. MessageBox.Show(StringProvider.GetString("CodeError"));
  455. return false;
  456. }
  457. if (SelectedAlertLevel == null)
  458. {
  459. MessageBox.Show(StringProvider.GetString("AlertLevelNotEmpty"));
  460. return false;
  461. }
  462. //else
  463. // alertRule.Rank = SelectedAlertLevel.Code;
  464. if (SelectedAlertCategory == null)
  465. {
  466. MessageBox.Show(StringProvider.GetString("AlertCategoryNotEmpty"));
  467. return false;
  468. }
  469. //else
  470. // alertRule.Category = SelectedAlertCategory.Code;
  471. switch (SelectedAlertCategory.Code)
  472. {
  473. case "1": //风机
  474. if (SelectedItemsStation == null || SelectedItemsStation.Count == 0)
  475. {
  476. MessageBox.Show(StringProvider.GetString("StationNotEmpty"));
  477. return false;
  478. }
  479. else if (SelectedItemsModel == null || SelectedItemsModel.Count == 0)
  480. {
  481. MessageBox.Show(StringProvider.GetString("ModelNotEmpty"));
  482. return false;
  483. }
  484. else if (SelectedItemsWindturbine == null || SelectedItemsWindturbine.Count == 0 )
  485. {
  486. MessageBox.Show(StringProvider.GetString("WindturbineNotEmpty"));
  487. return false;
  488. }
  489. else
  490. {
  491. alertRule.Station = "";
  492. alertRule.ModelId = "";
  493. alertRule.Windturbine = "";
  494. alertRule.Project = "";
  495. alertRule.Line = "";
  496. alertRule.Range = "";
  497. foreach (var v in SelectedItemsStation.Values)
  498. {
  499. WindPowerStation w = v as WindPowerStation;
  500. if (w != null)
  501. {
  502. alertRule.Station += w.Id;
  503. alertRule.Station += ",";
  504. }
  505. }
  506. foreach (var v in SelectedItemsModel.Values)
  507. {
  508. EquipmentModel w = v as EquipmentModel;
  509. if (w != null)
  510. {
  511. alertRule.ModelId += w.Id;
  512. alertRule.ModelId += ",";
  513. }
  514. }
  515. foreach (var v in SelectedItemsWindturbine.Values)
  516. {
  517. WindTurbine w = v as WindTurbine;
  518. if (w!= null)
  519. {
  520. alertRule.Windturbine += w.Id;
  521. alertRule.Windturbine += ",";
  522. alertRule.Range += w.Name;
  523. alertRule.Range += ",";
  524. }
  525. }
  526. }
  527. break;
  528. case "2": //风场
  529. if (SelectedItemsStation == null || SelectedItemsStation.Count == 0)
  530. {
  531. MessageBox.Show(StringProvider.GetString("StationNotEmpty"));
  532. return false;
  533. }
  534. else
  535. {
  536. alertRule.Station = "";
  537. alertRule.ModelId = "";
  538. alertRule.Windturbine = "";
  539. alertRule.Project = "";
  540. alertRule.Line = "";
  541. alertRule.Range = "";
  542. foreach (var v in SelectedItemsStation.Values)
  543. {
  544. WindPowerStation w = v as WindPowerStation;
  545. if (w != null)
  546. {
  547. alertRule.Station += w.Id;
  548. alertRule.Station += ",";
  549. alertRule.Range += w.Name;
  550. alertRule.Range += ",";
  551. }
  552. }
  553. }
  554. break;
  555. case "3": //工程
  556. if (SelectedItemsProject == null || SelectedItemsProject.Count == 0)
  557. {
  558. MessageBox.Show(StringProvider.GetString("ProjectNotEmpty"));
  559. return false;
  560. }
  561. else
  562. {
  563. alertRule.Station = "";
  564. alertRule.ModelId = "";
  565. alertRule.Windturbine = "";
  566. alertRule.Project = "";
  567. alertRule.Line = "";
  568. alertRule.Range = "";
  569. foreach (var v in SelectedItemsProject.Values)
  570. {
  571. Project w = v as Project;
  572. if (w != null)
  573. {
  574. alertRule.Project += w.Id;
  575. alertRule.Project += ",";
  576. alertRule.Range += w.Name;
  577. alertRule.Range += ",";
  578. }
  579. }
  580. }
  581. break;
  582. case "4": //线路
  583. if (SelectedItemsLine == null || SelectedItemsLine.Count == 0)
  584. {
  585. MessageBox.Show(StringProvider.GetString("LineNotEmpty"));
  586. return false;
  587. }
  588. else
  589. {
  590. alertRule.Station = "";
  591. alertRule.ModelId = "";
  592. alertRule.Windturbine = "";
  593. alertRule.Project = "";
  594. alertRule.Line = "";
  595. alertRule.Range = "";
  596. foreach (var v in SelectedItemsLine.Values)
  597. {
  598. Line w = v as Line;
  599. if (w != null)
  600. {
  601. alertRule.Line += w.Id;
  602. alertRule.Line += ",";
  603. alertRule.Range += w.Name;
  604. alertRule.Range += ",";
  605. }
  606. }
  607. }
  608. break;
  609. case "5": //电气
  610. if (SelectedItemsStation == null || SelectedItemsStation.Count == 0)
  611. {
  612. MessageBox.Show(StringProvider.GetString("StationNotEmpty"));
  613. return false;
  614. }
  615. else
  616. {
  617. alertRule.Station = "";
  618. alertRule.ModelId = "";
  619. alertRule.Windturbine = "";
  620. alertRule.Project = "";
  621. alertRule.Line = "";
  622. alertRule.Range = "";
  623. foreach (var v in SelectedItemsStation.Values)
  624. {
  625. WindPowerStation w = v as WindPowerStation;
  626. if (w != null)
  627. {
  628. alertRule.Station += w.Id;
  629. alertRule.Station += ",";
  630. alertRule.Range += w.Name;
  631. alertRule.Range += ",";
  632. }
  633. }
  634. }
  635. break;
  636. }
  637. return true;
  638. }
  639. private void SaveCommandExecute()
  640. {
  641. try
  642. {
  643. if (!ValidateForm())
  644. return;
  645. ExpressionCompilerResults results = ExpressionCompiler.CompileExpression(alertRule);
  646. if (results.HasErrors)
  647. {
  648. MessageBox.Show(StringProvider.GetString("ExpressionError") + results.ErrorText);
  649. return;
  650. }
  651. Messenger.Default.Send(new LoadingMessage(true));
  652. if (alertRule.Range.Length > 1500)
  653. alertRule.Range = alertRule.Range.Substring(0, 1497);
  654. AlertRuleRepository.SaveAlertRule(alertRule);
  655. navService.NavigateToRuleList();
  656. }
  657. catch (Exception ex)
  658. {
  659. Messenger.Default.Send(new LoadingMessage(false));
  660. CustomDialogMessage message = new CustomDialogMessage(() => { }, StringResources.UnexpectedError, Visibility.Collapsed);
  661. Messenger.Default.Send<CustomDialogMessage>(message);
  662. }
  663. }
  664. private void CancelCommandExecute()
  665. {
  666. if (isDirty && fMode != FormMode.ReadOnly)
  667. {
  668. CustomDialogMessage message = new CustomDialogMessage(() =>
  669. {
  670. navService.NavigateToRuleList();
  671. }, String.Format(Resources.Strings.StringResources.ConfirmCancelMessage), Visibility.Visible);
  672. Messenger.Default.Send<CustomDialogMessage>(message);
  673. }
  674. else
  675. navService.NavigateToRuleList();
  676. }
  677. #endregion
  678. #region 表达式设计器
  679. #region AI测点
  680. private IList<TestingPointBase> allAI;
  681. private ObservableCollection<TestingPointBase> aiList;
  682. public ObservableCollection<TestingPointBase> AiList
  683. {
  684. get
  685. {
  686. if (aiList == null && this.alertRule != null)// && equipmentList != null)
  687. {
  688. var lst = WindTurbineRepository.GetTestPointAIsByModelId(alertRule.ModelId);
  689. allAI = Convert2TestingPointBase(lst);
  690. aiList = new ObservableCollection<TestingPointBase>(allAI);
  691. }
  692. return aiList;
  693. }
  694. set
  695. {
  696. aiList = value;
  697. RaisePropertyChanged(() => AiList);
  698. }
  699. }
  700. private string aiFilter;
  701. public string AIFilter
  702. {
  703. get { return this.aiFilter; }
  704. set
  705. {
  706. this.aiFilter = value;
  707. SearchAIs();
  708. RaisePropertyChanged(() => AIFilter);
  709. }
  710. }
  711. private void SearchAIs()
  712. {
  713. if (allAI == null) return;
  714. var aaa = allAI.Where(s => (s.UniformCode.Contains(aiFilter) || s.Name.Contains(aiFilter))).ToList();
  715. AiList = new ObservableCollection<TestingPointBase>(allAI.Where(s => (s.UniformCode.Contains(aiFilter) || s.Name.Contains(aiFilter))).ToList());
  716. }
  717. #endregion
  718. #region DI测点
  719. private IList<TestingPointBase> allDI;
  720. private ObservableCollection<TestingPointBase> diList;
  721. public ObservableCollection<TestingPointBase> DiList
  722. {
  723. get
  724. {
  725. if (diList == null && this.alertRule != null)// && equipmentList != null)
  726. {
  727. var lst = WindTurbineRepository.GetTestPointDIsByModelId(alertRule.ModelId);
  728. allDI = Convert2TestingPointBase(lst);
  729. diList = new ObservableCollection<TestingPointBase>(allDI);
  730. }
  731. return diList;
  732. }
  733. set
  734. {
  735. diList = value;
  736. RaisePropertyChanged(() => DiList);
  737. }
  738. }
  739. private string diFilter;
  740. public string DIFilter
  741. {
  742. get { return this.diFilter; }
  743. set
  744. {
  745. this.diFilter = value;
  746. SearchDIs();
  747. RaisePropertyChanged(() => DIFilter);
  748. }
  749. }
  750. private void SearchDIs()
  751. {
  752. if (allDI == null) return;
  753. DiList = new ObservableCollection<TestingPointBase>(allDI.Where(s => (s.UniformCode.Contains(diFilter) || s.Name.Contains(diFilter))).ToList());
  754. }
  755. #endregion
  756. #region 函数
  757. private ObservableCollection<string> methodList;
  758. public ObservableCollection<string> MethodList
  759. {
  760. get
  761. {
  762. if (methodList == null)
  763. methodList = AllMethodes;
  764. return methodList;
  765. }
  766. set
  767. {
  768. methodList = value;
  769. RaisePropertyChanged(() => MethodList);
  770. }
  771. }
  772. private ObservableCollection<string> allMethodes;
  773. private ObservableCollection<string> AllMethodes
  774. {
  775. get
  776. {
  777. if (allMethodes == null)
  778. allMethodes = LoadMethodList();
  779. return allMethodes;
  780. }
  781. }
  782. private ObservableCollection<string> LoadMethodList()
  783. {
  784. ObservableCollection<string> lst = new ObservableCollection<string>();
  785. lst.Add("RiseExceed");
  786. lst.Add("MR");
  787. lst.Add("MAR");
  788. lst.Add("Sustain");
  789. lst.Add("LastUpdateTime");
  790. lst.Add("abs");
  791. lst.Add("acos");
  792. lst.Add("asin");
  793. lst.Add("atan");
  794. lst.Add("atan2");
  795. lst.Add("ceiling");
  796. lst.Add("cos");
  797. lst.Add("cosh");
  798. lst.Add("exp");
  799. lst.Add("floor");
  800. lst.Add("log");
  801. lst.Add("log10");
  802. lst.Add("max");
  803. lst.Add("min");
  804. lst.Add("pow");
  805. lst.Add("round");
  806. lst.Add("sign");
  807. lst.Add("sin");
  808. lst.Add("sinh");
  809. lst.Add("sqrt");
  810. lst.Add("tan");
  811. lst.Add("tanh");
  812. lst.Add("E");
  813. lst.Add("PI");
  814. return lst;
  815. }
  816. private string methodFilter;
  817. public string MethodFilter
  818. {
  819. get { return this.methodFilter; }
  820. set
  821. {
  822. this.methodFilter = value;
  823. SearchMethodes();
  824. RaisePropertyChanged(() => MethodFilter);
  825. }
  826. }
  827. private void SearchMethodes()
  828. {
  829. if (AllMethodes == null) return;
  830. MethodList = new ObservableCollection<string>(AllMethodes.Where(s => s.Contains(methodFilter)).ToList());
  831. }
  832. #endregion
  833. #region 验证和显示控制
  834. private bool showExpressionEditor = true;
  835. public bool ShowExpressionEditor
  836. {
  837. get
  838. {
  839. return showExpressionEditor;
  840. }
  841. set
  842. {
  843. showExpressionEditor = value;
  844. RaisePropertyChanged(() => ShowExpressionEditor);
  845. }
  846. }
  847. private RelayCommand showExpressionEditorCommand;
  848. public ICommand ShowExpressionEditorCommand
  849. {
  850. get { return showExpressionEditorCommand; }
  851. }
  852. private void ShowExpressionEditorCommandExecute()
  853. {
  854. ShowExpressionEditor = !showExpressionEditor;
  855. }
  856. private RelayCommand checkExpressionCommand;
  857. public ICommand CheckExpressionCommand
  858. {
  859. get { return checkExpressionCommand; }
  860. }
  861. private void CheckExpressionCommandExecute()
  862. {
  863. if (string.IsNullOrWhiteSpace(alertRule.Expression))
  864. return;
  865. try
  866. {
  867. ExpressionCompilerResults results = ExpressionCompiler.CompileExpression(alertRule);
  868. if (results.HasErrors)
  869. MessageBox.Show(results.ErrorText);
  870. else
  871. MessageBox.Show(StringProvider.GetString("CheckSuccess"));
  872. }
  873. catch (ExpressionException eex)
  874. {
  875. MessageBox.Show(eex.Message);
  876. }
  877. catch (Exception ex)
  878. {
  879. MessageBox.Show(ex.Message);
  880. }
  881. }
  882. #endregion
  883. #endregion
  884. #region 范围选择
  885. #region 风电场
  886. private Dictionary<string, object> _itemsStation;
  887. private Dictionary<string, object> _selectedItemsStation;
  888. public Dictionary<string, object> ItemsStation
  889. {
  890. get
  891. {
  892. return _itemsStation;
  893. }
  894. set
  895. {
  896. _itemsStation = value;
  897. RaisePropertyChanged(() => ItemsStation);
  898. }
  899. }
  900. public Dictionary<string, object> SelectedItemsStation
  901. {
  902. get
  903. {
  904. return _selectedItemsStation;
  905. }
  906. set
  907. {
  908. _selectedItemsStation = value;
  909. RaisePropertyChanged(() => SelectedItemsStation);
  910. }
  911. }
  912. private RelayCommand<EventArgs> stationSelectionChangedCommand;
  913. public ICommand StationSelectionChangedCommand
  914. {
  915. get { return stationSelectionChangedCommand; }
  916. }
  917. private void StationSelectionChangedCommandExecute(EventArgs e)
  918. {
  919. if (SelectedAlertCategory.Code == "5")
  920. {
  921. UpdateElectricalTestingPoint();
  922. }
  923. else if (SelectedAlertCategory.Code=="1")
  924. {
  925. //更新设备类型列表
  926. InitEquipmentModel();
  927. //更新风机列表
  928. InitWindturbine();
  929. //更新AI、DI测点
  930. UpdateWindturbineTestingPoint();
  931. }
  932. }
  933. private IList<string> GetSelectedStationIds()
  934. {
  935. IList<string> selectedStationIds = new List<string>();
  936. if (SelectedItemsStation != null && SelectedItemsStation.Count > 0)
  937. {
  938. foreach (var v in SelectedItemsStation.Values)
  939. {
  940. WindPowerStation w = v as WindPowerStation;
  941. if (w != null)
  942. selectedStationIds.Add(w.Id);
  943. }
  944. }
  945. return selectedStationIds;
  946. }
  947. #endregion
  948. #region 风机类型
  949. private Dictionary<string, object> _itemsModel;
  950. private Dictionary<string, object> _selectedItemsModel;
  951. public Dictionary<string, object> ItemsModel
  952. {
  953. get
  954. {
  955. return _itemsModel;
  956. }
  957. set
  958. {
  959. _itemsModel = value;
  960. RaisePropertyChanged(() => ItemsModel);
  961. }
  962. }
  963. public Dictionary<string, object> SelectedItemsModel
  964. {
  965. get
  966. {
  967. return _selectedItemsModel;
  968. }
  969. set
  970. {
  971. _selectedItemsModel = value;
  972. RaisePropertyChanged(() => SelectedItemsModel);
  973. }
  974. }
  975. private IList<string> GetSelectedModelIds()
  976. {
  977. IList<string> selectedModelIds = new List<string>();
  978. if (SelectedItemsModel != null && SelectedItemsModel.Count > 0)
  979. {
  980. foreach (var v in SelectedItemsModel.Values)
  981. {
  982. EquipmentModel w = v as EquipmentModel;
  983. if (w != null)
  984. selectedModelIds.Add(w.Id);
  985. }
  986. }
  987. return selectedModelIds;
  988. }
  989. private RelayCommand<EventArgs> modelSelectionChangedCommand;
  990. public ICommand ModelSelectionChangedCommand
  991. {
  992. get { return modelSelectionChangedCommand; }
  993. }
  994. private void ModelSelectionChangedCommandExecute(EventArgs e)
  995. {
  996. //更新风机列表
  997. InitWindturbine();
  998. //更新AI、DI测点
  999. UpdateWindturbineTestingPoint();
  1000. }
  1001. #endregion
  1002. #region 风机
  1003. private Dictionary<string, object> _itemsWindturbine;
  1004. private Dictionary<string, object> _selectedItemsWindturbine;
  1005. public Dictionary<string, object> ItemsWindturbine
  1006. {
  1007. get
  1008. {
  1009. return _itemsWindturbine;
  1010. }
  1011. set
  1012. {
  1013. _itemsWindturbine = value;
  1014. RaisePropertyChanged(() => ItemsWindturbine);
  1015. }
  1016. }
  1017. public Dictionary<string, object> SelectedItemsWindturbine
  1018. {
  1019. get
  1020. {
  1021. return _selectedItemsWindturbine;
  1022. }
  1023. set
  1024. {
  1025. _selectedItemsWindturbine = value;
  1026. RaisePropertyChanged(() => SelectedItemsWindturbine);
  1027. }
  1028. }
  1029. #endregion
  1030. #region 线路
  1031. private Dictionary<string, object> _itemsLine;
  1032. private Dictionary<string, object> _selectedItemsLine;
  1033. public Dictionary<string, object> ItemsLine
  1034. {
  1035. get
  1036. {
  1037. return _itemsLine;
  1038. }
  1039. set
  1040. {
  1041. _itemsLine = value;
  1042. RaisePropertyChanged(() => ItemsLine);
  1043. }
  1044. }
  1045. public Dictionary<string, object> SelectedItemsLine
  1046. {
  1047. get
  1048. {
  1049. return _selectedItemsLine;
  1050. }
  1051. set
  1052. {
  1053. _selectedItemsLine = value;
  1054. RaisePropertyChanged(() => SelectedItemsLine);
  1055. }
  1056. }
  1057. #endregion
  1058. #region 工程
  1059. private Dictionary<string, object> _itemsProject;
  1060. private Dictionary<string, object> _selectedItemsProject;
  1061. public Dictionary<string, object> ItemsProject
  1062. {
  1063. get
  1064. {
  1065. return _itemsProject;
  1066. }
  1067. set
  1068. {
  1069. _itemsProject = value;
  1070. RaisePropertyChanged(() => ItemsProject);
  1071. }
  1072. }
  1073. public Dictionary<string, object> SelectedItemsProject
  1074. {
  1075. get
  1076. {
  1077. return _selectedItemsProject;
  1078. }
  1079. set
  1080. {
  1081. _selectedItemsProject = value;
  1082. RaisePropertyChanged(() => SelectedItemsProject);
  1083. }
  1084. }
  1085. #endregion
  1086. #region 根据报警类型显示范围选择项
  1087. private bool showStation = true;
  1088. public bool ShowStation
  1089. {
  1090. get
  1091. {
  1092. return showStation;
  1093. }
  1094. set
  1095. {
  1096. showStation = value;
  1097. RaisePropertyChanged(() => ShowStation);
  1098. }
  1099. }
  1100. private bool showModel = true;
  1101. public bool ShowModel
  1102. {
  1103. get
  1104. {
  1105. return showModel;
  1106. }
  1107. set
  1108. {
  1109. showModel = value;
  1110. RaisePropertyChanged(() => ShowModel);
  1111. }
  1112. }
  1113. private bool showWindturbine = true;
  1114. public bool ShowWindturbine
  1115. {
  1116. get
  1117. {
  1118. return showWindturbine;
  1119. }
  1120. set
  1121. {
  1122. showWindturbine = value;
  1123. RaisePropertyChanged(() => ShowWindturbine);
  1124. }
  1125. }
  1126. private bool showProject = false;
  1127. public bool ShowProject
  1128. {
  1129. get
  1130. {
  1131. return showProject;
  1132. }
  1133. set
  1134. {
  1135. showProject = value;
  1136. RaisePropertyChanged(() => ShowProject);
  1137. }
  1138. }
  1139. private bool showLine = false;
  1140. public bool ShowLine
  1141. {
  1142. get
  1143. {
  1144. return showLine;
  1145. }
  1146. set
  1147. {
  1148. showLine = value;
  1149. RaisePropertyChanged(() => ShowLine);
  1150. }
  1151. }
  1152. #endregion
  1153. #region 报警类型变动
  1154. private RelayCommand<SelectionChangedEventArgs> categorySelectionChangedCommand;
  1155. public ICommand CategorySelectionChangedCommand
  1156. {
  1157. get { return categorySelectionChangedCommand; }
  1158. }
  1159. private void CategorySelectionChangedCommandExecute(SelectionChangedEventArgs e)
  1160. {
  1161. if (SelectedAlertCategory == null) return;
  1162. switch (SelectedAlertCategory.Code)
  1163. {
  1164. case "1": //风机
  1165. ShowWindturbine = true;
  1166. ShowStation = true;
  1167. ShowModel = true;
  1168. ShowProject = false;
  1169. ShowLine = false;
  1170. UpdateWindturbineTestingPoint();
  1171. break;
  1172. case "2": //风场
  1173. ShowStation = true;
  1174. ShowWindturbine = false;
  1175. ShowModel = false;
  1176. ShowProject = false;
  1177. ShowLine = false;
  1178. UpdateLevelTypeTestingPoint("windpowerstation");
  1179. break;
  1180. case "3": //工程
  1181. ShowProject = true;
  1182. ShowStation = false;
  1183. ShowWindturbine = false;
  1184. ShowModel = false;
  1185. ShowLine = false;
  1186. UpdateLevelTypeTestingPoint("project");
  1187. break;
  1188. case "4": //线路
  1189. ShowStation = false;
  1190. ShowWindturbine = false;
  1191. ShowModel = false;
  1192. ShowProject = false;
  1193. ShowLine = true;
  1194. UpdateLevelTypeTestingPoint("line");
  1195. break;
  1196. case "5": //电气
  1197. ShowStation = true;
  1198. ShowWindturbine = false;
  1199. ShowModel = false;
  1200. ShowProject = false;
  1201. ShowLine = false;
  1202. UpdateElectricalTestingPoint();
  1203. break;
  1204. }
  1205. }
  1206. #endregion
  1207. #endregion
  1208. #region 测点列表更新
  1209. /// <summary>
  1210. /// 更新风机测点
  1211. /// </summary>
  1212. private void UpdateWindturbineTestingPoint()
  1213. {
  1214. AIFilter = "";
  1215. DIFilter = "";
  1216. if(oriAlertRule != null && AlertRule.Category == oriAlertRule.Category)
  1217. {
  1218. AlertRule.Expression = oriAlertRule.Expression;
  1219. AlertRule.Tag = oriAlertRule.Tag;
  1220. }
  1221. else
  1222. {
  1223. AlertRule.Expression = "";
  1224. AlertRule.Tag = "";
  1225. }
  1226. var lstModels = GetSelectedModelIds();
  1227. if (lstModels == null || lstModels.Count == 0)
  1228. {
  1229. allAI = null;
  1230. allDI = null;
  1231. AiList = null;
  1232. DiList = null;
  1233. }
  1234. else
  1235. {
  1236. var lstA = WindTurbineRepository.GetTestPointAIsByModels(lstModels);
  1237. var lstD = WindTurbineRepository.GetTestPointDIsByModels(lstModels);
  1238. allAI = Convert2TestingPointBase(lstA);
  1239. allDI = Convert2TestingPointBase(lstD);
  1240. AiList = new ObservableCollection<TestingPointBase>(allAI);
  1241. DiList = new ObservableCollection<TestingPointBase>(allDI);
  1242. }
  1243. }
  1244. /// <summary>
  1245. /// 更新电气测点
  1246. /// </summary>
  1247. private void UpdateElectricalTestingPoint()
  1248. {
  1249. AIFilter = "";
  1250. DIFilter = "";
  1251. if (oriAlertRule != null && AlertRule.Category == oriAlertRule.Category)
  1252. {
  1253. AlertRule.Expression = oriAlertRule.Expression;
  1254. AlertRule.Tag = oriAlertRule.Tag;
  1255. }
  1256. else
  1257. {
  1258. AlertRule.Expression = "";
  1259. AlertRule.Tag = "";
  1260. }
  1261. var lstStations = GetSelectedStationIds();
  1262. if (lstStations == null || lstStations.Count == 0)
  1263. {
  1264. allAI = null;
  1265. allDI = null;
  1266. AiList = null;
  1267. DiList = null;
  1268. }
  1269. else
  1270. {
  1271. var lstA = WindPowerStationRepository.GetElecTestPointAIsByStations(lstStations);
  1272. var lstD = WindPowerStationRepository.GetElecTestPointDIsByStations(lstStations);
  1273. allAI = Convert2TestingPointBase(lstA);
  1274. allDI = Convert2TestingPointBase(lstD);
  1275. AiList = new ObservableCollection<TestingPointBase>(allAI);
  1276. DiList = new ObservableCollection<TestingPointBase>(allDI);
  1277. }
  1278. }
  1279. /// <summary>
  1280. /// 更新风场、工程、线路 测点
  1281. /// </summary>
  1282. private void UpdateLevelTypeTestingPoint(string levelType)
  1283. {
  1284. AIFilter = "";
  1285. DIFilter = "";
  1286. if (oriAlertRule != null && AlertRule.Category == oriAlertRule.Category)
  1287. {
  1288. AlertRule.Expression = oriAlertRule.Expression;
  1289. AlertRule.Tag = oriAlertRule.Tag;
  1290. }
  1291. else
  1292. {
  1293. AlertRule.Expression = "";
  1294. AlertRule.Tag = "";
  1295. }
  1296. IList<LevelTypeTestingPoint> lstAI = DataCache.Instance.GetTestingPointByLevelType(levelType);
  1297. allAI = Convert2TestingPointBase(lstAI);
  1298. AiList = new ObservableCollection<TestingPointBase>(lstAI);
  1299. DiList = null;
  1300. }
  1301. private IList<TestingPointBase> Convert2TestingPointBase(IList<TestingPointAI> lst)
  1302. {
  1303. IList<TestingPointBase> ret = new List<TestingPointBase>();
  1304. if (lst != null)
  1305. {
  1306. foreach (var t in lst)
  1307. {
  1308. ret.Add((TestingPointBase)t);
  1309. }
  1310. }
  1311. return ret;
  1312. }
  1313. private IList<TestingPointBase> Convert2TestingPointBase(IList<TestingPointDI> lst)
  1314. {
  1315. IList<TestingPointBase> ret = new List<TestingPointBase>();
  1316. if (lst != null)
  1317. {
  1318. foreach (var t in lst)
  1319. {
  1320. ret.Add((TestingPointBase)t);
  1321. }
  1322. }
  1323. return ret;
  1324. }
  1325. private IList<TestingPointBase> Convert2TestingPointBase(IList<LevelTypeTestingPoint> lst)
  1326. {
  1327. IList<TestingPointBase> ret = new List<TestingPointBase>();
  1328. if (lst != null)
  1329. {
  1330. foreach (var t in lst)
  1331. {
  1332. ret.Add((TestingPointBase)t);
  1333. }
  1334. }
  1335. return ret;
  1336. }
  1337. private IList<TestingPointBase> Convert2TestingPointBase(IList<ElectricalTestingPointAI> lst)
  1338. {
  1339. IList<TestingPointBase> ret = new List<TestingPointBase>();
  1340. if (lst != null)
  1341. {
  1342. foreach (var t in lst)
  1343. {
  1344. ret.Add((TestingPointBase)t);
  1345. }
  1346. }
  1347. return ret;
  1348. }
  1349. private IList<TestingPointBase> Convert2TestingPointBase(IList<ElectricalTestingPointDI> lst)
  1350. {
  1351. IList<TestingPointBase> ret = new List<TestingPointBase>();
  1352. if (lst != null)
  1353. {
  1354. foreach (var t in lst)
  1355. {
  1356. ret.Add((TestingPointBase)t);
  1357. }
  1358. }
  1359. return ret;
  1360. }
  1361. private bool IsLetter(char c)
  1362. {
  1363. if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
  1364. return true;
  1365. return false;
  1366. }
  1367. #endregion
  1368. }
  1369. }