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.
305 lines
11 KiB
C#
305 lines
11 KiB
C#
using DS.Module.Core;
|
|
using DS.Module.Core.Data;
|
|
using DS.Module.Core.Extensions;
|
|
using DS.Module.Core.Helpers;
|
|
using DS.Module.SqlSugar;
|
|
using DS.Module.UserModule;
|
|
using DS.WMS.Core.Code.Entity;
|
|
using DS.WMS.Core.Sys.Dtos;
|
|
using DS.WMS.Core.Sys.Entity;
|
|
using DS.WMS.Core.Sys.Interface;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using SqlSugar;
|
|
|
|
namespace DS.WMS.Core.Sys.Method;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class UserService : IUserService
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly ISqlSugarClient db;
|
|
private readonly IUser user;
|
|
private readonly ICommonService _commonService;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
public UserService(IServiceProvider serviceProvider)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
user = _serviceProvider.GetRequiredService<IUser>();
|
|
_commonService = _serviceProvider.GetRequiredService<ICommonService>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 列表查询
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public DataResult<List<UserViewModel>> GetListByPage(PageRequest request)
|
|
{
|
|
var query = _commonService.GetDataRuleFilter<SysUser>();
|
|
|
|
// var number =_commonService.GetSequenceNext<SysUser>();
|
|
|
|
//序列化查询条件
|
|
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
|
|
var data = query
|
|
.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)
|
|
{
|
|
var roleIds = db.Queryable<SysRoleUser>().Where(x => x.UserId == long.Parse(id)).Select(x => x.RoleId).ToArray();
|
|
|
|
var orgIds = db.Queryable<SysOrgUser>().Where(x => x.UserId == long.Parse(id)).Select(x => x.OrgId).ToArray();
|
|
var laneIds = db.Queryable<SysUserLane>().Where(x => x.UserId == long.Parse(id)).Select(x => x.LaneId).ToArray();
|
|
|
|
var data = db.Queryable<SysUser>()
|
|
.Where(x => x.Id == long.Parse(id))
|
|
.Select<UserViewModel>()
|
|
.Mapper(it =>
|
|
{
|
|
it.RoleIds = roleIds;
|
|
it.OrgIds = orgIds;
|
|
it.LaneIds = laneIds;
|
|
})
|
|
.First();
|
|
return DataResult<UserViewModel>.Success(data,MultiLanguageConst.DataQuerySuccess);
|
|
}
|
|
|
|
public async Task<DataResult> CheckUserCode(string code)
|
|
{
|
|
var isExist = await db.Queryable<SysUser>().Filter(null, true).Where(x => x.UserCode == code.Trim()).FirstAsync();
|
|
if (isExist != null)
|
|
{
|
|
return await Task.FromResult(DataResult.Failed("用户唯一编码已存在!", MultiLanguageConst.UserCodeExist));
|
|
}
|
|
return await Task.FromResult(DataResult.Successed("用户唯一编码检测成功!", MultiLanguageConst.DataQuerySuccess));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑用户信息
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public DataResult EditUser(UserReq model)
|
|
{
|
|
if (model.Id == 0)
|
|
{
|
|
var isExist = db.Queryable<SysUser>().Filter(null,true).Where(x => x.UserCode == model.UserCode).First();
|
|
if (isExist != null)
|
|
{
|
|
return DataResult.Failed("用户唯一编码已存在!",MultiLanguageConst.UserCodeExist);
|
|
}
|
|
|
|
#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
|
|
|
|
var data = model.MapTo<UserReq, SysUser>();
|
|
data.PinYinCode = PinYinUtil.GetFristLetter(data.UserName);
|
|
data.MD5Password = MD5Helper.MD5Encrypt(data.Password);
|
|
|
|
var entity = db.Insertable(data).ExecuteReturnEntity();
|
|
|
|
if (model.RoleIds.Count() > 0)
|
|
{
|
|
foreach (var item in model.RoleIds)
|
|
{
|
|
var roleUser = new SysRoleUser
|
|
{
|
|
RoleId = item,
|
|
UserId = entity.Id,
|
|
};
|
|
db.Insertable(roleUser).ExecuteCommand();
|
|
}
|
|
}
|
|
|
|
if (model.OrgIds.Count() > 0)
|
|
{
|
|
foreach (var item in model.OrgIds)
|
|
{
|
|
var orgUser = new SysOrgUser
|
|
{
|
|
OrgId = item,
|
|
UserId = entity.Id,
|
|
};
|
|
db.Insertable(orgUser).ExecuteCommand();
|
|
}
|
|
}
|
|
if (model.LaneIds.Count() > 0)
|
|
{
|
|
foreach (var item in model.LaneIds)
|
|
{
|
|
var laneUser = new SysUserLane
|
|
{
|
|
LaneId = item,
|
|
UserId = entity.Id,
|
|
};
|
|
db.Insertable(laneUser).ExecuteCommand();
|
|
}
|
|
}
|
|
return DataResult.Successed("添加成功!", entity.Id,MultiLanguageConst.DataCreateSuccess);
|
|
}
|
|
else
|
|
{
|
|
var userId = model.Id;
|
|
var info = db.Queryable<SysUser>().Where(x => x.Id == userId).First();
|
|
|
|
info = model.MapTo<UserReq, SysUser>();
|
|
info.PinYinCode = PinYinUtil.GetFristLetter(info.UserName);
|
|
|
|
db.Updateable(info).IgnoreColumns(it => new { it.TenantId,it.TenantName, it.Password,it.MD5Password }).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
|
|
|
|
#region 处理用户角色
|
|
|
|
var existRoles = db.Queryable<SysRoleUser>().Where(x => x.UserId == userId).Select(n => n.RoleId)
|
|
.ToArray();
|
|
|
|
|
|
IEnumerable<long?> delRoles = existRoles.AsQueryable().Except(model.RoleIds);
|
|
if (delRoles.Count() > 0)
|
|
{
|
|
db.Deleteable<SysRoleUser>()
|
|
.Where(it => it.UserId == userId && delRoles.Contains(it.RoleId))
|
|
.ExecuteCommand();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
#endregion 处理用户角色
|
|
|
|
|
|
#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 处理用户机构
|
|
|
|
#region 处理用户航线
|
|
|
|
var existLanes = db.Queryable<SysUserLane>().Where(x => x.UserId == userId).Select(n => n.LaneId)
|
|
.ToArray();
|
|
|
|
|
|
IEnumerable<long?> delLanes = existLanes.AsQueryable().Except(model.LaneIds);
|
|
if (delLanes.Count() > 0)
|
|
{
|
|
db.Deleteable<SysUserLane>()
|
|
.Where(it => it.UserId == userId && delLanes.Contains(it.LaneId))
|
|
.ExecuteCommand();
|
|
}
|
|
|
|
IEnumerable<long?> addLanes = model.LaneIds.AsQueryable().Except(existLanes);
|
|
if (addLanes.Count() > 0)
|
|
{
|
|
foreach (var item in addLanes)
|
|
{
|
|
var laneUser = new SysUserLane
|
|
{
|
|
LaneId = item,
|
|
UserId = model.Id,
|
|
};
|
|
db.Insertable(laneUser).ExecuteCommand();
|
|
}
|
|
}
|
|
#endregion 处理用户航线
|
|
|
|
return DataResult.Successed("更新成功!",MultiLanguageConst.DataUpdateSuccess);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 删除用户
|
|
/// </summary>
|
|
/// <param name="req"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult> BatchDelUser(IdModel req)
|
|
{
|
|
if (req.Ids.Contains(long.Parse(user.UserId)))
|
|
{
|
|
return await Task.FromResult(DataResult.Failed("不能删除本人账号!"));
|
|
}
|
|
var list = await db.Queryable<SysUser>().Where(x => req.Ids.Contains(x.Id)).ToListAsync();
|
|
if (list.Count > 0)
|
|
{
|
|
foreach (var item in list)
|
|
{
|
|
|
|
var roleUsers = await db.Queryable<SysRoleUser>().Where(x => x.UserId == item.Id).ToListAsync();
|
|
if (roleUsers.Count > 0)
|
|
await db.Deleteable(roleUsers).ExecuteCommandAsync();
|
|
|
|
var orgUsers = await db.Queryable<SysOrgUser>().Where(x => x.UserId == item.Id).ToListAsync();
|
|
if (orgUsers.Count > 0)
|
|
await db.Deleteable(orgUsers).ExecuteCommandAsync();
|
|
|
|
var userLanes = await db.Queryable<SysUserLane>().Where(x => x.UserId == item.Id).ToListAsync();
|
|
if (userLanes.Count > 0)
|
|
await db.Deleteable(userLanes).ExecuteCommandAsync();
|
|
}
|
|
await db.Deleteable(list).ExecuteCommandAsync();
|
|
|
|
}
|
|
return await Task.FromResult(DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess));
|
|
|
|
}
|
|
} |