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 { /// /// 自动费用模板后台任务 /// public class FeeCustTemplateJob : IJob { static readonly ApiFox api; ISqlSugarClient? db; IConfiguration configuration; ILogger logger; static FeeCustTemplateJob() { api = new ApiFox(); } /// /// 初始化 /// /// public FeeCustTemplateJob(IServiceProvider serviceProvider) { db = serviceProvider.GetRequiredService(); configuration = serviceProvider.GetRequiredService(); logger = serviceProvider.GetRequiredService>(); } /// /// 生成费用 /// /// /// public async Task Execute(IJobExecutionContext context) { logger.LogInformation("开始运行【自动费用模板】生成..."); 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) { 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()}),详情查看日志!"); } } }