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 { /// /// 邮箱账号服务 /// public class EmailUserAccountService : IEmailUserAccountService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; /// /// 邮件解析service /// /// public EmailUserAccountService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); } /// /// 邮件账户配置台账查询 /// /// 邮件账户配置台账查询请求 /// 返回结果 public Task>> GetPageAsync(PageRequest request) { //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition); var query = db.Queryable() .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().ToQueryPageAsync(request.PageCondition); } /// /// 保存邮箱配置 /// /// /// public async Task> 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() .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(); 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(); 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().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() .Filter(null, true) .First(a => a.Deleted == false && a.GID == gid); info.Adapt(model); await db.Updateable(model).ExecuteCommandAsync(); await db.Deleteable(x => x.EmailAccountId == gid).ExecuteCommandAsync(); //解析配置列表 if (info.ParserList != null && info.ParserList.Count > 0) { var insList = new List(); 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); } /// /// 获取详情 /// /// /// public async Task> GetInfo(string gid) { DataResult result = new DataResult(); var model = await db.Queryable().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(); var parList = db.Queryable() .Filter(null, true) .InnerJoin((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; } /// /// 删除 /// /// /// 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.EmailAccountId == record.GID).ExecuteCommandAsync(); } result.Code = ResultCode.Success; result.Message = MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.EmailOperateSuccess)); return result; } /// /// 检索邮件解析配置 /// /// /// /// public async Task>> QueryEmailParserConfigList(string queryItem, int topNum = 10) { var result = new DataResult>(); var parList = db.Queryable() .InnerJoin((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; } } }