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.

360 lines
14 KiB
C#

using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.Helper;
using Myshipping.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Myshipping.Application
{
/// <summary>
///
/// </summary>
[ApiDescriptionSettings("Application", Name = "EmailParserConfigServices", Order = 9)]
public class EmailParserConfigService : IEmailParserConfigService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<EmailParserConfigInfo> _emailParserConfigInfoRepository;
private readonly SqlSugarRepository<EmailUserAccountParserRelationInfo> _emailUserAccountParserRelationInfoRepository;
private readonly SqlSugarRepository<EmailExcuteCodeInjectConfigInfo> _emailExcuteCodeInjectConfigInfoRepository;
private readonly ILogger<EmailParserConfigService> _logger;
public EmailParserConfigService(ILogger<EmailParserConfigService> logger,
SqlSugarRepository<EmailParserConfigInfo> emailParserConfigInfoRepository,
SqlSugarRepository<EmailUserAccountParserRelationInfo> emailUserAccountParserRelationInfoRepository,
SqlSugarRepository<EmailExcuteCodeInjectConfigInfo> emailExcuteCodeInjectConfigInfoRepository)
{
_logger = logger;
_emailParserConfigInfoRepository = emailParserConfigInfoRepository;
_emailUserAccountParserRelationInfoRepository = emailUserAccountParserRelationInfoRepository;
_emailExcuteCodeInjectConfigInfoRepository = emailExcuteCodeInjectConfigInfoRepository;
}
/// <summary>
/// 保存邮件解析配置
/// </summary>
/// <param name="info">邮件解析配置信息</param>
/// <returns>返回回执</returns>
[HttpPost("/EmailParserConfig/Save")]
public async Task<TaskManageOrderResultDto> Save(EmailParserConfigDto info)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
/*
1
2
*/
string gid = info.GID;
if (string.IsNullOrWhiteSpace(info.ParserCode))
throw Oops.Oh($"解析器代码不能为空", typeof(InvalidOperationException));
if (string.IsNullOrWhiteSpace(info.ParserName))
throw Oops.Oh($"解析器名称不能为空", typeof(InvalidOperationException));
if (string.IsNullOrWhiteSpace(info.SubjectRegexPattern)
&& string.IsNullOrWhiteSpace(info.BodyRegexPattern)
&& string.IsNullOrWhiteSpace(info.SenderRegexPattern)
&& string.IsNullOrWhiteSpace(info.AttachRegexPattern))
throw Oops.Oh($"邮件标题匹配、邮件正文匹配、邮件发送人匹配、邮件附件匹配至少要填写一项匹配", typeof(InvalidOperationException));
DateTime nowDate = DateTime.Now;
if (string.IsNullOrWhiteSpace(gid))
{
var num = _emailParserConfigInfoRepository.AsQueryable().Count(a => a.PARSER_CODE.Equals(info.ParserCode));
if (num > 0)
throw Oops.Oh($"解析器代码不能重复", typeof(InvalidOperationException));
gid = IDGen.NextID().ToString();
var entity = info.Adapt<EmailParserConfigInfo>();
entity.GID = gid;
entity.CreatedTime = nowDate;
entity.UpdatedTime = nowDate;
entity.CreatedUserId = UserManager.UserId;
entity.CreatedUserName = UserManager.Name;
await _emailParserConfigInfoRepository.InsertAsync(entity);
}
else
{
var num = _emailParserConfigInfoRepository.AsQueryable().Count(a =>
a.PARSER_CODE.Equals(info.ParserCode) && a.GID != gid);
if (num > 0)
throw Oops.Oh($"解析器代码不能重复", typeof(InvalidOperationException));
var model = _emailParserConfigInfoRepository.AsQueryable()
.First(a => a.GID == gid);
var entity = info.Adapt<EmailParserConfigInfo>();
entity.UpdatedTime = nowDate;
entity.UpdatedUserId = UserManager.UserId;
entity.UpdatedUserName = UserManager.Name;
await _emailParserConfigInfoRepository.AsUpdateable(entity)
.IgnoreColumns(it => new
{
it.TenantId,
it.TenantName,
it.CreatedTime,
it.CreatedUserId,
it.CreatedUserName,
it.IsDeleted
}).ExecuteCommandAsync();
}
result.succ = true;
result.msg = "保存成功";
result.ext = gid;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"保存派车异常,原因:{ex.Message}";
}
return result;
}
#region 获取邮件解析配置详情
/// <summary>
/// 获取邮件解析配置详情
/// </summary>
/// <param name="gid">邮件解析配置主键</param>
/// <returns>返回回执</returns>
[HttpGet("/EmailParserConfig/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 _emailParserConfigInfoRepository.AsQueryable()
.FirstAsync(a => a.GID == gid);
if (model == null)
throw Oops.Oh($"邮件解析配置获取失败,邮件解析配置不存在或已作废", typeof(InvalidOperationException));
var showModel = model.Adapt<EmailParserConfigDto>();
if(!string.IsNullOrWhiteSpace(model.INJECT_ID))
{
var injectModel = _emailExcuteCodeInjectConfigInfoRepository.AsQueryable()
.First(a => a.GID == model.INJECT_ID);
if(injectModel != null)
{
showModel.InjectCode = injectModel.INJECT_CODE;
showModel.InjectName = injectModel.INJECT_NAME;
showModel.InjectFullName = injectModel.INJECT_NAME;
}
}
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("/EmailParserConfig/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 = _emailParserConfigInfoRepository.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 _emailParserConfigInfoRepository.UpdateAsync(x => x.GID == record.GID,
x => new EmailParserConfigInfo { IsDeleted = true });
await _emailUserAccountParserRelationInfoRepository.DeleteAsync(x =>
x.EMAIL_PARSER_ID == record.GID);
});
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("/EmailParserConfig/GetPage")]
public async Task<SqlSugarPagedList<EmailParserConfigPageDto>> GetPageAsync(QueryEmailParserConfigDto QuerySearch)
{
var query = _emailParserConfigInfoRepository.AsQueryable();
#region 查询条件
//接收时间起始
if (!string.IsNullOrWhiteSpace(QuerySearch.CreateBegin))
{
DateTime currDate = DateTime.MinValue;
if (!DateTime.TryParse(QuerySearch.CreateBegin, out currDate))
throw Oops.Oh($"创建日期起始日期格式错误,{QuerySearch.CreateBegin}");
query = query.Where(t => t.CreatedTime >= currDate);
}
//接收时间结束
if (!string.IsNullOrWhiteSpace(QuerySearch.CreateEnd))
{
DateTime currDate = DateTime.MinValue;
if (!DateTime.TryParse(QuerySearch.CreateEnd, out currDate))
throw Oops.Oh($"创建日期结束日期格式错误,{QuerySearch.CreateEnd}");
currDate = currDate.AddDays(1);
query = query.Where(t => t.CreatedTime < 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.ParserCode))
{
query = query.Where(t => t.PARSER_CODE.Contains(QuerySearch.ParserCode));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.ParserName))
{
query = query.Where(t => t.PARSER_NAME.Contains(QuerySearch.ParserName));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.SubjectRegexPattern))
{
query = query.Where(t => t.SUBJECT_REGEX_PATTERN.Contains(QuerySearch.SubjectRegexPattern));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.BodyRegexPattern))
{
query = query.Where(t => t.BODY_REGEX_PATTERN.Contains(QuerySearch.BodyRegexPattern));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.SenderRegexPattern))
{
query = query.Where(t => t.SENDER_REGEX_PATTERN.Contains(QuerySearch.SenderRegexPattern));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.AttachRegexPattern))
{
query = query.Where(t => t.ATTACH_REGEX_PATTERN.Contains(QuerySearch.AttachRegexPattern));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.CreateUser))
{
query = query.Where(t => t.CreatedUserName.Contains(QuerySearch.CreateUser));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.UpdateUser))
{
query = query.Where(t => t.UpdatedUserName.Equals(QuerySearch.UpdateUser));
}
#endregion
string entityOrderCol = "CreatedTime";
//这里因为返回给前端的台账数据是DTO所以这里排序时候需要转换成Entity对应的字段
if (!string.IsNullOrWhiteSpace(QuerySearch.SortField))
entityOrderCol = MapsterExtHelper.GetAdaptProperty<EmailParserConfigDto, EmailParserConfigInfo>(QuerySearch.SortField);
var entities = await query
.OrderBy(entityOrderCol + (QuerySearch.descSort ? " asc " : " desc "))
.ToPagedListAsync(QuerySearch.PageNo, QuerySearch.PageSize);
return entities.Adapt<SqlSugarPagedList<EmailParserConfigPageDto>>();
}
#endregion
}
}