|
|
|
|
using AngleSharp.Dom;
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.Module.Core.Extensions;
|
|
|
|
|
using DS.Module.DjyServiceStatus;
|
|
|
|
|
using DS.Module.UserModule;
|
|
|
|
|
using DS.WMS.Core.EmailParse.Dtos;
|
|
|
|
|
using DS.WMS.Core.EmailParse.Entity;
|
|
|
|
|
using DS.WMS.Core.EmailParse.Interface;
|
|
|
|
|
using DS.WMS.Core.Op.Dtos;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using NPOI.SS.Formula.Functions;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DS.WMS.Core.EmailParse.Method
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 邮箱账号服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class EmailUserAccountService : IEmailUserAccountService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ISqlSugarClient db;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 邮件解析service
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="serviceProvider"></param>
|
|
|
|
|
public EmailUserAccountService(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 邮件账户配置台账查询
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">邮件账户配置台账查询请求</param>
|
|
|
|
|
/// <returns>返回结果</returns>
|
|
|
|
|
public Task<DataResult<List<EmailUserAccountPageDto>>> GetPageAsync(PageRequest<QueryEmailUserAccountDto> request)
|
|
|
|
|
{
|
|
|
|
|
//序列化查询条件
|
|
|
|
|
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
|
|
|
|
|
var query = db.Queryable<EmailUserAccountInfo>()
|
|
|
|
|
.Filter(null, true)
|
|
|
|
|
.Where(x => x.Deleted == false)
|
|
|
|
|
.Where(whereList);
|
|
|
|
|
|
|
|
|
|
#region 查询条件
|
|
|
|
|
|
|
|
|
|
//接收时间起始
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.CreateBegin))
|
|
|
|
|
{
|
|
|
|
|
DateTime currDate = DateTime.MinValue;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(request.OtherQueryCondition.CreateBegin, out currDate))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(t => t.CreateTime >= currDate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//接收时间结束
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.CreateEnd))
|
|
|
|
|
{
|
|
|
|
|
DateTime currDate = DateTime.MinValue;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(request.OtherQueryCondition.CreateEnd, out currDate))
|
|
|
|
|
{
|
|
|
|
|
currDate = currDate.AddDays(1);
|
|
|
|
|
query = query.Where(t => t.CreateTime < currDate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//更新时间起始
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.UpdateBegin))
|
|
|
|
|
{
|
|
|
|
|
DateTime currDate = DateTime.MinValue;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(request.OtherQueryCondition.UpdateBegin, out currDate))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(t => t.UpdateTime >= currDate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//更新时间结束
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.UpdateEnd))
|
|
|
|
|
{
|
|
|
|
|
DateTime currDate = DateTime.MinValue;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(request.OtherQueryCondition.UpdateEnd, out currDate))
|
|
|
|
|
{
|
|
|
|
|
currDate = currDate.AddDays(1);
|
|
|
|
|
|
|
|
|
|
query = query.Where(t => t.UpdateTime < currDate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.EmailAccount))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(t => t.EmailAccount.Contains(request.OtherQueryCondition.EmailAccount));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.ReceiveServer))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(t => t.ReceiveServer.Contains(request.OtherQueryCondition.ReceiveServer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.SmtpServer))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(t => t.SmtpServer.Contains(request.OtherQueryCondition.SmtpServer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.CreateUser))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(t => t.CreateUserName.Contains(request.OtherQueryCondition.CreateUser));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.OtherQueryCondition.UpdateUser))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(t => t.UpdateUserName.Equals(request.OtherQueryCondition.UpdateUser));
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
return query.Select<EmailUserAccountPageDto>().ToQueryPageAsync(request.PageCondition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存邮箱配置
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult<EmailUserAccountDto>> Save(EmailUserAccountDto info)
|
|
|
|
|
{
|
|
|
|
|
var gid = "";
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(info.EmailAccount))
|
|
|
|
|
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailAccountCanNotEmpty)));
|
|
|
|
|
|
|
|
|
|
if (!Regex.IsMatch(info.EmailAccount, "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$"))
|
|
|
|
|
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailAccountInvalid)));
|
|
|
|
|
|
|
|
|
|
DateTime nowDate = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
//新增
|
|
|
|
|
if (string.IsNullOrEmpty(info.GID))
|
|
|
|
|
{
|
|
|
|
|
var num = db.Queryable<EmailUserAccountInfo>()
|
|
|
|
|
.Filter(null, true)
|
|
|
|
|
.Where(x => x.Deleted == false)
|
|
|
|
|
.Count(a => a.EmailAccount.Equals(info.EmailAccount));
|
|
|
|
|
|
|
|
|
|
if (num > 0)
|
|
|
|
|
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailAccountCanNotRepeat)));
|
|
|
|
|
|
|
|
|
|
var entity = info.Adapt<EmailUserAccountInfo>();
|
|
|
|
|
entity.GID = Guid.NewGuid().ToString();
|
|
|
|
|
|
|
|
|
|
await db.Insertable(entity).ExecuteCommandAsync();
|
|
|
|
|
gid = entity.GID;
|
|
|
|
|
|
|
|
|
|
//解析配置列表
|
|
|
|
|
if (info.ParserList != null && info.ParserList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var insList = new List<EmailUserAccountParserRelationInfo>();
|
|
|
|
|
foreach (var parser in info.ParserList)
|
|
|
|
|
{
|
|
|
|
|
var rela = new EmailUserAccountParserRelationInfo
|
|
|
|
|
{
|
|
|
|
|
GID = Guid.NewGuid().ToString(),
|
|
|
|
|
EmailAccount = entity.EmailAccount,
|
|
|
|
|
EmailAccountId = gid,
|
|
|
|
|
EmailParserId = parser.GID
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
insList.Add(rela);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db.Insertable(insList).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//修改
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var num = db.Queryable<EmailUserAccountInfo>().Count(a =>
|
|
|
|
|
a.EmailAccount.Equals(info.EmailAccount) && a.GID != gid);
|
|
|
|
|
|
|
|
|
|
if (num > 0)
|
|
|
|
|
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailAccountCanNotRepeat)));
|
|
|
|
|
|
|
|
|
|
var model = db.Queryable<EmailUserAccountInfo>()
|
|
|
|
|
.Filter(null, true)
|
|
|
|
|
.First(a => a.Deleted == false && a.GID == gid);
|
|
|
|
|
info.Adapt(model);
|
|
|
|
|
await db.Updateable(model).ExecuteCommandAsync();
|
|
|
|
|
await db.Deleteable<EmailUserAccountParserRelationInfo>(x => x.EmailAccountId == gid).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
//解析配置列表
|
|
|
|
|
if (info.ParserList != null && info.ParserList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var insList = new List<EmailUserAccountParserRelationInfo>();
|
|
|
|
|
foreach (var parser in info.ParserList)
|
|
|
|
|
{
|
|
|
|
|
var rela = new EmailUserAccountParserRelationInfo
|
|
|
|
|
{
|
|
|
|
|
GID = Guid.NewGuid().ToString(),
|
|
|
|
|
EmailAccount = model.EmailAccount,
|
|
|
|
|
EmailAccountId = gid,
|
|
|
|
|
EmailParserId = parser.GID
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
insList.Add(rela);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db.Insertable(insList).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await GetInfo(gid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取详情
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="gid"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult<EmailUserAccountDto>> GetInfo(string gid)
|
|
|
|
|
{
|
|
|
|
|
DataResult<EmailUserAccountDto> result = new DataResult<EmailUserAccountDto>();
|
|
|
|
|
var model = await db.Queryable<EmailUserAccountInfo>().Filter(null, true).FirstAsync(a => a.Deleted == false && a.GID == gid);
|
|
|
|
|
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
result.Code = ResultCode.Fail;
|
|
|
|
|
result.Message = MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailParserDataNotFound));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var showModel = model.Adapt<EmailUserAccountDto>();
|
|
|
|
|
|
|
|
|
|
var parList = db.Queryable<EmailUserAccountParserRelationInfo>()
|
|
|
|
|
.Filter(null, true)
|
|
|
|
|
.InnerJoin<EmailParserConfigInfo>((rela, par) => rela.EmailParserId == par.GID)
|
|
|
|
|
.Where(rela => rela.EmailAccountId == model.GID)
|
|
|
|
|
.Select((rela, par) => par).ToList();
|
|
|
|
|
|
|
|
|
|
if (parList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
showModel.ParserList = parList.Select(a => new EmailParserConfigDto
|
|
|
|
|
{
|
|
|
|
|
GID = a.GID,
|
|
|
|
|
ParserCode = a.ParserCode,
|
|
|
|
|
ParserName = a.ParserName,
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<EmailUserAccountInfo>().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.EmailAccountId == 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<List<EmailParserConfigWithInjectDto>>> QueryEmailParserConfigList(string queryItem, int topNum = 10)
|
|
|
|
|
{
|
|
|
|
|
var result = new DataResult<List<EmailParserConfigWithInjectDto>>();
|
|
|
|
|
var parList = db.Queryable<EmailParserConfigInfo>()
|
|
|
|
|
.InnerJoin<EmailExcuteCodeInjectConfigInfo>((par, inj) => par.InjectId == inj.GID)
|
|
|
|
|
.WhereIF(!string.IsNullOrWhiteSpace(queryItem), (par, inj) =>
|
|
|
|
|
par.ParserCode.Contains(queryItem) || par.ParserName.Contains(queryItem) || inj.InjectCode.Contains(queryItem)
|
|
|
|
|
|| inj.InjectName.Contains(queryItem) || inj.InjectFullName.Contains(queryItem))
|
|
|
|
|
.OrderBy((par, inj) => par.ParserName)
|
|
|
|
|
.Select((par, inj) => new { par = par, inj = inj })
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var list = parList.Select(t =>
|
|
|
|
|
new EmailParserConfigWithInjectDto
|
|
|
|
|
{
|
|
|
|
|
ParserGid = t.par.GID,
|
|
|
|
|
ParserCode = t.par.ParserCode,
|
|
|
|
|
ParserName = t.par.ParserName,
|
|
|
|
|
InjectId = t.inj.GID,
|
|
|
|
|
InjectCode = t.inj.InjectCode,
|
|
|
|
|
InjectName = t.inj.InjectName,
|
|
|
|
|
InjectFullName = t.inj.InjectFullName
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
result.Code = ResultCode.Success;
|
|
|
|
|
result.Data = list.Take(topNum).ToList();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|