CommunicationVM.cs 5.72 KB
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.Model;
using HHECS.DAQClient.View.CommunicationView;
using LinqKit;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Windows;
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 Dictionary<string, CommunicationTypeConst?> communicationTypes = new Dictionary<string, CommunicationTypeConst?>();

        [ObservableProperty]
        private CommunicationTypeConst? communicationType;

        [ObservableProperty]
        private int pageIndex = 1;

        [ObservableProperty]
        private long maxPage;

        [ObservableProperty]
        private int pageSize = 30;

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

        private readonly IFreeSql _freeSql;

        public CommunicationVM(IFreeSql freeSql)
        {
            _freeSql = freeSql;
            InitialData();
            LoadData();
        }

        private void InitialData()
        {
            var keyValuePairs = new Dictionary<string, CommunicationTypeConst?>()
            {
                { "全部", null },
            };

            foreach (var item in Enum.GetNames<CommunicationTypeConst>())
            {
                keyValuePairs.Add(item, Enum.Parse<CommunicationTypeConst>(item));
            }
            CommunicationTypes = keyValuePairs;
        }

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

        [RelayCommand]
        public void Add()
        {
            var view = new CommunicationAddOrEditView();
            var result = view.ShowDialog();
            if (result == true)
            {
                LoadData();
            }
        }

        [RelayCommand]
        public void Edit(int communicationId)
        {
            var vm = new CommunicationAddOrEditView(communicationId);
            var result = vm.ShowDialog();
            if (result == true)
            {
                LoadData();
            }
        }

        [RelayCommand]
        public void Delete(int communicationId)
        {
            try
            {
                var result = MessageBox.Ask($"确认删除数据?");
                if (result != MessageBoxResult.OK)
                {
                    return;
                }
                _freeSql.Delete<CommunicationConfig>().Where(x => x.Id == communicationId).ExecuteAffrows();
                LoadData();
                MessageBox.Success("删除数据成功");
            }
            catch (Exception ex)
            {
                MessageBox.Error($"删除数据失败.{ex.Message}");
            }
        }

        [RelayCommand]
        public void BatchDelete(IList sender)
        {
            try
            {
                var temps = sender.Cast<CommunicationConfig>().ToList();
                if (temps.Count == 0)
                {
                    MessageBox.Warning("未选中任何数据!");
                    return;
                }
                var result = MessageBox.Ask($"确认删除选中数据?");
                if (result == MessageBoxResult.OK)
                {
                    return;
                }
                var communicationIds = temps.Select(x => x.Id).ToList();
                _freeSql.Delete<CommunicationConfig>().Where(x => communicationIds.Contains(x.Id)).ExecuteAffrows();
                LoadData();
                MessageBox.Success($"成功删除{temps.Count}条数据");
            }
            catch (Exception ex)
            {
                MessageBox.Error($"删除失败.{ex.Message}");
            }
        }

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

        private BllResult LoadData()
        {
            try
            {
                var query = _freeSql.Queryable<CommunicationConfig>().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));
            }
            if (CommunicationType != null)
            {
                filter = filter.And(x => x.CommunicationType.Equals(CommunicationType));
            }
            return filter;
        }
    }
}