Beispiel #1
0
        private bool AddRelationStoreModule(string IDs, string ModuleIDs, int createUserID = 0)
        {
            if (string.IsNullOrWhiteSpace(IDs) || string.IsNullOrWhiteSpace(ModuleIDs))
            {
                return(false);
            }

            if (createUserID == 0)
            {
                createUserID = CurrentInfo.CurrentUser.ID;
            }


            List <int> storesIDArr = IDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => Convert.ToInt32(t)).ToList();
            List <int> moduleArr   = ModuleIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => Convert.ToInt32(t)).ToList();

            //把所有菜单的父菜单取出来,但是只能保证三级菜单,多级有可能有bug
            IModuleService moduleService = ServiceFactory.Create <IModuleService>();
            List <Module>  allListModule = new List <Module>();

            allListModule.AddRange(moduleService.GetEntities(t => moduleArr.Contains(t.ID)).ToList());
            //foreach (var item in allListModule)
            //{
            //    Module parentModule = moduleService.GetFirstOrDefault(t => t.ID == item.ParentID);
            //    if (parentModule != null)
            //    {
            //        if (allListModule.Where(t => t.ID == parentModule.ID).Count() == 0)
            //        {
            //            allListModule.Add(parentModule);
            //        }
            //    }
            //}

            for (int i = 0; i < allListModule.Count(); i++)
            {
                int    parentID     = allListModule[i].ParentID;
                Module parentModule = moduleService.GetFirstOrDefault(t => t.ID == parentID);
                if (parentModule != null)
                {
                    if (allListModule.Where(t => t.ID == parentModule.ID).Count() == 0)
                    {
                        allListModule.Add(parentModule);
                    }
                }
            }
            //从新生成菜单ID
            moduleArr = allListModule.Select(t => t.ID).ToList();

            IRelationStoresModuleService relationStoresModuleService = ServiceFactory.Create <IRelationStoresModuleService>();

            List <RelationStoresModule> listRelationStoresModule = new List <RelationStoresModule>();
            int addCount = 0;

            using (TransactionScope scope = TransactionScopeHelper.GetTran())
            {
                foreach (var storeID in storesIDArr)
                {
                    //删除当前人员的所有的权限,然后在添加新的权限
                    var userModule = relationStoresModuleService.GetEntities(t => t.StoresID == storeID);
                    relationStoresModuleService.DeleteEntities(userModule.ToList());
                    foreach (var moduleID in moduleArr)
                    {
                        RelationStoresModule model = new RelationStoresModule();
                        model.StoresID     = storeID;
                        model.ModuleID     = moduleID;
                        model.CreateUserID = createUserID;
                        model.CreateTime   = DateTime.Now;
                        listRelationStoresModule.Add(model);
                    }
                }
                addCount = relationStoresModuleService.AddEntities(listRelationStoresModule).Count();
                scope.Complete();
            }

            if (addCount > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public JsonResult AddRelationStoreModule(string IDs, string ModuleIDs)
        {
            if (string.IsNullOrWhiteSpace(IDs) || string.IsNullOrWhiteSpace(ModuleIDs))
            {
                return(Json(new Result(false, "参数错误"), JsonRequestBehavior.AllowGet));
            }

            List <int> storesIDArr = IDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => Convert.ToInt32(t)).ToList();
            List <int> moduleArr   = ModuleIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => Convert.ToInt32(t)).ToList();

            //把所有菜单的父菜单取出来,但是只能保证三级菜单,多级有可能有bug
            IModuleService moduleService = ServiceFactory.Create <IModuleService>();
            List <Module>  allListModule = new List <Module>();

            allListModule.AddRange(moduleService.GetEntities(t => moduleArr.Contains(t.ID)).ToList());

            //复制一份副本,用于循环使用
            Module[] tempAllListModule = new Module[allListModule.Count];
            allListModule.CopyTo(tempAllListModule);

            foreach (var item in tempAllListModule)
            {
                Module parentModule = moduleService.GetFirstOrDefault(t => t.ID == item.ParentID);
                if (parentModule != null)
                {
                    if (allListModule.Where(t => t.ID == parentModule.ID).Count() == 0)
                    {
                        allListModule.Add(parentModule);
                    }
                }
            }
            //从新生成菜单ID
            moduleArr = allListModule.Select(t => t.ID).ToList();

            IRelationStoresModuleService relationStoresModuleService = ServiceFactory.Create <IRelationStoresModuleService>();

            List <RelationStoresModule> listRelationStoresModule = new List <RelationStoresModule>();
            int addCount = 0;

            using (TransactionScope scope = TransactionScopeHelper.GetTran())
            {
                foreach (var storeID in storesIDArr)
                {
                    //删除当前人员的所有的权限,然后在添加新的权限
                    var userModule = relationStoresModuleService.GetEntities(t => t.StoresID == storeID);
                    relationStoresModuleService.DeleteEntities(userModule.ToList());
                    foreach (var moduleID in moduleArr)
                    {
                        RelationStoresModule model = new RelationStoresModule();
                        model.StoresID     = storeID;
                        model.ModuleID     = moduleID;
                        model.CreateUserID = CurrentInfo.CurrentUser.ID;
                        model.CreateTime   = DateTime.Now;
                        listRelationStoresModule.Add(model);
                    }
                }
                addCount = relationStoresModuleService.AddEntities(listRelationStoresModule).Count();
                scope.Complete();
            }

            return(Json(new Result(addCount > 0, "成功分配权限"), JsonRequestBehavior.AllowGet));
        }