using Dlhis.Entity.Canteen.Entities; using Dlhis.Entity.Canteen.Enums; using Dlhis.Jip.Api; using Dlhis.Jip.Api.Requests; using Dlhis.ScheduledTask.Attributes; using Newtonsoft.Json; 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 = s.AddMonths(1); var endDayOfMonth = e.AddDays(-1); //从余额找人 var userCodeAll = await DbContext.Queryable() .Where(x => SqlFunc.Subqueryable().Where(p => p.Year == year && p.Month == month).Where(p => p.UserCode == x.UserCode).NotAny() ) .Select(x => x.UserCode) .Distinct() .ToListAsync(); //var userCodeAll = await DbContext.Queryable().Where(x => x.Cost < 0).Select(x => x.UserCode).ToListAsync(); Logger.LogInformation("CostUploadJob消费上传人员:{count},统计区间为[{s}{e})", userCodeAll.Count, s.ToString("yyyy-MM-dd"), endDayOfMonth.ToString("yyyy-MM-dd")); foreach (var userCodes in userCodeAll.Chunk(100)) { var dict = new Dictionary(userCodes.Length); 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); if (balance != 0) { dict.Add(userCode, Math.Abs(balance)); } } if (!dict.Any()) { Logger.LogInformation("CostUploadJob统计人员:{count}", dict.Count); continue; } var request = new UploadCostRequest { Data = dict.Select(x => new UploadCostItemRequest { WorkCode = x.Key, BeginDate = endDayOfMonth.ToString("yyyy-MM-dd"), Charge = x.Value, Code = "2086" }).ToList() }; try { var response = await jipApi.UploadCostAsync(request); var handleData = response.Data.Where(x => x.Success == "S").ToList(); Logger.LogInformation("CostUploadJob消费上传成功人员:{count}", handleData.Count); if (!handleData.Any()) { continue; } var workCodes = handleData.Select(x => x.WorkCode).ToList(); var records = await DbContext.Queryable() .Where(x => workCodes.Contains(x.UserCode)) .Where(x => x.Year == s.Year && x.Month == s.Month) .ToListAsync(); var updatedRecords = new List(); var newRecords = new List(); foreach (var item in handleData) { var record = records.FirstOrDefault(x => x.UserCode == item.WorkCode); if (record != null) { record.Cost = dict[item.WorkCode]; updatedRecords.Add(record); } else { newRecords.Add(new UserBalanceUploadRecordEntity { UserCode = item.WorkCode, Year = s.Year, Month = s.Month, Cost = dict[item.WorkCode] }); } } if (updatedRecords.Any()) { await DbContext.UpdateAsync(updatedRecords); } if (newRecords.Any()) { await DbContext.InsertAsync(newRecords); } } catch (Exception ex) { Logger.LogError(ex, "BirthdayCostUploadJob上传消费数据失败-{userCode}:{error}", JsonConvert.SerializeObject(request), ex.Message); } } } }