public string AddRoleFunction(int roleID, string functionID)
        {
            try
            {
                if (string.IsNullOrEmpty(functionID))
                {
                    throw new KnownException("参数无效!");
                }

                using (var db = new BCBackContext())
                {
                    db.RFAAuthorizations.Add(new RFAAuthorization()
                    {
                        RoleID = roleID, FunctionID = functionID, UpdateTime = DateTime.Now
                    });
                    if (1 == db.SaveChanges())
                    {
                        return(functionID);
                    }
                    else
                    {
                        throw new KnownException();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool DeleteRole(int roleId)
        {
            try
            {
                if (0 > roleId)
                {
                    throw new KnownException();
                }

                using (var db = new BCBackContext())
                {
                    var fun  = db.RFAAuthorizations.Where(x => x.RoleID == roleId && !x.Deleted);
                    var role = db.UserRoles.FirstOrDefault(x => x.RoleID == roleId && !x.Deleted);
                    if (role != null)
                    {
                        throw new KnownException("该角色被占用,无法删除");
                    }
                    else
                    {
                        foreach (var f in fun)
                        {
                            f.Deleted = true;
                        }
                        role.Deleted = true;
                    }
                    //db.RFARoles.Attach(del);
                    //db.RFARoles.Remove(del);
                    return(0 < db.SaveChanges());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public bool DeleteRoleFunction(int roleID, string functionID)
 {
     try
     {
         if (string.IsNullOrEmpty(functionID))
         {
             throw new KnownException("参数无效!");
         }
         using (var db = new BCBackContext())
         {
             RFAAuthorization au = new RFAAuthorization {
                 RoleID = roleID, FunctionID = functionID
             };
             db.RFAAuthorizations.Attach(au);
             db.RFAAuthorizations.Remove(au);
             if (1 == db.SaveChanges())
             {
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public string CreateUser(Account.Dtos.UserDto userDto)
 {
     using (var db = new BCBackContext())
     {
         if (string.IsNullOrEmpty(userDto.Password))
         {
             throw new KnownException("密码不允许为空");
         }
         var uid  = Ioc.GetService <ML.BC.Services.Common.IUniqeIdGenerator>().GeneratorBackUserID();
         var user = new BackUser
         {
             UserID     = uid,
             Name       = userDto.Name,
             Password   = CryptoService.MD5Encrypt(userDto.Password),
             Mobile     = userDto.Mobile,
             RegistDate = DateTime.Now,
             Closed     = userDto.Closed,
             UpdateTime = DateTime.Now
         };
         db.BackUsers.Add(user);
         if (db.SaveChanges() > 0)
         {
             return(user.UserID);
         }
         else
         {
             return(null);
         }
     }
 }
 public bool UpdateUser(Account.Dtos.UserDto user)
 {
     try
     {
         using (var db = new BCBackContext())
         {
             var temp = db.BackUsers.First(x => x.UserID == user.UserID);
             if (null == temp)
             {
                 throw new KnownException("该对象不存在");
             }
             temp.Closed = user.Closed;
             temp.Mobile = user.Mobile;
             if (!string.IsNullOrEmpty(user.Password))
             {
                 temp.Password = CryptoService.MD5Encrypt(user.Password);
             }
             temp.Name       = user.Name;
             temp.UpdateTime = DateTime.Now;
             return(0 < db.SaveChanges());
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
 public bool DeleteUser(string userId)
 {
     try
     {
         if (string.IsNullOrEmpty(userId))
         {
             return(false);
         }
         using (var db = new BCBackContext())
         {
             var temp = db.BackUsers.FirstOrDefault(obj => obj.UserID.Equals(userId));
             IQueryable <ML.BC.BCBackData.Model.UserRole> userrolelist;
             if (null != temp)
             {
                 userrolelist = db.UserRoles.Where(x => x.UserID == temp.UserID);
             }
             //                    db.BackUsers.Attach(temp);
             db.BackUsers.Remove(temp);
             return(0 < db.SaveChanges());
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
 public bool DeleteUserRole(string userID, int roleID)
 {
     try
     {
         if (string.IsNullOrEmpty(userID))
         {
             throw new KnownException("传递参数有误!");
         }
         using (var db = new BCBackContext())
         {
             var del = new UserRole()
             {
                 UserID = userID, RoleID = roleID
             };
             db.UserRoles.Attach(del);
             db.UserRoles.Remove(del);
             if (1 == db.SaveChanges())
             {
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public int AddRole(RolesDto role)
        {
            try
            {
                if (null == role)
                {
                    throw new KnownException();
                }

                if (!string.IsNullOrEmpty(role.FunctionIDs))
                {
                    using (var db = new BCBackContext())
                    {
                        //  事务处理RoleFunction表添加成功和RFARole表添加成功需要同时满足。
                        using (TransactionScope transaction = new TransactionScope())
                        {
                            db.RFARoles.Add(role);
                            db.SaveChanges();
                            var roleId = db.RFARoles.Where(obj => obj.Name == role.Name).Select(obj => obj.RoleID).ToList();
                            if (0 >= roleId.First())
                            {
                                throw new KnownException("保存角色失败!");
                            }

                            //  db.SaveChange()已在SetFunctin函数执行
                            var result = SetFunction(roleId.First(), role.FunctionIDs);
                            if (null == result)
                            {
                                throw new KnownException("保存角色功能失败!");
                            }
                            else
                            {
                                transaction.Complete();
                                return(role.RoleID);
                            }
                        }
                    }
                }

                using (var db = new BCBackContext())
                {
                    db.RFARoles.Add(role);
                    if (1 == db.SaveChanges())
                    {
                        return(role.RoleID);
                    }
                    else
                    {
                        throw new KnownException("角色保存失败!");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public bool UpdateUserLogin(string userId, string lastIP)
 {
     using (var db = new BCBackContext())
     {
         var user = db.BackUsers.First(n => n.UserID == userId);
         user.LastDate   = DateTime.Now;
         user.LastIP     = lastIP;
         user.UpdateTime = DateTime.Now;
         db.Entry <BackUser>(user).State = System.Data.Entity.EntityState.Modified;
         return(db.SaveChanges() > 0);
     }
 }
        private List <AuthorizationsDto> SetFunction(int roleID, string functionIDs)
        {
            try
            {
                if (0 > roleID || string.IsNullOrEmpty(functionIDs))
                {
                    throw new KnownException("参数无效!");
                }
                //  返回设置完成后的角色功能信息
                var list = new List <AuthorizationsDto>();

                var strS    = functionIDs.Split('|');
                var listFuc = strS.ToList();

                using (var db = new BCBackContext())
                {
                    foreach (var funId in listFuc)
                    {
                        //  更新或者添加功能
                        var au = new RFAAuthorization()
                        {
                            RoleID = roleID, FunctionID = funId, UpdateTime = DateTime.Now
                        };
                        db.RFAAuthorizations.AddOrUpdate(au);
                    }

                    //  删除不再具有的功能
                    var query = db.RFAAuthorizations.Where(obj => obj.RoleID == roleID).Select(obj => obj.FunctionID).ToList();
                    foreach (var f in query)
                    {
                        if (!listFuc.Contains(f))
                        {
                            var del = db.RFAAuthorizations.FirstOrDefault(obj => obj.RoleID == roleID && obj.FunctionID == f);
                            db.RFAAuthorizations.Remove(del);
                        }
                    }

                    if (0 < db.SaveChanges())
                    {
                        foreach (var fun in listFuc)
                        {
                            list.Add(db.RFAAuthorizations.FirstOrDefault(obj => obj.RoleID == roleID && obj.FunctionID == fun));
                        }
                    }
                }
                return(list);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //public int Delete(string FunctionID)
        //{
        //    using (var db = new BCBackContext())
        //    {
        //        RFAFunction temp = db.RFAFunctions.FirstOrDefault(x => x.FunctionID == FunctionID);

        //        if (null == temp)
        //            return 0;

        //        db.RFAFunctions.Remove(temp);

        //        Action<string> deleteChildren = null;
        //        deleteChildren = (parentId) =>
        //        {
        //            List<RFAFunction> list = db.RFAFunctions.Where(x => x.ParentID == FunctionID).ToList<RFAFunction>();
        //            for (int i = 0; i < list.Count; i++)
        //            {
        //                var tempFunction = list[i];
        //                db.RFAFunctions.Remove(tempFunction);
        //                deleteChildren(tempFunction.FunctionID);
        //            }
        //        };
        //        deleteChildren(temp.FunctionID);

        //        return db.SaveChanges();
        //    }
        //}

        public int Add(FunctionDto obj)
        {
            using (var db = new BCBackContext())
            {
                RFAFunction temp = new RFAFunction();

                temp.FunctionID = obj.FunctionID;
                temp.MyID       = obj.MyID;
                temp.Name       = obj.Name;
                temp.ParentID   = obj.ParentID;
                temp.Available  = obj.Available;
                temp.Desription = obj.Desription;

                db.RFAFunctions.Add(temp);
                //                db.Entry<RFAFunction>(temp).State = EntityState.Added;

                return(db.SaveChanges());
            }
        }
        //public List<RFAFunction> Select(Expression<Func<RFAFunction, bool>> foo)
        //{
        //    using (var db = new BCBackContext())
        //    {
        //        List<RFAFunction> list = new List<RFAFunction>();
        //        list = db.RFAFunctions.Where(foo).ToList<RFAFunction>();
        //        return list;
        //    }
        //}

        public int Update(FunctionDto obj)
        {
            using (var db = new BCBackContext())
            {
                RFAFunction temp = db.RFAFunctions.Where(objx => objx.FunctionID.Equals(obj.FunctionID)).First();
                if (temp == null)
                {
                    return(0);
                }
                //temp.FunctionID = obj.FunctionID;
                temp.MyID = obj.MyID;
                temp.Name = obj.Name;
                //temp.ParentID = obj.ParentID;
                temp.Available  = obj.Available;
                temp.Desription = obj.Desription;

                return(db.SaveChanges());
            }
        }
        public bool UpdateRole(RolesDto role)
        {
            try
            {
                if (null == role || 0 > role.RoleID)
                {
                    throw new KnownException("关键信息缺失,无法更新!");
                }

                using (var db = new BCBackContext())
                {
                    using (TransactionScope transaction = new TransactionScope())
                    {
                        var temp = db.RFARoles.FirstOrDefault(obj => obj.RoleID == role.RoleID);
                        if (null == temp)
                        {
                            throw new KnownException("不存在该记录,无法更新");
                        }

                        temp.Name        = role.Name;
                        temp.OwnerID     = role.OwnerID;
                        temp.Description = role.Description;
                        temp.Available   = role.Available;
                        db.SaveChanges();

                        var auths = SetFunction(role.RoleID, role.FunctionIDs);
                        if (auths == null || auths.Count <= 0)
                        {
                            throw new KnownException("角色功能信息更新失败!");
                        }

                        transaction.Complete();
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string AddUserRole(string userID, int roleID)
        {
            try
            {
                if (string.IsNullOrEmpty(userID))
                {
                    throw new KnownException("传递参数有误!");
                }

                using (var db = new BCBackContext())
                {
                    if (!db.BackUsers.Any(obj => obj.UserID == userID))
                    {
                        throw new KnownException("不存在的用户ID无法添加用户角色!");
                    }
                    if (!db.RFARoles.Any(obj => obj.RoleID == roleID))
                    {
                        throw new KnownException("不存在的角色ID无法添加用户角色!");
                    }

                    db.UserRoles.Add(new UserRole()
                    {
                        UserID = userID, RoleID = roleID, UpdateTime = DateTime.Now
                    });
                    var re = db.SaveChanges();
                    if (1 == re)
                    {
                        return(userID);
                    }
                    else
                    {
                        throw new KnownException();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool UpdateRoleFunction(int roleID, string functionID, bool available)
        {
            if (string.IsNullOrEmpty(functionID))
            {
                throw new KnownException("更新信息不全!");
            }
            using (var db = new BCBackContext())
            {
                var q = db.RFAAuthorizations.FirstOrDefault(obj => obj.RoleID == roleID && obj.FunctionID == functionID);
                if (null == q)
                {
                    throw new KnownException("该记录不存在!");
                }

                db.RFAAuthorizations.AddOrUpdate(new RFAAuthorization()
                {
                    RoleID = roleID, FunctionID = functionID, UpdateTime = DateTime.Now
                });

                return(1 == db.SaveChanges());
            }
        }
        private int DeleteFunctionsons(BCBackContext db, string FunctionID)
        {
            RFAFunction temp = db.RFAFunctions.FirstOrDefault(x => x.FunctionID.Equals(FunctionID));

            if (null == temp)
            {
                return(0);
            }

            db.RFAFunctions.Remove(temp);

            List <RFAFunction> list = db.RFAFunctions.Where(x => x.ParentID.Equals(FunctionID)).ToList <RFAFunction>();

            for (int i = 0; i < list.Count; i++)
            {
                if (0 == DeleteFunctionsons(db, list[i].FunctionID))
                {
                    break;
                }
            }
            return(db.SaveChanges());
        }