using EC.Entity.CameraInfo; using EC.Service.CameraInfo; using ECMonitor.Code.Models; using ECMonitor.MVVM; using Prism.Commands; using Prism.Events; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading; using System.Windows.Controls; namespace ECMonitor.Page.SystemSet { public class ManageNvrViewModel : BaseModel { private ManageNvr ThatPage { get; set; } public ObservableCollection InfoList { get; set; } = new(); private ReaderWriterLockSlim InfoListrwl { get; } = new(); private List AddNumberList { get; } = new(); private List UpdateNumberList { get; } = new(); public ObservableCollection NvrDeviceList { get; set; } = new(); #region Command public DelegateCommand LoadedCommand { get; private set; } public DelegateCommand LoadedCommand2 { get; private set; } public DelegateCommand AddCommand { get; private set; } public DelegateCommand DeleteCommand { get; private set; } public ExtendCommand UpdateCommand { get; private set; } public DelegateCommand SaveCommand { get; private set; } public DelegateCommand RevokeCommand { get; private set; } public DelegateCommand RefreshCommand { get; private set; } public DelegateCommand ImportCommand { get; private set; } public DelegateCommand ExportCommand { get; private set; } #endregion Command public ManageNvrViewModel(IRegionManager regionManager, IEventAggregator ea) : base(regionManager, ea) { LoadedCommand = new DelegateCommand(LoadedPageAction); AddCommand = new DelegateCommand(AddInfoAction); DeleteCommand = new DelegateCommand(DeleteInfoAction); UpdateCommand = new ExtendCommand(UpdateInfoAction); SaveCommand = new DelegateCommand(SaveInfosAction); RevokeCommand = new DelegateCommand(RevokeInfoAction); RefreshCommand = new DelegateCommand(RefreshAction); ImportCommand = new DelegateCommand(ImportInfosAction); ExportCommand = new DelegateCommand(ExportInfosAction); } #region Event Action private void LoadedPageAction(UserControl view) { if (ThatPage != null) { return; } ThatPage = (ManageNvr)view; RefreshAction(); } #endregion Event Action #region Operate Action private void AddInfoAction() { NvrInfo info = new(); info.Number = GetInfoNewNumber(); info.NvrName = $"录像机#{info.Number}"; info.NvrEnableBool = true; OperateInfoListByLock(() => { AddInfo(info); }); DataGrid infoGrid = ThatPage.InfoGrid; infoGrid.SelectedIndex = infoGrid.Items.Count - 1; } private async void DeleteInfoAction() { DataGrid infoGrid = ThatPage.InfoGrid; NvrInfo info = (NvrInfo)infoGrid.SelectedItem; int index = infoGrid.SelectedIndex; if (info == null) { return; } if (info.NvrId <= 0) { OperateInfoListByLock(() => { RemoveInfo(info); }); infoGrid.SelectedIndex = (index < infoGrid.Items.Count) ? index : infoGrid.Items.Count - 1; return; } int ret = 0; using IpcInfoContext ctx1 = new(); List ipcInfoList = await ctx1.GetListAsync(); ipcInfoList.ForEach(ipc => { if (ipc.NvrName.Equals(info.NvrName)) { ctx1.Remove(ipc); } }); ret = await ctx1.SaveChangesAsync(); if (ret > 0) { using NvrInfoContext ctx2 = new(); _ = ctx2.Remove(info); ret = await ctx2.SaveChangesAsync(); if (ret > 0) { OperateInfoListByLock(() => { RemoveInfo(info); }); infoGrid.SelectedIndex = (index < infoGrid.Items.Count) ? index : infoGrid.Items.Count - 1; } } } private void UpdateInfoAction(DataGridCellEditEndingEventArgs e) { if (e.EditAction != DataGridEditAction.Commit) { return; } var elm = e.EditingElement; DataGridRow row = e.Row; DataGridColumn column = e.Column; NvrInfo info = (NvrInfo)row.Item; if (info != null && info.NvrId > 0) { if (!UpdateNumberList.Exists(item => item.Equals(info.Number))) { UpdateNumberList.Add(info.Number); } } } private async void SaveInfosAction() { //ValidationError validationError = WPFAssist.GetDataGridRowsFirstError(ThatPage.InfoGrid); //if (WPFAssist.GetDataGridRowsHasError(ThatPage.InfoGrid)) { return; } List list = null; using NvrInfoContext ctx = new(); int ret = 0; if (AddNumberList.Count > 0) { list ??= GetInfoList(); foreach (int number in AddNumberList) { NvrInfo info = list.Find(item => item.Number.Equals(number)); if (info != null) { _ = ctx.Add(info); } } ret = await ctx.SaveChangesAsync(); if (ret > 0) { AddNumberList.Clear(); } else { return; } } if (UpdateNumberList.Count > 0) { list ??= GetInfoList(); foreach (int number in UpdateNumberList) { NvrInfo info = list.Find(item => item.Number.Equals(number)); if (info != null) { _ = ctx.Update(info); } } ret = await ctx.SaveChangesAsync(); if (ret > 0) { UpdateNumberList.Clear(); } else { return; } } RefreshAction(); } private void RevokeInfoAction() { } private void RefreshAction() { OperateInfoListByLock(() => { AddNumberList.Clear(); UpdateNumberList.Clear(); LoadNvrDeviceList(); LoadInfoList(); }); } private void ImportInfosAction() { } private void ExportInfosAction() { } #endregion Operate Action #region Operate InfoList private void OperateInfoListByLock(Action action) { if (InfoListrwl.IsWriteLockHeld) { return; } if (!InfoListrwl.TryEnterWriteLock(100)) { return; } try { action?.Invoke(); } finally { InfoListrwl.ExitWriteLock(); } } private async void LoadInfoList() { InfoList.Clear(); using NvrInfoContext ctx = new(); List list = await ctx.GetListAsync(); int number = 0; foreach (NvrInfo info in list) { info.Number = ++number; InfoList.Add(info); } } private void AddInfo(NvrInfo info) { InfoList.Add(info); if (!AddNumberList.Exists(item => item.Equals(info.Number))) { AddNumberList.Add(info.Number); } } private void RemoveInfo(NvrInfo info) { _ = InfoList.Remove(info); _ = AddNumberList.RemoveAll(item => item.Equals(info.Number)); _ = UpdateNumberList.RemoveAll(item => item.Equals(info.Number)); } private List GetInfoList() { return new(InfoList); } private int GetInfoNewNumber() { int number = InfoList.Count > 0 ? InfoList[^1].Number + 1 : 1; return number; } #endregion Operate InfoList #region Operate OtherList private void LoadNvrDeviceList() { NvrDeviceList.Clear(); using NvrInfoContext ctx = new(); List list = ctx.GetNvrDeviceList(); list.ForEach(item => { NvrDeviceList.Add(item); }); } #endregion Operate OtherList } }