/// <summary> /// 新增角色信息 /// </summary> /// <returns></returns> public bool AddRole(RoleOperateDTO dto) { var result = false; using (var tcdmse = new Entities.TCDMS_MasterDataEntities()) { var pp = tcdmse.master_RoleInfo.AsNoTracking().Where(p => p.RoleName == dto.RoleName).FirstOrDefault(); if (pp != null) { throw new Exception("角色名不能重复!"); } var newrole = new master_RoleInfo(); newrole = Mapper.Map <RoleOperateDTO, master_RoleInfo>(dto); tcdmse.master_RoleInfo.Add(newrole); tcdmse.SaveChanges(); if (dto.RoleAuthority != null && dto.RoleAuthority.Count > 0) { for (int i = 0; i < dto.RoleAuthority.Count; i++) { var newauth = new master_RoleAuthority() { RoleButtonAuthority = dto.RoleAuthority[i].RoleButtonAuthority, StructureID = dto.RoleAuthority[i].StructureID, RoleID = newrole.RoleID }; tcdmse.master_RoleAuthority.Add(newauth); } } result = tcdmse.SaveChanges() >= 0; } return(result); }
/// <summary> /// 修改角色信息 /// </summary> /// <returns></returns> public bool UpdateRole(RoleOperateDTO dto) { var result = false; using (var tcdmse = new Entities.TCDMS_MasterDataEntities()) { var pp = tcdmse.master_RoleInfo.Where(p => p.RoleID == dto.RoleID).FirstOrDefault(); if (pp == null) { throw new Exception("此条信息不存在"); } var ss = tcdmse.master_RoleInfo.AsNoTracking().Where(p => p.RoleName == dto.RoleName && p.RoleID != dto.RoleID).FirstOrDefault(); if (ss != null) { throw new Exception("角色名不能重复!"); } dto.CreateTime = pp.CreateTime; dto.CreateUser = pp.CreateUser; Mapper.Map <RoleOperateDTO, master_RoleInfo>(dto, pp); var remove = tcdmse.master_RoleAuthority.Where(p => p.RoleID == pp.RoleID).ToList(); tcdmse.master_RoleAuthority.RemoveRange(remove); if (dto.RoleAuthority != null && dto.RoleAuthority.Count > 0) { for (int i = 0; i < dto.RoleAuthority.Count; i++) { var newauth = new master_RoleAuthority() { RoleButtonAuthority = dto.RoleAuthority[i].RoleButtonAuthority, StructureID = dto.RoleAuthority[i].StructureID, RoleID = pp.RoleID }; tcdmse.master_RoleAuthority.Add(newauth); } } result = tcdmse.SaveChanges() > 0; } return(result); }