public async Task UpdateSaveAsync(StaffDTO dto) { using (UCDbContext ctx = new UCDbContext()) { var id = dto.Id; var entity = await ctx.Staffs.Where(s => s.Id == id).FirstOrDefaultAsync(); if (entity == null) { throw new Exception("编辑的人员不存在"); } entity.JobTitle = dto.JobTitle; entity.ParentId = dto.ParentId; entity.PhoneNumber = dto.PhoneNumber; entity.ResignationDate = dto.ResignationDate; entity.StaffCode = dto.StaffCode; entity.Telephone = dto.Telephone; entity.Birthday = dto.Birthday; entity.CALLNumber = dto.CALLNumber; entity.ChName = dto.ChName; entity.DepartmentId = dto.DepartmentId; entity.EmploymentDate = dto.EmploymentDate; entity.EnName = dto.EnName; entity.Email = dto.Email; entity.Email = dto.Remark; entity.UpdateTime = dto.UpdateTime; entity.UserName = dto.UserName; await ctx.SaveChangesAsync(); } }
public async Task <long> AddNewAsync(StaffDTO addDto) { using (UCDbContext ctx = new UCDbContext()) { var entity = ToEntity(addDto); var staff = ctx.Staffs.Add(entity); await ctx.SaveChangesAsync(); return(staff.Id); } }
public async Task DeleteByIdAsync(long id) { using (UCDbContext ctx = new UCDbContext()) { var entity = await ctx.Staffs.Where(s => s.Id == id).FirstOrDefaultAsync(); if (entity != null) { ctx.Staffs.Remove(entity); await ctx.SaveChangesAsync(); } } }
public async Task BatchDeleteAsync(string ids) { var idArr = ids.Split(',').Select(s => { return(Convert.ToInt64(s)); }); using (UCDbContext ctx = new UCDbContext()) { var entitys = await ctx.Staffs.Where(s => idArr.Contains(s.Id)).ToListAsync(); if (entitys != null) { ctx.Staffs.RemoveRange(entitys); await ctx.SaveChangesAsync(); } } }
public async Task AddUserToGroupAsync(long userGroupId, long userId) { using (UCDbContext db = new UCDbContext()) { var group = db.UserGroups.SingleOrDefaultAsync(u => u.Id == userGroupId); if (group == null) { throw new ArgumentException("userGroupId" + userGroupId + "已存在", nameof(userGroupId)); } var user = db.Users.SingleOrDefaultAsync(u => u.Id == userId); if (user == null) { throw new ArgumentException("userId=" + userId + "不存在", nameof(userId)); } await db.SaveChangesAsync(); } }
public async Task AddUserToGroupAsync(long userGroupId, long userId) { using (UCDbContext ctx = new UCDbContext()) { var group = await ctx.UserGroups.SingleOrDefaultAsync(e => e.Id == userGroupId); if (group == null) { throw new ArgumentException("userGroupId=" + userGroupId + "不存在", nameof(userGroupId)); } var user = ctx.Users.SingleOrDefault(e => e.Id == userId); if (user == null) { throw new ArgumentException("userId=" + userId + "不存在", nameof(userId)); } group.Users.Add(user); user.Groups.Add(group); await ctx.SaveChangesAsync(); } }
/// <summary> /// 从用户组关系表中删除用户 /// </summary> /// <param name="userGroupId"></param> /// <param name="userId"></param> /// <returns></returns> public async Task RemoveUserFromGroupAsync(long userGroupId, long userId) { using (UCDbContext db = new UCDbContext()) { var group = await db.UserGroups.SingleOrDefaultAsync(g => g.Id == userGroupId); if (group == null) { throw new ArgumentException("userGroupId" + userGroupId + "的用户组", nameof(userGroupId)); } var user = await db.Users.SingleOrDefaultAsync(u => u.Id == userId); if (user == null) { throw new ArgumentException("不存在userId=" + userId + "的用户", nameof(userGroupId)); } group.Users.Remove(user); user.Groups.Remove(group); await db.SaveChangesAsync(); } }
/// <summary> /// 新增一个用户 /// </summary> /// <param name="phoneNum"></param> /// <param name="nickName"></param> /// <param name="password"></param> /// <returns></returns> public async Task <long> AddNewAsync(string phoneNum, string nickName, string password) { User userEntity = new User(); using (UCDbContext ctx = new UCDbContext()) { if (await ctx.Users.AnyAsync(u => u.PhoneNum == phoneNum)) { throw new ApplicationException("手机号" + phoneNum + "已经存在"); } userEntity.PhoneNum = phoneNum; userEntity.NickName = nickName; string salt = new Random().Next(10000, 9999).ToString(); userEntity.PasswordSalt = salt; string hash = CommonHelper.CalMD5(password + salt); userEntity.PasswordHash = hash; ctx.Users.Add(userEntity); await ctx.SaveChangesAsync(); return(userEntity.Id); } }