You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
15 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.EventBus;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MimeKit;
using Myshipping.Application.Entity;
using Myshipping.Application.Helper;
using Myshipping.Core;
using MySqlX.XDevAPI.Common;
using Org.BouncyCastle.Crypto;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Channels;
using System.Threading;
using System.Threading.Tasks;
namespace Myshipping.Application
{
/// <summary>
/// 邮件接收记录服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "EmaiReceiveRecordService", Order = 9)]
public class EmaiReceiveRecordService : IEmaiReceiveRecordService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<EmaiReceiveRecordInfo> _emaiReceiveRecordeInfoRepository;
private readonly SqlSugarRepository<EmaiReceiveRecordDetailInfo> _emaiReceiveRecordDetailInfoRepository;
private readonly ILogger<EmaiReceiveRecordService> _logger;
public EmaiReceiveRecordService(ILogger<EmaiReceiveRecordService> logger,
SqlSugarRepository<EmaiReceiveRecordInfo> emaiReceiveRecordeInfoRepository,
SqlSugarRepository<EmaiReceiveRecordDetailInfo> emaiReceiveRecordDetailInfoRepository)
{
_logger = logger;
_emaiReceiveRecordeInfoRepository = emaiReceiveRecordeInfoRepository;
_emaiReceiveRecordDetailInfoRepository = emaiReceiveRecordDetailInfoRepository;
}
#region 获取邮件接收记录详情
/// <summary>
/// 获取邮件接收记录详情
/// </summary>
/// <param name="gid">邮件接收记录主键</param>
/// <returns>返回回执</returns>
[HttpGet("/EmaiReceiveRecord/GetInfo")]
public async Task<TaskManageOrderResultDto> GetInfo(string gid)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
if (string.IsNullOrWhiteSpace(gid))
{
throw Oops.Oh($"邮件接收记录主键不能为空", typeof(InvalidOperationException));
}
var model = await _emaiReceiveRecordeInfoRepository.AsQueryable().FirstAsync(a => a.GID == gid);
if (model == null)
throw Oops.Oh($"邮件接收记录获取失败,邮件接收记录不存在或已作废", typeof(InvalidOperationException));
var showModel = model.Adapt<EmaiReceiveRecordDto>();
if (!string.IsNullOrWhiteSpace(model.MAIL_ATTACHMENTS))
{
showModel.AttachmentList = model.MAIL_ATTACHMENTS.Split(new char[] { ';' }).Where(t => !string.IsNullOrWhiteSpace(t)).Select(t => t.Trim()).ToList();
}
result.succ = true;
result.ext = showModel;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取邮件接收记录异常,原因:{ex.Message}";
}
return result;
}
#endregion
#region 删除
/// <summary>
/// 删除
/// </summary>
/// <param name="gIds">邮件接收记录主键数组</param>
/// <returns>返回回执</returns>
[HttpPost("/EmaiReceiveRecord/Delete")]
public async Task<TaskManageOrderResultDto> Delete([FromBody] string[] gIds)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
if (gIds.Length == 0)
{
throw Oops.Oh($"邮件接收记录主键数组不能为空", typeof(InvalidOperationException));
}
var list = _emaiReceiveRecordeInfoRepository.AsQueryable()
.Where(a => gIds.Contains(a.GID)).ToList();
if (list.Count == 0)
throw Oops.Oh($"邮件接收记录获取失败,请确认邮件接收记录是否存在", typeof(InvalidOperationException));
if (list.Count != gIds.Length)
throw Oops.Oh($"部分邮件接收记录获取失败,请确认邮件接收记录是否存在", typeof(InvalidOperationException));
List<TaskManageOrderResultDto> rltList = new List<TaskManageOrderResultDto>();
list.ForEach(async record =>
{
await _emaiReceiveRecordeInfoRepository.UpdateAsync(x => x.GID == record.GID,
x => new EmaiReceiveRecordInfo { IsDeleted = true });
});
result.succ = true;
result.msg = "删除成功";
_logger.LogInformation("删除邮件接收记录成功 ids={id} user={usr}", gIds, UserManager.UserId);
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"删除邮件接收记录异常,原因:{ex.Message}";
}
return result;
}
#endregion
#region 邮件接收记录台账查询
/// <summary>
/// 邮件接收记录台账查询
/// </summary>
/// <param name="QuerySearch">邮件接收记录台账查询请求</param>
/// <returns>返回结果</returns>
[HttpPost("/EmaiReceiveRecord/GetPage")]
public async Task<SqlSugarPagedList<EmaiReceiveRecordPageDto>> GetPageAsync(QueryEmaiReceiveRecordDto QuerySearch)
{
var query = _emaiReceiveRecordeInfoRepository.AsQueryable();
#region 查询条件
//接收时间起始
if (!string.IsNullOrWhiteSpace(QuerySearch.RecBegin))
{
DateTime currDate = DateTime.MinValue;
if (!DateTime.TryParse(QuerySearch.RecBegin, out currDate))
throw Oops.Oh($"接收时间起始日期格式错误,{QuerySearch.RecBegin}");
query = query.Where(t => t.REC_TIME >= currDate);
}
//接收时间结束
if (!string.IsNullOrWhiteSpace(QuerySearch.RecEnd))
{
DateTime currDate = DateTime.MinValue;
if (!DateTime.TryParse(QuerySearch.RecEnd, out currDate))
throw Oops.Oh($"接收时间结束日期格式错误,{QuerySearch.RecEnd}");
currDate = currDate.AddDays(1);
query = query.Where(t => t.REC_TIME < currDate);
}
//更新时间起始
if (!string.IsNullOrWhiteSpace(QuerySearch.UpdateBegin))
{
DateTime currDate = DateTime.MinValue;
if (!DateTime.TryParse(QuerySearch.UpdateBegin, out currDate))
throw Oops.Oh($"更新时间起始日期格式错误,{QuerySearch.UpdateBegin}");
query = query.Where(t => t.UpdatedTime >= currDate);
}
//更新时间结束
if (!string.IsNullOrWhiteSpace(QuerySearch.UpdateEnd))
{
DateTime currDate = DateTime.MinValue;
if (!DateTime.TryParse(QuerySearch.UpdateEnd, out currDate))
throw Oops.Oh($"更新时间结束日期格式错误,{QuerySearch.UpdateEnd}");
currDate = currDate.AddDays(1);
query = query.Where(t => t.UpdatedTime < currDate);
}
if (!string.IsNullOrWhiteSpace(QuerySearch.MailSubject))
{
query = query.Where(t => t.MAIL_SUBJECT.Contains(QuerySearch.MailSubject));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.Sender))
{
query = query.Where(t => t.SENDER.Contains(QuerySearch.Sender));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.MailAccount))
{
query = query.Where(t => t.MAIL_ACCOUNT.Contains(QuerySearch.MailAccount));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.StepNote))
{
query = query.Where(t => t.STEP_NOTE.Contains(QuerySearch.StepNote));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.Status))
{
query = query.Where(t => t.STATUS.Equals(QuerySearch.Status));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.IsParse))
{
query = query.Where(t => t.IS_PARSE == (QuerySearch.IsParse == "1" ? true : false));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.IsProcess))
{
query = query.Where(t => t.IS_PROCESS == (QuerySearch.IsProcess == "1" ? true : false));
}
#endregion
string entityOrderCol = "REC_TIME";
//这里因为返回给前端的台账数据是DTO所以这里排序时候需要转换成Entity对应的字段
if (!string.IsNullOrWhiteSpace(QuerySearch.SortField))
entityOrderCol = MapsterExtHelper.GetAdaptProperty<QueryEmaiReceiveRecordDto, EmaiReceiveRecordInfo>(QuerySearch.SortField);
var entities = await query
.OrderBy(entityOrderCol + (QuerySearch.descSort ? " desc " : " asc "))
.ToPagedListAsync(QuerySearch.PageNo, QuerySearch.PageSize);
return entities.Adapt<SqlSugarPagedList<EmaiReceiveRecordPageDto>>();
}
#endregion
/// <summary>
/// 获取重新解析邮件详情
/// </summary>
/// <param name="gid">邮件接收记录主键</param>
/// <returns>返回回执</returns>
[HttpPost("/EmaiReceiveRecord/GetReParseInfo")]
public async Task<TaskManageOrderResultDto> GetReParseInfo(string gid)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
if (string.IsNullOrWhiteSpace(gid))
{
throw Oops.Oh($"邮件接收记录主键不能为空", typeof(InvalidOperationException));
}
var model = await _emaiReceiveRecordeInfoRepository.AsQueryable().FirstAsync(a => a.GID == gid);
if (model == null)
throw Oops.Oh($"邮件接收记录获取失败,邮件接收记录不存在或已作废", typeof(InvalidOperationException));
//这里读取一下收件处理明细表
var detailList = _emaiReceiveRecordDetailInfoRepository.AsQueryable()
.Where(a => a.P_GID == model.GID).ToList();
var showModel = model.Adapt<EmaiReceiveRecordDto>();
if (detailList != null && detailList.Count > 0)
{
showModel.detailList = detailList.OrderByDescending(a => a.CreatedTime)
.Select(a => new EmaiReceiveRecordDetailDto
{
GID = a.GID,
FileName = a.FILE_NAME,
Status = a.STATUS,
OperType = a.OPER_TYPE,
ResponseNote = a.RESPONSE_NOTE,
CreatedTime = a.CreatedTime,
UpdatedTime = a.UpdatedTime
})
.ToList();
}
result.succ = true;
result.msg = "成功";
result.ext = showModel;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取重新解析邮件详情异常,原因:{ex.Message}";
}
return result;
}
#region 获取邮件接收记录的邮件详情
/// <summary>
/// 获取邮件接收记录的邮件详情
/// </summary>
/// <param name="gid">邮件接收记录主键</param>
/// <returns>返回回执</returns>
[HttpPost("/EmaiReceiveRecord/GetEmailInfo")]
public async Task<TaskManageOrderResultDto> GetEmailInfo(string gid)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
if (string.IsNullOrWhiteSpace(gid))
{
throw Oops.Oh($"邮件接收记录主键不能为空", typeof(InvalidOperationException));
}
var model = await _emaiReceiveRecordeInfoRepository.AsQueryable().FirstAsync(a => a.GID == gid);
if (model == null)
throw Oops.Oh($"邮件接收记录获取失败,邮件接收记录不存在或已作废", typeof(InvalidOperationException));
if (string.IsNullOrWhiteSpace(model.MAIL_FILEPATH))
throw Oops.Oh($"邮件文件提取失败,请联系管理员", typeof(InvalidOperationException));
//读取邮件,解析邮件正文
MimeMessage mimeMsg = MimeMessage.Load(model.MAIL_FILEPATH);
var strBody = mimeMsg.HtmlBody;
result.succ = true;
result.msg = "成功";
result.ext = strBody;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取邮件接收记录的邮件详情异常,原因:{ex.Message}";
}
return result;
}
#endregion
/// <summary>
/// 重新解析邮件
/// </summary>
/// <param name="model">邮件接收记录详情</param>
/// <returns>返回回执</returns>
[HttpPost("/EmaiReceiveRecord/ReParseEmail")]
public async Task<TaskManageOrderResultDto> ReParseEmail(EmaiReceiveRecordDto model)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
result.succ = true;
result.msg = "成功";
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取重新解析邮件详情异常,原因:{ex.Message}";
}
return result;
}
/// <summary>
/// 重新解析邮件(批量)
/// </summary>
/// <param name="gIds">邮件接收记录主键数组</param>
/// <returns>返回回执</returns>
[HttpPost("/EmaiReceiveRecord/ReParseEmailBatch")]
public async Task<TaskManageOrderResultDto> ReParseEmailBatch([FromBody] string[] gIds)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
result.succ = true;
result.msg = "成功";
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取重新解析邮件详情异常,原因:{ex.Message}";
}
return result;
}
}
}