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
{
///
/// 邮件执行代码注入配置
///
[ApiDescriptionSettings("Application", Name = "EmailExcuteCodeInjectConfigServices", Order = 9)]
public class EmailExcuteCodeInjectConfigService : IEmailExcuteCodeInjectConfigService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _emailExcuteCodeInjectConfigInfoRepository;
private readonly SqlSugarRepository _emailParserConfigInfoRepository;
private readonly ILogger _logger;
public EmailExcuteCodeInjectConfigService(SqlSugarRepository emailExcuteCodeInjectConfigInfoRepository,
ILogger logger,
SqlSugarRepository emailParserConfigInfoRepository)
{
_emailExcuteCodeInjectConfigInfoRepository = emailExcuteCodeInjectConfigInfoRepository;
_logger = logger;
_emailParserConfigInfoRepository = emailParserConfigInfoRepository;
}
#region 保存邮件执行代码注入配置
///
/// 保存邮件执行代码注入配置
///
/// 邮件执行代码注入配置信息
/// 返回回执
[HttpPost("/EmailExcuteCodeInjectConfig/Save")]
public async Task 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();
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();
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 获取邮件执行代码注入配置详情
///
/// 获取邮件执行代码注入配置详情
///
/// 邮件执行代码注入配置主键
/// 返回回执
[HttpGet("/EmailExcuteCodeInjectConfig/GetInfo")]
public async Task 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();
result.succ = true;
result.ext = showModel;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取邮件执行代码注入配置异常,原因:{ex.Message}";
}
return result;
}
#endregion
#region 删除
///
/// 删除
///
/// 邮件执行代码注入配置主键数组
/// 返回回执
[HttpPost("/EmailExcuteCodeInjectConfig/Delete")]
public async Task 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 rltList = new List();
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 邮件执行代码注入配置台账查询
///
/// 邮件执行代码注入配置台账查询
///
/// 邮件执行代码注入配置查询请求
/// 返回结果
[HttpPost("/EmailExcuteCodeInjectConfig/GetPage")]
public async Task> 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(QuerySearch.SortField);
var entities = await query
.OrderBy(entityOrderCol + (QuerySearch.descSort ? " desc " : " asc "))
.ToPagedListAsync(QuerySearch.PageNo, QuerySearch.PageSize);
return entities.Adapt>();
}
#endregion
///
/// 检索所有使用此执行代码注入的解析配置列表
///
/// 邮件执行代码注入配置主键
/// 返回结果
[HttpGet("/EmailExcuteCodeInjectConfig/QueryUseParserConfigEmailList")]
public async Task 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()).OrderBy(t => t.ParserName).ToList();
result.succ = true;
result.ext = list;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"检索所有使用此执行代码注入的解析配置列表异常,原因:{ex.Message}";
}
return result;
}
}
}