Files
YuPu-OrpaonEMS/OrpaonEMS.App/ViewModels/BmsViewModel.cs
2025-02-28 22:23:13 +08:00

470 lines
14 KiB
C#

using OrpaonEMS.App.Com;
using OrpaonEMS.App.Models;
using OrpaonEMS.App.Services;
using OrpaonEMS.Core;
using Prism.Commands;
using Prism.Services.Dialogs;
using Syncfusion.UI.Xaml.Controls.DataPager;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace OrpaonEMS.App.ViewModels
{
public class BmsViewModel : NavigationViewModel
{
/// <summary>
/// 弹窗服务
/// </summary>
public IDialogService DialogService { get; }
public BmsViewModel(IDialogService dialogService, BmsDataService bmsDataService)
{
DialogService = dialogService;
//分页的默认值
this.PageIndex = 0;
this.PageSize = 20;
NumericButtonCount = 5;
//分页的默认值
this.ConfigPageIndex = 0;
this.ConfigPageSize = 20;
ConfigNumericButtonCount = 5;
this.bmsDataService = bmsDataService;
ListBmsRoCell = bmsDataService.ListBmsRoCell;
ListBmsRwCell= bmsDataService.ListBmsRwCell.ToList();
ListBmsPageRwCell = bmsDataService.ListBmsRwCell;
//分页数据PageSize下拉框
PageSizeComboBoxList = new List<ComboBoxModel>() {
new ComboBoxModel(){ Key="1",Text="20"},
new ComboBoxModel(){ Key="2",Text="40"},
new ComboBoxModel(){ Key="3",Text="60"}
};
//分页数据PageSize下拉框
ConfigPageSizeComboBoxList = new List<ComboBoxModel>() {
new ComboBoxModel(){ Key="1",Text="20"},
new ComboBoxModel(){ Key="2",Text="40"},
new ComboBoxModel(){ Key="3",Text="60"}
};
this.TotalCount = (int)ListBmsRoCell.Count;
this.PageCount = ((int)Math.Ceiling(this.TotalCount * 1.0 / this.PageSize));
this.ConfigTotalCount = (int)ListBmsRwCell.Count;
this.ConfigPageCount = ((int)Math.Ceiling(this.ConfigTotalCount * 1.0 / this.ConfigPageSize));
LoadGridData(PageIndex, PageSize);
ConfigLoadGridData(ConfigPageIndex, ConfigPageSize);
}
/// <summary>
/// BMS只读实时数据集合
/// </summary>
public List<BMSRoCell> ListBmsRoCell { get; set; }
/// <summary>
/// BMS只读实时数据集合
/// </summary>
public ObservableCollection<BMSRoCell> ListBMSPageRoCell { get; set; } = new ObservableCollection<BMSRoCell>();
/// <summary>
/// BMS读写实时数据集合
/// </summary>
public List<BmsRwCell> ListBmsRwCell { get; set; }
/// <summary>
/// BMS读写实时数据集合
/// </summary>
public ObservableCollection<BmsRwCell> ListBmsPageRwCell { get; set; }
/// <summary>
/// Bms数据服务
/// </summary>
public BmsDataService bmsDataService { get; set; }
#region
private BmsRwCell _BmsSelectedConfig;
/// <summary>
/// 当前选中的
/// </summary>
public BmsRwCell BmsSelectedConfig
{
get { return _BmsSelectedConfig; }
set { _BmsSelectedConfig = value; RaisePropertyChanged(); }
}
private DelegateCommand<BmsRwCell> _ConfigBmsSelectedBtnCmd;
/// <summary>
/// 刷新数据命令
/// </summary>
public DelegateCommand<BmsRwCell> ConfigBmsSelectedBtnCmd
{
set
{
_ConfigBmsSelectedBtnCmd = value;
}
get
{
if (_ConfigBmsSelectedBtnCmd == null)
{
_ConfigBmsSelectedBtnCmd = new DelegateCommand<BmsRwCell>((par) => ConfigBmsSelectedBtnCmdMethod(par));
}
return _ConfigBmsSelectedBtnCmd;
}
}
private void ConfigBmsSelectedBtnCmdMethod(BmsRwCell par)
{
if (par != null)
{
BmsSelectedConfig = par;
//弹窗
DialogService.ShowDialog("DialogBmsConfigView", new DialogParameters() { { "Model", BmsSelectedConfig } }, (par) =>
{
if (par.Result == ButtonResult.OK)
{
//程序名称
var ReturnValue = par.Parameters.GetValue<BmsRwCell>("Model");
//查询是否重复
var IsSuccess= bmsDataService.WriteValue(ReturnValue);
if (!IsSuccess)
{
MessageBox.Show("写入失败!", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
}
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
}
private DelegateCommand<object> _ConfigBmsSelectedChangedCmd;
/// <summary>
/// 刷新数据命令
/// </summary>
public DelegateCommand<object> ConfigBmsSelectedChangedCmd
{
set
{
_ConfigBmsSelectedChangedCmd = value;
}
get
{
if (_ConfigBmsSelectedChangedCmd == null)
{
_ConfigBmsSelectedChangedCmd = new DelegateCommand<object>((str) => ConfigBmsSelectedChangedCmdMethod(str));
}
return _ConfigBmsSelectedChangedCmd;
}
}
/// <summary>
/// 选中行的执行方法
/// </summary>
/// <param name="str"></param>
/// <exception cref="NotImplementedException"></exception>
private void ConfigBmsSelectedChangedCmdMethod(object par)
{
var SelectedData = par as BmsRwCell;
if (SelectedData != null)
{
BmsSelectedConfig = SelectedData;
}
}
#endregion
#region
private List<ComboBoxModel> _PageSizeComboBoxList;
/// <summary>
/// 分页PageSize下拉框列表
/// </summary>
public List<ComboBoxModel> PageSizeComboBoxList
{
get { return _PageSizeComboBoxList; }
set { _PageSizeComboBoxList = value; RaisePropertyChanged(); }
}
private DelegateCommand<OnDemandLoadingEventArgs> _PageIndexChangeCmd;
/// <summary>
/// 编辑命令
/// </summary>
public DelegateCommand<OnDemandLoadingEventArgs> PageIndexChangeCmd
{
get
{
if (_PageIndexChangeCmd == null)
{
_PageIndexChangeCmd = new DelegateCommand<OnDemandLoadingEventArgs>(PageIndexChange);
}
return _PageIndexChangeCmd;
}
}
/// <summary>
/// 分页改变
/// </summary>
/// <param name="args"></param>
private void PageIndexChange(OnDemandLoadingEventArgs args)
{
var pageIndex = 0;
if (args.PageSize != 0)
{
pageIndex = args.StartIndex / args.PageSize;
}
LoadGridData(pageIndex, args.PageSize);
}
/// <summary>
/// 加载Grid数据
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageSize"></param>
private void LoadGridData(int PageIndex, int PageSize)
{
//获取全部数据
var ListData = ListBmsRoCell
.Skip(PageIndex * PageSize)
.Take(PageSize);
this.TotalCount = (int)ListBmsRoCell.Count;
this.PageCount = ((int)Math.Ceiling(this.TotalCount * 1.0 / this.PageSize));
//清除数据
ListBMSPageRoCell.Clear();
//填充VM的列表模型数据
if (ListData != null)
{
ListBMSPageRoCell.AddRange(ListData);
}
}
/// <summary>
/// 当前页索引
/// </summary>
private int _PageIndex;
public int PageIndex
{
get { return _PageIndex; }
set { _PageIndex = value; RaisePropertyChanged(); }
}
/// <summary>
/// 每页显示多少条记录/页面大小
/// </summary>
private int _PageSize;
public int PageSize
{
get { return _PageSize; }
set { _PageSize = value; RaisePropertyChanged(); }
}
/// <summary>
///总条数
/// </summary>
private int _TotalCount;
public int TotalCount
{
get { return _TotalCount; }
set { _TotalCount = value; RaisePropertyChanged(); }
}
///// <summary>
///// 选中页项
///// </summary>
//private object _SelectedPageItem;
//public object SelectedPageItem
//{
// get { return _SelectedPageItem; }
// set { _SelectedPageItem = value; RaisePropertyChanged(); }
//}
/// <summary>
/// 分页控件显示按钮数量
/// </summary>
private int _NumericButtonCount;
public int NumericButtonCount
{
get { return _NumericButtonCount; }
set { _NumericButtonCount = value; RaisePropertyChanged(); }
}
/// <summary>
///总页数
/// </summary>
private int _PageCount;
public int PageCount
{
get { return _PageCount; }
set { _PageCount = value; RaisePropertyChanged(); }
}
#endregion
#region Config表格分页
private List<ComboBoxModel> _ConfigPageSizeComboBoxList;
/// <summary>
/// 分页PageSize下拉框列表
/// </summary>
public List<ComboBoxModel> ConfigPageSizeComboBoxList
{
get { return _ConfigPageSizeComboBoxList; }
set { _ConfigPageSizeComboBoxList = value; RaisePropertyChanged(); }
}
private DelegateCommand<OnDemandLoadingEventArgs> _ConfigPageIndexChangeCmd;
/// <summary>
/// 编辑命令
/// </summary>
public DelegateCommand<OnDemandLoadingEventArgs> ConfigPageIndexChangeCmd
{
get
{
if (_ConfigPageIndexChangeCmd == null)
{
_ConfigPageIndexChangeCmd = new DelegateCommand<OnDemandLoadingEventArgs>(ConfigPageIndexChange);
}
return _ConfigPageIndexChangeCmd;
}
}
/// <summary>
/// 分页改变
/// </summary>
/// <param name="args"></param>
private void ConfigPageIndexChange(OnDemandLoadingEventArgs args)
{
var pageIndex = 0;
if (args.PageSize != 0)
{
pageIndex = args.StartIndex / args.PageSize;
}
ConfigLoadGridData(pageIndex, args.PageSize);
}
/// <summary>
/// 加载Grid数据
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageSize"></param>
private void ConfigLoadGridData(int PageIndex, int PageSize)
{
//获取全部数据
var ListData = ListBmsRwCell
.Skip(PageIndex * PageSize)
.Take(PageSize).ToList();
this.ConfigTotalCount = (int)ListBmsRwCell.Count;
this.ConfigPageCount = ((int)Math.Ceiling(this.ConfigTotalCount * 1.0 / this.ConfigPageSize));
//清除数据
ListBmsRwCell.Clear();
//填充VM的列表模型数据
if (ListData != null)
{
//ListBmsPageRwCell=new ObservableCollection<BmsRwCell> (ListData);
ListBmsRwCell.AddRange(ListData);
}
}
/// <summary>
/// 当前页索引
/// </summary>
private int _ConfigPageIndex;
public int ConfigPageIndex
{
get { return _ConfigPageIndex; }
set { _ConfigPageIndex = value; RaisePropertyChanged(); }
}
/// <summary>
/// 每页显示多少条记录/页面大小
/// </summary>
private int _ConfigPageSize;
public int ConfigPageSize
{
get { return _ConfigPageSize; }
set { _ConfigPageSize = value; RaisePropertyChanged(); }
}
/// <summary>
///总条数
/// </summary>
private int _ConfigTotalCount;
public int ConfigTotalCount
{
get { return _ConfigTotalCount; }
set { _ConfigTotalCount = value; RaisePropertyChanged(); }
}
///// <summary>
///// 选中页项
///// </summary>
//private object _SelectedPageItem;
//public object SelectedPageItem
//{
// get { return _SelectedPageItem; }
// set { _SelectedPageItem = value; RaisePropertyChanged(); }
//}
/// <summary>
/// 分页控件显示按钮数量
/// </summary>
private int _ConfigNumericButtonCount;
public int ConfigNumericButtonCount
{
get { return _ConfigNumericButtonCount; }
set { _ConfigNumericButtonCount = value; RaisePropertyChanged(); }
}
/// <summary>
///总页数
/// </summary>
private int _ConfigPageCount;
public int ConfigPageCount
{
get { return _ConfigPageCount; }
set { _ConfigPageCount = value; RaisePropertyChanged(); }
}
#endregion
}
}