using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Windows; using DM_Weight.Models; using DM_Weight.util; using System.Data; using System.Configuration; using MaterialDesignThemes.Wpf; using System.Windows.Media; namespace DM_Weight.ViewModels { public class RoleManagerWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime { private List? _roleList; public List? RoleList { get { return _roleList; } set { SetProperty(ref _roleList, value); } } //public RoleManagerWindowViewModel() //{ // _allPremissions = Clone(defaultAll); //} public static ObservableCollection Clone(object List) { using (Stream objectStream = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(objectStream, List); objectStream.Seek(0, SeekOrigin.Begin); return formatter.Deserialize(objectStream) as ObservableCollection; } } private RoleDm _role = new RoleDm(); public RoleDm Role { get { return _role; } set { CompareList(value); SetProperty(ref _role, value); } } private void CompareList(RoleDm Role) { //AllPremissions = Clone(defaultAll); //RightPremissions.Clear(); //if (Role != null && Role.Permissions != null) //{ // List Permissions = Role.Permissions; // Permissions.ForEach(it => // { // // 父菜单 // PremissionDm p = AllPremissions.ToList().Find(it2 => it2.Id == it.Id); // int index = AllPremissions.ToList().FindIndex(it2 => it2.Id == it.Id); // if (p.Id != 6) // { // it.Children.ToList().ForEach(it3 => // { // int i =p.Children.ToList().FindIndex(it4 => it4.Id == it3.Id); // if (i > 0 && AllPremissions.ElementAt(index).Children.Count > i) // { // AllPremissions.ElementAt(index).Children.RemoveAt(i); // } // }); // if (p.Children.Count == 0) // { // AllPremissions.RemoveAt(index); // } // } else // { // AllPremissions.RemoveAt(index); // } // }); // RightPremissions = new ObservableCollection(Permissions); //} AllPremissions = Clone(defaultAll); RightPremissions.Clear(); if (Role != null && Role.Permissions != null) { List Permissions = Role.Permissions; Permissions.ForEach(it => { // 父菜单 PremissionDm p = AllPremissions.ToList().Find(it2 => it2.Id == it.Id); int index = AllPremissions.ToList().FindIndex(it2 => it2.Id == it.Id); if (p != null) { if (p != null && p.Children != null && it != null && it.Children != null && p.Children.Count == it.Children.Count) { AllPremissions.RemoveAt(index); } else { if (p.Id != 6) { it.Children.ToList().ForEach(it3 => { int i = p.Children.ToList().FindIndex(it4 => it4.Id == it3.Id); //if (i > 0 && AllPremissions.ElementAt(index).Children.Count > i) if (p.Children.Count > 0 && i >= 0 && p.Children[i] != null) { AllPremissions.ElementAt(index).Children.RemoveAt(i); } }); if (p.Children.Count == 0) { AllPremissions.RemoveAt(index); } } else { AllPremissions.RemoveAt(index); } } } }); RightPremissions = new ObservableCollection(Permissions); } } private string? _searchValue; /// /// 查询条件 查询字段值 /// public string? SearchValue { get { return _searchValue; } set { SetProperty(ref _searchValue, value); RequestData(); } } public static ObservableCollection defaultAll = new ObservableCollection(); static RoleManagerWindowViewModel() { #region 取药菜单 PremissionDm quyao = new PremissionDm { Id = 1, //PremissionName = "取药", PremissionName = "手术间操作", PremissionImage = "/Images/TbQyao.png", }; ObservableCollection quyaoChild = new ObservableCollection(); //PremissionDm quyao1 = new PremissionDm //{ // Id = 11, // PremissionName = "处方取药", // PremissionPath = "OrderTakeDrugWindow", //}; //PremissionDm quyao1 = new PremissionDm //{ // Id = 11, // PremissionName = "处方取药", // PremissionPath = "OrderTakeDrugNewWindow", //}; //开药箱 //PremissionDm openBox = new PremissionDm //{ // Id = 11, // PremissionName = "开药箱", // PremissionPath = "OpenBoxWindow", //}; //开药箱 //PremissionDm openBox = new PremissionDm //{ // Id = 11, // PremissionName = "开药箱", // PremissionPath = "OpenBoxNewWindow", //}; //开药箱核对名下单子 PremissionDm openBoxWithOrder = new PremissionDm { Id = 11, PremissionName = "操作手术间", PremissionPath = "CheckSelfOrderWindow", }; //核对处方 PremissionDm checkOrder = new PremissionDm { Id = 12, PremissionName = "核对处方", //PremissionPath = "CheckOrderWindow", PremissionPath = "CheckOrderNewWindow", }; //核对处方 PremissionDm addition = new PremissionDm { Id = 13, PremissionName = "手术间补药", //PremissionPath = "CheckOrderWindow", PremissionPath = "AdditionWindow", }; //PremissionDm quyao1 = new PremissionDm //{ // Id = 11, // PremissionName = "交接柜补药", // PremissionPath = "AddToJiaoJieWindow", //}; //PremissionDm quyao2 = new PremissionDm //{ // Id = 12, // PremissionName = "调拨出库", // PremissionPath = "InvoiceOutWindow", //}; PremissionDm quyao3 = new PremissionDm { Id = 13, PremissionName = "抽屉取药", PremissionPath = "DrawerTakeDrugWindow", }; PremissionDm quyao4 = new PremissionDm { Id = 14, PremissionName = "自选取药", PremissionPath = "SelfTakeDrugWindow", }; PremissionDm quyao5 = new PremissionDm { Id = 15, PremissionName = "取药记录", PremissionPath = "TakeRecordWindow", }; if (ConfigurationManager.AppSettings["returnDrugMode"].ToString().Equals("1")) { PremissionDm mulityOrder = new PremissionDm { Id = 16, PremissionName = "多处方取药", PremissionPath = "MultiOrderTakeDrugWindow", }; //quyaoChild.Add(mulityOrder); } //quyaoChild.Add(quyao1); //quyaoChild.Add(quyao2); //quyaoChild.Add(quyao3); //quyaoChild.Add(quyao4); //quyaoChild.Add(quyao5); //quyaoChild.Add(openBox); quyaoChild.Add(checkOrder); quyaoChild.Add(addition); quyaoChild.Add(openBoxWithOrder); quyao.Children = quyaoChild; defaultAll.Add(quyao); #endregion #region 加药菜单 PremissionDm jiayao = new PremissionDm { Id = 2, PremissionName = "加药", PremissionImage = "/Images/TbJiay.png", }; ObservableCollection jiayaoChild = new ObservableCollection(); PremissionDm jiayao1 = new PremissionDm { Id = 21, PremissionName = "自选加药", PremissionPath = "SelfAddWindow", }; PremissionDm jiayao2 = new PremissionDm { Id = 22, PremissionName = "调拨入库", PremissionPath = "InvoiceInWindow", }; PremissionDm jiayao3 = new PremissionDm { Id = 23, PremissionName = "抽屉加药", PremissionPath = "DrawerAddDrugWindow", }; PremissionDm jiayao4 = new PremissionDm { Id = 24, PremissionName = "加药记录", PremissionPath = "AddRecordWindow", }; if (ConfigurationManager.AppSettings["MultiBatch"].ToString().Equals("1")) { PremissionDm jiayao5 = new PremissionDm { Id = 25, PremissionName = "多批次抽屉加药", PremissionPath = "AddDrugControl", }; jiayaoChild.Add(jiayao5); } PremissionDm jiayao6 = new PremissionDm { Id = 26, PremissionName = "药品请领", PremissionPath = "CollectDrugWindow", }; PremissionDm jiayao7 = new PremissionDm { Id = 27, PremissionName = "请领列表", PremissionPath = "ApplyListWindow", }; PremissionDm jiayao8 = new PremissionDm { Id = 28, PremissionName = "请领入库", PremissionPath = "ApplyInStockWindow", }; //jiayaoChild.Add(jiayao1); //jiayaoChild.Add(jiayao2); //jiayaoChild.Add(jiayao3); //jiayaoChild.Add(jiayao6); //jiayaoChild.Add(jiayao7); //jiayaoChild.Add(jiayao8); //jiayaoChild.Add(jiayao4); //jiayao.Children = jiayaoChild; //defaultAll.Add(jiayao); #endregion #region 还药菜单 PremissionDm huanyao = new PremissionDm { Id = 3, PremissionName = "还药", PremissionImage = "/Images/TbTuiy.png", }; ObservableCollection huanyaoChild = new ObservableCollection(); PremissionDm huanyao1 = new PremissionDm { Id = 31, PremissionName = "归还药品", PremissionPath = "ReturnDrugWindow", }; PremissionDm huanyao11 = new PremissionDm { Id = 31, PremissionName = "归还药品", PremissionPath = "ReturnDrugWindow2", }; //PremissionDm huanyao2 = new PremissionDm //{ // Id = 32, // PremissionName = "归还空瓶", // PremissionPath = "ReturnEmptyWindow", //}; PremissionDm huanyao2 = new PremissionDm { Id = 32, PremissionName = "空瓶取出", PremissionPath = "ReturnEmptyDestoryWindow", }; PremissionDm huanyao3 = new PremissionDm { Id = 33, PremissionName = "归还记录", PremissionPath = "ReturnRecordWindow", }; //PremissionDm surgery = new PremissionDm //{ // Id = 17, // PremissionName = "手术核销", // PremissionPath = "SurgeryTakeWindow" //}; //huanyaoChild.Add(Convert.ToInt32(ConfigurationManager.AppSettings["returnDrugMode"]) == 1 ? huanyao11 : huanyao1); //huanyaoChild.Add(huanyao2); ////huanyaoChild.Add(surgery); //huanyaoChild.Add(huanyao3); //huanyao.Children = huanyaoChild; //defaultAll.Add(huanyao); #endregion #region 库存管理菜单 //PremissionDm kuguan = new PremissionDm //{ // Id = 4, // PremissionName = "库存管理", // PremissionImage = "/Images/TbKuc.png", //}; //ObservableCollection kuguanChild = new ObservableCollection(); //PremissionDm kuguan1 = new PremissionDm //{ // Id = 41, // PremissionName = "库存列表", // PremissionPath = "StockListWindow", //}; //PremissionDm kuguan2 = new PremissionDm //{ // Id = 42, // PremissionName = "库存盘点", // PremissionPath = "CheckStockWindow", //}; //PremissionDm kuguan3 = new PremissionDm //{ // Id = 43, // PremissionName = "盘点记录", // PremissionPath = "CheckRecordWindow", //}; //PremissionDm kuguan4 = new PremissionDm //{ // Id = 44, // PremissionName = "药品列表", // PremissionPath = "DrugListWindow", //}; //PremissionDm kuguan5 = new PremissionDm //{ // Id = 45, // PremissionName = "交接班记录", // PremissionPath = "ChangeShiftsListWindow", //}; //PremissionDm kuguan6 = new PremissionDm //{ // Id = 46, // PremissionName = "账册", // PremissionPath = "AccountWindow", //}; //kuguanChild.Add(kuguan1); //kuguanChild.Add(kuguan2); //kuguanChild.Add(kuguan4); //kuguanChild.Add(kuguan6); //kuguanChild.Add(kuguan3); //kuguanChild.Add(kuguan5); //kuguan.Children = kuguanChild; //defaultAll.Add(kuguan); #endregion #region 系统设置菜单 PremissionDm sysset = new PremissionDm { Id = 5, PremissionName = "系统设置", PremissionImage = "/Images/TbSet.png", }; ObservableCollection syssetChild = new ObservableCollection(); PremissionDm sysset1 = new PremissionDm { Id = 51, PremissionName = "用户管理", PremissionPath = "UserManagerWindow", }; PremissionDm sysset2 = new PremissionDm { Id = 52, PremissionName = "角色管理", PremissionPath = "RoleManagerWindow", }; PremissionDm sysset3 = new PremissionDm { Id = 53, PremissionName = "绑定药品", PremissionPath = "BindBoxPackageWindow", }; PremissionDm settingBox = new PremissionDm { Id = 54, PremissionName = "手术间设置", PremissionPath = "SettingBoxWindow", }; PremissionDm machineRecord= new PremissionDm { Id = 55, PremissionName = "操作记录", PremissionPath = "MachineRecordWindow", }; PremissionDm wSDRecord= new PremissionDm { Id = 56, PremissionName = "温湿度记录", PremissionPath = "WSDRecordWindow", }; //PremissionDm sysset3; //if (Convert.ToInt32(ConfigurationManager.AppSettings["hasFridge"]) > 0) //{ // //有冰箱,需要冰箱设置(两个冰箱) // sysset3 = new PremissionDm // { // Id = 53, // PremissionName = "设置", // PremissionPath = "SettingMainWindow", // }; //} //else //{ // sysset3 = new PremissionDm // { // Id = 53, // PremissionName = "设置", // PremissionPath = "SettingWindow", // }; //} PremissionDm syssetLogin = new PremissionDm { Id = 57, PremissionName = "登录设置", PremissionPath = "SettingWindow", }; syssetChild.Add(sysset1); syssetChild.Add(sysset2); syssetChild.Add(syssetLogin); syssetChild.Add(settingBox); syssetChild.Add(machineRecord); syssetChild.Add(wSDRecord); syssetChild.Add(sysset3); sysset.Children = syssetChild; defaultAll.Add(sysset); #endregion #region 退出菜单 PremissionDm logout = new PremissionDm { Id = 6, PremissionName = "退出", PremissionImage = "/Images/TbExit.png", }; defaultAll.Add(logout); #endregion } private ObservableCollection _allPremissions = Clone(defaultAll); public ObservableCollection AllPremissions { get => _allPremissions; set => SetProperty(ref _allPremissions, value); } private PremissionDm? _leftPremission; public PremissionDm? LeftPremission { get => _leftPremission; set => SetProperty(ref _leftPremission, value); } private ObservableCollection _rightPremissions = new ObservableCollection(); public ObservableCollection RightPremissions { get => _rightPremissions; set => SetProperty(ref _rightPremissions, value); } private PremissionDm? _rightPremission; public PremissionDm? RightPremission { get => _rightPremission; set => SetProperty(ref _rightPremission, value); } public bool KeepAlive => false; public T DeepCopyByReflect(T obj) { //如果是字符串或值类型则直接返回 if (obj == null || (obj is string) || (obj.GetType().IsValueType)) return obj; object retval = Activator.CreateInstance(obj.GetType()); FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (FieldInfo field in fields) { try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); } catch { } } return (T)retval; } public DelegateCommand Query { get => new DelegateCommand(() => { RequestData(); }); } public DelegateCommand ToRight => new DelegateCommand(() => { // 说明是父菜单 if (LeftPremission.Children != null || LeftPremission.Id == 6) { // 右侧有没有此菜单 int index = RightPremissions.ToList().FindIndex((it) => it.Id == LeftPremission.Id); // 已经存在 if (index > -1) { // 获取右侧中的菜单 PremissionDm item = RightPremissions.ElementAt(index); // 向其中添加子菜单 LeftPremission.Children.ToList().ForEach(it => { int v = it.Id % 10; if (v > item.Children.Count) { item.Children.Add(it); } else { item.Children.Insert(v - 1, it); } }); } else { int v = LeftPremission.Id % 10; if (v > RightPremissions.Count) { RightPremissions.Add(LeftPremission); } else { RightPremissions.Insert(v - 1, LeftPremission); } } AllPremissions.RemoveAt(AllPremissions.ToList().FindIndex(it => it.Id == LeftPremission.Id)); } // 子菜单 else { // 查找所选子菜单的父菜单 PremissionDm item = AllPremissions.ToList().Find(it => (int)(LeftPremission.Id / 10) == it.Id); // 查找父菜单是否在右侧 int index = RightPremissions.ToList().FindIndex(it => it.Id == item.Id); // 存在 if (index > -1) { ObservableCollection a = RightPremissions.ElementAt(index).Children; int v = LeftPremission.Id % 10; if (v > a.Count) { a.Add(LeftPremission); } else { a.Insert(v - 1, LeftPremission); } item.Children.RemoveAt(item.Children.ToList().FindIndex(it => it.Id == LeftPremission.Id)); if (item.Children.Count == 0) { AllPremissions.RemoveAt(AllPremissions.ToList().FindIndex(it => it.Id == LeftPremission.Id)); } } // 不存在 else { // 拷贝 PremissionDm item2 = DeepCopyByReflect(item); ObservableCollection child = new ObservableCollection(); child.Add(LeftPremission); item2.Children = child; int v = item2.Id % 10; if (v > RightPremissions.Count) { RightPremissions.Add(item2); } else { RightPremissions.Insert(v - 1, item2); } item.Children.RemoveAt(item.Children.ToList().FindIndex(it => it.Id == LeftPremission.Id)); } } }); public DelegateCommand LeftSelectedItemChangedCommand { get => new DelegateCommand((PremissionDm _selected) => { LeftPremission = _selected; }); } public DelegateCommand RightSelectedItemChangedCommand { get => new DelegateCommand((PremissionDm _selected) => { RightPremission = _selected; }); } public DelegateCommand ToLeft { get => new DelegateCommand(() => { // 说明是父菜单 if (RightPremission.Children != null || RightPremission.Id == 6) { // 左侧有没有此菜单 int index = AllPremissions.ToList().FindIndex((it) => it.Id == RightPremission.Id); // 已经存在 if (index > -1) { // 获取左侧中的菜单 PremissionDm item = AllPremissions.ElementAt(index); // 向其中添加子菜单 RightPremission.Children.ToList().ForEach(it => { int v = it.Id % 10; if (v > item.Children.Count) { item.Children.Add(it); } else { item.Children.Insert(v - 1, it); } }); } else { int v = RightPremission.Id % 10; if (v > AllPremissions.Count) { AllPremissions.Add(RightPremission); } else { AllPremissions.Insert(v - 1, RightPremission); } } RightPremissions.RemoveAt(RightPremissions.ToList().FindIndex(it => it.Id == RightPremission.Id)); } // 子菜单 else { // 查找所选子菜单的父菜单 PremissionDm item = RightPremissions.ToList().Find(it => (int)(RightPremission.Id / 10) == it.Id); // 查找父菜单是否在左侧 int index = AllPremissions.ToList().FindIndex(it => it.Id == item.Id); // 存在 if (index > -1) { ObservableCollection a = AllPremissions.ElementAt(index).Children; int v = RightPremission.Id % 10; if (v > a.Count) { a.Add(RightPremission); } else { a.Insert(v - 1, RightPremission); } item.Children.RemoveAt(item.Children.ToList().FindIndex(it => it.Id == RightPremission.Id)); if (item.Children.Count == 0) { RightPremissions.RemoveAt(RightPremissions.ToList().FindIndex(it => it.Id == RightPremission.Id)); } } // 不存在 else { // 拷贝 PremissionDm item2 = DeepCopyByReflect(item); ObservableCollection child = new ObservableCollection(); child.Add(RightPremission); item2.Children = child; int v = item2.Id % 10; if (v > AllPremissions.Count) { AllPremissions.Add(item2); } else { AllPremissions.Insert(v - 1, item2); } item.Children.RemoveAt(item.Children.ToList().FindIndex(it => it.Id == RightPremission.Id)); } } }); } public DelegateCommand AllToRight { get => new DelegateCommand(() => { RightPremissions = Clone(defaultAll); AllPremissions.Clear(); }); } public DelegateCommand AllToLeft { get => new DelegateCommand(() => { AllPremissions = Clone(defaultAll); RightPremissions.Clear(); }); } private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3)); public ISnackbarMessageQueue SnackbarMessageQueue { get => _snackbarMessageQueue; set => SetProperty(ref _snackbarMessageQueue, value); } private SolidColorBrush _colorBrush; public SolidColorBrush SnackbarBackground { get => _colorBrush; set => SetProperty(ref _colorBrush, value); } public DelegateCommand AddRole { get => new DelegateCommand(() => { Role.Id = 0; Role.MachineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1"; Role.Permissions = RightPremissions.ToList(); List roleList = SqlSugarHelper.Db.Queryable().Where(r => r.RoleName == Role.RoleName).ToList(); if (roleList.Count > 0) { SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); SnackbarMessageQueue.Enqueue("该角色已存在!"); return; } SqlSugarHelper.Db.Insertable(Role).ExecuteCommand(); RequestData(); }); } public DelegateCommand EditRole { get => new DelegateCommand(() => { Role.Permissions = RightPremissions.ToList(); SqlSugarHelper.Db.Updateable(Role).ExecuteCommand(); RequestData(); }); } //这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作 public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { continuationCallback(true); } //接收导航传过来的参数 public void OnNavigatedTo(NavigationContext navigationContext) { //查询表格数据 RequestData(); } void RequestData() { RoleList = SqlSugarHelper.Db.Queryable() .Where(di => di.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")) .WhereIF(!String.IsNullOrEmpty(SearchValue), (di) => di.RoleName.Contains(SearchValue ?? "")) .Select(r => r) .ToList() ; } //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建 public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } //这个方法用于拦截请求 public void OnNavigatedFrom(NavigationContext navigationContext) { } } }