123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using AutoMapper;
- using BizService;
- using ImTools;
- using Microsoft.Extensions.Logging;
- using Model.Dto;
- using Model.Entities;
- using PLCTool.Common;
- using PLCTool.Models;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Security.Cryptography.Xml;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Automation;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
- namespace PLCTool.ViewModels.BasicConfigViewModel
- {
- internal class CopySchViewModel : BindableBase, IDialogAware
- {
- private readonly IMapper _mapper;
- private readonly ILogger _logger;
- private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
- private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
- private string schNameAgo = String.Empty;
-
- public CopySchViewModel(IDialogService dialog, IEventAggregator aggregator, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IMapper mapper, ILogger logger)
- {
-
- _basicPlcTestSchemeService = basicPlcTestSchemeService;
- _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
- _mapper = mapper;
- _logger = logger;
- SureCommand = new DelegateCommand<string>(Sure);
- UpCommand = new DelegateCommand<object>(DataUp);
- DownCommand = new DelegateCommand<object>(DataDown);
- SelectAllCommand = new DelegateCommand<object>(SelectAll);
- UnSelectAllCommand = new DelegateCommand<object>(UnSelecttAll);
- }
- private void UnSelecttAll(object obj)
- {
- SchProjectList.ToList().FindAll(p => p.IsSelected = false);
- }
- private void SelectAll(object obj)
- {
- SchProjectList.ToList().FindAll(p => p.IsSelected = true);
- }
- /// <summary>
- /// 下移
- /// </summary>
- /// <param name="obj"></param>
- private void DataDown(object obj)
- {
- int id = Convert.ToInt32(obj);
- var findScheExist = SchProjectList.FirstOrDefault(x => x.Id == id);
- if (findScheExist != null)
- {
- int index = SchProjectList.IndexOf(findScheExist);
- if(index!=-1)
- {
- if (index == SchProjectList.Count - 1)
- return;
- SchProjectList.RemoveAt(index);
- index = index + 1;
- SchProjectList.Insert(index, findScheExist);
- }
-
- }
- }
- /// <summary>
- /// 上移
- /// </summary>
- /// <param name="obj"></param>
- private void DataUp(object obj)
- {
- int id = Convert.ToInt32(obj);
- var findScheExist = SchProjectList.FirstOrDefault(x=>x.Id==id);
- if (findScheExist != null)
- {
- int index = SchProjectList.IndexOf(findScheExist);
- if(index!=-1)
- {
- if (index == 0)
- return;
- SchProjectList.RemoveAt(index);
- index = index - 1;
- SchProjectList.Insert(index, findScheExist);
- }
-
- }
- }
- private void Sure(string obj)
- {
- //查找是否有这个测试方案
- var findExist = _basicPlcTestSchemeService.FindByName(ScheduleName);
- if (findExist != null)
- {
- MessageBoxResult boxResult = MessageBox.Show("已有次测试方案,请填写其他方案名!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- //查找以前测试方案的信息
- var finsSche = _basicPlcTestSchemeService.FindByName(schNameAgo);
- if (finsSche != null)
- {
- //增加测试方案到数据库
- bas_plc_test_scheme schDto = new bas_plc_test_scheme();
- schDto.scheme_name = ScheduleName;
- schDto.device_name = DeviceName;
- schDto.create_by = Appsession.UserName;
- schDto.create_time = DateTime.Now;
- schDto.update_time = DateTime.Now;
- schDto.update_by = Appsession.UserName;
- _basicPlcTestSchemeService.Add(schDto);
- //以前复制的schID
- long schID = finsSche.scheme_id;
- //查找此方案下的所有的测试项,并增加到测试项详细表中
- long? schINewID = _basicPlcTestSchemeService.FindByName(ScheduleName)?.scheme_id;
- //这里要注意顺序,所以用了SchProjectList循环
- foreach (var sch in SchProjectList)
- {
- if(sch.IsSelected)
- {
- int dtlId = Convert.ToInt32(sch.Id);
- var unit = _basicPlcTestSchemeDtlService.Find(dtlId);
- if (unit != null)
- {
- bas_plc_test_scheme_dtl schDtlDto = new bas_plc_test_scheme_dtl();
- schDtlDto.scheme_id = schINewID;
- schDtlDto.item_name = unit.item_name;
- schDtlDto.item_type = unit.item_type;
- schDtlDto.precondition = unit.precondition;
- schDtlDto.precondition_describe = unit.precondition_describe;
- schDtlDto.action = unit.action;
- schDtlDto.action_describe = unit.action_describe;
- schDtlDto.judgement_result = unit.judgement_result;
- schDtlDto.judgement_result_describe = unit.judgement_result_describe;
- schDtlDto.create_by = Appsession.UserName;
- schDtlDto.create_time = DateTime.Now;
- schDtlDto.update_time = DateTime.Now;
- schDtlDto.update_by = Appsession.UserName;
- bool isAddSucc = _basicPlcTestSchemeDtlService.Add(schDtlDto);
- }
- }
-
-
- }
- _logger.LogInformation($"复制方案成功。方案名{ScheduleName}");
- }
- RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
- }
- public string Title { set; get; } ="复制方案";
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
-
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- var getMsg = parameters.GetValues<string>("Key");
-
- if (getMsg != null)
- {
- foreach (var item in getMsg)
- {
- schNameAgo = item.ToString();
- ScheduleName = schNameAgo + "_copy";
- var findSche = _basicPlcTestSchemeService.FindByName(schNameAgo);
- if (findSche != null)
- {
- //显示设备名称
- DeviceName= findSche.device_name;
- long schID = findSche.scheme_id;
- //查找此方案下的所有的测试项,并增加到表格
- var schDtls = _basicPlcTestSchemeDtlService.QueryList()?.FindAll(x => x.scheme_id == schID);
- foreach (var unit in schDtls)
- {
- string itemType = string.Empty;
- switch (unit.item_type)
- {
- case 0:
- itemType = "前置项";
- break;
- case 1:
- itemType = "PLC点位测试项";
- break;
- case 2:
- itemType = "Robot动作测试";
- break;
- }
- SchProjectList.Add(new SelectItemModel
- {
- Id = unit.scheme_dtl_id,
- ItemName = unit.item_name,
- ItemType = itemType,
- }); ;
- }
- }
- }
- }
- }
- #region 命令绑定
- public DelegateCommand<string> SureCommand { set; get; }
-
-
- public DelegateCommand<object> DownCommand { set; get; }
- public DelegateCommand<object> UpCommand { set; get; }
- public DelegateCommand<object> SelectAllCommand { set; get; }
- public DelegateCommand<object> UnSelectAllCommand { set; get; }
- #endregion
- #region 数据绑定
- /// <summary>
- /// 测试方案编码
- /// </summary>
- private string scheduleName;
- public string ScheduleName
- {
- get { return scheduleName; }
- set { scheduleName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备名称
- /// </summary>
- private string deviceName;
- public string DeviceName
- {
- get { return deviceName; }
- set { deviceName = value; RaisePropertyChanged(); }
- }
- private ObservableCollection<SelectItemModel> schProjectList = new ObservableCollection<SelectItemModel>();
- public ObservableCollection<SelectItemModel> SchProjectList
- {
- get { return schProjectList; }
- set { SetProperty(ref schProjectList, value); }
- }
-
- #endregion
- }
- }
|