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;
///
/// 邮件解析service
///
///
public EmailParserConfigService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
}
///
/// 台账查询
///
///
///
public Task>> GetPageAsync(PageRequest cond)
{
var query = db.Queryable();
#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().ToQueryPageAsync(cond.PageCondition);
}
///
/// 保存
///
///
///
public async Task> Save(EmailParserConfigDto info)
{
string gid = info.GID;
if (string.IsNullOrWhiteSpace(info.ParserCode))
return new DataResult(ResultCode.Fail, "解析器代码不能为空", MultiLanguageConst.EmailParserCodeCanNotEmpty);
if (string.IsNullOrWhiteSpace(info.ParserName))
return new DataResult(ResultCode.Fail, "解析器名称不能为空", MultiLanguageConst.EmailParserNameCanNotEmpty);
if (string.IsNullOrWhiteSpace(info.SubjectRegexPattern)
&& string.IsNullOrWhiteSpace(info.BodyRegexPattern)
&& string.IsNullOrWhiteSpace(info.SenderRegexPattern)
&& string.IsNullOrWhiteSpace(info.AttachRegexPattern))
return new DataResult(ResultCode.Fail, "邮件标题匹配、邮件正文匹配、邮件发送人匹配、邮件附件匹配至少要填写一项匹配", MultiLanguageConst.EmailParserNoOneMatch);
//新增
if (string.IsNullOrWhiteSpace(gid))
{
var num = db.Queryable().Count(a => a.ParserCode.Equals(info.ParserCode));
if (num > 0)
return new DataResult(ResultCode.Fail, "解析器代码不能重复", MultiLanguageConst.EmailParserCodeCanNotRepeat);
gid = Guid.NewGuid().ToString();
var entity = info.Adapt();
entity.GID = gid;
await db.Insertable(entity).ExecuteCommandAsync();
}
//修改
else
{
var num = db.Queryable().Count(a =>
a.ParserCode.Equals(info.ParserCode) && a.GID != gid);
if (num > 0)
return new DataResult(ResultCode.Fail, "解析器代码不能重复", MultiLanguageConst.EmailParserCodeCanNotRepeat);
var model = db.Queryable()
.First(a => a.GID == gid);
var entity = info.Adapt();
await db.Updateable(entity).ExecuteCommandAsync();
}
return await GetInfo(gid);
}
///
/// 获取详情
///
///
///
public async Task> GetInfo(string gid)
{
DataResult result = new DataResult();
var model = await db.Queryable()
.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();
if (!string.IsNullOrWhiteSpace(model.InjectId))
{
var injectModel = db.Queryable()
.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;
}
///
/// 删除
///
///
///
public async Task Delete(string[] gIds)
{
DataResult result = new DataResult();
var list = db.Queryable().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(x => x.EmailParserId == record.GID).ExecuteCommandAsync();
}
result.Code = ResultCode.Success;
result.Message = MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailOperateSuccess));
return result;
}
///
/// 检索邮件执行代码注入配置
///
///
///
///
public async Task> QueryExcuteCodeInjectConfigList(string queryItem, int topNum = 10)
{
var result = new DataResult();
var parList = db.Queryable()
.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;
}
///
/// 检索所有使用此邮件解析配置邮箱列表
///
///
///
public async Task>> QueryUseParserConfigEmailList(string gid)
{
var result = new DataResult>();
var accountList = db.Queryable()
.InnerJoin((par, rela) => par.GID == rela.EmailParserId)
.InnerJoin((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()).OrderBy(t => t.EmailAccount).ToList();
result.Data = list;
result.Code = ResultCode.Success;
return result;
}
}
}