EquipmentVM.cs 6.89 KB
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.DataAccess;
using HHECS.DAQClient.View.EquipmentPropView;
using HHECS.DAQClient.View.EquipmentView;
using HHECS.EquipmentModel;
using LinqKit;
using System.Collections;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq.Expressions;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

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

        [ObservableProperty]
        private string equipmentName;

        [ObservableProperty]
        private Dictionary<string, int> equipmentTypes = new Dictionary<string, int>();

        [ObservableProperty]
        private string destinationArea;

        [ObservableProperty]
        private int equipmentTypeId = 0;

        [ObservableProperty]
        private int pageIndex = 1;

        [ObservableProperty]
        private long maxPage;

        [ObservableProperty]
        private int pageSize = 30;

        [ObservableProperty]
        private ObservableCollection<Equipment> equipments = new ObservableCollection<Equipment>();

        private readonly DataContext _context;

        public EquipmentVM(DataContext context)
        {
            _context = context;
            InitialData();
            LoadData();
        }

        private void InitialData()
        {
            try
            {
                var result = _context.EquipmentType.Where(x => true).ToList();
                var keyValuePairs = new Dictionary<string, int>
                {
                    { "全部",0 }
                };
                foreach (var item in result)
                {
                    keyValuePairs.Add(item.Name, item.Id);
                }
                EquipmentTypes = keyValuePairs;
                DestinationArea = ConfigurationManager.AppSettings["Area"];
            }
            catch (Exception ex)
            {
                MessageBox.Error($"加载数据失败.{ex.Message}");
            }
        }

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

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

        [RelayCommand]
        public void Edit(int equipmentId)
        {
            var view = new EquipmentAddOrEditView(equipmentId);
            var result = view.ShowDialog();
            if (result == true)
            {
                LoadData();
            }
        }

        [RelayCommand]
        public void Delete(int equipmentId)
        {
            try
            {
                var result = MessageBox.Ask($"确认删除数据?");
                if (result != MessageBoxResult.OK)
                {
                    return;
                }
                _context.Equipment.Remove(new Equipment { Id = equipmentId });
                var propIds = _context.EquipmentProp.Where(x => x.EquipmentId == equipmentId).ToList(x => x.Id);
                var props = propIds.Select(x => new EquipmentProp { Id = x }).ToList();
                _context.EquipmentProp.RemoveRange(props);
                _context.SaveChanges();
                LoadData();
                MessageBox.Success("删除数据成功");
            }
            catch (Exception ex)
            {
                MessageBox.Error($"删除数据失败.{ex.Message}");
            }
        }

        [RelayCommand]
        public void Detail(Equipment equipment)
        {
            var view = new EquipmentPropMonitor(equipment);
            view.ShowDialog();
        }

        [RelayCommand]
        public void BatchDelete(IList sender)
        {
            try
            {
                var equipments = sender.Cast<Equipment>().ToList();
                if (equipments.Count == 0)
                {
                    MessageBox.Warning("未选中任何数据!");
                    return;
                }

                var result = MessageBox.Ask($"确认删除选中数据?");
                if (result != MessageBoxResult.OK)
                {
                    return;
                }

                var equipmentIds = equipments.Select(x => x.Id).ToList();
                var equipmentProps = _context.EquipmentProp.Where(x => equipmentIds.Contains(x.EquipmentId)).ToList(x => new EquipmentProp
                {
                    Id = x.Id
                });

                _context.Equipment.RemoveRange(equipments);
                _context.EquipmentProp.RemoveRange(equipmentProps);
                _context.SaveChanges();
                LoadData();
                MessageBox.Success($"成功删除{equipments.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 = _context.Equipment.Where(GetFilter()).Include(x => x.EquipmentType);
                var total = query.Count();
                MaxPage = total / PageSize + Convert.ToInt32(total % PageSize != 0);
                var result = query.Page(PageIndex, PageSize).ToList();
                Equipments = new ObservableCollection<Equipment>(result);
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        private Expression<Func<Equipment, bool>> GetFilter()
        {
            var filter = PredicateBuilder.New<Equipment>(true);
            if (!string.IsNullOrWhiteSpace(EquipmentCode))
            {
                filter = filter.And(x => x.Code.Contains(EquipmentCode));
            }
            if (!string.IsNullOrWhiteSpace(EquipmentName))
            {
                filter = filter.And(x => x.Name.Contains(EquipmentName));
            }
            if (EquipmentTypeId != 0)
            {
                filter = filter.And(x => x.EquipmentTypeId == EquipmentTypeId);
            }
            if (!string.IsNullOrWhiteSpace(DestinationArea))
            {
                filter = filter.And(x => x.DestinationArea == DestinationArea);
            }

            return filter;
        }
    }
}