Beispiel #1
0
        public bool AddRelationShopModule(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> shopIDArr = 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());


            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();

            IRelationShopsModuleService relationShopsModuleService = ServiceFactory.Create <IRelationShopsModuleService>();
            List <RelationShopsModule>  listRelationShopsModule    = new List <RelationShopsModule>();

            int addCount = 0;

            using (TransactionScope scope = TransactionScopeHelper.GetTran())
            {
                foreach (var shopID in shopIDArr)
                {
                    //删除当前人员的所有的权限,然后在添加新的权限
                    var roleModule = relationShopsModuleService.GetEntities(t => t.ShopID == shopID);
                    relationShopsModuleService.DeleteEntities(roleModule.ToList());
                    foreach (var moduleID in moduleArr)
                    {
                        RelationShopsModule model = new RelationShopsModule();
                        model.ShopID       = shopID;
                        model.ModuleID     = moduleID;
                        model.CreateUserID = createUserID;
                        model.CreateTime   = DateTime.Now;
                        listRelationShopsModule.Add(model);
                    }
                }
                addCount = relationShopsModuleService.AddEntities(listRelationShopsModule).Count();
                scope.Complete();
            }

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

            List <int> shopIDArr = 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();

            IRelationShopsModuleService relationShopsModuleService = ServiceFactory.Create <IRelationShopsModuleService>();
            List <RelationShopsModule>  listRelationShopsModule    = new List <RelationShopsModule>();

            int addCount = 0;

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

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