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.
BookingHeChuan/Myshipping.Application/Service/EmailParserServerManage/EmailExcuteCodeInjectConfig...

357 lines
14 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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.Threading.Tasks;
namespace Myshipping.Application.Service.EmailParserServerManage
{
/// <summary>
/// 邮件执行代码注入配置
/// </summary>
[ApiDescriptionSettings("Application", Name = "EmailExcuteCodeInjectConfigServices", Order = 9)]
public class EmailExcuteCodeInjectConfigService : IEmailExcuteCodeInjectConfigService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<EmailExcuteCodeInjectConfigInfo> _emailExcuteCodeInjectConfigInfoRepository;
private readonly SqlSugarRepository<EmailParserConfigInfo> _emailParserConfigInfoRepository;
private readonly ILogger<EmailExcuteCodeInjectConfigService> _logger;
public EmailExcuteCodeInjectConfigService(SqlSugarRepository<EmailExcuteCodeInjectConfigInfo> emailExcuteCodeInjectConfigInfoRepository,
ILogger<EmailExcuteCodeInjectConfigService> logger,
SqlSugarRepository<EmailParserConfigInfo> emailParserConfigInfoRepository)
{
_emailExcuteCodeInjectConfigInfoRepository = emailExcuteCodeInjectConfigInfoRepository;
_logger = logger;
_emailParserConfigInfoRepository = emailParserConfigInfoRepository;
}
#region 保存邮件执行代码注入配置
/// <summary>
/// 保存邮件执行代码注入配置
/// </summary>
/// <param name="info">邮件执行代码注入配置信息</param>
/// <returns>返回回执</returns>
[HttpPost("/EmailExcuteCodeInjectConfig/Save")]
public async Task<TaskManageOrderResultDto> Save(EmailExcuteCodeInjectConfigDto info)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
/*
1、邮箱账户不能重复
2、邮箱账户需要做正则匹配。
*/
string gid = info.GID;
if (string.IsNullOrWhiteSpace(info.InjectCode))
throw Oops.Oh($"注入代码不能为空", typeof(InvalidOperationException));
if (string.IsNullOrWhiteSpace(info.InjectName))
throw Oops.Oh($"注入名称不能为空", typeof(InvalidOperationException));
if (string.IsNullOrWhiteSpace(info.InjectFullName))
throw Oops.Oh($"注入代码完整路径不能为空", typeof(InvalidOperationException));
DateTime nowDate = DateTime.Now;
if (string.IsNullOrWhiteSpace(gid))
{
var num = _emailExcuteCodeInjectConfigInfoRepository.AsQueryable()
.Count(a => a.INJECT_CODE.Equals(info.InjectCode));
if (num > 0)
throw Oops.Oh($"解析器代码不能重复", typeof(InvalidOperationException));
gid = IDGen.NextID().ToString();
var entity = info.Adapt<EmailExcuteCodeInjectConfigInfo>();
entity.GID = gid;
entity.CreatedTime = nowDate;
entity.UpdatedTime = nowDate;
entity.CreatedUserId = UserManager.UserId;
entity.CreatedUserName = UserManager.Name;
await _emailExcuteCodeInjectConfigInfoRepository.InsertAsync(entity);
}
else
{
var num = _emailExcuteCodeInjectConfigInfoRepository.AsQueryable().Count(a =>
a.INJECT_CODE.Equals(info.InjectCode) && a.GID != gid);
if (num > 0)
throw Oops.Oh($"解析器代码不能重复", typeof(InvalidOperationException));
var model = _emailExcuteCodeInjectConfigInfoRepository.AsQueryable()
.First(a => a.GID == gid);
var entity = info.Adapt<EmailExcuteCodeInjectConfigInfo>();
entity.UpdatedTime = nowDate;
entity.UpdatedUserId = UserManager.UserId;
entity.UpdatedUserName = UserManager.Name;
await _emailExcuteCodeInjectConfigInfoRepository.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;
}
#endregion
#region 获取邮件执行代码注入配置详情
/// <summary>
/// 获取邮件执行代码注入配置详情
/// </summary>
/// <param name="gid">邮件执行代码注入配置主键</param>
/// <returns>返回回执</returns>
[HttpGet("/EmailExcuteCodeInjectConfig/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 _emailExcuteCodeInjectConfigInfoRepository.AsQueryable()
.FirstAsync(a => a.GID == gid);
if (model == null)
throw Oops.Oh($"邮件执行代码注入配置获取失败,邮件执行代码注入配置不存在或已作废", typeof(InvalidOperationException));
var showModel = model.Adapt<EmailExcuteCodeInjectConfigInfo>();
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("/EmailExcuteCodeInjectConfig/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 = _emailExcuteCodeInjectConfigInfoRepository.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 _emailExcuteCodeInjectConfigInfoRepository.UpdateAsync(x => x.GID == record.GID,
x => new EmailExcuteCodeInjectConfigInfo { IsDeleted = true });
});
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("/EmailExcuteCodeInjectConfig/GetPage")]
public async Task<SqlSugarPagedList<EmailExcuteCodeInjectConfigPageDto>> GetPageAsync(QueryEmailExcuteCodeInjectConfigDto QuerySearch)
{
var query = _emailExcuteCodeInjectConfigInfoRepository.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.InjectCode))
{
query = query.Where(t => t.INJECT_CODE.Contains(QuerySearch.InjectCode));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.InjectName))
{
query = query.Where(t => t.INJECT_NAME.Contains(QuerySearch.InjectName));
}
if (!string.IsNullOrWhiteSpace(QuerySearch.InjectFullName))
{
query = query.Where(t => t.INJECT_FULLNAME.Contains(QuerySearch.InjectFullName));
}
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<EmailExcuteCodeInjectConfigDto, EmailExcuteCodeInjectConfigInfo>(QuerySearch.SortField);
var entities = await query
.OrderBy(entityOrderCol + (QuerySearch.descSort ? " asc " : " desc "))
.ToPagedListAsync(QuerySearch.PageNo, QuerySearch.PageSize);
return entities.Adapt<SqlSugarPagedList<EmailExcuteCodeInjectConfigPageDto>>();
}
#endregion
/// <summary>
/// 检索所有使用此执行代码注入的解析配置列表
/// </summary>
/// <param name="gid">邮件执行代码注入配置主键</param>
/// <returns>返回结果</returns>
[HttpGet("/EmailExcuteCodeInjectConfig/QueryUseParserConfigEmailList")]
public async Task<TaskManageOrderResultDto> QueryUseParserConfigEmailList(string gid)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
var accountList = _emailParserConfigInfoRepository.AsQueryable()
.Where(par => par.INJECT_ID == gid)
.ToList();
var list = accountList.Select(t =>
t.Adapt<EmailParserConfigDto>()).OrderBy(t => t.ParserName).ToList();
result.succ = true;
result.ext = list;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"检索所有使用此执行代码注入的解析配置列表异常,原因:{ex.Message}";
}
return result;
}
}
}