123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- using AutoMapper;
- using BizService;
- using BlankApp1.Events;
- using Microsoft.Extensions.Logging;
- using Model.Dto;
- using Model.Entities;
- using Newtonsoft.Json;
- using PLCTool.Common;
- using PLCTool.Events;
- using PLCTool.Models;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PLCTool.ViewModels.BasicConfigViewModel
- {
-
- public class EditSchViewModel : BindableBase, INavigationAware
- {
- private readonly IDialogService _dialog;
- private readonly IEventAggregator _aggregator;
- private readonly IRegionManager _regionManager;
- private readonly IOptionConfigService _optionConfigService;
- private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
- private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
- private readonly IBasicDeviceKindService _iBasicDeviceKindService;
- private readonly IMapper _mapper;
- private readonly ILogger _logger;
- private List<OptionConfigDto> _optionConfigs;
- private bas_plc_test_scheme bas_Plc_Test_Scheme;//测试方案
- private int schDtlMainId = 0;//测试方案明细主键id
- public event Action<NavigationResult> RequestClose;
- public ObservableCollection<SelectItemModel> allSchProjectList = new ObservableCollection<SelectItemModel>(); //所有测试项
- public EditSchViewModel(IDialogService dialog, IEventAggregator aggregator, IRegionManager regionManager, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBasicDeviceKindService iBasicDeviceKindService, IMapper mapper, ILogger logger)
- {
- _dialog = dialog;
- _aggregator = aggregator;
- _regionManager = regionManager;
- _optionConfigService = optionConfigService;
- _basicPlcTestSchemeService = basicPlcTestSchemeService;
- _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
- _iBasicDeviceKindService = iBasicDeviceKindService;
- _mapper = mapper;
- _logger = logger;
- CloseCommand = new DelegateCommand(Close);
-
- ResetCommand = new DelegateCommand<string>(ResetMethod);
-
- OnLoadCommand = new DelegateCommand<object>(OnLoad);
- TxtLostFocusCommand = new DelegateCommand(GetExistSchs);
-
- UpCommand = new DelegateCommand<object>(DataUp);
- DownCommand = new DelegateCommand<object>(DataDown);
- SaveAllSchCommand = new DelegateCommand<object>(SaveAllSch);
- CancelCommand = new DelegateCommand<object>(Cancel);
- DetailCommand = new DelegateCommand<object>(CheckDetail);
- DeleteCommand = new DelegateCommand<object>(Delete);
- QueryCommand = new DelegateCommand<object>(Query);
- EditCommand = new DelegateCommand<object>(EditSchDtl);
- AddItemCommand = new DelegateCommand<object>(AddSchDtl);
- //获取设备类型
- DeviceKindNameList = _iBasicDeviceKindService.FindAllDeviceKind();
-
- }
- private void AddSchDtl(object obj)
- {
- object[] multiObj = obj as object[];
- //测试方案明细主键ID
- string deviceKindName = (string)multiObj[0];
- string scheduleName = (string)multiObj[1];
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", deviceKindName);
- parm.Add("Key2", scheduleName);
- //蒙层显示
- _aggregator.GetEvent<MaskEvent>().Publish(true);
- //弹出详情对话框
- _dialog.ShowDialog("AddItemDetailView", parm, async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- //获取已经有的测试项
- GetExistSchs();
- }
- });
- //蒙层显示
- _aggregator.GetEvent<MaskEvent>().Publish(false);
- }
- private void EditSchDtl(object obj)
- {
- //蒙层显示
- _aggregator.GetEvent<MaskEvent>().Publish(true);
- long schDtlId = Convert.ToInt64(obj);
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", schDtlId);
- //弹出详情对话框
- _dialog.ShowDialog("EditItemDetailView", parm, async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- //获取已经有的测试项
- GetExistSchs();
- }
- });
- //蒙层显示
- _aggregator.GetEvent<MaskEvent>().Publish(false);
- }
- private void Query(object obj)
- {
- var conditions = (from a in allSchProjectList
- where ((SelectTest=="---") ? true : (a.ItemType == SelectTest))
- && (string.IsNullOrEmpty(TestName) ? true : (a.ItemName == TestName))
- select a).ToList();
- SchProjectList = new ObservableCollection<SelectItemModel>(conditions);
- }
- #region idialog接口实现
- public string Title { set; get; } = "新增方案";
- #endregion
- #region 私有方法
- /// <summary>
- /// 获取配置
- /// </summary>
- private void GetConfigOption()
- {
- var configList = _optionConfigService.QueryList();
- _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
- var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
- TestKinds.Clear();
- TestKinds.Add("---");
- foreach (var test in tests)
- {
- TestKinds.Add(test.ContentOption);
- }
- }
- /// <summary>
- /// 发送log之界面显示
- /// </summary>
- /// <param name="msg"></param>
- private void SendLogToDis(string msg)
- {
- _aggregator.GetEvent<LogEvent>().Publish(new LogMessage
- {
- LogTime = DateTime.Now,
- LogMsg = msg
- });
- }
- private void Delete(object obj)
- {
- int id = Convert.ToInt32(obj);
- bool isResult = _basicPlcTestSchemeDtlService.Delete(id);
- if (isResult)
- {
- MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
- //更新已有的测试项
- GetExistSchs();
- SendLogToDis($"删除测试项,测试项id为{id},方案名称为{ScheduleName}");
- }
- }
- private void CheckDetail(object obj)
- {
- }
- /// <summary>
- /// 取消返回列表界面
- /// </summary>
- /// <param name="obj"></param>
- private void Cancel(object obj)
- {
- _regionManager.Regions["ContentRegion"].RequestNavigate("BaseConfigView");
- }
-
- /// <summary>
- /// 保存所有测试项
- /// </summary>
- /// <param name="obj"></param>
- /// <exception cref="NotImplementedException"></exception>
- private void SaveAllSch(object obj)
- {
- //先保存一下当前测试项
-
- //可能调整了测试项的顺序,所以先删除所有测试项
- var findSche = _basicPlcTestSchemeService.FindByName(ScheduleName);
- if (findSche != null)
- {
- //显示设备名称
- DeviceKindName = findSche.devicekind_name;
- long schID = findSche.scheme_id;
- //查找此方案下的所有的测试项,并
- var schDtls = _basicPlcTestSchemeDtlService.QueryList()?.FindAll(x => x.scheme_id == schID);
- //先删除
- foreach (var unit in schDtls)
- {
- int id = Convert.ToInt32(unit.scheme_dtl_id);
- _basicPlcTestSchemeDtlService.Delete(id);
- }
- //按照调整后的顺序添加
- foreach (var item in SchProjectList)
- {
- //测试项目名称
- string schItenName = item.ItemName;
- var dtl = schDtls.FirstOrDefault(x => x.item_name == schItenName);
- if (dtl != null)
- {
- //要将主键改为0,否则还是原来的主键
- dtl.scheme_dtl_id = 0;
- _basicPlcTestSchemeDtlService.Add(dtl);
- }
- }
- MessageBox.Show("保存成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
- //发布消息
- _aggregator.GetEvent<ChangeEvent>().Publish("Change");
- _regionManager.Regions["ContentRegion"].RequestNavigate("BaseConfigView");
- SendLogToDis($"修改测试方案,方案名称为{ScheduleName}");
- }
- }
-
- /// <summary>
- /// 获取测试项
- /// </summary>
- private void GetExistSchs()
- {
- allSchProjectList.Clear();
- SchProjectList.Clear();
- var findSche = _basicPlcTestSchemeService.FindByName(ScheduleName);
- if (findSche != null)
- {
- //显示设备名称
- DeviceKindName = findSche.devicekind_name;
- long schID = findSche.scheme_id;
- //查找此方案下的所有的测试项,并增加到表格
- var schDtls = _basicPlcTestSchemeDtlService.QueryList()?.FindAll(x => x.scheme_id == schID)?.OrderBy(x => x.scheme_dtl_id);
- 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;
- }
- allSchProjectList.Add(new SelectItemModel
- {
- SchDtlId = unit.scheme_dtl_id,
- ItemName = unit.item_name,
- ItemType = itemType,
- BeforeDetail = unit.precondition_describe,
- InDetail = unit.action_describe,
- OutDetail = unit.judgement_result_describe
- });
- }
- foreach (var item in allSchProjectList)
- {
- SchProjectList.Add(item);
- }
- }
- }
- /// <summary>
- /// 下移
- /// </summary>
- /// <param name="obj"></param>
- private void DataDown(object obj)
- {
- int id = Convert.ToInt32(obj);
- var findScheExist = SchProjectList.FirstOrDefault(x => x.SchDtlId == 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.SchDtlId == 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 OnLoad(object obj)
- {
- GetConfigOption();
- }
-
- /// <summary>
- /// 关闭按钮
- /// </summary>
- private void Close()
- {
- //RequestClose?.Invoke(new NavigationResult(new NavigationContext(),true);
- }
-
- /// <summary>
- /// mode转json
- /// </summary>
- /// <param name="itemType"></param>
- /// <param name="itemLogical"></param>
- /// <param name="description"></param>
- /// <param name="conList"></param>
- /// <returns></returns>
- private string ModelToJsonToStr(string itemType, string itemLogical, string description, ObservableCollection<BasPlcItemConfigDto> conList)
- {
- JsonModel prefixJsonModel = new JsonModel();
- prefixJsonModel.ItemType = itemType;
- prefixJsonModel.ItemLogical = itemLogical;
- prefixJsonModel.Description = description;
- prefixJsonModel.DetailInfo = new List<DetailPLC>();
- foreach (var item in conList)
- {
- DetailPLC plcItem = new DetailPLC();
- plcItem.Id = item.Id;
- plcItem.PlcItem = item.PlcItem;
- plcItem.PlcAddress = item.PlcAddress;
- plcItem.PlcAddType = item.PlcAddType;
- plcItem.PlcValue = item.PlcValue;
- plcItem.Remark = item.Remark;
- prefixJsonModel.DetailInfo.Add(plcItem);
- }
- string prefixJsonStr = JsonConvert.SerializeObject(prefixJsonModel);
- return prefixJsonStr;
- }
- /// <summary>
- /// 重置
- /// </summary>
- /// <param name="obj"></param>
- private void ResetMethod(string obj)
- {
-
- SelectTest = "---";
- TestName = string.Empty;
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- //编辑
- var getMsg = navigationContext.Parameters.GetValues<string>("Key");
- ///值不为空,表示修改,为空表示新增
- if (getMsg != null)
- {
- foreach (var item in getMsg)
- {
- try
- {
- Title = "编辑方案";
- ScheduleName = item.ToString();
- //获取已经有的测试项
- GetExistSchs();
-
- }
- catch (Exception ex)
- {
- _logger.LogError(ex.ToString());
- }
- }
- }
- var getMsg2 = navigationContext.Parameters.GetValues<string>("Key2");
- if (getMsg2 != null)
- {
- foreach (var item in getMsg2)
- {
- DeviceKindName = item;
- }
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- #endregion
- #region 命令绑定
- public DelegateCommand CloseCommand { set; get; }
- public DelegateCommand<string> SureCommand { set; get; }
- public DelegateCommand<string> ResetCommand { set; get; }
-
- public DelegateCommand<Object> OnLoadCommand { set; get; }
- public DelegateCommand TxtLostFocusCommand { set; get; }
- public DelegateCommand<string> OpenExistCommand { set; get; }
- public DelegateCommand<object> DownCommand { set; get; }
- public DelegateCommand<object> UpCommand { set; get; }
-
-
- public DelegateCommand<object> CancelCommand { set; get; }
- public DelegateCommand<object> DetailCommand { set; get; }
- public DelegateCommand<object> DeleteCommand { set; get; }
- public DelegateCommand<object> SaveAllSchCommand { set; get; }
- public DelegateCommand<object> QueryCommand { set; get; }
- public DelegateCommand<object> EditCommand { set; get; }
- public DelegateCommand<object> AddItemCommand { set; get; }
-
- #endregion
- #region 变量绑定
- /// <summary>
- /// 测试方案编码
- /// </summary>
- private string scheduleName;
- public string ScheduleName
- {
- get { return scheduleName; }
- set { scheduleName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备类型
- /// </summary>
- private string deviceKindName;
- public string DeviceKindName
- {
- get { return deviceKindName; }
- set { deviceKindName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 测试项名称
- /// </summary>
- private string testName;
- public string TestName
- {
- get { return testName; }
- set { testName = value; RaisePropertyChanged(); }
- }
-
-
- /// <summary>
- /// 测试项类型
- /// </summary>
- private ObservableCollection<string> testKinds = new ObservableCollection<string>();
- public ObservableCollection<string> TestKinds
- {
- get { return testKinds; }
- set { testKinds = value; RaisePropertyChanged(); }
- }
- /// <summary>
- ///
- /// </summary>
- private string selectTest;
- public string SelectTest
- {
- get { return selectTest; }
- set { selectTest = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备类型
- /// </summary>
- private List<string> deviceKindNameList;
- public List<string> DeviceKindNameList
- {
- get { return deviceKindNameList; }
- set { deviceKindNameList = value; RaisePropertyChanged(); }
- }
- private ObservableCollection<SelectItemModel> schProjectList = new ObservableCollection<SelectItemModel>();
- public ObservableCollection<SelectItemModel> SchProjectList
- {
- get { return schProjectList; }
- set { SetProperty(ref schProjectList, value); }
- }
- #endregion
- }
- }
|