using Dlhis.Entity.Canteen.Entities; using Dlhis.Entity.Canteen.Enums; using Dlhis.Jip.Api; using Dlhis.Jip.Api.Requests; using Dlhis.ScheduledTask.Attributes; using SqlSugar; namespace Dlhis.ScheduledTask.Jobs; /// /// 每月8号上传消费数据 /// [RecurringJob(nameof(CostUploadJob), "0 0 0 8 * ? ", Name = "每月8号上传消费数据")] public class CostUploadJob : BaseJob { public CostUploadJob(IServiceProvider serviceProvider) : base(serviceProvider) { } protected override async Task RunAsync(IServiceProvider serviceProvider) { var jipApi = serviceProvider.GetService(); var now = DateTime.Now; var year = now.Month == 1 ? now.Year - 1 : now.Year; var month = now.Month == 1 ? 12 : now.Month - 1; var s = new DateTime(year, month, 1); var e = new DateTime(year, month + 1, 1); var endDayOfMonth = e.AddDays(-1); //从余额找人 var userCodes = await DbContext.Queryable() .Where(x => SqlFunc.Subqueryable() .Where(p => p.UserCode == x.UserCode) .Where(p => p.Year == s.Year && p.Month == s.Month) .NotAny() ) .Select(x => x.UserCode) .Distinct() .ToListAsync(); foreach (var userCode in userCodes) { var balance = await DbContext.Queryable() .Where(x => x.UserCode == userCode) .Where(x => x.Status == BalanceRecordStatusEnumType.Valid) .Where(x => x.Type == BalanceRecordTypeEnumType.Out) .Where(x => x.CostDate >= s && x.CostDate < e) .Where(x => !BalanceRecordSourceConstants.IgnoreSources.Contains(x.Source)) .SumAsync(x => x.Amount); var response = await jipApi.UploadCostAsync(new UploadCostRequest { Data = new List { new UploadCostItemRequest { WorkCode = userCode, BeginDate = endDayOfMonth.ToString("yyyy-MM-dd"), Charge = balance, Code= "2086" } } }); if (response.Data.FirstOrDefault(x => x.WorkCode == userCode)?.Success == "S") { if (!await DbContext.Queryable() .Where(x => x.UserCode == userCode) .Where(x => x.Year == s.Year && x.Month == s.Month) .AnyAsync()) { await DbContext.InsertAsync(new UserBalanceUploadRecordEntity { Year = s.Year, Month = s.Month, UserCode = userCode, Cost = balance, }); } } } } }