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.

316 lines
12 KiB
C#

using DS.Module.Core;
using DS.Module.UserModule;
using DS.WMS.Core.Code.Dtos;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.EmailParse.Dtos;
using DS.WMS.Core.EmailParse.Entity;
using DS.WMS.Core.EmailParse.Interface;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using DS.Module.Core.Extensions;
using DS.Module.DjyServiceStatus;
using DS.WMS.Core.Op.Dtos;
using Mapster;
using Microsoft.AspNetCore.Identity;
using NPOI.SS.Formula.Functions;
using LanguageExt.ClassInstances;
namespace DS.WMS.Core.EmailParse.Method
{
public class EmailParserConfigService : IEmailParserConfigService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
/// <summary>
/// 邮件解析service
/// </summary>
/// <param name="serviceProvider"></param>
public EmailParserConfigService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
}
/// <summary>
/// 台账查询
/// </summary>
/// <param name="cond"></param>
/// <returns></returns>
public Task<DataResult<List<EmailParserConfigPageDto>>> GetPageAsync(PageRequest<QueryEmailParserConfigDto> cond)
{
var query = db.Queryable<EmailParserConfigInfo>();
#region 查询条件
//接收时间起始
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.CreateBegin))
{
DateTime currDate = DateTime.MinValue;
//if (!DateTime.TryParse(cond.OtherQueryCondition.CreateBegin, out currDate))
// throw Oops.Oh($"创建日期起始日期格式错误,{cond.OtherQueryCondition.CreateBegin}");
query = query.Where(t => t.CreateTime >= currDate);
}
//接收时间结束
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.CreateEnd))
{
DateTime currDate = DateTime.MinValue;
//if (!DateTime.TryParse(cond.OtherQueryCondition.CreateEnd, out currDate))
// throw Oops.Oh($"创建日期结束日期格式错误,{cond.OtherQueryCondition.CreateEnd}");
currDate = currDate.AddDays(1);
query = query.Where(t => t.CreateTime < currDate);
}
//更新时间起始
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.UpdateBegin))
{
DateTime currDate = DateTime.MinValue;
//if (!DateTime.TryParse(cond.OtherQueryCondition.UpdateBegin, out currDate))
// throw Oops.Oh($"更新时间起始日期格式错误,{cond.OtherQueryCondition.UpdateBegin}");
query = query.Where(t => t.UpdateTime >= currDate);
}
//更新时间结束
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.UpdateEnd))
{
DateTime currDate = DateTime.MinValue;
//if (!DateTime.TryParse(cond.OtherQueryCondition.UpdateEnd, out currDate))
// throw Oops.Oh($"更新时间结束日期格式错误,{cond.OtherQueryCondition.UpdateEnd}");
currDate = currDate.AddDays(1);
query = query.Where(t => t.UpdateTime < currDate);
}
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.ParserCode))
{
query = query.Where(t => t.ParserCode.Contains(cond.OtherQueryCondition.ParserCode));
}
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.ParserName))
{
query = query.Where(t => t.ParserName.Contains(cond.OtherQueryCondition.ParserName));
}
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.SubjectRegexPattern))
{
query = query.Where(t => t.SubjectRegexPattern.Contains(cond.OtherQueryCondition.SubjectRegexPattern));
}
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.BodyRegexPattern))
{
query = query.Where(t => t.BodyRegexPattern.Contains(cond.OtherQueryCondition.BodyRegexPattern));
}
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.SenderRegexPattern))
{
query = query.Where(t => t.SenderRegexPattern.Contains(cond.OtherQueryCondition.SenderRegexPattern));
}
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.AttachRegexPattern))
{
query = query.Where(t => t.AttachRegexPattern.Contains(cond.OtherQueryCondition.AttachRegexPattern));
}
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.CreateUser))
{
query = query.Where(t => t.CreateUserName.Contains(cond.OtherQueryCondition.CreateUser));
}
if (!string.IsNullOrWhiteSpace(cond.OtherQueryCondition.UpdateUser))
{
query = query.Where(t => t.UpdateUserName.Equals(cond.OtherQueryCondition.UpdateUser));
}
#endregion
return query.Select<EmailParserConfigPageDto>().ToQueryPageAsync(cond.PageCondition);
}
/// <summary>
/// 保存
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public async Task<DataResult<EmailParserConfigDto>> Save(EmailParserConfigDto info)
{
string gid = info.GID;
if (string.IsNullOrWhiteSpace(info.ParserCode))
return new DataResult<EmailParserConfigDto>(ResultCode.Fail, "解析器代码不能为空", MultiLanguageConst.EmailParserCodeCanNotEmpty);
if (string.IsNullOrWhiteSpace(info.ParserName))
return new DataResult<EmailParserConfigDto>(ResultCode.Fail, "解析器名称不能为空", MultiLanguageConst.EmailParserNameCanNotEmpty);
if (string.IsNullOrWhiteSpace(info.SubjectRegexPattern)
&& string.IsNullOrWhiteSpace(info.BodyRegexPattern)
&& string.IsNullOrWhiteSpace(info.SenderRegexPattern)
&& string.IsNullOrWhiteSpace(info.AttachRegexPattern))
return new DataResult<EmailParserConfigDto>(ResultCode.Fail, "邮件标题匹配、邮件正文匹配、邮件发送人匹配、邮件附件匹配至少要填写一项匹配", MultiLanguageConst.EmailParserNoOneMatch);
//新增
if (string.IsNullOrWhiteSpace(gid))
{
var num = db.Queryable<EmailParserConfigInfo>().Count(a => a.ParserCode.Equals(info.ParserCode));
if (num > 0)
return new DataResult<EmailParserConfigDto>(ResultCode.Fail, "解析器代码不能重复", MultiLanguageConst.EmailParserCodeCanNotRepeat);
gid = Guid.NewGuid().ToString();
var entity = info.Adapt<EmailParserConfigInfo>();
entity.GID = gid;
await db.Insertable(entity).ExecuteCommandAsync();
}
//修改
else
{
var num = db.Queryable<EmailParserConfigInfo>().Count(a =>
a.ParserCode.Equals(info.ParserCode) && a.GID != gid);
if (num > 0)
return new DataResult<EmailParserConfigDto>(ResultCode.Fail, "解析器代码不能重复", MultiLanguageConst.EmailParserCodeCanNotRepeat);
var model = db.Queryable<EmailParserConfigInfo>()
.First(a => a.GID == gid);
var entity = info.Adapt<EmailParserConfigInfo>();
await db.Updateable(entity).ExecuteCommandAsync();
}
return await GetInfo(gid);
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="gid"></param>
/// <returns></returns>
public async Task<DataResult<EmailParserConfigDto>> GetInfo(string gid)
{
DataResult<EmailParserConfigDto> result = new DataResult<EmailParserConfigDto>();
var model = await db.Queryable<EmailParserConfigInfo>()
.FirstAsync(a => a.GID == gid);
if (model == null)
{
result.Code = ResultCode.Fail;
result.Message = MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailParserDataNotFound));
return result;
}
var showModel = model.Adapt<EmailParserConfigDto>();
if (!string.IsNullOrWhiteSpace(model.InjectId))
{
var injectModel = db.Queryable<EmailExcuteCodeInjectConfigInfo>()
.First(a => a.GID == model.InjectId);
if (injectModel != null)
{
showModel.InjectCode = injectModel.InjectCode;
showModel.InjectName = injectModel.InjectName;
showModel.InjectFullName = injectModel.InjectFullName;
}
}
result.Code = ResultCode.Success;
result.Data = showModel;
return result;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="gIds"></param>
/// <returns></returns>
public async Task<DataResult> Delete(string[] gIds)
{
DataResult result = new DataResult();
var list = db.Queryable<EmailParserConfigInfo>().Where(a => gIds.Contains(a.GID)).ToList();
if (list.Count == 0)
{
result.Code = ResultCode.Fail;
result.Message = MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailParserDataNotFound));
return result;
}
foreach (var record in list)
{
record.Deleted = true;
await db.Updateable(record).ExecuteCommandAsync();
//删除关联的配置
await db.Deleteable<EmailUserAccountParserRelationInfo>(x => x.EmailParserId == record.GID).ExecuteCommandAsync();
}
result.Code = ResultCode.Success;
result.Message = MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailOperateSuccess));
return result;
}
/// <summary>
/// 检索邮件执行代码注入配置
/// </summary>
/// <param name="queryItem"></param>
/// <param name="topNum"></param>
/// <returns></returns>
public async Task<DataResult<EmailExcuteCodeInjectConfigDto>> QueryExcuteCodeInjectConfigList(string queryItem, int topNum = 10)
{
var result = new DataResult<EmailExcuteCodeInjectConfigDto>();
var parList = db.Queryable<EmailExcuteCodeInjectConfigInfo>()
.WhereIF(!string.IsNullOrWhiteSpace(queryItem), inj =>
inj.InjectCode.Contains(queryItem) || inj.InjectName.Contains(queryItem) || inj.InjectFullName.Contains(queryItem))
.OrderBy(inj => inj.InjectName)
.ToList();
var list = parList.Select(t =>
new EmailExcuteCodeInjectConfigDto
{
GID = t.GID,
InjectCode = t.InjectCode,
InjectName = t.InjectName,
InjectFullName = t.InjectFullName
}).ToList();
return result;
}
/// <summary>
/// 检索所有使用此邮件解析配置邮箱列表
/// </summary>
/// <param name="gid"></param>
/// <returns></returns>
public async Task<DataResult<List<EmailUserAccountDto>>> QueryUseParserConfigEmailList(string gid)
{
var result = new DataResult<List<EmailUserAccountDto>>();
var accountList = db.Queryable<EmailParserConfigInfo>()
.InnerJoin<EmailUserAccountParserRelationInfo>((par, rela) => par.GID == rela.EmailParserId)
.InnerJoin<EmailUserAccountInfo>((par, rela, acc) => rela.EmailAccountId == acc.GID)
.Where(par => par.GID == gid)
.Select((par, rela, acc) => acc)
.ToList();
var list = accountList.Select(t => t.Adapt<EmailUserAccountDto>()).OrderBy(t => t.EmailAccount).ToList();
result.Data = list;
result.Code = ResultCode.Success;
return result;
}
}
}