EditItemDetailViewModel.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 NetTaste;
  8. using Newtonsoft.Json;
  9. using PLCTool.Common;
  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 EditItemDetailViewModel : BindableBase, IDialogAware
  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 EditItemDetailViewModel(IDialogService dialog, IEventAggregator aggregator, IRegionManager regionManager, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBasicDeviceKindService iBasicDeviceKindService, IMapper mapper, ILogger logger)
  40. {
  41. _dialog = dialog;
  42. _aggregator = aggregator;
  43. _regionManager = regionManager;
  44. _optionConfigService = optionConfigService;
  45. _basicPlcTestSchemeService = basicPlcTestSchemeService;
  46. _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
  47. _iBasicDeviceKindService = iBasicDeviceKindService;
  48. _mapper = mapper;
  49. _logger = logger;
  50. CloseCommand = new DelegateCommand(Close);
  51. SureCommand = new DelegateCommand<string>(Sure);
  52. ResetCommand = new DelegateCommand<string>(ResetMethod);
  53. AddDetailCommand = new DelegateCommand<string>(AddPLCDetail);
  54. EditInCommand = new DelegateCommand<object>(EditIn);
  55. EditOutCommand = new DelegateCommand<object>(EditOut);
  56. EditBeforeCommand = new DelegateCommand<Object>(EditBefore);
  57. DeleteBeforeCommand = new DelegateCommand<Object>(DeleteBefore);
  58. DeleteInCommand = new DelegateCommand<object>(DeleteIn);
  59. DeleteOutCommand = new DelegateCommand<object>(DeleteOut);
  60. OnLoadCommand = new DelegateCommand<object>(OnLoad);
  61. TxtLostFocusCommand = new DelegateCommand(GetExistSchs);
  62. OpenExistCommand = new DelegateCommand<string>(OpenExist);
  63. UpCommand = new DelegateCommand<object>(DataUp);
  64. DownCommand = new DelegateCommand<object>(DataDown);
  65. DetailCommand = new DelegateCommand<object>(CheckDetail);
  66. DeleteCommand = new DelegateCommand<object>(Delete);
  67. BeforeConList = new ObservableCollection<BasPlcItemConfigDto>();
  68. InConList = new ObservableCollection<BasPlcItemConfigDto>();
  69. OutConList = new ObservableCollection<BasPlcItemConfigDto>();
  70. //获取设备类型
  71. DeviceKindNameList = _iBasicDeviceKindService.FindAllDeviceKind();
  72. GetConfigOption();
  73. }
  74. private void Delete(object obj)
  75. {
  76. int id = Convert.ToInt32(obj);
  77. bool isResult = _basicPlcTestSchemeDtlService.Delete(id);
  78. if (isResult)
  79. {
  80. MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  81. //更新已有的测试项
  82. GetExistSchs();
  83. }
  84. }
  85. private void CheckDetail(object obj)
  86. {
  87. }
  88. #region idialog接口实现
  89. public string Title { set; get; } = "新增方案";
  90. public event Action<IDialogResult> RequestClose;
  91. #endregion
  92. #region 私有方法
  93. /// <summary>
  94. /// 测试方案明细主键ID 查找测试方案id 及方案名
  95. /// </summary>
  96. /// <param name="schDtlId"></param>
  97. private void GetDetaiMsg(int schDtlId)
  98. {
  99. //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  100. var findEntity = _basicPlcTestSchemeDtlService.Find(schDtlId);
  101. var findresult = _mapper.Map<bas_plc_test_scheme_dtl, BasicPlcTestSchemeDtlDto>(findEntity);
  102. if (findresult != null)
  103. {
  104. //根据方案id 在方案表中查找方案名 设备名
  105. int schID = Convert.ToInt32(findresult.SchemeId);
  106. //测试方案
  107. bas_Plc_Test_Scheme = _basicPlcTestSchemeService.Find(schID);
  108. if (bas_Plc_Test_Scheme != null)
  109. {
  110. ScheduleName = bas_Plc_Test_Scheme.scheme_name;
  111. DeviceKindName = bas_Plc_Test_Scheme.devicekind_name;
  112. }
  113. TestName = findresult.ItemName;
  114. SelectTest = findresult.ItemType;
  115. //前置项解析
  116. string preconStr = findresult.Precondition.ToString();
  117. if (!string.IsNullOrEmpty(preconStr))
  118. {
  119. JsonModel preconditionModel = JsonConvert.DeserializeObject<JsonModel>(preconStr);
  120. BeforeSelectJudge = preconditionModel.ItemType;
  121. SelectLogic = preconditionModel.ItemLogical;
  122. BeforeDetail = preconditionModel.Description;
  123. BeforeConList.Clear();
  124. foreach (var detail in preconditionModel.DetailInfo)
  125. {
  126. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  127. basPlcItemConfigDto.Id = detail.Id;
  128. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  129. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  130. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  131. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  132. basPlcItemConfigDto.Remark = detail.Remark;
  133. BeforeConList.Add(basPlcItemConfigDto);
  134. }
  135. }
  136. //输入项解析
  137. string inStr = findresult.Action.ToString();
  138. if (!string.IsNullOrEmpty(inStr))
  139. {
  140. JsonModel inModel = JsonConvert.DeserializeObject<JsonModel>(inStr);
  141. SelectInJudge = inModel.ItemType;
  142. InSelectLogic = inModel.ItemLogical;
  143. InDetail = inModel.Description;
  144. InConList.Clear();
  145. foreach (var detail in inModel.DetailInfo)
  146. {
  147. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  148. basPlcItemConfigDto.Id = detail.Id;
  149. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  150. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  151. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  152. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  153. basPlcItemConfigDto.Remark = detail.Remark;
  154. InConList.Add(basPlcItemConfigDto);
  155. }
  156. }
  157. //输出项解析
  158. string outStr = findresult.JudgementResult.ToString();
  159. if (!string.IsNullOrEmpty(outStr))
  160. {
  161. JsonModel outModel = JsonConvert.DeserializeObject<JsonModel>(outStr);
  162. SelectOutJudge = outModel.ItemType;
  163. OutSelectLogic = outModel.ItemLogical;
  164. OutDetail = outModel.Description;
  165. OutConList.Clear();
  166. foreach (var detail in outModel.DetailInfo)
  167. {
  168. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  169. basPlcItemConfigDto.Id = detail.Id;
  170. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  171. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  172. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  173. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  174. basPlcItemConfigDto.Remark = detail.Remark;
  175. OutConList.Add(basPlcItemConfigDto);
  176. }
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// 打开已有方案
  182. /// </summary>
  183. /// <param name="obj"></param>
  184. /// <exception cref="NotImplementedException"></exception>
  185. private void OpenExist(string str)
  186. {
  187. if (string.IsNullOrEmpty(DeviceKindName))
  188. {
  189. MessageBox.Show("请选择设备类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  190. return;
  191. }
  192. if (string.IsNullOrEmpty(ScheduleName))
  193. {
  194. MessageBox.Show("请填写方案名称!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  195. return;
  196. }
  197. //设备类型名称
  198. DialogParameters parm = new DialogParameters();
  199. parm.Add("Key", DeviceKindName);
  200. parm.Add("Key2", ScheduleName);
  201. //已有的测试项
  202. parm.Add("Key3", SchProjectList);
  203. //弹出详情对话框
  204. _dialog.ShowDialog("CopySchView", parm, async callback =>
  205. {
  206. if (callback.Result == ButtonResult.OK)
  207. {
  208. List<bas_plc_test_scheme_dtl> returnValue = callback.Parameters.GetValue<List<bas_plc_test_scheme_dtl>>("ReturnValue");
  209. //更新表格,重新获取
  210. GetExistSchs();
  211. }
  212. });
  213. }
  214. /// <summary>
  215. /// 获取测试项
  216. /// </summary>
  217. private void GetExistSchs()
  218. {
  219. SchProjectList.Clear();
  220. var findSche = _basicPlcTestSchemeService.FindByName(ScheduleName);
  221. if (findSche != null)
  222. {
  223. //显示设备名称
  224. DeviceKindName = findSche.devicekind_name;
  225. long schID = findSche.scheme_id;
  226. //查找此方案下的所有的测试项,并增加到表格
  227. var schDtls = _basicPlcTestSchemeDtlService.QueryList()?.FindAll(x => x.scheme_id == schID)?.OrderBy(x => x.scheme_dtl_id);
  228. foreach (var unit in schDtls)
  229. {
  230. string itemType = string.Empty;
  231. switch (unit.item_type)
  232. {
  233. case 0:
  234. itemType = "前置项";
  235. break;
  236. case 1:
  237. itemType = "PLC点位测试项";
  238. break;
  239. case 2:
  240. itemType = "Robot动作测试";
  241. break;
  242. }
  243. SchProjectList.Add(new SelectItemModel
  244. {
  245. SchDtlId = unit.scheme_dtl_id,
  246. ItemName = unit.item_name,
  247. ItemType = itemType,
  248. BeforeDetail = unit.precondition_describe,
  249. InDetail = unit.action_describe,
  250. OutDetail = unit.judgement_result_describe
  251. });
  252. }
  253. }
  254. }
  255. /// <summary>
  256. /// 下移
  257. /// </summary>
  258. /// <param name="obj"></param>
  259. private void DataDown(object obj)
  260. {
  261. int id = Convert.ToInt32(obj);
  262. var findScheExist = SchProjectList.FirstOrDefault(x => x.SchDtlId == id);
  263. if (findScheExist != null)
  264. {
  265. int index = SchProjectList.IndexOf(findScheExist);
  266. if (index != -1)
  267. {
  268. if (index == SchProjectList.Count - 1)
  269. return;
  270. SchProjectList.RemoveAt(index);
  271. index = index + 1;
  272. SchProjectList.Insert(index, findScheExist);
  273. }
  274. }
  275. }
  276. /// <summary>
  277. /// 上移
  278. /// </summary>
  279. /// <param name="obj"></param>
  280. private void DataUp(object obj)
  281. {
  282. int id = Convert.ToInt32(obj);
  283. var findScheExist = SchProjectList.FirstOrDefault(x => x.SchDtlId == id);
  284. if (findScheExist != null)
  285. {
  286. int index = SchProjectList.IndexOf(findScheExist);
  287. if (index != -1)
  288. {
  289. if (index == 0)
  290. return;
  291. SchProjectList.RemoveAt(index);
  292. index = index - 1;
  293. SchProjectList.Insert(index, findScheExist);
  294. }
  295. }
  296. }
  297. private void OnLoad(object obj)
  298. {
  299. }
  300. private void OpenEditDialog(Object obj, ObservableCollection<BasPlcItemConfigDto> conList)
  301. {
  302. int id = Convert.ToInt32(obj);
  303. var edit = conList.FirstOrDefault(x => x.Id == id);
  304. if (edit != null)
  305. {
  306. DialogParameters parm = new DialogParameters();
  307. parm.Add("Key", edit);
  308. //弹出详情对话框
  309. _dialog.ShowDialog("AddDetailView", parm, async callback =>
  310. {
  311. if (callback.Result == ButtonResult.OK)
  312. {
  313. BasPlcItemConfigDto returnValue = callback.Parameters.GetValue<BasPlcItemConfigDto>("ReturnValue");
  314. //更新
  315. var after = conList.FirstOrDefault(x => x.Id == id);
  316. if (after != null)
  317. {
  318. after.PlcItem = returnValue.PlcItem;
  319. after.PlcAddress = returnValue.PlcAddress;
  320. after.PlcAddType = returnValue.PlcAddType;
  321. after.PlcValue = returnValue.PlcValue;
  322. after.Remark = returnValue.Remark;
  323. }
  324. }
  325. });
  326. }
  327. }
  328. /// <summary>
  329. /// 前置条件编辑
  330. /// </summary>
  331. /// <param name="obj"></param>
  332. /// <exception cref="NotImplementedException"></exception>
  333. private void EditBefore(Object obj)
  334. {
  335. OpenEditDialog(obj, BeforeConList);
  336. }
  337. private void EditOut(object obj)
  338. {
  339. OpenEditDialog(obj, OutConList);
  340. }
  341. private void EditIn(object obj)
  342. {
  343. OpenEditDialog(obj, InConList);
  344. }
  345. /// <summary>
  346. /// 前置条件删除
  347. /// </summary>
  348. /// <param name="obj"></param>
  349. /// <exception cref="NotImplementedException"></exception>
  350. private void DeleteBefore(Object obj)
  351. {
  352. int id = Convert.ToInt32(obj);
  353. MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
  354. if (boxResult == MessageBoxResult.OK)
  355. {
  356. var del = BeforeConList.FirstOrDefault(x => x.Id == id);
  357. if (del != null)
  358. {
  359. BeforeConList.Remove(del);
  360. }
  361. }
  362. }
  363. private void DeleteOut(object obj)
  364. {
  365. int id = Convert.ToInt32(obj);
  366. MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
  367. if (boxResult == MessageBoxResult.OK)
  368. {
  369. var del = OutConList.FirstOrDefault(x => x.Id == id);
  370. if (del != null)
  371. {
  372. OutConList.Remove(del);
  373. }
  374. }
  375. }
  376. private void DeleteIn(object obj)
  377. {
  378. int id = Convert.ToInt32(obj);
  379. MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
  380. if (boxResult == MessageBoxResult.OK)
  381. {
  382. var del = InConList.FirstOrDefault(x => x.Id == id);
  383. if (del != null)
  384. {
  385. InConList.Remove(del);
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// 获取配置
  391. /// </summary>
  392. private void GetConfigOption()
  393. {
  394. var configList = _optionConfigService.QueryList();
  395. _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
  396. var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
  397. TestKinds = new List<string>();
  398. foreach (var test in tests)
  399. {
  400. TestKinds.Add(test.ContentOption);
  401. }
  402. //测试项类型
  403. var judges = _optionConfigs.FindAll(x => x.TypeID == 2);
  404. foreach (var judge in judges)
  405. {
  406. BeforeJudgeKinds.Add(judge.ContentOption);
  407. InJudgeKinds.Add(judge.ContentOption);
  408. OutJudgeKinds.Add(judge.ContentOption);
  409. }
  410. //判定逻辑
  411. var logics = _optionConfigs.FindAll(x => x.TypeID == 3);
  412. foreach (var logic in logics)
  413. {
  414. JudgeLogicKinds.Add(logic.ContentOption);
  415. InJudgeLogicKinds.Add(logic.ContentOption);
  416. OutJudgeLogicKinds.Add(logic.ContentOption);
  417. }
  418. }
  419. /// <summary>
  420. /// 新增
  421. /// </summary>
  422. /// <param name="str"></param>
  423. private void AddPLCDetail(string str)
  424. {
  425. DialogParameters parm = new DialogParameters();
  426. parm.Add("Key", "");
  427. //弹出详情对话框
  428. _dialog.ShowDialog("AddDetailView", parm, async callback =>
  429. {
  430. if (callback.Result == ButtonResult.OK)
  431. {
  432. BasPlcItemConfigDto returnValue = callback.Parameters.GetValue<BasPlcItemConfigDto>("ReturnValue");
  433. switch (str)
  434. {
  435. case "Before":
  436. returnValue.Id = BeforeConList.Count + 1;
  437. BeforeConList.Add(returnValue); break;
  438. case "In":
  439. returnValue.Id = InConList.Count + 1;
  440. InConList.Add(returnValue); break;
  441. case "Out":
  442. returnValue.Id = OutConList.Count + 1;
  443. OutConList.Add(returnValue); break;
  444. }
  445. }
  446. });
  447. }
  448. /// <summary>
  449. /// 初始化
  450. /// </summary>
  451. /// <summary>
  452. /// 关闭按钮
  453. /// </summary>
  454. private void Close()
  455. {
  456. //RequestClose?.Invoke(new NavigationResult(new NavigationContext(),true);
  457. }
  458. /// <summary>
  459. /// 确认
  460. /// </summary>
  461. /// <param name="obj"></param>
  462. private void Sure(string obj)
  463. {
  464. if (string.IsNullOrEmpty(ScheduleName))
  465. {
  466. MessageBox.Show("请填写测试方案名称!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  467. return;
  468. }
  469. if (string.IsNullOrEmpty(DeviceKindName))
  470. {
  471. MessageBox.Show("请填写设备类型名称!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  472. return;
  473. }
  474. if (string.IsNullOrEmpty(SelectTest))
  475. {
  476. MessageBox.Show("请选择测试项类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  477. return;
  478. }
  479. if (string.IsNullOrEmpty(TestName))
  480. {
  481. MessageBox.Show("请填写测试项名称!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  482. return;
  483. }
  484. if (string.IsNullOrEmpty(BeforeSelectJudge))
  485. {
  486. MessageBox.Show("请填写前置项测试项测试类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  487. return;
  488. }
  489. //前置项
  490. if (string.IsNullOrEmpty(BeforeSelectJudge))
  491. {
  492. MessageBox.Show("请填写前置项测试项测试类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  493. return;
  494. }
  495. if (string.IsNullOrEmpty(SelectLogic))
  496. {
  497. MessageBox.Show("请填写前置项判定逻辑!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  498. return;
  499. }
  500. //输入项
  501. if (string.IsNullOrEmpty(SelectInJudge))
  502. {
  503. MessageBox.Show("请填写输入项测试项测试类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  504. return;
  505. }
  506. if (string.IsNullOrEmpty(InSelectLogic))
  507. {
  508. MessageBox.Show("请填写输入项判定逻辑!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  509. return;
  510. }
  511. //输出项
  512. if (string.IsNullOrEmpty(SelectOutJudge))
  513. {
  514. MessageBox.Show("请填写结果项测试项测试类型!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  515. return;
  516. }
  517. if (string.IsNullOrEmpty(OutSelectLogic))
  518. {
  519. MessageBox.Show("请填写结果项判定逻辑!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  520. return;
  521. }
  522. try
  523. {
  524. //更新测试方案名称
  525. if (bas_Plc_Test_Scheme != null)
  526. {
  527. bas_Plc_Test_Scheme.scheme_name = ScheduleName;
  528. bas_Plc_Test_Scheme.devicekind_name = DeviceKindName;
  529. _basicPlcTestSchemeService.Edit(bas_Plc_Test_Scheme);
  530. }
  531. //查找测试方案id
  532. var finsScheHave = _basicPlcTestSchemeService.FindByName(ScheduleName);
  533. if (finsScheHave != null)
  534. {
  535. long id = finsScheHave.scheme_id;
  536. //增加或修改方案明细表
  537. AddOrEditDetailsToDb(id);
  538. }
  539. //更新已有的测试项
  540. GetExistSchs();
  541. //清空已填写的内容
  542. BeforeConList = new ObservableCollection<BasPlcItemConfigDto>();
  543. InConList = new ObservableCollection<BasPlcItemConfigDto>();
  544. OutConList = new ObservableCollection<BasPlcItemConfigDto>();
  545. }
  546. catch (Exception ex)
  547. {
  548. _logger.LogError(ex.ToString());
  549. }
  550. }
  551. /// <summary>
  552. /// 方案明细添加到数据库
  553. /// </summary>
  554. private void AddOrEditDetailsToDb(long scheduleId)
  555. {
  556. //前置项json转化
  557. string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  558. //输入项json转化
  559. string inJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
  560. //输出项json转化
  561. string outJsonStr = ModelToJsonToStr(SelectOutJudge, OutSelectLogic, OutDetail, OutConList);
  562. //添加达到测试主表
  563. //增加测试方案
  564. BasicPlcTestSchemeDtlDto schDtlDto = new BasicPlcTestSchemeDtlDto();
  565. schDtlDto.SchemeId = scheduleId;
  566. schDtlDto.ItemName = TestName;
  567. schDtlDto.ItemType = SelectTest;
  568. schDtlDto.Precondition = prefixJsonStr;
  569. schDtlDto.PreconditionDescribe = BeforeDetail;
  570. schDtlDto.Action = inJsonStr;
  571. schDtlDto.ActionDescribe = InDetail;
  572. schDtlDto.JudgementResult = outJsonStr;
  573. schDtlDto.JudgementResultDescribe = OutDetail;
  574. schDtlDto.CreateBy = Appsession.UserName;
  575. schDtlDto.CreateTime = DateTime.Now;
  576. schDtlDto.UpdateTime = DateTime.Now;
  577. schDtlDto.UpdateBy = Appsession.UserName;
  578. bas_plc_test_scheme_dtl schDtlEntity = _mapper.Map<BasicPlcTestSchemeDtlDto, bas_plc_test_scheme_dtl>(schDtlDto);
  579. schDtlEntity.item_type = StrToItemType(schDtlDto.ItemType);
  580. try
  581. {
  582. {
  583. {
  584. schDtlDto.SchemeDtlId = schDtlMainId;
  585. bas_plc_test_scheme_dtl schDtlEntityEdit = _mapper.Map<BasicPlcTestSchemeDtlDto, bas_plc_test_scheme_dtl>(schDtlDto);
  586. //ItemType 转化
  587. schDtlEntityEdit.item_type = StrToItemType(schDtlDto.ItemType);
  588. bool isSucc = _basicPlcTestSchemeDtlService.Edit(schDtlEntityEdit);
  589. if (isSucc)
  590. {
  591. _logger.LogInformation($"更新方案成功。方案名{ScheduleName},测试项{TestName}");
  592. MessageBoxResult boxResult = MessageBox.Show("保存成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  593. }
  594. else
  595. {
  596. MessageBoxResult boxResult = MessageBox.Show("保存失败", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  597. return;
  598. }
  599. }
  600. }
  601. }
  602. catch (Exception ex)
  603. {
  604. _logger.LogError(ex.ToString());
  605. }
  606. }
  607. /// <summary>
  608. /// 转化
  609. /// </summary>
  610. /// <param name="typeStr"></param>
  611. /// <returns></returns>
  612. private int StrToItemType(string typeStr)
  613. {
  614. int itemInt = 0;
  615. switch (typeStr)
  616. {
  617. case "前置项":
  618. itemInt = 0;
  619. break;
  620. case "PLC点位测试项":
  621. itemInt = 1;
  622. break;
  623. case "Robot动作测试":
  624. itemInt = 2;
  625. break;
  626. }
  627. return itemInt;
  628. }
  629. /// <summary>
  630. /// mode转json
  631. /// </summary>
  632. /// <param name="itemType"></param>
  633. /// <param name="itemLogical"></param>
  634. /// <param name="description"></param>
  635. /// <param name="conList"></param>
  636. /// <returns></returns>
  637. private string ModelToJsonToStr(string itemType, string itemLogical, string description, ObservableCollection<BasPlcItemConfigDto> conList)
  638. {
  639. JsonModel prefixJsonModel = new JsonModel();
  640. prefixJsonModel.ItemType = itemType;
  641. prefixJsonModel.ItemLogical = itemLogical;
  642. prefixJsonModel.Description = description;
  643. prefixJsonModel.DetailInfo = new List<DetailPLC>();
  644. foreach (var item in conList)
  645. {
  646. DetailPLC plcItem = new DetailPLC();
  647. plcItem.Id = item.Id;
  648. plcItem.PlcItem = item.PlcItem;
  649. plcItem.PlcAddress = item.PlcAddress;
  650. plcItem.PlcAddType = item.PlcAddType;
  651. plcItem.PlcValue = item.PlcValue;
  652. plcItem.Remark = item.Remark;
  653. prefixJsonModel.DetailInfo.Add(plcItem);
  654. }
  655. string prefixJsonStr = JsonConvert.SerializeObject(prefixJsonModel);
  656. return prefixJsonStr;
  657. }
  658. /// <summary>
  659. /// 重置
  660. /// </summary>
  661. /// <param name="obj"></param>
  662. private void ResetMethod(string obj)
  663. {
  664. ScheduleName = string.Empty;
  665. DeviceKindName = string.Empty;
  666. TestName = string.Empty;
  667. }
  668. public bool CanCloseDialog()
  669. {
  670. return true;
  671. }
  672. public void OnDialogClosed()
  673. {
  674. }
  675. public void OnDialogOpened(IDialogParameters parameters)
  676. {
  677. //编辑
  678. var getMsg = parameters.GetValues<long>("Key");
  679. ///值不为空,表示修改,为空表示新增
  680. if (getMsg != null)
  681. {
  682. foreach (var item in getMsg)
  683. {
  684. int schDtlId = Convert.ToInt32(item);
  685. schDtlMainId = schDtlId;//全局变量赋值,测试方案明细id
  686. GetDetaiMsg(schDtlId);
  687. }
  688. }
  689. }
  690. #endregion
  691. #region 命令绑定
  692. public DelegateCommand CloseCommand { set; get; }
  693. public DelegateCommand<string> SureCommand { set; get; }
  694. public DelegateCommand<string> ResetCommand { set; get; }
  695. /// <summary>
  696. /// 新增对话框按钮
  697. /// </summary>
  698. public DelegateCommand<string> AddDetailCommand { set; get; }
  699. /// <summary>
  700. /// 表格删除
  701. /// </summary>
  702. public DelegateCommand<Object> DeleteBeforeCommand { set; get; }
  703. /// <summary>
  704. /// 表格编辑按钮
  705. /// </summary>
  706. public DelegateCommand<Object> EditBeforeCommand { set; get; }
  707. /// <summary>
  708. /// 输入项
  709. /// </summary>
  710. public DelegateCommand<Object> DeleteInCommand { set; get; }
  711. public DelegateCommand<Object> EditInCommand { set; get; }
  712. /// <summary>
  713. /// 输出项
  714. /// </summary>
  715. public DelegateCommand<Object> DeleteOutCommand { set; get; }
  716. public DelegateCommand<Object> EditOutCommand { set; get; }
  717. public DelegateCommand<Object> OnLoadCommand { set; get; }
  718. public DelegateCommand TxtLostFocusCommand { set; get; }
  719. public DelegateCommand<string> OpenExistCommand { set; get; }
  720. public DelegateCommand<object> DownCommand { set; get; }
  721. public DelegateCommand<object> UpCommand { set; get; }
  722. public DelegateCommand<object> EditDetailCommand { set; get; }
  723. public DelegateCommand<object> DisAnoCommand { set; get; }
  724. public DelegateCommand<object> CancelCommand { set; get; }
  725. public DelegateCommand<object> DetailCommand { set; get; }
  726. public DelegateCommand<object> DeleteCommand { set; get; }
  727. #endregion
  728. #region 变量绑定
  729. /// <summary>
  730. /// 测试方案编码
  731. /// </summary>
  732. private string scheduleName;
  733. public string ScheduleName
  734. {
  735. get { return scheduleName; }
  736. set { scheduleName = value; RaisePropertyChanged(); }
  737. }
  738. /// <summary>
  739. /// 设备类型
  740. /// </summary>
  741. private string deviceKindName;
  742. public string DeviceKindName
  743. {
  744. get { return deviceKindName; }
  745. set { deviceKindName = value; RaisePropertyChanged(); }
  746. }
  747. /// <summary>
  748. /// 测试项名称
  749. /// </summary>
  750. private string testName;
  751. public string TestName
  752. {
  753. get { return testName; }
  754. set { testName = value; RaisePropertyChanged(); }
  755. }
  756. /// <summary>
  757. /// 测试项类型
  758. /// </summary>
  759. private List<string> testKinds = new List<string>();
  760. public List<string> TestKinds
  761. {
  762. get { return testKinds; }
  763. set { testKinds = value; RaisePropertyChanged(); }
  764. }
  765. /// <summary>
  766. ///
  767. /// </summary>
  768. private string selectTest;
  769. public string SelectTest
  770. {
  771. get { return selectTest; }
  772. set { selectTest = value; RaisePropertyChanged(); }
  773. }
  774. /// <summary>
  775. /// 判定类型(前置项)
  776. /// </summary>
  777. private List<string> beforeJudgeKinds = new List<string>();
  778. public List<string> BeforeJudgeKinds
  779. {
  780. get { return beforeJudgeKinds; }
  781. set { beforeJudgeKinds = value; RaisePropertyChanged(); }
  782. }
  783. private string beforeSelectJudge;
  784. public string BeforeSelectJudge
  785. {
  786. get { return beforeSelectJudge; }
  787. set { beforeSelectJudge = value; RaisePropertyChanged(); }
  788. }
  789. /// <summary>
  790. /// 逻辑判断
  791. /// </summary>
  792. private List<string> judgeLogicKinds = new List<string>();
  793. public List<string> JudgeLogicKinds
  794. {
  795. get { return judgeLogicKinds; }
  796. set { judgeLogicKinds = value; RaisePropertyChanged(); }
  797. }
  798. private string selectLogic;
  799. public string SelectLogic
  800. {
  801. get { return selectLogic; }
  802. set { selectLogic = value; RaisePropertyChanged(); }
  803. }
  804. private string beforeDetail;
  805. public string BeforeDetail
  806. {
  807. get { return beforeDetail; }
  808. set { beforeDetail = value; RaisePropertyChanged(); }
  809. }
  810. private ObservableCollection<BasPlcItemConfigDto> beforeConList;
  811. public ObservableCollection<BasPlcItemConfigDto> BeforeConList
  812. {
  813. get { return beforeConList; }
  814. set { beforeConList = value; RaisePropertyChanged(); }
  815. }
  816. /// <summary>
  817. /// 判定类型(输入项)
  818. /// </summary>
  819. private List<string> inJudgeKinds = new List<string>();
  820. public List<string> InJudgeKinds
  821. {
  822. get { return inJudgeKinds; }
  823. set { inJudgeKinds = value; RaisePropertyChanged(); }
  824. }
  825. private string selectInJudge;
  826. public string SelectInJudge
  827. {
  828. get { return selectInJudge; }
  829. set { selectInJudge = value; RaisePropertyChanged(); }
  830. }
  831. /// <summary>
  832. /// 逻辑判断
  833. /// </summary>
  834. private List<string> inJudgeLogicKinds = new List<string>();
  835. public List<string> InJudgeLogicKinds
  836. {
  837. get { return inJudgeLogicKinds; }
  838. set { inJudgeLogicKinds = value; RaisePropertyChanged(); }
  839. }
  840. private string inSelectLogic;
  841. public string InSelectLogic
  842. {
  843. get { return inSelectLogic; }
  844. set { inSelectLogic = value; RaisePropertyChanged(); }
  845. }
  846. private string inDetail;
  847. public string InDetail
  848. {
  849. get { return inDetail; }
  850. set { inDetail = value; RaisePropertyChanged(); }
  851. }
  852. private ObservableCollection<BasPlcItemConfigDto> inConList;
  853. public ObservableCollection<BasPlcItemConfigDto> InConList
  854. {
  855. get { return inConList; }
  856. set { inConList = value; RaisePropertyChanged(); }
  857. }
  858. /// <summary>
  859. /// 判定类型(结果项)
  860. /// </summary>
  861. private List<string> outJudgeKinds = new List<string>();
  862. public List<string> OutJudgeKinds
  863. {
  864. get { return outJudgeKinds; }
  865. set { outJudgeKinds = value; RaisePropertyChanged(); }
  866. }
  867. private string selectOutJudge;
  868. public string SelectOutJudge
  869. {
  870. get { return selectOutJudge; }
  871. set { selectOutJudge = value; RaisePropertyChanged(); }
  872. }
  873. /// <summary>
  874. /// 逻辑判断
  875. /// </summary>
  876. private List<string> outJudgeLogicKinds = new List<string>();
  877. public List<string> OutJudgeLogicKinds
  878. {
  879. get { return outJudgeLogicKinds; }
  880. set { outJudgeLogicKinds = value; RaisePropertyChanged(); }
  881. }
  882. private string outSelectLogic;
  883. public string OutSelectLogic
  884. {
  885. get { return outSelectLogic; }
  886. set { outSelectLogic = value; RaisePropertyChanged(); }
  887. }
  888. private string outDetail;
  889. public string OutDetail
  890. {
  891. get { return outDetail; }
  892. set { outDetail = value; RaisePropertyChanged(); }
  893. }
  894. private ObservableCollection<BasPlcItemConfigDto> outConList;
  895. public ObservableCollection<BasPlcItemConfigDto> OutConList
  896. {
  897. get { return outConList; }
  898. set { outConList = value; RaisePropertyChanged(); }
  899. }
  900. /// <summary>
  901. /// 设备类型
  902. /// </summary>
  903. private List<string> deviceKindNameList;
  904. public List<string> DeviceKindNameList
  905. {
  906. get { return deviceKindNameList; }
  907. set { deviceKindNameList = value; RaisePropertyChanged(); }
  908. }
  909. private ObservableCollection<SelectItemModel> schProjectList = new ObservableCollection<SelectItemModel>();
  910. public ObservableCollection<SelectItemModel> SchProjectList
  911. {
  912. get { return schProjectList; }
  913. set { SetProperty(ref schProjectList, value); }
  914. }
  915. #endregion
  916. }
  917. }