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

namespace HHECS.DAQClient.ViewModel.CommunicationVM
{
    public partial class CommunicationVM : ObservableObject
    {
        [ObservableProperty]
        private string code;

        [ObservableProperty]
        private string name;

        [ObservableProperty]
        private int pageIndex = 1;

        [ObservableProperty]
        private long maxPage;

        [ObservableProperty]
        private int pageSize = 30;

        [ObservableProperty]
        private ObservableCollection<CommunicationConfig> communicationConfigs = new ObservableCollection<CommunicationConfig>();

        private readonly DataContext _context;

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

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

        [RelayCommand]
        public static void Add()
        {
            var view = new CommunicationAddView();
            view.ShowDialog();
        }

        [RelayCommand]
        public void Edit()
        {
        }

        [RelayCommand]
        public void Delete()
        {

        }

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

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

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