Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        public async Task <UserGroupDTO> GetByIdAsync(long id)
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var group = await ctx.UserGroups.SingleOrDefaultAsync(e => e.Id == id);

                if (group == null)
                {
                    return(null);
                }
                return(ToUserGroupDTO(group));
            }
        }
Esempio n. 3
0
        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();
                }
            }
        }
Esempio n. 4
0
        public async Task <bool> CheckLoginAsync(string phoneNum, string password)
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var user = await ctx.Users.SingleOrDefaultAsync(e => e.PhoneNum == phoneNum);

                if (user == null)
                {
                    return(false);
                }
                string inputHash = MD5Helper.ComputeMd5(password + user.PasswordSalt);
                return(user.PasswordHash == inputHash);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 检查用户登陆状态
        /// </summary>
        /// <param name="phoneNum"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <bool> CheckLoginAsync(string phoneNum, string password)
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var user = await ctx.Users.SingleOrDefaultAsync(e => e.PhoneNum == phoneNum);

                if (user == null)
                {
                    return(false);
                }
                string salt = user.PasswordSalt;
                string hash = CommonHelper.CalMD5(password + salt);
                return(hash == user.PasswordHash);
            }
        }
Esempio n. 6
0
        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();
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 根据Id获得用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <UserDTO> GetByIdAsync(long id)
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var user = await ctx.Users.SingleOrDefaultAsync(e => e.Id == id);

                if (user == null)
                {
                    return(null);
                }
                else
                {
                    return(TODTO(user));
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 获取到员工上级下拉数据
        /// </summary>
        /// <returns></returns>
        public async Task <IEnumerable <SelectListResultModel> > GetStaffParentSelectItem()
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var entitys = await ctx.Staffs.Where(s => s.Status).ToListAsync();

                List <SelectListResultModel> result = new List <SelectListResultModel>();
                foreach (var item in entitys)
                {
                    result.Add(new SelectListResultModel {
                        text = item.StaffCode, value = item.Id
                    });
                }
                return(result);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// 根据appkey得到
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public async Task <AppInfoDTO> GetByAppKeyAsyc(string appkey)
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var appInfo = await ctx.AppInfos.SingleOrDefaultAsync(a => a.AppKey == appkey);

                if (appkey == null)
                {
                    return(null);
                }
                else
                {
                    return(TODTO(appInfo));
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        ///根据手机号获取用户信息
        /// </summary>
        /// <param name="phoneNum"></param>
        /// <returns></returns>
        public async Task <UserDTO> GetByPhoneNumAsync(string phoneNum)
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var user = await ctx.Users.SingleOrDefaultAsync(s => s.PhoneNum == phoneNum);

                if (user == null)
                {
                    return(null);
                }
                else
                {
                    return(TODTO(user));
                }
            }
        }
Esempio n. 11
0
        public async Task <UserGroupDTO> GetByIdAsync(long id)
        {
            using (UCDbContext db = new UCDbContext())
            {
                var userGroup = await db.UserGroups.SingleOrDefaultAsync(e => e.Id == id);

                if (userGroup == null)
                {
                    return(null);
                }
                else
                {
                    return(ToDTO(userGroup));
                }
            }
        }
