You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.3 KiB
C#

12 months ago
using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
using DS.Module.UserModule;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
12 months ago
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Sys.Method;
12 months ago
/// <summary>
///
/// </summary>
public class UserService : IUserService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
12 months ago
private readonly ICommonService _commonService;
12 months ago
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public UserService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
12 months ago
_commonService = _serviceProvider.GetRequiredService<ICommonService>();
12 months ago
}
/// <summary>
/// 列表查询
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<UserViewModel>> GetListByPage(PageRequest request)
11 months ago
{
11 months ago
var query = _commonService.GetDataRuleFilter<SysUser>();
// var number =_commonService.GetSequenceNext<SysUser>();
12 months ago
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
12 months ago
var data = query
12 months ago
.Where(x => x.UserCode != "admin")
.Where(whereList).Select<UserViewModel>()
.ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 获取单个用户信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<UserViewModel> GetUserInfo(string id)
{
11 months ago
var roleIds = db.Queryable<SysRoleUser>().Where(x => x.UserId == long.Parse(id)).Select(x => x.RoleId).ToArray();
10 months ago
var orgIds = db.Queryable<SysOrgUser>().Where(x => x.UserId == long.Parse(id)).Select(x => x.OrgId).ToArray();
11 months ago
var data = db.Queryable<SysUser>()
11 months ago
.Where(x => x.Id == long.Parse(id))
12 months ago
.Select<UserViewModel>()
10 months ago
.Mapper(it =>
{
it.RoleIds = roleIds;
it.OrgIds = orgIds;
})
12 months ago
.First();
return DataResult<UserViewModel>.Success(data,MultiLanguageConst.DataQuerySuccess);
12 months ago
}
/// <summary>
/// 编辑用户信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
11 months ago
public DataResult EditUser(UserReq model)
12 months ago
{
11 months ago
if (model.Id == 0)
12 months ago
{
var isExist = db.Queryable<SysUser>().Filter(null,true).Where(x => x.UserCode == model.UserCode).First();
12 months ago
if (isExist != null)
{
return DataResult.Failed("用户唯一编码已存在!",MultiLanguageConst.UserCodeExist);
12 months ago
}
#region 用户数量检测
var tenant = db.Queryable<SysTenant>().Where(x=>x.Id ==long.Parse(user.TenantId)).First();
var userAuth = db.Queryable<SysTenantUserAuth>().First();
if (userAuth.IsNotNull())
{
var authNum = EncrypteHelper.DecryptData(userAuth.AuthNum, tenant.AppSecret);
var userCount = db.Queryable<SysUser>().Count();
if (userCount>= int.Parse(authNum))
{
return DataResult.Failed(string.Format("用户数量超出授权数量{0}", int.Parse(authNum)),MultiLanguageConst.UserAuthNumLimit);
}
}
#endregion
11 months ago
var data = model.MapTo<UserReq, SysUser>();
12 months ago
data.PinYinCode = PinYinUtil.GetFristLetter(data.UserName);
10 months ago
data.MD5Password = MD5Helper.MD5Encrypt(data.Password);
11 months ago
var entity = db.Insertable(data).ExecuteReturnEntity();
12 months ago
if (model.RoleIds.Count() > 0)
{
foreach (var item in model.RoleIds)
{
11 months ago
var roleUser = new SysRoleUser
12 months ago
{
11 months ago
RoleId = item,
UserId = entity.Id,
12 months ago
};
db.Insertable(roleUser).ExecuteCommand();
}
}
10 months ago
if (model.OrgIds.Count() > 0)
{
foreach (var item in model.OrgIds)
{
var orgUser = new SysOrgUser
{
OrgId = item,
UserId = entity.Id,
};
db.Insertable(orgUser).ExecuteCommand();
}
}
12 months ago
return DataResult.Successed("添加成功!", entity.Id,MultiLanguageConst.DataCreateSuccess);
12 months ago
}
else
{
11 months ago
var userId = model.Id;
var info = db.Queryable<SysUser>().Where(x => x.Id == userId).First();
12 months ago
11 months ago
info = model.MapTo<UserReq, SysUser>();
12 months ago
info.PinYinCode = PinYinUtil.GetFristLetter(info.UserName);
db.Updateable(info).IgnoreColumns("TenantId").IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
12 months ago
#region 处理用户角色
11 months ago
var existRoles = db.Queryable<SysRoleUser>().Where(x => x.UserId == userId).Select(n => n.RoleId)
12 months ago
.ToArray();
11 months ago
11 months ago
IEnumerable<long?> delRoles = existRoles.AsQueryable().Except(model.RoleIds);
12 months ago
if (delRoles.Count() > 0)
{
11 months ago
db.Deleteable<SysRoleUser>()
.Where(it => it.UserId == userId && delRoles.Contains(it.RoleId))
12 months ago
.ExecuteCommand();
}
11 months ago
11 months ago
IEnumerable<long?> addRoles = model.RoleIds.AsQueryable().Except(existRoles);
if (addRoles.Count() > 0)
{
foreach (var item in addRoles)
{
var roleUser = new SysRoleUser
{
RoleId = item,
UserId = model.Id,
};
db.Insertable(roleUser).ExecuteCommand();
}
}
12 months ago
#endregion 处理用户角色
10 months ago
#region 处理用户机构
var existOrgs = db.Queryable<SysOrgUser>().Where(x => x.UserId == userId).Select(n => n.OrgId)
.ToArray();
IEnumerable<long?> delOrgs = existOrgs.AsQueryable().Except(model.OrgIds);
if (delOrgs.Count() > 0)
{
db.Deleteable<SysOrgUser>()
.Where(it => it.UserId == userId && delOrgs.Contains(it.OrgId))
.ExecuteCommand();
}
IEnumerable<long?> addOrgs = model.OrgIds.AsQueryable().Except(existOrgs);
if (addOrgs.Count() > 0)
{
foreach (var item in addOrgs)
{
var orgUser = new SysOrgUser
{
OrgId = item,
UserId = model.Id,
};
db.Insertable(orgUser).ExecuteCommand();
}
}
#endregion 处理用户机构
12 months ago
return DataResult.Successed("更新成功!",MultiLanguageConst.DataUpdateSuccess);
12 months ago
}
}
}