123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- using AutoMapper;
- using BizService;
- using MiniExcelLibs;
- using Model.Dto;
- using Model.Entities;
- using NLog;
- using Prism.Commands;
- using Prism.Mvvm;
- 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.BusinessManageViewModel
- {
- public class TestOperViewModel : BindableBase
- {
- private readonly IDialogService _dialog;
- private readonly IOptionConfigService _optionConfigService;
- private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
- private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
- private readonly ILogger _logger;
- private readonly IMapper _mapper;
- private readonly IMenuService _menuService;
- private List<BasicPlcTestSchemeDto> allConfigList = new List<BasicPlcTestSchemeDto>();//所有方案
- private List<BasicPlcTestSchemeDto> conditionConfig = new List<BasicPlcTestSchemeDto>();//所有方案
- private const int PageCount = 1; //每一页显示个数
- public TestOperViewModel(IDialogService dialog, IMenuService menuService, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IMapper mapper)
- {
- _dialog = dialog;
- _optionConfigService = optionConfigService;
- _basicPlcTestSchemeService = basicPlcTestSchemeService;
- _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
- //_logger = logger;
- _mapper = mapper;
- _menuService = menuService;
- QueryCommand = new DelegateCommand<object>(Query);
- ResetCommand = new DelegateCommand<object>(Reset);
- ExportCommand = new DelegateCommand<string>(Export);
-
- EditCommand = new DelegateCommand<object>(Edit);
- DeleteCommand = new DelegateCommand<object>(Delete);
- //手动 自动测试
- AutoTestCommand = new DelegateCommand<object>(AutoDest);
- ManualTestCommand = new DelegateCommand<object>(ManualTest);
- OnLoadCommand = new DelegateCommand(OnLoad);
- GetConfigOption();
-
- }
- #region 私有方法
- private void OnLoad()
- {
- GetContent();
- }
- private void ManualTest(object obj)
- {
- //测试方案明细主键ID
- long id = Convert.ToInt64(obj);
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", id);
- //弹出详情对话框
- //弹出详情对话框
- _dialog.ShowDialog("ManualTestView", parm, async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- //更新表格,重新获取
- GetContent();
- }
- });
- }
- /// <summary>
- /// 自动测试
- /// </summary>
- /// <param name="obj"></param>
- private void AutoDest(object obj)
- {
- //测试方案明细主键ID
- long id = Convert.ToInt64(obj);
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", id);
- //弹出详情对话框
- //弹出详情对话框
- _dialog.ShowDialog("AutoTestView",parm, async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- //更新表格,重新获取
- GetContent();
- }
- });
- }
- /// <summary>
- /// 重置
- /// </summary>
- /// <param name="obj"></param>
- private void Reset(object obj)
- {
- ScheduleName = string.Empty;
- DeviceName = string.Empty;
- TestName = string.Empty;
- SelectTest = string.Empty;
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="obj"></param>
- private void Query(object obj)
- {
- if (StartTime > EndTime)
- {
- MessageBox.Show("起始时间大于结束时间,请重新输入", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- conditionConfig = (from a in allConfigList
- where (EndTime == DateTime.MinValue ? true : (a.CreateTime < Convert.ToDateTime(EndTime)) && (Convert.ToDateTime(StartTime) < a.CreateTime))
- && (string.IsNullOrEmpty(ScheduleName) ? true : (a.SchemeName == ScheduleName))
- && (string.IsNullOrEmpty(TestName) ? true : (a.ItemName == TestName))
- && (string.IsNullOrEmpty(DeviceName) ? true : (a.DeviceName == DeviceName))
- && (string.IsNullOrEmpty(SelectTest) ? true : (a.ItemType == SelectTest))
- select a).ToList();
- //默认显示的第一页
- Getpage();
- //BaseConfigList = new ObservableCollection<BasicPlcTestSchemeDto>(conditionConfig);
- }
- /// <summary>
- /// 获取配置
- /// </summary>
- private void GetConfigOption()
- {
- var configList = _optionConfigService.QueryList();
- var _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
- var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
- TestKinds = new List<string>();
- foreach (var test in tests)
- {
- TestKinds.Add(test.ContentOption);
- }
- }
- /// <summary>
- /// 编辑
- /// </summary>
- /// <param name="obj"></param>
- private void Edit(object obj)
- {
- //测试方案明细主键ID
- long id = Convert.ToInt64(obj);
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", id);
- //弹出详情对话框
- _dialog.ShowDialog("AddOrEditSchView", parm, async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- //更新表格,重新获取
- GetContent();
- }
- });
- }
- /// <summary>
- /// 删除操作
- /// </summary>
- /// <param name="obj"></param>
- private void Delete(object obj)
- {
- int id = Convert.ToInt32(obj);
- MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
- if (boxResult == MessageBoxResult.OK)
- {
- _basicPlcTestSchemeDtlService.Delete(id);
- //更新表格,重新获取
- GetContent();
- }
- }
-
- /// <summary>
- /// excel导出
- /// </summary>
- /// <param name="obj"></param>
- private void Export(object obj)
- {
- using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
- {
- //设置文件类型
- //书写规则例如:txt files(*.txt)|*.txt
- Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
- //设置默认文件名(可以不设置)
- FileName = "配置导出",
- //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
- AddExtension = true,
- //保存对话框是否记忆上次打开的目录
- RestoreDirectory = true
- })
- {
- // Show save file dialog box
- //点了保存按钮进入
- if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- try
- {
- //获得文件路径
- string localFilePath = saveFileDialog.FileName.ToString();
- //string filePathWithName = localFilePath + ".xlsx";
- //获取文件内容
- MiniExcel.SaveAs(localFilePath, BaseConfigList);
- }
- catch (Exception ex)
- {
- }
- }
- }
- }
- /// <summary>
- /// 获取解决方案
- /// </summary>
- private void GetContent()
- {
- //所有测试方案
- var schlist = _basicPlcTestSchemeService.QueryList();
- var configList = _mapper.Map<List<bas_plc_test_scheme>, List<BasicPlcTestSchemeDto>>(schlist);
- //清空集合
- BaseConfigList.Clear();
- allConfigList.Clear();
- conditionConfig.Clear();
- //查找测试项名称
- foreach (var sch in configList)
- {
- long schId = sch.SchemeId;
- //在解决方案详细表中查找所有的测试项
- var items = _basicPlcTestSchemeDtlService.FindAllBySchId(schId);
- foreach (var item in items)
- {
- BasicPlcTestSchemeDto basicPlcTestSchemeDto = new BasicPlcTestSchemeDto();
- basicPlcTestSchemeDto.SchemeId = item.scheme_dtl_id;
- basicPlcTestSchemeDto.SchemeName = sch.SchemeName;
- basicPlcTestSchemeDto.DeviceName = sch.DeviceName;
- switch (item.item_type)
- {
- case 0:
- basicPlcTestSchemeDto.ItemType = "前置项";
- break;
- case 1:
- basicPlcTestSchemeDto.ItemType = "PLC点位测试项";
- break;
- case 2:
- basicPlcTestSchemeDto.ItemType = "Robot动作测试";
- break;
- }
- basicPlcTestSchemeDto.ItemName = item.item_name;
- basicPlcTestSchemeDto.CreateBy = sch.CreateBy;
- basicPlcTestSchemeDto.CreateTime = sch.CreateTime;
- allConfigList.Add(basicPlcTestSchemeDto);
- conditionConfig.Add(basicPlcTestSchemeDto);
- }
- //默认显示的第一页
- Getpage();
- }
- }
- /// <summary>
- /// 获取页面
- /// </summary>
- private void Getpage()
- {
- CurrentPage = 1;
- TotalCount = conditionConfig.Count;
- CurrentPageChanged();
- }
- /// <summary>
- /// 页面变化
- /// </summary>
- private void CurrentPageChanged()
- {
- BaseConfigList.Clear();
- foreach (var i in conditionConfig.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
- {
- BaseConfigList.Add(i);
- }
- }
- #endregion
- #region 命令绑定
- public DelegateCommand<object> EditCommand { set; get; }
- public DelegateCommand<string> AddPlanCommand { set; get; }
- public DelegateCommand<object> QueryCommand { set; get; }
- public DelegateCommand<object> ResetCommand { set; get; }
- public DelegateCommand<object> DeleteCommand { set; get; }
- public DelegateCommand<string> ExportCommand { set; get; }
- public DelegateCommand PageUpdatedCmd { set; get; }
- public DelegateCommand<object> AutoTestCommand { set; get; }
- public DelegateCommand<object> ManualTestCommand { set; get; }
- public DelegateCommand OnLoadCommand { 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(); }
- }
- /// <summary>
- /// 测试项名称
- /// </summary>
- private string testName;
- public string TestName
- {
- get { return testName; }
- set { testName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 测试项类型
- /// </summary>
- private List<string> testKinds = new List<string>();
- public List<string> TestKinds
- {
- get { return testKinds; }
- set { testKinds = value; RaisePropertyChanged(); }
- }
- /// <summary>
- ///
- /// </summary>
- private string selectTest;
- public string SelectTest
- {
- get { return selectTest; }
- set { selectTest = value; RaisePropertyChanged(); }
- }
- private DateTime startTime = DateTime.Now;
- public DateTime StartTime
- {
- get { return startTime; }
- set { startTime = value; RaisePropertyChanged(); }
- }
- private DateTime endTime = DateTime.Now;
- public DateTime EndTime
- {
- get { return endTime; }
- set { endTime = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 总条数
- /// </summary>
- private int totalCount;
- public int TotalCount
- {
- get { return totalCount; }
- set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- /// <summary>
- /// 每页数量
- /// </summary>
- private int countPerPage = 10;
- public int CountPerPage
- {
- get { return countPerPage; }
- set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- /// <summary>
- /// 单前页
- /// </summary>
- private int currentPage = 1;
- public int CurrentPage
- {
- get { return currentPage; }
- set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- private ObservableCollection<BasicPlcTestSchemeDto> baseConfigList = new ObservableCollection<BasicPlcTestSchemeDto>();
- public ObservableCollection<BasicPlcTestSchemeDto> BaseConfigList
- {
- get { return baseConfigList; }
- set { SetProperty(ref baseConfigList, value); }
- }
- #endregion
- }
- }
|