Esempio n. 12
0
        public async Task <IEnumerable <StaffQueryDTO> > GetPageListAsync(StaffQueryCondModel queryCond)
        {
            var page      = queryCond.Page;
            var page_size = queryCond.Page_size;

            using (UCDbContext ctx = new UCDbContext())
            {
                var queryAble = ctx.Staffs.AsNoTracking().OrderBy(s => s.Id).Where(s => s.Status);//用asNotracking可以提升一点性能,不跟踪状态变化
                if (!string.IsNullOrEmpty(queryCond.StaffCode))
                {
                    queryAble = queryAble.Where(s => s.StaffCode.Contains(queryCond.StaffCode));
                }
                if (!string.IsNullOrEmpty(queryCond.ChName))
                {
                    queryAble = queryAble.Where(s => s.ChName.Contains(queryCond.ChName));
                }
                if (!string.IsNullOrEmpty(queryCond.ParentCode))
                {
                    queryAble = queryAble.Where(s => s.ParentStaff.StaffCode.Contains(queryCond.ParentCode));
                }
                if (queryCond.BeginEmploymentDate != DateTime.MinValue && queryCond.EndEmploymentDate != DateTime.MinValue)
                {
                    queryAble = queryAble.Where(s => (DbFunctions.DiffDays(s.EmploymentDate, queryCond.BeginEmploymentDate) <= 0) && (DbFunctions.DiffDays(s.EmploymentDate, queryCond.EndEmploymentDate) >= 0));//后面减前面 &&翻译成 AND
                }
                else
                {
                    if (queryCond.BeginEmploymentDate != DateTime.MinValue)
                    {
                        queryAble = queryAble.Where(s => DbFunctions.DiffDays(s.EmploymentDate, queryCond.BeginEmploymentDate) <= 0);//后面减前面
                    }
                    if (queryCond.EndEmploymentDate != DateTime.MinValue)
                    {
                        queryAble = queryAble.Where(s => DbFunctions.DiffDays(s.EmploymentDate, queryCond.EndEmploymentDate) >= 0);
                    }
                }
                queryAble = queryAble.Skip((page - 1) * page_size);
                queryAble = queryAble.Take(page_size);
                var datas = await queryAble.ToListAsync();

                List <StaffQueryDTO> staffDtos = new List <StaffQueryDTO>();
                foreach (var item in datas)
                {
                    staffDtos.Add(ToDTO(item));
                }
                return(staffDtos);
            }
        }
Esempio n. 13
0
        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();
            }
        }
Esempio n. 14
0
        /// <summary>
        /// 根据userId得到用户组信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task <UserGroupDTO[]> GetGroupsAsync(long userId)
        {
            using (UCDbContext db = new UCDbContext())
            {
                var user = await db.Users.SingleOrDefaultAsync(e => e.Id == userId);

                if (user == null)
                {
                    return(null);
                }
                var groups = user.Groups;
                List <UserGroupDTO> dtos = new List <UserGroupDTO>();
                foreach (var group in groups)
                {
                    dtos.Add(ToUserGroupDTO(group));
                }
                return(dtos.ToArray());
            }
        }
Esempio n. 15
0
        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();
            }
        }
Esempio n. 16
0
        /// <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();
            }
        }
Esempio n. 17
0
        public async Task <AppInfoDTO> GetByAppKeyAsync(string appKey)
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var appInfo = await ctx.AppInfos.FirstOrDefaultAsync(e => e.AppKey == appKey && e.IsEnabled == true);

                if (appInfo == null)
                {
                    return(null);
                }
                else
                {
                    AppInfoDTO dto = new AppInfoDTO();
                    dto.AppKey    = appInfo.AppKey;
                    dto.AppSecret = appInfo.AppSecret;
                    dto.Id        = appInfo.Id;
                    dto.Name      = appInfo.Name;
                    dto.IsEnabled = appInfo.IsEnabled;
                    return(dto);
                }
            }
        }
Esempio n. 18
0
        public async Task <UserDTO[]> GetGroupUsersAsync(long userGroupId)
        {
            using (UCDbContext ctx = new UCDbContext())
            {
                var group = await ctx.UserGroups.SingleOrDefaultAsync(e => e.Id == userGroupId);

                if (group == null)
                {
                    return(null);
                }
                List <UserDTO> dtos = new List <UserDTO>();
                foreach (var user in group.Users)
                {
                    UserDTO userDto = new UserDTO();
                    userDto.Id       = user.Id;
                    userDto.NickName = user.NickName;
                    userDto.PhoneNum = user.PhoneNum;
                    dtos.Add(userDto);
                }
                return(dtos.ToArray());
            }
        }
Esempio n. 19
0
        /// <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);
            }
        }
Esempio n. 20
0
        /// <summary>
        /// 根据用户组Id得到用户信息
        /// </summary>
        /// <param name="userGroupId"></param>
        /// <returns></returns>
        public async Task <UserDTO[]> GetGroupUsersAsync(long userGroupId)
        {
            using (UCDbContext db = new UCDbContext())
            {
                var group = await db.UserGroups.SingleOrDefaultAsync(s => s.Id == userGroupId);

                if (group == null)
                {
                    return(null);
                }
                var            users = group.Users;
                List <UserDTO> dtos  = new List <UserDTO>();
                foreach (var item in users)
                {
                    UserDTO dto = new UserDTO();
                    dto.NickName = item.NickName;
                    dto.Id       = item.Id;
                    dto.PhoneNum = item.PhoneNum;
                    dtos.Add(dto);
                }
                return(dtos.ToArray());
            }
        }