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.

147 lines
4.9 KiB
C#

2 years ago
using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
2 years ago
using DS.Module.UserModule;
2 years ago
using DS.WMS.Common.Enum;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.System.Method;
/// <summary>
///
/// </summary>
public class UserService : IUserService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public UserService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
}
/// <summary>
/// 列表查询
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<UserViewModel>> GetListByPage(PageRequest request)
{
2 years ago
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysUser>()
.Where(x=>x.UserCode!="admin")
.Where(whereList).Select<UserViewModel>()
.ToQueryPage(request.PageCondition);
2 years ago
return data;
}
/// <summary>
/// 获取单个用户信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<UserViewModel> GetUserInfo(string id)
{
var roleIds = db.Queryable<SysRoleUser>().Where(x => x.UserId == id).Select(x => x.RoleId).ToArray();
var data = db.Queryable<SysUser>()
.Where(x => x.Id == id)
2 years ago
.Select<UserViewModel>()
.Mapper(it => { it.RoleIds = roleIds; })
2 years ago
.First();
return DataResult<UserViewModel>.Success(data);
}
2 years ago
/// <summary>
/// 编辑用户信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DataResult EditUser(UserInput model)
{
if (model.Id.IsNullOrEmpty())
2 years ago
{
var isExist = db.Queryable<SysUser>().Where(x => x.UserCode == model.UserCode).First();
if (isExist!=null)
2 years ago
{
return DataResult.Failed("用户唯一编码已存在!");
2 years ago
}
var data = model.MapTo<UserInput,SysUser>();
data.PinYinCode = PinYinUtil.GetFristLetter(data.UserName);
data.MD5Password = MD5Helper.MD5Encrypt(data.Password);
//处理客户companyId
var companyId = db.Queryable<SysUser>().Filter(null, true).First(x => x.Id == model.Id)?.CompanyId;
if (!companyId.IsNullOrEmpty())
2 years ago
{
data.CompanyId = companyId;
2 years ago
}
var user = db.Insertable(data).ExecuteReturnEntity();
if (model.RoleIds.Count()>0)
{
foreach (var item in model.RoleIds)
2 years ago
{
var roleUser = new SysRoleUser
2 years ago
{
RoleId = item,
UserId = user.Id,
};
db.Insertable(roleUser).ExecuteCommand();
}
}
2 years ago
return DataResult.Successed("添加成功!");
2 years ago
}
else
{
var info =db.Queryable<SysUser>().Where(x => x.Id == model.Id).First();
info = model.MapTo<UserInput,SysUser>();
info.PinYinCode = PinYinUtil.GetFristLetter(info.UserName);
db.Updateable(info).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand();
#region 处理用户角色
var existRoles = db.Queryable<SysRoleUser>().Where(x => x.UserId == model.Id).Select(n=>n.RoleId).ToArray();
IEnumerable<string> delRoles = existRoles.AsQueryable().Except(model.RoleIds);
if (delRoles.Count()>0)
{
db.Deleteable<SysRoleUser>()
.Where(it => it.UserId == model.Id && delRoles.Contains(it.RoleId))
.ExecuteCommand();
}
IEnumerable<string> addRoles = model.RoleIds.AsQueryable().Except(existRoles);
if (addRoles.Count()>0)
{
foreach (var item in addRoles)
2 years ago
{
var roleUser = new SysRoleUser
2 years ago
{
RoleId = item,
UserId = model.Id,
2 years ago
};
db.Insertable(roleUser).ExecuteCommand();
}
}
2 years ago
#endregion
return DataResult.Successed("更新成功!");
2 years ago
}
}
}