using GDZZ.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using GDZZ.Application.Entity;
using Microsoft.Extensions.Options;
using GDZZ.Core.Entity;
using System.Threading;
using Furion.FriendlyException;
using GDZZ.Core.Service;
using System.Collections.Generic;
namespace GDZZ.Application
{
///
/// 基础用户服务
///
[ApiDescriptionSettings("Application",Name = "BaseUser", Order = 1)]
public class BaseUserService : IBaseUserService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository Self; //职业仓储
private readonly SqlSugarRepository SysPosRep;//职位查询
private readonly SqlSugarRepository _sysUserRep; // 用户表仓储
private readonly SqlSugarRepository _sysOnlineUerRep; // 在线用户表仓储
//服务
private readonly ISysEmpService _sysEmpService;
public BaseUserService(SqlSugarRepository rep,
SqlSugarRepository Self,
SqlSugarRepository SysPosRep,ISysEmpService _sysEmpService,
SqlSugarRepository _sysOnlineUerRep,
SqlSugarRepository _sysUserRep)
{
_rep = rep;
this.Self= Self;
this._sysEmpService = _sysEmpService;
this.SysPosRep = SysPosRep;
this._sysUserRep = _sysUserRep;
this._sysOnlineUerRep= _sysOnlineUerRep;
}
///
/// 更新基础用户
///
///
///
[HttpPost("/Mini/User/edit")]
public async Task Update(UpdateBaseUserInput input)
{
//后台系统用户
var sysuser = await this._sysUserRep.SingleAsync(x => x.Id == UserManager.UserId);
if (sysuser == null)
{
throw Oops.Oh("修改失败!", sysuser);
}
var user = this._rep.AsQueryable()
.Filter("TenantId", true)
.First(x => x.CreatedUserId == UserManager.UserId);
var Self = await this.Self.FirstOrDefaultAsync(x => x.CreatedUserId == sysuser.Id);
if(Self.IsEmpty())
{
await this.Self.InsertAsync(new SeIF
{
Name = input.Self,
});
}
else
{
Self.Name = input.Self;
await this.Self.AsUpdateable(Self).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
user.AvatarUrl = input.AvatarUrl;
user.Describe = input.Describe;
sysuser.Avatar = input.AvatarUrl;
sysuser.Sex = input.Sex;
await this._sysUserRep.AsUpdateable(sysuser).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
await this._rep.AsUpdateable(user).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取教师列表
///
///
[HttpGet("/Mini/BaseUser/GetTeacherList")]
public async Task GetTeacherList()
{
List userls = new List();
var users = await this._sysUserRep.AsQueryable().Filter("TenantId", true)
.LeftJoin((u, o) => u.Id == o.UserId)
.Where((u, o) => u.AdminType == AdminType.None&&o.ConnectionId !=null)
.Select((u, o) => new UserOutput
{
Account = u.Account,
Avatar = u.Avatar,
Birthday = u.Birthday,
Email = u.Email,
Id = u.Id.ToString(),
Name = u.Name,
NickName = u.NickName,
Phone = u.Phone,
Sex = (int)u.Sex,
Status = (int)u.Status,
Tel = u.Tel,
TenantId = u.TenantId,
ConnectionId = o.ConnectionId,
Profile = u.Profile,
})
.ToListAsync();
foreach (var user in users)
{
var userDto = user.Adapt();
userDto.SysEmpInfo = await _sysEmpService.GetEmpTSInfo(long.Parse(user.Id));
if (!userDto.SysEmpInfo.Positions.Count.IsNullOrZero())
userls.Add(userDto);
}
return userls;
}
///
/// 获取用户职业
///
///
///
[HttpGet("/Mini/User/GetUserSelf")]
public async Task GetUserSelf(long userID)
{
return await this.Self.FirstOrDefaultAsync(s => s.CreatedUserId == userID);
}
///
/// 分页查询基础用户
///
///
///
[HttpGet("/BaseUser/page")]
public async Task Page([FromQuery] BaseUserInput input)
{
var entities = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.UserName), u => u.UserName.Contains(input.UserName.Trim()))
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
///
/// 增加基础用户
///
///
///
[HttpPost("/BaseUser/add")]
public async Task Add(AddBaseUserInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
}
///
/// 删除基础用户
///
///
///
[HttpPost("/BaseUser/delete")]
public async Task Delete(DeleteBaseUserInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
///
/// 获取基础用户
///
///
///
[HttpGet("/BaseUser/Detail")]
public async Task Get([FromQuery] QueryeBaseUserInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
///
/// 获取基础用户列表
///
///
///
[HttpGet("/BaseUser/list")]
public async Task List([FromQuery] BaseUserInput input)
{
return await _rep.ToListAsync();
}
}
}