Esempio n. 1
0
        public async Task <IResultModel> Add(RoleUserAddModel model)
        {
            if (!await _roleRepository.ExistsAsync(m => m.Id == model.RoleId))
            {
                return(ResultModel.Failed("角色不存在"));
            }

            if (!model.UserIds.Any())
            {
                return(ResultModel.Failed("请选择用户"));
            }

            var entities = new List <RoleUserEntity>();

            foreach (var userId in model.UserIds)
            {
                var isExist = await _roleUserRepository.ExistsAsync(m => m.RoleId == model.RoleId && m.UserId == userId && m.IsDel == false);

                if (isExist)
                {
                    continue;
                }

                entities.Add(new RoleUserEntity()
                {
                    RoleId = model.RoleId,
                    UserId = userId
                });
            }

            if (!entities.Any())
            {
                return(ResultModel.Success());
            }

            var result = await _roleUserRepository.InsertRangeAsync(entities);

            return(ResultModel.Result(result));
        }