EditSchViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. using AutoMapper;
  2. using BizService;
  3. using BlankApp1.Events;
  4. using Microsoft.Extensions.Logging;
  5. using Model.Dto;
  6. using Model.Entities;
  7. using Newtonsoft.Json;
  8. using PLCTool.Common;
  9. using PLCTool.Events;
  10. using PLCTool.Models;
  11. using Prism.Commands;
  12. using Prism.Events;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using Prism.Services.Dialogs;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Collections.ObjectModel;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. namespace PLCTool.ViewModels.BasicConfigViewModel
  24. {
  25. public class EditSchViewModel : BindableBase, INavigationAware
  26. {
  27. private readonly IDialogService _dialog;
  28. private readonly IEventAggregator _aggregator;
  29. private readonly IRegionManager _regionManager;
  30. private readonly IOptionConfigService _optionConfigService;
  31. private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
  32. private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
  33. private readonly IBasicDeviceKindService _iBasicDeviceKindService;
  34. private readonly IMapper _mapper;
  35. private readonly ILogger _logger;
  36. private List<OptionConfigDto> _optionConfigs;
  37. private bas_plc_test_scheme bas_Plc_Test_Scheme;//测试方案
  38. private int schDtlMainId = 0;//测试方案明细主键id
  39. public event Action<NavigationResult> RequestClose;
  40. public ObservableCollection<SelectItemModel> allSchProjectList = new ObservableCollection<SelectItemModel>(); //所有测试项
  41. public EditSchViewModel(IDialogService dialog, IEventAggregator aggregator, IRegionManager regionManager, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBasicDeviceKindService iBasicDeviceKindService, IMapper mapper, ILogger logger)
  42. {
  43. _dialog = dialog;
  44. _aggregator = aggregator;
  45. _regionManager = regionManager;
  46. _optionConfigService = optionConfigService;
  47. _basicPlcTestSchemeService = basicPlcTestSchemeService;
  48. _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
  49. _iBasicDeviceKindService = iBasicDeviceKindService;
  50. _mapper = mapper;
  51. _logger = logger;
  52. CloseCommand = new DelegateCommand(Close);
  53. ResetCommand = new DelegateCommand<string>(ResetMethod);
  54. OnLoadCommand = new DelegateCommand<object>(OnLoad);
  55. TxtLostFocusCommand = new DelegateCommand(GetExistSchs);
  56. UpCommand = new DelegateCommand<object>(DataUp);
  57. DownCommand = new DelegateCommand<object>(DataDown);
  58. SaveAllSchCommand = new DelegateCommand<object>(SaveAllSch);
  59. CancelCommand = new DelegateCommand<object>(Cancel);
  60. DetailCommand = new DelegateCommand<object>(CheckDetail);
  61. DeleteCommand = new DelegateCommand<object>(Delete);
  62. QueryCommand = new DelegateCommand<object>(Query);
  63. EditCommand = new DelegateCommand<object>(EditSchDtl);
  64. AddItemCommand = new DelegateCommand<object>(AddSchDtl);
  65. //获取设备类型
  66. DeviceKindNameList = _iBasicDeviceKindService.FindAllDeviceKind();
  67. }
  68. private void AddSchDtl(object obj)
  69. {
  70. object[] multiObj = obj as object[];
  71. //测试方案明细主键ID
  72. string deviceKindName = (string)multiObj[0];
  73. string scheduleName = (string)multiObj[1];
  74. DialogParameters parm = new DialogParameters();
  75. parm.Add("Key", deviceKindName);
  76. parm.Add("Key2", scheduleName);
  77. //蒙层显示
  78. _aggregator.GetEvent<MaskEvent>().Publish(true);
  79. //弹出详情对话框
  80. _dialog.ShowDialog("AddItemDetailView", parm, async callback =>
  81. {
  82. if (callback.Result == ButtonResult.OK)
  83. {
  84. //获取已经有的测试项
  85. GetExistSchs();
  86. }
  87. });
  88. //蒙层显示
  89. _aggregator.GetEvent<MaskEvent>().Publish(false);
  90. }
  91. private void EditSchDtl(object obj)
  92. {
  93. //蒙层显示
  94. _aggregator.GetEvent<MaskEvent>().Publish(true);
  95. long schDtlId = Convert.ToInt64(obj);
  96. DialogParameters parm = new DialogParameters();
  97. parm.Add("Key", schDtlId);
  98. //弹出详情对话框
  99. _dialog.ShowDialog("EditItemDetailView", parm, async callback =>
  100. {
  101. if (callback.Result == ButtonResult.OK)
  102. {
  103. //获取已经有的测试项
  104. GetExistSchs();
  105. }
  106. });
  107. //蒙层显示
  108. _aggregator.GetEvent<MaskEvent>().Publish(false);
  109. }
  110. private void Query(object obj)
  111. {
  112. var conditions = (from a in allSchProjectList
  113. where ((SelectTest=="---") ? true : (a.ItemType == SelectTest))
  114. && (string.IsNullOrEmpty(TestName) ? true : (a.ItemName == TestName))
  115. select a).ToList();
  116. SchProjectList = new ObservableCollection<SelectItemModel>(conditions);
  117. }
  118. #region idialog接口实现
  119. public string Title { set; get; } = "新增方案";
  120. #endregion
  121. #region 私有方法
  122. /// <summary>
  123. /// 获取配置
  124. /// </summary>
  125. private void GetConfigOption()
  126. {
  127. var configList = _optionConfigService.QueryList();
  128. _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
  129. var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
  130. TestKinds.Clear();
  131. TestKinds.Add("---");
  132. foreach (var test in tests)
  133. {
  134. TestKinds.Add(test.ContentOption);
  135. }
  136. }
  137. /// <summary>
  138. /// 发送log之界面显示
  139. /// </summary>
  140. /// <param name="msg"></param>
  141. private void SendLogToDis(string msg)
  142. {
  143. _aggregator.GetEvent<LogEvent>().Publish(new LogMessage
  144. {
  145. LogTime = DateTime.Now,
  146. LogMsg = msg
  147. });
  148. }
  149. private void Delete(object obj)
  150. {
  151. int id = Convert.ToInt32(obj);
  152. bool isResult = _basicPlcTestSchemeDtlService.Delete(id);
  153. if (isResult)
  154. {
  155. MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  156. //更新已有的测试项
  157. GetExistSchs();
  158. SendLogToDis($"删除测试项,测试项id为{id},方案名称为{ScheduleName}");
  159. }
  160. }
  161. private void CheckDetail(object obj)
  162. {
  163. }
  164. /// <summary>
  165. /// 取消返回列表界面
  166. /// </summary>
  167. /// <param name="obj"></param>
  168. private void Cancel(object obj)
  169. {
  170. _regionManager.Regions["ContentRegion"].RequestNavigate("BaseConfigView");
  171. }
  172. /// <summary>
  173. /// 保存所有测试项
  174. /// </summary>
  175. /// <param name="obj"></param>
  176. /// <exception cref="NotImplementedException"></exception>
  177. private void SaveAllSch(object obj)
  178. {
  179. //先保存一下当前测试项
  180. //可能调整了测试项的顺序,所以先删除所有测试项
  181. var findSche = _basicPlcTestSchemeService.FindByName(ScheduleName);
  182. if (findSche != null)
  183. {
  184. //显示设备名称
  185. DeviceKindName = findSche.devicekind_name;
  186. long schID = findSche.scheme_id;
  187. //查找此方案下的所有的测试项,并
  188. var schDtls = _basicPlcTestSchemeDtlService.QueryList()?.FindAll(x => x.scheme_id == schID);
  189. //先删除
  190. foreach (var unit in schDtls)
  191. {
  192. int id = Convert.ToInt32(unit.scheme_dtl_id);
  193. _basicPlcTestSchemeDtlService.Delete(id);
  194. }
  195. //按照调整后的顺序添加
  196. foreach (var item in SchProjectList)
  197. {
  198. //测试项目名称
  199. string schItenName = item.ItemName;
  200. var dtl = schDtls.FirstOrDefault(x => x.item_name == schItenName);
  201. if (dtl != null)
  202. {
  203. //要将主键改为0,否则还是原来的主键
  204. dtl.scheme_dtl_id = 0;
  205. _basicPlcTestSchemeDtlService.Add(dtl);
  206. }
  207. }
  208. MessageBox.Show("保存成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  209. //发布消息
  210. _aggregator.GetEvent<ChangeEvent>().Publish("Change");
  211. _regionManager.Regions["ContentRegion"].RequestNavigate("BaseConfigView");
  212. SendLogToDis($"修改测试方案,方案名称为{ScheduleName}");
  213. }
  214. }
  215. /// <summary>
  216. /// 获取测试项
  217. /// </summary>
  218. private void GetExistSchs()
  219. {
  220. allSchProjectList.Clear();
  221. SchProjectList.Clear();
  222. var findSche = _basicPlcTestSchemeService.FindByName(ScheduleName);
  223. if (findSche != null)
  224. {
  225. //显示设备名称
  226. DeviceKindName = findSche.devicekind_name;
  227. long schID = findSche.scheme_id;
  228. //查找此方案下的所有的测试项,并增加到表格
  229. var schDtls = _basicPlcTestSchemeDtlService.QueryList()?.FindAll(x => x.scheme_id == schID)?.OrderBy(x => x.scheme_dtl_id);
  230. foreach (var unit in schDtls)
  231. {
  232. string itemType = string.Empty;
  233. switch (unit.item_type)
  234. {
  235. case 0:
  236. itemType = "前置项";
  237. break;
  238. case 1:
  239. itemType = "PLC点位测试项";
  240. break;
  241. case 2:
  242. itemType = "Robot动作测试";
  243. break;
  244. }
  245. allSchProjectList.Add(new SelectItemModel
  246. {
  247. SchDtlId = unit.scheme_dtl_id,
  248. ItemName = unit.item_name,
  249. ItemType = itemType,
  250. BeforeDetail = unit.precondition_describe,
  251. InDetail = unit.action_describe,
  252. OutDetail = unit.judgement_result_describe
  253. });
  254. }
  255. foreach (var item in allSchProjectList)
  256. {
  257. SchProjectList.Add(item);
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// 下移
  263. /// </summary>
  264. /// <param name="obj"></param>
  265. private void DataDown(object obj)
  266. {
  267. int id = Convert.ToInt32(obj);
  268. var findScheExist = SchProjectList.FirstOrDefault(x => x.SchDtlId == id);
  269. if (findScheExist != null)
  270. {
  271. int index = SchProjectList.IndexOf(findScheExist);
  272. if (index != -1)
  273. {
  274. if (index == SchProjectList.Count - 1)
  275. return;
  276. SchProjectList.RemoveAt(index);
  277. index = index + 1;
  278. SchProjectList.Insert(index, findScheExist);
  279. }
  280. }
  281. }
  282. /// <summary>
  283. /// 上移
  284. /// </summary>
  285. /// <param name="obj"></param>
  286. private void DataUp(object obj)
  287. {
  288. int id = Convert.ToInt32(obj);
  289. var findScheExist = SchProjectList.FirstOrDefault(x => x.SchDtlId == id);
  290. if (findScheExist != null)
  291. {
  292. int index = SchProjectList.IndexOf(findScheExist);
  293. if (index != -1)
  294. {
  295. if (index == 0)
  296. return;
  297. SchProjectList.RemoveAt(index);
  298. index = index - 1;
  299. SchProjectList.Insert(index, findScheExist);
  300. }
  301. }
  302. }
  303. private void OnLoad(object obj)
  304. {
  305. GetConfigOption();
  306. }
  307. /// <summary>
  308. /// 关闭按钮
  309. /// </summary>
  310. private void Close()
  311. {
  312. //RequestClose?.Invoke(new NavigationResult(new NavigationContext(),true);
  313. }
  314. /// <summary>
  315. /// mode转json
  316. /// </summary>
  317. /// <param name="itemType"></param>
  318. /// <param name="itemLogical"></param>
  319. /// <param name="description"></param>
  320. /// <param name="conList"></param>
  321. /// <returns></returns>
  322. private string ModelToJsonToStr(string itemType, string itemLogical, string description, ObservableCollection<BasPlcItemConfigDto> conList)
  323. {
  324. JsonModel prefixJsonModel = new JsonModel();
  325. prefixJsonModel.ItemType = itemType;
  326. prefixJsonModel.ItemLogical = itemLogical;
  327. prefixJsonModel.Description = description;
  328. prefixJsonModel.DetailInfo = new List<DetailPLC>();
  329. foreach (var item in conList)
  330. {
  331. DetailPLC plcItem = new DetailPLC();
  332. plcItem.Id = item.Id;
  333. plcItem.PlcItem = item.PlcItem;
  334. plcItem.PlcAddress = item.PlcAddress;
  335. plcItem.PlcAddType = item.PlcAddType;
  336. plcItem.PlcValue = item.PlcValue;
  337. plcItem.Remark = item.Remark;
  338. prefixJsonModel.DetailInfo.Add(plcItem);
  339. }
  340. string prefixJsonStr = JsonConvert.SerializeObject(prefixJsonModel);
  341. return prefixJsonStr;
  342. }
  343. /// <summary>
  344. /// 重置
  345. /// </summary>
  346. /// <param name="obj"></param>
  347. private void ResetMethod(string obj)
  348. {
  349. SelectTest = "---";
  350. TestName = string.Empty;
  351. }
  352. public void OnNavigatedTo(NavigationContext navigationContext)
  353. {
  354. //编辑
  355. var getMsg = navigationContext.Parameters.GetValues<string>("Key");
  356. ///值不为空,表示修改,为空表示新增
  357. if (getMsg != null)
  358. {
  359. foreach (var item in getMsg)
  360. {
  361. try
  362. {
  363. Title = "编辑方案";
  364. ScheduleName = item.ToString();
  365. //获取已经有的测试项
  366. GetExistSchs();
  367. }
  368. catch (Exception ex)
  369. {
  370. _logger.LogError(ex.ToString());
  371. }
  372. }
  373. }
  374. var getMsg2 = navigationContext.Parameters.GetValues<string>("Key2");
  375. if (getMsg2 != null)
  376. {
  377. foreach (var item in getMsg2)
  378. {
  379. DeviceKindName = item;
  380. }
  381. }
  382. }
  383. public bool IsNavigationTarget(NavigationContext navigationContext)
  384. {
  385. return true;
  386. }
  387. public void OnNavigatedFrom(NavigationContext navigationContext)
  388. {
  389. }
  390. #endregion
  391. #region 命令绑定
  392. public DelegateCommand CloseCommand { set; get; }
  393. public DelegateCommand<string> SureCommand { set; get; }
  394. public DelegateCommand<string> ResetCommand { set; get; }
  395. public DelegateCommand<Object> OnLoadCommand { set; get; }
  396. public DelegateCommand TxtLostFocusCommand { set; get; }
  397. public DelegateCommand<string> OpenExistCommand { set; get; }
  398. public DelegateCommand<object> DownCommand { set; get; }
  399. public DelegateCommand<object> UpCommand { set; get; }
  400. public DelegateCommand<object> CancelCommand { set; get; }
  401. public DelegateCommand<object> DetailCommand { set; get; }
  402. public DelegateCommand<object> DeleteCommand { set; get; }
  403. public DelegateCommand<object> SaveAllSchCommand { set; get; }
  404. public DelegateCommand<object> QueryCommand { set; get; }
  405. public DelegateCommand<object> EditCommand { set; get; }
  406. public DelegateCommand<object> AddItemCommand { set; get; }
  407. #endregion
  408. #region 变量绑定
  409. /// <summary>
  410. /// 测试方案编码
  411. /// </summary>
  412. private string scheduleName;
  413. public string ScheduleName
  414. {
  415. get { return scheduleName; }
  416. set { scheduleName = value; RaisePropertyChanged(); }
  417. }
  418. /// <summary>
  419. /// 设备类型
  420. /// </summary>
  421. private string deviceKindName;
  422. public string DeviceKindName
  423. {
  424. get { return deviceKindName; }
  425. set { deviceKindName = value; RaisePropertyChanged(); }
  426. }
  427. /// <summary>
  428. /// 测试项名称
  429. /// </summary>
  430. private string testName;
  431. public string TestName
  432. {
  433. get { return testName; }
  434. set { testName = value; RaisePropertyChanged(); }
  435. }
  436. /// <summary>
  437. /// 测试项类型
  438. /// </summary>
  439. private ObservableCollection<string> testKinds = new ObservableCollection<string>();
  440. public ObservableCollection<string> TestKinds
  441. {
  442. get { return testKinds; }
  443. set { testKinds = value; RaisePropertyChanged(); }
  444. }
  445. /// <summary>
  446. ///
  447. /// </summary>
  448. private string selectTest;
  449. public string SelectTest
  450. {
  451. get { return selectTest; }
  452. set { selectTest = value; RaisePropertyChanged(); }
  453. }
  454. /// <summary>
  455. /// 设备类型
  456. /// </summary>
  457. private List<string> deviceKindNameList;
  458. public List<string> DeviceKindNameList
  459. {
  460. get { return deviceKindNameList; }
  461. set { deviceKindNameList = value; RaisePropertyChanged(); }
  462. }
  463. private ObservableCollection<SelectItemModel> schProjectList = new ObservableCollection<SelectItemModel>();
  464. public ObservableCollection<SelectItemModel> SchProjectList
  465. {
  466. get { return schProjectList; }
  467. set { SetProperty(ref schProjectList, value); }
  468. }
  469. #endregion
  470. }
  471. }