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.

101 lines
3.4 KiB
C#

using System.Net.Http.Headers;
using DS.Module.Core;
using DS.WMS.Core.Sys.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using SqlSugar;
namespace DS.WMS.Core.QuarztJobs
{
/// <summary>
/// 自动费用模板后台任务
/// </summary>
public class FeeCustTemplateJob : IJob
{
static readonly ApiFox api;
ISqlSugarClient? db;
IConfiguration configuration;
static FeeCustTemplateJob()
{
api = new ApiFox();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="serviceProvider"></param>
public FeeCustTemplateJob(IServiceProvider serviceProvider)
{
db = serviceProvider.GetRequiredService<ISqlSugarClient>();
configuration = serviceProvider.GetRequiredService<IConfiguration>();
}
/// <summary>
/// 生成费用
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task Execute(IJobExecutionContext context)
{
db.QueryFilter.Clear();
var dbLinks = await db.Queryable<Module.SqlSugar.SysTenantLink>().ToListAsync();
foreach (var dbLink in dbLinks)
{
var adminUser = await db.Queryable<SysUser>()
.Where(x => x.TenantId == dbLink.TenantId && x.Status == 0 && x.UserType == 1)
.OrderByDescending(x => x.CreateTime)
.Select(x => new
{
x.Id,
x.UserName,
x.Password,
x.DefaultOrgId,
x.DefaultOrgName,
x.TenantId,
x.TenantName
}).FirstAsync();
if (adminUser == null)
{
Console.WriteLine($"未能获取租户系统管理员租户ID{dbLink.TenantId}");
continue;
}
var tokenModel = new JwtHelper.JwtTokenModel
{
Uid = adminUser.Id.ToString(),
Name = adminUser.UserName,
OrgId = adminUser.DefaultOrgId.ToString(),
TenantId = adminUser.TenantId.ToString(),
TenantName = adminUser.TenantName
};
var token = JwtHelper.Encrypt(tokenModel, false, true);
await SendRequestAsync(token);
}
}
internal async Task SendRequestAsync(string token)
{
if (api.BaseUri == null)
{
var baseUrl = configuration["AutoFeeTemplate:BaseUrl"];
if (string.IsNullOrEmpty(baseUrl))
throw new ApplicationException("未配置自动费用模板请求基础URL");
api.BaseUri = new Uri(baseUrl, UriKind.Absolute);
}
api.DefaultHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await api.SendRequestAsync(HttpMethod.Post, configuration["AutoFeeTemplate:GenerateFeesUrl"]!, new
{
etd = DateTime.Now.Date
});
if (!response.IsSuccessStatusCode)
throw new ApplicationException("自动费用模板生成费用失败,详情查看日志");
}
}
}