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.5 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.HangfireJob.Interface;
using DS.WMS.Core.Sys.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.HangfireJob.Method
{
/// <summary>
/// 自动费用模板后台任务
/// </summary>
public class FeeCustTemplateJobService : IFeeCustTemplateJobService
{
static readonly ApiFox api;
ISqlSugarClient? db;
IConfiguration configuration;
static FeeCustTemplateJobService()
{
api = new ApiFox();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="serviceProvider"></param>
public FeeCustTemplateJobService(IServiceProvider serviceProvider)
{
db = serviceProvider.GetRequiredService<ISqlSugarClient>();
configuration = serviceProvider.GetRequiredService<IConfiguration>();
}
/// <summary>
/// 生成费用
/// </summary>
/// <returns></returns>
public async Task GenerateFeesAsync()
{
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("自动费用模板生成费用失败,详情查看日志");
}
}
}