123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using System;
- using System.Collections.Generic;
- using System.Data.Common;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using YiSha.Data;
- using YiSha.Data.Repository;
- using YiSha.Entity.OrganizationManage;
- using YiSha.Enum;
- using YiSha.Enum.OrganizationManage;
- using YiSha.IService.OrganizationManage;
- using YiSha.Model.Param.OrganizationManage;
- using YiSha.Util;
- using YiSha.Util.Extension;
- using YiSha.Util.Model;
- namespace YiSha.Service.OrganizationManage
- {
- public partial class UserService
- {
- private IRepositoryFactory _baseRepository;
- private IUserBelongService _userBelongService;
- public UserService(IRepositoryFactory baseRepository, IUserBelongService userBelongService)
- {
- _baseRepository = baseRepository;
- _userBelongService = userBelongService;
- }
- #region 获取数据
- public async Task<List<UserEntity>> GetListPartial(UserListParam param)
- {
- var expression = ListFilterPartial(param);
- var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression);
- return list.ToList();
- }
- public async Task<List<UserEntity>> GetPageListPartial(UserListParam param, Pagination pagination)
- {
- var expression = ListFilterPartial(param);
- var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression, pagination);
- return list.ToList();
- }
- public async Task<UserEntity> GetEntityPartial(int id)
- {
- return await _baseRepository.BaseRepository(dbConnectType).FindEntity<UserEntity>(id);
- }
- public async Task<UserEntity> GetEntityByUserNamePartial(string userName)
- {
- return await _baseRepository.BaseRepository(dbConnectType).FindEntity<UserEntity>(p => p.UserName == userName);
- }
- public async Task<UserEntity> CheckLoginPartial(string userName)
- {
- var expression = LinqExtensions.True<UserEntity>();
- expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
- expression = expression.And(t => t.UserName == userName);
- expression = expression.Or(t => t.Mobile == userName);
- expression = expression.Or(t => t.Email == userName);
- return await _baseRepository.BaseRepository(dbConnectType).FindEntity(expression);
- }
- public bool ExistUserNamePartial(UserEntity entity)
- {
- var expression = LinqExtensions.True<UserEntity>();
- expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
- if (entity.Id.IsNullOrZero())
- {
- expression = expression.And(t => t.UserName == entity.UserName);
- }
- else
- {
- expression = expression.And(t => t.UserName == entity.UserName && t.Id != entity.Id);
- }
- return _baseRepository.BaseRepository(dbConnectType).IQueryable(expression).Count() > 0 ? true : false;
- }
- #endregion
- #region 提交数据
- public async Task UpdateUserPartial(UserEntity entity)
- {
- await _baseRepository.BaseRepository(dbConnectType).Update(entity);
- }
- public async Task SaveFormPartial(UserEntity entity)
- {
- var isAdd = false;
- try
- {
- //如果id为0,先在数据库中新增用户信息,获取用户的userid便于职位、角色信息的关联(事务中无法获取到用户的userid)
- if (entity.Id.IsNullOrZero())
- {
- isAdd = true;
- //新增用户
- await entity.Create();
- await _baseRepository.BaseRepository(dbConnectType).Insert(entity);
- }
- // 职位
- var userBelongPositionList = new List<UserBelongEntity>();
- if (!string.IsNullOrEmpty(entity.PositionIds))
- {
- foreach (int positionId in TextHelper.SplitToArray<int>(entity.PositionIds, ','))
- {
- UserBelongEntity positionBelongEntity = new UserBelongEntity();
- positionBelongEntity.UserId = entity.Id;
- positionBelongEntity.BelongId = positionId;
- positionBelongEntity.BelongType = UserBelongTypeEnum.Position.ParseToInt();
- await positionBelongEntity.Create();
- userBelongPositionList.Add(positionBelongEntity);
- }
- }
- // 角色
- var userBelongRoleList = new List<UserBelongEntity>();
- if (!string.IsNullOrEmpty(entity.RoleIds))
- {
- foreach (int roleId in TextHelper.SplitToArray<int>(entity.RoleIds, ','))
- {
- UserBelongEntity departmentBelongEntity = new UserBelongEntity();
- departmentBelongEntity.UserId = entity.Id;
- departmentBelongEntity.BelongId = roleId;
- departmentBelongEntity.BelongType = UserBelongTypeEnum.Role.ParseToInt();
- await departmentBelongEntity.Create();
- userBelongRoleList.Add(departmentBelongEntity);
- }
- }
- //开启事务
- var db = await _baseRepository.BaseRepository(dbConnectType).BeginTrans();
- try
- {
- if (!isAdd)
- {
- //修改用户
- await db.Delete<UserBelongEntity>(t => t.UserId == entity.Id);
- // 密码不进行更新,有单独的方法更新密码
- entity.Password = null;
- await entity.Modify();
- await db.Update(entity);
- }
- if (userBelongPositionList.Any())
- await _userBelongService.InsertMany(userBelongPositionList);
- if (userBelongRoleList.Any())
- await _userBelongService.InsertMany(userBelongRoleList);
- await db.CommitTrans();
- }
- catch
- {
- await db.RollbackTrans();
- throw;
- }
- }
- catch
- {
- //如果id为0且用户的userid不为零,则需要删除新增的用户信息
- if (isAdd)
- {
- await _baseRepository.BaseRepository(dbConnectType).Delete(entity);
- }
- }
- }
- public async Task DeleteFormPartial(string ids)
- {
- var db = await _baseRepository.BaseRepository(dbConnectType).BeginTrans();
- try
- {
- //删除用户信息
- await DeleteFormById(ids);
- //删除所属信息
- ids = $"{ string.Join(",", ids.Split(","))}";
- List<DbParameter> parameters = new List<DbParameter>();
- parameters.Add(DbParameterExtension.CreateDbParameter("@userid", ids, dbConnectType));
- await _userBelongService.DeleteFormByWhere(" FIND_IN_SET (userid,@userid) ", parameters);
- await db.CommitTrans();
- }
- catch
- {
- await db.RollbackTrans();
- throw;
- }
- }
- public async Task ResetPasswordPartial(UserEntity entity)
- {
- await entity.Modify();
- await _baseRepository.BaseRepository(dbConnectType).Update(entity);
- }
- public async Task ChangeUserPartial(UserEntity entity)
- {
- await entity.Modify();
- await _baseRepository.BaseRepository(dbConnectType).Update(entity);
- }
- #endregion
- #region 私有方法
- private Expression<Func<UserEntity, bool>> ListFilterPartial(UserListParam param)
- {
- var expression = LinqExtensions.True<UserEntity>();
- expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
- if (param != null)
- {
- if (!string.IsNullOrEmpty(param.UserName))
- {
- expression = expression.And(t => t.UserName.Contains(param.UserName));
- }
- if (!string.IsNullOrEmpty(param.UserIds))
- {
- int[] userIdList = TextHelper.SplitToArray<int>(param.UserIds, ',');
- expression = expression.And(t => userIdList.Contains(t.Id));
- }
- if (!string.IsNullOrEmpty(param.Mobile))
- {
- expression = expression.And(t => t.Mobile.Contains(param.Mobile));
- }
- if (param.UserStatus > -1)
- {
- expression = expression.And(t => t.UserStatus == param.UserStatus);
- }
- if (!string.IsNullOrEmpty(param.StartTime.ParseToString()))
- {
- expression = expression.And(t => t.BaseModifyTime >= param.StartTime);
- }
- if (!string.IsNullOrEmpty(param.EndTime.ParseToString()))
- {
- param.EndTime = param.EndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
- expression = expression.And(t => t.BaseModifyTime <= param.EndTime);
- }
- if (param.ChildrenDepartmentIdList != null && param.ChildrenDepartmentIdList.Count > 0)
- {
- expression = expression.And(t => param.ChildrenDepartmentIdList.Contains((int)t.DepartmentId));
- }
- }
- return expression;
- }
- #endregion
- }
- }
|