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; /// /// /// public class UserService : IUserService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; private readonly ICommonService _commonService; /// /// /// /// public UserService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); _commonService = _serviceProvider.GetRequiredService(); } /// /// 列表查询 /// /// /// public DataResult> GetListByPage(PageRequest request) { var query = _commonService.GetDataRuleFilter(); //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition); var data = query .Where(x => x.UserCode != "admin") .Where(whereList).Select() .ToQueryPage(request.PageCondition); return data; } /// /// 获取单个用户信息 /// /// /// public DataResult GetUserInfo(string id) { var roleIds = db.Queryable().Where(x => x.UserId == long.Parse(id)).Select(x => x.RoleId).ToArray(); var data = db.Queryable() .Where(x => x.Id == long.Parse(id)) .Select() .Mapper(it => { it.RoleIds = roleIds; }) .First(); return DataResult.Success(data); } /// /// 编辑用户信息 /// /// /// public DataResult EditUser(UserReq model) { if (model.Id == 0) { var isExist = db.Queryable().Where(x => x.UserCode == model.UserCode).First(); if (isExist != null) { return DataResult.Failed("用户唯一编码已存在!"); } var data = model.MapTo(); data.PinYinCode = PinYinUtil.GetFristLetter(data.UserName); data.MD5Password = MD5Helper.MD5Encrypt(data.Password); // //处理客户companyId // var companyId = db.Queryable().Filter(null, true).First(x => x.Id == model.Id)?.CompanyId; // if (!companyId.IsNullOrEmpty()) // { // data.CompanyId = companyId; // } // data.CompanyId = "Comcab2d43f60454327af30a131fc1d3abd"; //data.GID = Guid.NewGuid(); 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(); } } return DataResult.Successed("添加成功!", entity.Id); } else { var userId = model.Id; var info = db.Queryable().Where(x => x.Id == userId).First(); info = model.MapTo(); info.PinYinCode = PinYinUtil.GetFristLetter(info.UserName); db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); #region 处理用户角色 var existRoles = db.Queryable().Where(x => x.UserId == userId).Select(n => n.RoleId) .ToArray(); IEnumerable delRoles = existRoles.AsQueryable().Except(model.RoleIds); if (delRoles.Count() > 0) { db.Deleteable() .Where(it => it.UserId == userId && delRoles.Contains(it.RoleId)) .ExecuteCommand(); } IEnumerable 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 处理用户角色 return DataResult.Successed("更新成功!"); } } }