一、DTO
using System;
using System.Collections.Generic;
using System.Text;
namespace Leeway.Operation.Dto
{
public class OperationLogDto
{
/// <summary>
/// 编码
/// </summary>
public string Code { get; set; }
/// <summary>
/// 操作动作
/// </summary>
public string ActionName { get; set; }
/// <summary>
/// 单据编号
/// </summary>
public string BillNo { get; set; }
/// <summary>
/// 操作备注
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 执行动作的状态值
/// </summary>
public string Status { get; set; }
/// <summary>
/// 执行动作的状态值名称
/// </summary>
public string StatusName { get; set; }
/// <summary>
/// 操作人userid
/// </summary>
public Guid EmployeeUserId { get; set; }
/// <summary>
/// 操作人工号
/// </summary>
public string EmployeeNo { get; set; }
public DateTime CreationTime { get; set; }
/// <summary>
/// 操作人姓名
/// </summary>
public string EmployeeName
{
get; set;
}
}
}
二、新建事务总线处理器
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
namespace EventHandlers
{
public class LeewayEventHandler : ILocalEventHandler<OperationLogDto>, ITransientDependency
{
ILeewayService _leewayService;
public StatementEventHandler(ILeewayService leewayService)
{
_leewayService= leewayService;
}
public async Task HandleEventAsync(OperationLogDto eventData)
{
await _leewayService.AddOperationHistoryAsync(eventData);
}
}
}
三、ILeewayService
public iterface ILeewayService{
Task<bool> AddOperationHistoryAsync(OperationLogDto dto);
}
四、LeewayService
public class LeewayService : ApplicationService, ILeewayService
{
private readonly ILocalEventBus _localEventBus;
public LeewayService(ILocalEventBus localEventBus)
{
_localEventBus = localEventBus;
}
public async Task<bool> AddOperationHistoryAsync(OperationLogDto dto)
{
//这里用到AutoMapper,需在相关类里添加,例如新建类ApplicationAutoMapperProfile
var model = ObjectMapper.Map<OperationLogDto, OperationLog>(dto);
var result = await _operationLogRepository.InsertAsync(model); ;
return result != null;
}
public async Task<bool> test(){
var user = await _userRepository.FindAsync(CurrentUser.GetId());
await _localEventBus.PublishAsync(new OperationLogDto
{
Code = "Leeway",
ActionName = "操作日志",
BillNo = "Test111111",
Remark = "",
Status = "1",
EmployeeUserId = new Guid(CurrentUser.Id.ToString()),
EmployeeNo = CurrentUser.UserName,
EmployeeName = user.Surname + user.Name
});
return true;
}
}