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.

169 lines
5.6 KiB
C#

using Ds.Modules.DsEntity.Auth;
using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
using DS.Module.UserModule;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
namespace DS.WMS.Core.System.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<Ds_Sys_Users>();
//序列化查询条件
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)
{
if (string.IsNullOrEmpty(id))
{
return DataResult<UserViewModel>.Failed("id 为空 ");
}
var roleIds = db.Queryable<Ds_Sys_User_Roles>()
.Where(x => x.UserId == long.Parse(id))
.Select(x => x.RoleId).ToArray();
var data = db.Queryable<Ds_Sys_Users>()
.Where(x => x.Id == long.Parse(id))
.Select<UserViewModel>()
.Mapper(it => { it.RoleIds = roleIds; })
.First();
return DataResult<UserViewModel>.Success(data);
}
/// <summary>
/// 编辑用户信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DataResult EditUser(UserInput model)
{
if (model.Id.IsNullOrEmpty())
{
var isExist = db.Queryable<Ds_Sys_Users>().Where(x => x.UserCode == model.UserCode).First();
if (isExist != null)
{
return DataResult.Failed("用户唯一编码已存在!");
}
var data = model.MapTo<UserInput, Ds_Sys_Users>();
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())
// {
// data.CompanyId = companyId;
// }
data.CompanyId = "Comcab2d43f60454327af30a131fc1d3abd";
//data.GID = Guid.NewGuid();
data.Id = SnowFlakeSingle.Instance.NextId();
var user = db.Insertable(data).ExecuteReturnEntity();
if (model.RoleIds.Count() > 0)
{
foreach (var item in model.RoleIds)
{
var roleUser = new Ds_Sys_User_Roles
{
RoleId = long.Parse(item),
UserId = user.Id,
};
db.Insertable(roleUser).ExecuteCommand();
}
}
return DataResult.Successed("添加成功!", user.Id);
}
else
{
var longid = long.Parse(model.Id);
var info = db.Queryable<Ds_Sys_Users>().Where(x => x.Id == longid).First();
info = model.MapTo<UserInput, Ds_Sys_Users>();
info.PinYinCode = PinYinUtil.GetFristLetter(info.UserName);
db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
#region 处理用户角色
var existRoles = db.Queryable<Ds_Sys_User_Roles>().Where(x => x.UserId == longid).Select(n => n.RoleId)
.ToArray();
var longrod = new long[10];// model.RoleIds;
IEnumerable<long> delRoles = existRoles.AsQueryable().Except(longrod);
if (delRoles.Count() > 0)
{
db.Deleteable<Ds_Sys_User_Roles>()
.Where(it => it.UserId == longid && delRoles.Contains(it.RoleId))
.ExecuteCommand();
}
var longex = new string[10];//existRoles
//IEnumerable<long> addRoles = model.RoleIds.AsQueryable().Except(longex);
//if (addRoles.Count() > 0)
//{
// foreach (var item in addRoles)
// {
// var roleUser = new Ds_Sys_User_Roles
// {
// RoleId = item,
// UserId = long.Parse(model.Id),
// };
// db.Insertable(roleUser).ExecuteCommand();
// }
//}
#endregion 处理用户角色
return DataResult.Successed("更新成功!");
}
}
}