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 { /// /// 自动费用模板后台任务 /// public class FeeCustTemplateJob : IJob { static readonly ApiFox api; ISqlSugarClient? db; IConfiguration configuration; static FeeCustTemplateJob() { api = new ApiFox(); } /// /// 初始化 /// /// public FeeCustTemplateJob(IServiceProvider serviceProvider) { db = serviceProvider.GetRequiredService(); configuration = serviceProvider.GetRequiredService(); } /// /// 生成费用 /// /// /// public async Task Execute(IJobExecutionContext context) { db.QueryFilter.Clear(); var dbLinks = await db.Queryable().ToListAsync(); foreach (var dbLink in dbLinks) { if (configuration["AutoFeeTemplate:Tenant"] != dbLink.TenantId.ToString()) continue; var adminUser = await db.Queryable() .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("自动费用模板生成费用失败,详情查看日志"); } } }