AutoTestViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. using AutoMapper;
  2. using BizService;
  3. using Model.Dto;
  4. using Model.Entities;
  5. using Newtonsoft.Json;
  6. using NLog;
  7. using PLCTool.Common;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. namespace PLCTool.ViewModels.BusinessManageViewModel
  20. {
  21. public class AutoTestViewModel : BindableBase, IDialogAware
  22. {
  23. private readonly IDialogService _dialog;
  24. private readonly IEventAggregator _aggregator;
  25. private readonly IOptionConfigService _optionConfigService;
  26. private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
  27. private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
  28. private readonly IBizTestRecordService _iBizTestRecordService;
  29. private readonly IBizTestRecordDtlService _iBizTestRecordDtlService;
  30. private readonly IMapper _mapper;
  31. private readonly ILogger _logger;
  32. private List<OptionConfigDto> _optionConfigs;
  33. private DateTime startTime = DateTime.Now;
  34. private DateTime endTime = DateTime.Now;
  35. private int testStatus = 0; //测试状态
  36. private int testResult = 0; //测试结果
  37. private long schDetailId = 0; //测试方案明细ID
  38. public AutoTestViewModel(IDialogService dialog, IEventAggregator aggregator, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBizTestRecordService iBizTestRecordService, IBizTestRecordDtlService iBizTestRecordDtlService, IMapper mapper)
  39. {
  40. _dialog = dialog;
  41. _aggregator = aggregator;
  42. _optionConfigService = optionConfigService;
  43. _basicPlcTestSchemeService = basicPlcTestSchemeService;
  44. _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
  45. _iBizTestRecordDtlService = iBizTestRecordDtlService;
  46. _iBizTestRecordService = iBizTestRecordService;
  47. _mapper = mapper;
  48. //_logger = logger;
  49. CloseCommand = new DelegateCommand(Close);
  50. StartCommand = new DelegateCommand<object>(Start);
  51. DoneCommand = new DelegateCommand<object>(Done);
  52. BeforeConList = new ObservableCollection<BasPlcItemConfigDto>();
  53. InConList = new ObservableCollection<BasPlcItemConfigDto>();
  54. OutConList = new ObservableCollection<BasPlcItemConfigDto>();
  55. GetConfigOption();
  56. }
  57. #region idialog接口实现
  58. public string Title { set; get; } = "手动测试";
  59. public event Action<IDialogResult> RequestClose;
  60. public bool CanCloseDialog()
  61. {
  62. return true;
  63. }
  64. public void OnDialogClosed()
  65. {
  66. }
  67. public void OnDialogOpened(IDialogParameters parameters)
  68. {
  69. //编辑
  70. var getMsg = parameters.GetValues<long>("Key");
  71. ///值不为空,表示修改
  72. if (getMsg != null)
  73. {
  74. foreach (var item in getMsg)
  75. {
  76. schDetailId = item; //测试方案明细ID
  77. int id = Convert.ToInt32(item);
  78. //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  79. var findEntity = _basicPlcTestSchemeDtlService.Find(id);
  80. var findresult = _mapper.Map<bas_plc_test_scheme_dtl, BasicPlcTestSchemeDtlDto>(findEntity);
  81. if (findresult != null)
  82. {
  83. //根据方案id 在方案表中查找方案名 设备名
  84. int schID = Convert.ToInt32(findresult.SchemeId);
  85. var findSch = _basicPlcTestSchemeService.Find(schID);
  86. if (findSch != null)
  87. {
  88. ScheduleName = findSch.scheme_name;
  89. DeviceName = findSch.device_name;
  90. }
  91. TestName = findresult.ItemName;
  92. SelectTest = findresult.ItemType;
  93. //前置项解析
  94. string preconStr = findresult.Precondition.ToString();
  95. if (!string.IsNullOrEmpty(preconStr))
  96. {
  97. JsonModel preconditionModel = JsonConvert.DeserializeObject<JsonModel>(preconStr);
  98. BeforeSelectJudge = preconditionModel.ItemType;
  99. SelectLogic = preconditionModel.ItemLogical;
  100. BeforeDetail = preconditionModel.Description;
  101. BeforeConList.Clear();
  102. foreach (var detail in preconditionModel.DetailInfo)
  103. {
  104. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  105. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  106. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  107. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  108. basPlcItemConfigDto.Remark = detail.Remark;
  109. beforeConList.Add(basPlcItemConfigDto);
  110. }
  111. }
  112. //输入项解析
  113. string inStr = findresult.Action.ToString();
  114. if (!string.IsNullOrEmpty(inStr))
  115. {
  116. JsonModel inModel = JsonConvert.DeserializeObject<JsonModel>(inStr);
  117. SelectInJudge = inModel.ItemType;
  118. InSelectLogic = inModel.ItemLogical;
  119. InDetail = inModel.Description;
  120. InConList.Clear();
  121. foreach (var detail in inModel.DetailInfo)
  122. {
  123. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  124. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  125. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  126. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  127. basPlcItemConfigDto.Remark = detail.Remark;
  128. InConList.Add(basPlcItemConfigDto);
  129. }
  130. }
  131. //输出项解析
  132. string outStr = findresult.JudgementResult.ToString();
  133. if (!string.IsNullOrEmpty(outStr))
  134. {
  135. JsonModel outModel = JsonConvert.DeserializeObject<JsonModel>(outStr);
  136. SelectOutJudge = outModel.ItemType;
  137. OutSelectLogic = outModel.ItemLogical;
  138. OutDetail = outModel.Description;
  139. OutConList.Clear();
  140. foreach (var detail in outModel.DetailInfo)
  141. {
  142. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  143. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  144. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  145. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  146. basPlcItemConfigDto.Remark = detail.Remark;
  147. OutConList.Add(basPlcItemConfigDto);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. #endregion
  155. #region 私有发方法
  156. /// <summary>
  157. /// 完成时间
  158. /// </summary>
  159. /// <param name="obj"></param>
  160. private void Done(object obj)
  161. {
  162. endTime = DateTime.Now;
  163. //记录记录主表中是否有这个方案的记录,没有则添加
  164. var findRecord = _iBizTestRecordService.FindRecorddBySchname(ScheduleName);
  165. if (findRecord == null)
  166. {
  167. //状态status 没有赋值
  168. BizTestRecordDto bizTestRecordDto = new BizTestRecordDto();
  169. bizTestRecordDto.RecordName = ScheduleName + "_Record";
  170. bizTestRecordDto.SchemeName = ScheduleName;
  171. bizTestRecordDto.Tester = Appsession.UserName;
  172. bizTestRecordDto.StartTestTime = startTime;
  173. bizTestRecordDto.FinishTestTime = endTime;
  174. bizTestRecordDto.CreateBy = Appsession.UserName;
  175. bizTestRecordDto.CreateTime = DateTime.Now;
  176. bizTestRecordDto.UpdateBy = Appsession.UserName;
  177. bizTestRecordDto.UpdateTime = DateTime.Now;
  178. var testRecord = _mapper.Map<BizTestRecordDto, biz_test_record>(bizTestRecordDto);
  179. _iBizTestRecordService.Add(testRecord);
  180. }
  181. //查找record ID
  182. long recordId = 0;
  183. var findRecordID = _iBizTestRecordService.FindRecorddBySchname(ScheduleName);
  184. if (findRecordID != null)
  185. {
  186. recordId = findRecordID.record_id;
  187. }
  188. testStatus = 99;//测试状态赋值为99
  189. //记录记录明细表中是否有这个方案明细ID的记录,没有则添加,有则更新记录状态
  190. var findRecordDetail = _iBizTestRecordDtlService.FindRecordDetailBySchDtlID(schDetailId);
  191. if (findRecordDetail == null)
  192. {
  193. //状态status 没有赋值
  194. BizTestRecordDtlDto bizTestRecordDtlDto = new BizTestRecordDtlDto();
  195. bizTestRecordDtlDto.RecordId = recordId;
  196. bizTestRecordDtlDto.SchemeDtlId = schDetailId;
  197. bizTestRecordDtlDto.StartTestTime = startTime;
  198. bizTestRecordDtlDto.FinishTestTime = endTime;
  199. bizTestRecordDtlDto.Status = testStatus;
  200. bizTestRecordDtlDto.TestResult = testResult;
  201. bizTestRecordDtlDto.CreateBy = Appsession.UserName;
  202. bizTestRecordDtlDto.CreateTime = DateTime.Now;
  203. bizTestRecordDtlDto.UpdateBy = Appsession.UserName;
  204. bizTestRecordDtlDto.UpdateTime = DateTime.Now;
  205. var testRecordDtl = _mapper.Map<BizTestRecordDtlDto, biz_test_record_dtl>(bizTestRecordDtlDto);
  206. _iBizTestRecordDtlService.Add(testRecordDtl);
  207. }
  208. else
  209. {
  210. //如果有,则更新测试状态和测试结果
  211. findRecordDetail.start_test_time = startTime;
  212. findRecordDetail.finish_test_time = endTime;
  213. findRecordDetail.update_by = Appsession.UserName;
  214. findRecordDetail.update_time = DateTime.Now;
  215. findRecordDetail.status = testStatus;
  216. findRecordDetail.test_result = 99;
  217. _iBizTestRecordDtlService.Edit(findRecordDetail);
  218. }
  219. }
  220. private void Next(object obj)
  221. {
  222. }
  223. private void Previous(object obj)
  224. {
  225. }
  226. /// <summary>
  227. /// 开始检测
  228. /// </summary>
  229. /// <param name="obj"></param>
  230. private void Start(object obj)
  231. {
  232. //开始时间
  233. startTime = DateTime.Now;
  234. }
  235. /// <summary>
  236. /// 获取配置
  237. /// </summary>
  238. private void GetConfigOption()
  239. {
  240. var configList = _optionConfigService.QueryList();
  241. _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
  242. var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
  243. TestKinds = new List<string>();
  244. foreach (var test in tests)
  245. {
  246. TestKinds.Add(test.ContentOption);
  247. }
  248. //测试项类型
  249. var judges = _optionConfigs.FindAll(x => x.TypeID == 2);
  250. foreach (var judge in judges)
  251. {
  252. BeforeJudgeKinds.Add(judge.ContentOption);
  253. InJudgeKinds.Add(judge.ContentOption);
  254. OutJudgeKinds.Add(judge.ContentOption);
  255. }
  256. //判定逻辑
  257. var logics = _optionConfigs.FindAll(x => x.TypeID == 3);
  258. foreach (var logic in logics)
  259. {
  260. JudgeLogicKinds.Add(logic.ContentOption);
  261. InJudgeLogicKinds.Add(logic.ContentOption);
  262. OutJudgeLogicKinds.Add(logic.ContentOption);
  263. }
  264. }
  265. /// <summary>
  266. /// 关闭按钮
  267. /// </summary>
  268. private void Close()
  269. {
  270. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
  271. }
  272. /// <summary>
  273. /// mode转json
  274. /// </summary>
  275. /// <param name="itemType"></param>
  276. /// <param name="itemLogical"></param>
  277. /// <param name="description"></param>
  278. /// <param name="conList"></param>
  279. /// <returns></returns>
  280. private string ModelToJsonToStr(string itemType, string itemLogical, string description, ObservableCollection<BasPlcItemConfigDto> conList)
  281. {
  282. JsonModel prefixJsonModel = new JsonModel();
  283. prefixJsonModel.ItemType = itemType;
  284. prefixJsonModel.ItemLogical = itemLogical;
  285. prefixJsonModel.Description = description;
  286. prefixJsonModel.DetailInfo = new List<DetailPLC>();
  287. foreach (var item in conList)
  288. {
  289. DetailPLC plcItem = new DetailPLC();
  290. plcItem.PlcItem = item.PlcItem;
  291. plcItem.PlcAddress = item.PlcAddress;
  292. plcItem.PlcValue = item.PlcValue;
  293. plcItem.Remark = item.Remark;
  294. prefixJsonModel.DetailInfo.Add(plcItem);
  295. }
  296. string prefixJsonStr = JsonConvert.SerializeObject(prefixJsonModel);
  297. return prefixJsonStr;
  298. }
  299. #endregion
  300. #region 命令绑定
  301. public DelegateCommand CloseCommand { set; get; }
  302. public DelegateCommand<object> StartCommand { set; get; }
  303. public DelegateCommand<object> DoneCommand { set; get; }
  304. #endregion
  305. #region 变量绑定
  306. /// <summary>
  307. /// 测试方案编码
  308. /// </summary>
  309. private string scheduleName;
  310. public string ScheduleName
  311. {
  312. get { return scheduleName; }
  313. set { scheduleName = value; RaisePropertyChanged(); }
  314. }
  315. /// <summary>
  316. /// 设备名称
  317. /// </summary>
  318. private string deviceName;
  319. public string DeviceName
  320. {
  321. get { return deviceName; }
  322. set { deviceName = value; RaisePropertyChanged(); }
  323. }
  324. /// <summary>
  325. /// 测试项名称
  326. /// </summary>
  327. private string testName;
  328. public string TestName
  329. {
  330. get { return testName; }
  331. set { testName = value; RaisePropertyChanged(); }
  332. }
  333. /// <summary>
  334. /// 测试项类型
  335. /// </summary>
  336. private List<string> testKinds = new List<string>();
  337. public List<string> TestKinds
  338. {
  339. get { return testKinds; }
  340. set { testKinds = value; RaisePropertyChanged(); }
  341. }
  342. /// <summary>
  343. ///
  344. /// </summary>
  345. private string selectTest;
  346. public string SelectTest
  347. {
  348. get { return selectTest; }
  349. set { selectTest = value; RaisePropertyChanged(); }
  350. }
  351. /// <summary>
  352. /// 判定类型(前置项)
  353. /// </summary>
  354. private List<string> beforeJudgeKinds = new List<string>();
  355. public List<string> BeforeJudgeKinds
  356. {
  357. get { return beforeJudgeKinds; }
  358. set { beforeJudgeKinds = value; RaisePropertyChanged(); }
  359. }
  360. private string beforeSelectJudge;
  361. public string BeforeSelectJudge
  362. {
  363. get { return beforeSelectJudge; }
  364. set { beforeSelectJudge = value; RaisePropertyChanged(); }
  365. }
  366. /// <summary>
  367. /// 逻辑判断
  368. /// </summary>
  369. private List<string> judgeLogicKinds = new List<string>();
  370. public List<string> JudgeLogicKinds
  371. {
  372. get { return judgeLogicKinds; }
  373. set { judgeLogicKinds = value; RaisePropertyChanged(); }
  374. }
  375. private string selectLogic;
  376. public string SelectLogic
  377. {
  378. get { return selectLogic; }
  379. set { selectLogic = value; RaisePropertyChanged(); }
  380. }
  381. private string beforeDetail;
  382. public string BeforeDetail
  383. {
  384. get { return beforeDetail; }
  385. set { beforeDetail = value; RaisePropertyChanged(); }
  386. }
  387. private ObservableCollection<BasPlcItemConfigDto> beforeConList;
  388. public ObservableCollection<BasPlcItemConfigDto> BeforeConList
  389. {
  390. get { return beforeConList; }
  391. set { beforeConList = value; RaisePropertyChanged(); }
  392. }
  393. /// <summary>
  394. /// 判定类型(输入项)
  395. /// </summary>
  396. private List<string> inJudgeKinds = new List<string>();
  397. public List<string> InJudgeKinds
  398. {
  399. get { return inJudgeKinds; }
  400. set { inJudgeKinds = value; RaisePropertyChanged(); }
  401. }
  402. private string selectInJudge;
  403. public string SelectInJudge
  404. {
  405. get { return selectInJudge; }
  406. set { selectInJudge = value; RaisePropertyChanged(); }
  407. }
  408. /// <summary>
  409. /// 逻辑判断
  410. /// </summary>
  411. private List<string> inJudgeLogicKinds = new List<string>();
  412. public List<string> InJudgeLogicKinds
  413. {
  414. get { return inJudgeLogicKinds; }
  415. set { inJudgeLogicKinds = value; RaisePropertyChanged(); }
  416. }
  417. private string inSelectLogic;
  418. public string InSelectLogic
  419. {
  420. get { return inSelectLogic; }
  421. set { inSelectLogic = value; RaisePropertyChanged(); }
  422. }
  423. private string inDetail;
  424. public string InDetail
  425. {
  426. get { return inDetail; }
  427. set { inDetail = value; RaisePropertyChanged(); }
  428. }
  429. private ObservableCollection<BasPlcItemConfigDto> inConList;
  430. public ObservableCollection<BasPlcItemConfigDto> InConList
  431. {
  432. get { return inConList; }
  433. set { inConList = value; RaisePropertyChanged(); }
  434. }
  435. /// <summary>
  436. /// 判定类型(结果项)
  437. /// </summary>
  438. private List<string> outJudgeKinds = new List<string>();
  439. public List<string> OutJudgeKinds
  440. {
  441. get { return outJudgeKinds; }
  442. set { outJudgeKinds = value; RaisePropertyChanged(); }
  443. }
  444. private string selectOutJudge;
  445. public string SelectOutJudge
  446. {
  447. get { return selectOutJudge; }
  448. set { selectOutJudge = value; RaisePropertyChanged(); }
  449. }
  450. /// <summary>
  451. /// 逻辑判断
  452. /// </summary>
  453. private List<string> outJudgeLogicKinds = new List<string>();
  454. public List<string> OutJudgeLogicKinds
  455. {
  456. get { return outJudgeLogicKinds; }
  457. set { outJudgeLogicKinds = value; RaisePropertyChanged(); }
  458. }
  459. private string outSelectLogic;
  460. public string OutSelectLogic
  461. {
  462. get { return outSelectLogic; }
  463. set { outSelectLogic = value; RaisePropertyChanged(); }
  464. }
  465. private string outDetail;
  466. public string OutDetail
  467. {
  468. get { return outDetail; }
  469. set { outDetail = value; RaisePropertyChanged(); }
  470. }
  471. private ObservableCollection<BasPlcItemConfigDto> outConList;
  472. public ObservableCollection<BasPlcItemConfigDto> OutConList
  473. {
  474. get { return outConList; }
  475. set { outConList = value; RaisePropertyChanged(); }
  476. }
  477. private int stepIndex = 0;
  478. public int StepIndex
  479. {
  480. get { return stepIndex; }
  481. set { stepIndex = value; RaisePropertyChanged(); }
  482. }
  483. #endregion
  484. }
  485. }