using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
using Mapster;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlexJobApi.UserServer.Application.UserInfos.Commands
{
///
/// 用户信息命令处理器
///
public class UserInfoCommandHandler(
IRepository rep,
IRepository repUserBankCard
) :
IRequestHandler,
IRequestHandler,
IRequestHandler
{
private readonly IRepository rep = rep;
private readonly IRepository repUserBankCard = repUserBankCard;
///
/// 设置用户信息状态
///
///
///
///
public async Task Handle(SetUserInfoStatusCommand request, CancellationToken cancellationToken)
{
var entities = await rep.AsQueryable()
.Where(it => request.Ids.Contains(it.Id) && it.Status != request.Status)
.ToListAsync(cancellationToken);
foreach (var entity in entities)
{
entity.Status = request.Status;
}
return entities.Count;
}
///
/// 设置用户信息角色
///
///
///
///
public async Task Handle(SetUserInfoRolesCommand request, CancellationToken cancellationToken)
{
var entity = await rep.AsQueryable()
.Include(it => it.UserRoles)
.Where(it => it.Id == request.UserInfoId)
.FirstOrDefaultAsync(cancellationToken);
if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "用户");
entity.UserRoles = request.RoleIds
.Select(it => new UserRole
{
RoleId = it
})
.ToList();
await rep.UpdateAsync(entity);
return entity.UserRoles.Count;
}
///
/// 保存用户银行卡信息
///
///
///
///
public async Task Handle(SavePersonalUserBankCardCommand request, CancellationToken cancellationToken)
{
var logier = JwtUtils.GetCurrentLogier();
var entity = await repUserBankCard.AsQueryable()
.Where(it => it.UserId == logier.Id)
.FirstOrDefaultAsync();
var add = false;
if (entity == null)
{
entity = new UserBankCard();
entity.UserId = logier.Id;
add = true;
}
request.Adapt(entity);
if (entity.Access == EnumEnterpriseWalletAccess.Alipay)
{
entity.Bank = "支付宝";
entity.BankBranch = null;
}
if (add)
{
await repUserBankCard.InsertAsync(entity);
}
else
{
await repUserBankCard.UpdateAsync(entity);
}
return entity.Id;
}
}
}