using Hotline.Api.Filter;
using Hotline.Application.Users;
using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Tels;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.Permissions;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Settings.CommonOpinions;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.Users;
using Hotline.Share.Enums.CallCenter;
using Hotline.Share.Enums.Order;
using Hotline.Share.Enums.User;
using Hotline.Share.Requests;
using Hotline.Tools;
using Hotline.Users;
using MapsterMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using SqlSugar;
using System.Data;
using XF.Domain.Authentications;
using XF.Domain.Exceptions;
using XF.Domain.Options;
using XF.Domain.Repository;
using XF.Utility.EnumExtensions;
namespace Hotline.Api.Controllers;
///
/// 用户管理相关接口
///
public class UserController : BaseController
{
private readonly ISessionContext _sessionContext;
private readonly IUserDomainService _userDomainService;
private readonly ITelRepository _telRepository;
private readonly IRepository _userRepository;
private readonly IWorkRepository _workRepository;
private readonly ITelCacheManager _telCacheManager;
private readonly IUserCacheManager _userCacheManager;
private readonly IMapper _mapper;
private readonly IAccountRepository _accountRepository;
private readonly IAccountDomainService _accountDomainService;
private readonly IOptions _identityConfigurationAccessor;
private readonly ITelRestRepository _telRestRepository;
private readonly IRepository _commonOpinionRepository;
private readonly IUserApplication _userApplication;
public UserController(
ISessionContext sessionContext,
IUserDomainService userDomainService,
ITelRepository telRepository,
IRepository userRepository,
IWorkRepository workRepository,
ITelCacheManager telCacheManager,
IUserCacheManager userCacheManager,
IMapper mapper,
IAccountRepository accountRepository,
IAccountDomainService accountDomainService,
IOptions identityConfigurationAccessor,
ITelRestRepository telRestRepository,
IRepository commonOpinionRepository,
IUserApplication userApplication)
{
_sessionContext = sessionContext;
_userDomainService = userDomainService;
_telRepository = telRepository;
_userRepository = userRepository;
_workRepository = workRepository;
_telCacheManager = telCacheManager;
_userCacheManager = userCacheManager;
_mapper = mapper;
_accountRepository = accountRepository;
_accountDomainService = accountDomainService;
_identityConfigurationAccessor = identityConfigurationAccessor;
_telRestRepository = telRestRepository;
_commonOpinionRepository = commonOpinionRepository;
_userApplication = userApplication;
}
#region 小休申请
///
/// 小休申请列表
///
///
///
[HttpGet("rest-apply-paged")]
public async Task> RestApplyList([FromQuery] RestPagedDto dto)
{
var (total, items) = await _telRestRepository.Queryable(includeDeleted: false)
.WhereIF(!string.IsNullOrEmpty(dto.KeyWords),
d => d.UserName.Contains(dto.KeyWords) || d.StaffNo.Contains(dto.KeyWords))
.WhereIF(dto.BeginTime != null && dto.BeginTime != DateTime.MinValue, d => d.CreationTime >= dto.BeginTime)
.WhereIF(dto.EndTime != null && dto.EndTime != DateTime.MinValue, d => d.CreationTime <= dto.EndTime)
.WhereIF(!string.IsNullOrEmpty(dto.Reason), d => d.Reason == dto.Reason)
.WhereIF(dto.Status != null, d => d.ApplyStatus == dto.Status)
.OrderByDescending(d => d.CreationTime)
.ToPagedListAsync(dto.PageIndex, dto.PageSize, HttpContext.RequestAborted);
return new PagedDto(total, _mapper.Map>(items));
}
///
/// 小休申请页面基础信息
///
///
[HttpGet("rest-apply-basedata")]
public object RestApplyBaseData()
{
return new
{
RestApplyStatus = EnumExts.GetDescriptions(),
RestReason = _commonOpinionRepository.Queryable()
.Where(x => x.CommonType == Share.Enums.Settings.ECommonType.RestReason).ToList()
};
}
#endregion
///
/// 上班
///
[HttpPost("on-duty")]
public async Task OnDuty([FromBody] OnDutyDto dto)
{
var telNo = dto.TelNo;
if (string.IsNullOrEmpty(telNo))
{
var user = await _userRepository.GetAsync(d => d.Id == _sessionContext.RequiredUserId,
HttpContext.RequestAborted);
if (user == null)
throw UserFriendlyException.SameMessage("无效用户编号");
if (string.IsNullOrEmpty(user.DefaultTelNo))
throw UserFriendlyException.SameMessage("未设置默认分机号");
telNo = user.DefaultTelNo;
}
var tel = _telCacheManager.GetTel(telNo);
await _userDomainService.OnDutyAsync(_sessionContext.RequiredUserId, tel, HttpContext.RequestAborted);
}
///
/// 下班
///
[HttpPost("off-duty")]
public Task OffDuty()
{
return _userDomainService.OffDutyAsync(_sessionContext.RequiredUserId, HttpContext.RequestAborted);
}
///
/// 分页查询用户
///
///
[HttpGet("paged")]
public async Task> QueryPaged([FromQuery] UserPagedDto dto)
{
var query = _userApplication.QueryPaged(dto);
var (total, items) = await query.ToPagedListAsync(dto, HttpContext.RequestAborted);
//var (total, items) = await _userRepository.Queryable(includeDeleted: true)
// .Includes(d => d.Account)
// .Includes(d => d.Roles)
// .Includes(d => d.Organization)
// .Where(d => d.Account.AccountType == EAccountType.Personal && d.Id != SysAccountSeedData.Id)
// .WhereIF(_sessionContext.OrgIsCenter == false, d => d.OrgId.StartsWith(_sessionContext.RequiredOrgId))
// .WhereIF(!string.IsNullOrEmpty(dto.Keyword),
// d => d.Name.Contains(dto.Keyword!) || d.PhoneNo.Contains(dto.Keyword!) ||
// d.Account.UserName.Contains(dto.Keyword))
// .WhereIF(!string.IsNullOrEmpty(dto.OrgCode), d => d.OrgId == dto.OrgCode)
// .WhereIF(!string.IsNullOrEmpty(dto.Role), d => d.Roles.Any(x => x.Id == dto.Role))
// .WhereIF(!string.IsNullOrEmpty(dto.Name), d => d.Name.Contains(dto.Name))
// .WhereIF(!string.IsNullOrEmpty(dto.PhoneNo), d => d.PhoneNo.Contains(dto.PhoneNo))
// .WhereIF(!string.IsNullOrEmpty(dto.OrgName), d => d.Organization.Name.Contains(dto.OrgName))
// .OrderBy(d => d.Account.Status)
// .OrderBy(d => d.Organization.OrgType)
// //.OrderBy(d => d.Organization.Id)
// .OrderByDescending(d => d.CreationTime)
// .ToPagedListAsync(dto.PageIndex, dto.PageSize, HttpContext.RequestAborted);
return new PagedDto(total, _mapper.Map>(items));
}
///
/// 分页查询用户---导出
///
///
///
[HttpPost("paged/_export")]
public async Task QueryPagedExport([FromBody] ExportExcelDto dto)
{
var query = _userApplication.QueryPaged(dto.QueryDto);
List data;
if (dto.IsExportAll)
{
data = await query.ToListAsync(HttpContext.RequestAborted);
}
else
{
var (_, items) = await query.ToPagedListAsync(dto.QueryDto, HttpContext.RequestAborted);
data = items;
}
var dataDtos = _mapper.Map>(data);
dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass(dto.ColumnInfos);
var dtos = dataDtos
.Select(stu => _mapper.Map(stu, typeof(UserDto), dynamicClass))
.Cast