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 { /// /// 弹窗服务 /// 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() { new ComboBoxModel(){ Key="1",Text="20"}, new ComboBoxModel(){ Key="2",Text="40"}, new ComboBoxModel(){ Key="3",Text="60"} }; //分页数据PageSize下拉框 ConfigPageSizeComboBoxList = new List() { 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); } /// /// BMS只读实时数据集合 /// public List ListBmsRoCell { get; set; } /// /// BMS只读实时数据集合 /// public ObservableCollection ListBMSPageRoCell { get; set; } = new ObservableCollection(); /// /// BMS读写实时数据集合 /// public List ListBmsRwCell { get; set; } /// /// BMS读写实时数据集合 /// public ObservableCollection ListBmsPageRwCell { get; set; } /// /// Bms数据服务 /// public BmsDataService bmsDataService { get; set; } #region 配置值弹窗 private BmsRwCell _BmsSelectedConfig; /// /// 当前选中的 /// public BmsRwCell BmsSelectedConfig { get { return _BmsSelectedConfig; } set { _BmsSelectedConfig = value; RaisePropertyChanged(); } } private DelegateCommand _ConfigBmsSelectedBtnCmd; /// /// 刷新数据命令 /// public DelegateCommand ConfigBmsSelectedBtnCmd { set { _ConfigBmsSelectedBtnCmd = value; } get { if (_ConfigBmsSelectedBtnCmd == null) { _ConfigBmsSelectedBtnCmd = new DelegateCommand((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("Model"); //查询是否重复 var IsSuccess= bmsDataService.WriteValue(ReturnValue); if (!IsSuccess) { MessageBox.Show("写入失败!", "提示", MessageBoxButton.OK, MessageBoxImage.Hand); } } else if (par.Result == ButtonResult.Cancel) { //取消 } }); } } private DelegateCommand _ConfigBmsSelectedChangedCmd; /// /// 刷新数据命令 /// public DelegateCommand ConfigBmsSelectedChangedCmd { set { _ConfigBmsSelectedChangedCmd = value; } get { if (_ConfigBmsSelectedChangedCmd == null) { _ConfigBmsSelectedChangedCmd = new DelegateCommand((str) => ConfigBmsSelectedChangedCmdMethod(str)); } return _ConfigBmsSelectedChangedCmd; } } /// /// 选中行的执行方法 /// /// /// private void ConfigBmsSelectedChangedCmdMethod(object par) { var SelectedData = par as BmsRwCell; if (SelectedData != null) { BmsSelectedConfig = SelectedData; } } #endregion #region 表格分页 private List _PageSizeComboBoxList; /// /// 分页PageSize下拉框列表 /// public List PageSizeComboBoxList { get { return _PageSizeComboBoxList; } set { _PageSizeComboBoxList = value; RaisePropertyChanged(); } } private DelegateCommand _PageIndexChangeCmd; /// /// 编辑命令 /// public DelegateCommand PageIndexChangeCmd { get { if (_PageIndexChangeCmd == null) { _PageIndexChangeCmd = new DelegateCommand(PageIndexChange); } return _PageIndexChangeCmd; } } /// /// 分页改变 /// /// private void PageIndexChange(OnDemandLoadingEventArgs args) { var pageIndex = 0; if (args.PageSize != 0) { pageIndex = args.StartIndex / args.PageSize; } LoadGridData(pageIndex, args.PageSize); } /// /// 加载Grid数据 /// /// /// 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); } } /// /// 当前页索引 /// private int _PageIndex; public int PageIndex { get { return _PageIndex; } set { _PageIndex = value; RaisePropertyChanged(); } } /// /// 每页显示多少条记录/页面大小 /// private int _PageSize; public int PageSize { get { return _PageSize; } set { _PageSize = value; RaisePropertyChanged(); } } /// ///总条数 /// private int _TotalCount; public int TotalCount { get { return _TotalCount; } set { _TotalCount = value; RaisePropertyChanged(); } } ///// ///// 选中页项 ///// //private object _SelectedPageItem; //public object SelectedPageItem //{ // get { return _SelectedPageItem; } // set { _SelectedPageItem = value; RaisePropertyChanged(); } //} /// /// 分页控件显示按钮数量 /// private int _NumericButtonCount; public int NumericButtonCount { get { return _NumericButtonCount; } set { _NumericButtonCount = value; RaisePropertyChanged(); } } /// ///总页数 /// private int _PageCount; public int PageCount { get { return _PageCount; } set { _PageCount = value; RaisePropertyChanged(); } } #endregion #region Config表格分页 private List _ConfigPageSizeComboBoxList; /// /// 分页PageSize下拉框列表 /// public List ConfigPageSizeComboBoxList { get { return _ConfigPageSizeComboBoxList; } set { _ConfigPageSizeComboBoxList = value; RaisePropertyChanged(); } } private DelegateCommand _ConfigPageIndexChangeCmd; /// /// 编辑命令 /// public DelegateCommand ConfigPageIndexChangeCmd { get { if (_ConfigPageIndexChangeCmd == null) { _ConfigPageIndexChangeCmd = new DelegateCommand(ConfigPageIndexChange); } return _ConfigPageIndexChangeCmd; } } /// /// 分页改变 /// /// private void ConfigPageIndexChange(OnDemandLoadingEventArgs args) { var pageIndex = 0; if (args.PageSize != 0) { pageIndex = args.StartIndex / args.PageSize; } ConfigLoadGridData(pageIndex, args.PageSize); } /// /// 加载Grid数据 /// /// /// 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 (ListData); ListBmsRwCell.AddRange(ListData); } } /// /// 当前页索引 /// private int _ConfigPageIndex; public int ConfigPageIndex { get { return _ConfigPageIndex; } set { _ConfigPageIndex = value; RaisePropertyChanged(); } } /// /// 每页显示多少条记录/页面大小 /// private int _ConfigPageSize; public int ConfigPageSize { get { return _ConfigPageSize; } set { _ConfigPageSize = value; RaisePropertyChanged(); } } /// ///总条数 /// private int _ConfigTotalCount; public int ConfigTotalCount { get { return _ConfigTotalCount; } set { _ConfigTotalCount = value; RaisePropertyChanged(); } } ///// ///// 选中页项 ///// //private object _SelectedPageItem; //public object SelectedPageItem //{ // get { return _SelectedPageItem; } // set { _SelectedPageItem = value; RaisePropertyChanged(); } //} /// /// 分页控件显示按钮数量 /// private int _ConfigNumericButtonCount; public int ConfigNumericButtonCount { get { return _ConfigNumericButtonCount; } set { _ConfigNumericButtonCount = value; RaisePropertyChanged(); } } /// ///总页数 /// private int _ConfigPageCount; public int ConfigPageCount { get { return _ConfigPageCount; } set { _ConfigPageCount = value; RaisePropertyChanged(); } } #endregion } }