EditSchViewModel.cs 18 KB

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