using AutoMapper; using BizService; using Microsoft.Extensions.Logging; using MiniExcelLibs; using Model; using Model.Dto; using Model.Entities; using Prism.Commands; 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 BaseConfigViewModel : BindableBase { private readonly IDialogService _dialog; private readonly IOptionConfigService _optionConfigService; private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService; private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService; private readonly IBasicDeviceKindService _iBasicDeviceKindService; private readonly ILogger _logger; private readonly IMapper _mapper; private readonly IMenuService _menuService; private List allConfigList = new List();//所有方案 private List conditionConfig = new List();//符合条件的方案 private const int PageCount = 1; //每一页显示个数 public BaseConfigViewModel(IDialogService dialog, IMenuService menuService, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService , IBasicDeviceKindService iBasicDeviceKindService, IMapper mapper, ILogger logger) { _dialog = dialog; _optionConfigService = optionConfigService; _basicPlcTestSchemeService = basicPlcTestSchemeService; _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService; _iBasicDeviceKindService = iBasicDeviceKindService; _logger = logger; _mapper = mapper; _menuService = menuService; QueryCommand = new DelegateCommand(Query); ResetCommand = new DelegateCommand(Reset); ExportCommand = new DelegateCommand(Export); AddPlanCommand = new DelegateCommand(AddPlan); EditCommand = new DelegateCommand(Edit); DeleteCommand = new DelegateCommand(Delete); CopyCommand = new DelegateCommand(Copy); OnLoadCommand = new DelegateCommand(OnLoad); GetConfigOption(); GetContent(); } #region 私有方法 private void OnLoad() { DeviceKindNameList = _iBasicDeviceKindService.FindAllDeviceKind(); } private void Reset(object obj) { ScheduleName = string.Empty; DeviceKindName = string.Empty; TestName = string.Empty; SelectTest = string.Empty; StartTime = DateTime.MinValue; EndTime = DateTime.MinValue; } /// /// 查询 /// /// private void Query(object obj) { if (StartTime > EndTime) { MessageBox.Show("起始时间大于结束时间,请重新输入", "确认", MessageBoxButton.OK, MessageBoxImage.Warning); return; } //if (Convert.ToDateTime(StartTime).AddDays(2) < Convert.ToDateTime(EndTime)) //{ // MessageBox.Show("起始时间和结束时间间隔不能大于2天,请重新输入", "确认", 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(DeviceKindName) ? true : (a.DeviceKindName == DeviceKindName)) select a).ToList(); //默认显示的第一页 conditionConfig = conditionConfig.OrderBy(x => x.SchemeName).ThenBy(x => x.SchemeId).ToList(); Getpage(); //BaseConfigList = new ObservableCollection(conditionConfig); } /// /// 获取配置 /// private void GetConfigOption() { var configList = _optionConfigService.QueryList(); var _optionConfigs = _mapper.Map, List>(configList); var tests = _optionConfigs.FindAll(x => x.TypeID == 1); TestKinds = new List(); foreach (var test in tests) { TestKinds.Add(test.ContentOption); } } /// /// 编辑 /// /// 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(); } }); } /// /// 删除操作 /// /// private void Delete(object obj) { int id = Convert.ToInt32(obj); MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question); if (boxResult == MessageBoxResult.OK) { //查找测试方案名称的id var schID = _basicPlcTestSchemeDtlService.Find(id)?.scheme_id; _basicPlcTestSchemeDtlService.Delete(id); //如果测试项详细表中没有了这个测试方案,把测试方案表中的方案数据也删除 var findSchDetails=_basicPlcTestSchemeDtlService.FindAllBySchId(schID.Value); //删除测试方案表中的数据 if(findSchDetails.Count==0) { int delId =Convert.ToInt32(schID.Value); _basicPlcTestSchemeService.Delete(delId); } //更新表格,重新获取 GetContent(); } } /// /// 复制方案 /// /// private void Copy(object obj) { //测试方案名称 string schName = obj.ToString(); DialogParameters parm = new DialogParameters(); parm.Add("Key", schName); //弹出详情对话框 _dialog.ShowDialog("CopySchView", parm, async callback => { if (callback.Result == ButtonResult.OK) { //更新表格,重新获取 GetContent(); } }); } /// /// 新增按钮 /// /// private void AddPlan(string obj) { //弹出详情对话框 _dialog.ShowDialog("AddOrEditSchView", async callback => { if (callback.Result == ButtonResult.OK) { //更新表格,重新获取 GetContent(); } }); } /// /// excel导出 /// /// 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 = "配置导出" + string.Format("{0:yyyyMMddHHmm}", DateTime.Now), //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置) 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) { _logger.LogError(ex.ToString()); } } } } /// /// 获取解决方案 /// private void GetContent() { //所有测试方案 var schlist = _basicPlcTestSchemeService.QueryList(); var configList = _mapper.Map, List>(schlist); //清空集合 BaseConfigList.Clear(); allConfigList.Clear(); conditionConfig.Clear(); //查找测试项名称 foreach (var sch in configList) { allConfigList.Add(sch); conditionConfig.Add(sch); conditionConfig = conditionConfig.OrderBy(x => x.DeviceKindName).ThenBy(x => x.SchemeId).ToList(); //默认显示的第一页 Getpage(); } } /// /// 获取页面 /// private void Getpage() { CurrentPage = 1; TotalCount = conditionConfig.Count; CurrentPageChanged(); } /// /// 页面变化 /// private void CurrentPageChanged() { BaseConfigList.Clear(); foreach (var i in conditionConfig.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage)) { BaseConfigList.Add(i); } } #endregion #region 命令绑定 public DelegateCommand EditCommand { set; get; } public DelegateCommand AddPlanCommand { set; get; } public DelegateCommand QueryCommand { set; get; } public DelegateCommand ResetCommand { set; get; } public DelegateCommand DeleteCommand { set; get; } public DelegateCommand CopyCommand { set; get; } public DelegateCommand ExportCommand { set; get; } public DelegateCommand OnLoadCommand { set; get; } #endregion #region 数据绑定 /// /// 测试方案编码 /// private string scheduleName; public string ScheduleName { get { return scheduleName; } set { scheduleName = value; RaisePropertyChanged(); } } /// /// 设备名称 /// private string deviceKindName; public string DeviceKindName { get { return deviceKindName; } set { deviceKindName = value; RaisePropertyChanged(); } } /// /// 设备类型 /// private List deviceKindNameList; public List DeviceKindNameList { get { return deviceKindNameList; } set { deviceKindNameList = value; RaisePropertyChanged(); } } /// /// 测试项名称 /// private string testName; public string TestName { get { return testName; } set { testName = value; RaisePropertyChanged(); } } /// /// 测试项类型 /// private List testKinds = new List(); public List TestKinds { get { return testKinds; } set { testKinds = value; RaisePropertyChanged(); } } /// /// /// 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(); } } /// /// 总条数 /// private int totalCount; public int TotalCount { get { return totalCount; } set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); } } /// /// 每页数量 /// private int countPerPage = 15; public int CountPerPage { get { return countPerPage; } set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); } } /// /// 单前页 /// private int currentPage = 1; public int CurrentPage { get { return currentPage; } set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); } } private ObservableCollection baseConfigList = new ObservableCollection(); public ObservableCollection BaseConfigList { get { return baseConfigList; } set { SetProperty(ref baseConfigList, value); } } #endregion } }