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.

114 lines
4.0 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 System.Net.Http.Headers;
using DS.Module.Core;
using DS.WMS.Core.Sys.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Quartz;
using SqlSugar;
namespace DS.WMS.Core.QuarztJobs
{
/// <summary>
/// 自动费用模板后台任务
/// </summary>
public class FeeCustTemplateJob : IJob
{
static readonly ApiFox api;
ISqlSugarClient? db;
IConfiguration configuration;
ILogger<FeeCustTemplateJob> logger;
static FeeCustTemplateJob()
{
api = new ApiFox();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="serviceProvider"></param>
public FeeCustTemplateJob(IServiceProvider serviceProvider)
{
db = serviceProvider.GetRequiredService<ISqlSugarClient>();
configuration = serviceProvider.GetRequiredService<IConfiguration>();
logger = serviceProvider.GetRequiredService<ILogger<FeeCustTemplateJob>>();
}
/// <summary>
/// 生成费用
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task Execute(IJobExecutionContext context)
{
logger.LogInformation("开始运行【自动费用模板】生成...");
db.QueryFilter.Clear();
var dbLinks = await db.Queryable<Module.SqlSugar.SysTenantLink>().ToListAsync();
foreach (var dbLink in dbLinks)
{
//if (configuration["AutoFeeTemplate:Tenant"] != dbLink.TenantId.ToString())
// continue;
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)
{
logger.LogInformation("未能获取租户系统管理员租户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))
{
logger.LogInformation("未配置自动费用模板请求基础URL");
return;
}
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)
logger.LogInformation("【自动费用模板】生成完成...");
else
logger.LogInformation($"【自动费用模板】生成失败({response.ToString()}),详情查看日志!");
}
}
}