EquipmentDataQueueVM.cs 2.62 KB
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.DataAccess;
using HHECS.DAQClient.Model;
using LinqKit;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.DAQClient.ViewModel.EquipmentVM
{
    public partial class EquipmentDataQueueVM : ObservableObject
    {
        [ObservableProperty]
        private string equipmentCode;

        [ObservableProperty]
        private int pageIndex = 1;

        [ObservableProperty]
        private long maxPage;

        [ObservableProperty]
        private int pageSize = 30;

        [ObservableProperty]
        private ObservableCollection<EquipmentDataQueue> dataQueues = new ObservableCollection<EquipmentDataQueue>();

        private readonly DataContext _context;

        public EquipmentDataQueueVM(DataContext context)
        {
            _context = context;
            LoadData();
        }

        [RelayCommand]
        public void Serach()
        {
            var result = LoadData();
            if (!result.Success)
            {
                MessageBox.Error($"加载数据失败.{result.Msg}");
            }
        }

        /// <summary>
        /// 页码改变
        /// </summary>
        [RelayCommand]
        private void PageUpdated(FunctionEventArgs<int> info)
        {
            PageIndex = info.Info;
            LoadData();
        }

        private BllResult LoadData()
        {
            try
            {
                var query = _context.EquipmentDataQueue.Where(GetFilter());
                var total = query.Count();
                MaxPage = total / PageSize + Convert.ToInt32(total % PageSize != 0);
                var result = query.Page(PageIndex, PageSize).ToList();
                DataQueues = new ObservableCollection<EquipmentDataQueue>(result);
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        private Expression<Func<EquipmentDataQueue, bool>> GetFilter()
        {
            var filter = PredicateBuilder.New<EquipmentDataQueue>(true);
            if (!string.IsNullOrWhiteSpace(EquipmentCode))
            {
                filter = filter.And(x => x.EquipmentCode.Contains(EquipmentCode));
            }
            //if (!string.IsNullOrWhiteSpace(EquipmentName))
            //{
            //    filter = filter.And(x => x.Name.Contains(EquipmentName));
            //}
            return filter;
        }
    }
}