From 505787c95ce762bf11fab7d4c9e47ecf79bf7bc3 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Tue, 12 Aug 2025 15:11:26 +0800 Subject: [PATCH] tmp . --- .../Canteen/Entities/UserBalanceTmpEntity.cs | 28 ++++++++++ .../Controllers/AssignmentsController.cs | 51 ++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 Src/Dlhis.Entity/Canteen/Entities/UserBalanceTmpEntity.cs diff --git a/Src/Dlhis.Entity/Canteen/Entities/UserBalanceTmpEntity.cs b/Src/Dlhis.Entity/Canteen/Entities/UserBalanceTmpEntity.cs new file mode 100644 index 00000000..1fb925ec --- /dev/null +++ b/Src/Dlhis.Entity/Canteen/Entities/UserBalanceTmpEntity.cs @@ -0,0 +1,28 @@ +using Kurisu.AspNetCore.DataAccess.Entity; +using Kurisu.AspNetCore.DataAccess.SqlSugar; +using SqlSugar; + +namespace Dlhis.Entity.Canteen.Entities; + +/// +/// 用户余额表 +/// +[SugarTable(TableName = "UserBalanceTmp", TableDescription = "用户余额表")] +public class UserBalanceTmpEntity : SugarBaseEntity, IVersion +{ + /// + /// 学工号 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "学工号", UniqueGroupNameList = new[] { "idx_UserBalances_useCode" })] + public string UserCode { get; set; } + + /// + /// 余额 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "余额")] + public decimal Balance { get; set; } + + + [SugarColumn(IsNullable = false)] + public int Version { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs index 7cb59e4f..e07d75e9 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs @@ -1,10 +1,14 @@ -using Dlhis.ScheduledTask.TaskAssignment.Request; +using Dlhis.Entity.Canteen.Entities; +using Dlhis.Entity.Canteen.Enums; +using Dlhis.ScheduledTask.TaskAssignment.Request; using Dlhis.ScheduledTask.TaskAssignment.Response; using Dlhis.ScheduledTask.TaskAssignment.Services; using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop; +using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.UnifyResultAndValidation.Aop; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using SqlSugar; namespace Dlhis.ScheduledTask.TaskAssignment.Controllers; @@ -17,10 +21,12 @@ namespace Dlhis.ScheduledTask.TaskAssignment.Controllers; public class AssignmentsController : ControllerBase { private readonly IAssignmentService _assignmentService; + private readonly IDbContext _dbContext; - public AssignmentsController(IAssignmentService assignmentService) + public AssignmentsController(IAssignmentService assignmentService, IDbContext dbContext) { _assignmentService = assignmentService; + _dbContext = dbContext; } /// @@ -59,4 +65,45 @@ public class AssignmentsController : ControllerBase { await _assignmentService.UpdateDeviceTaskStatusAsync(requests); } + + [AllowAnonymous] + [HttpPost("balance/calc")] + [IgnoreTenant] + public async Task Calc() + { + var userCodes = await _dbContext.Queryable() + .Where(x => SqlFunc.Subqueryable().Where(p => p.UserCode == x.UserCode).NotAny()) + .Select(x => x.UserCode) + .Distinct() + .Take(5000) + .ToListAsync(); + + var d = new DateTime(2025, 6, 30); + + foreach (var userCode in userCodes) + { + if (!await _dbContext.Queryable().Where(x => x.UserCode == userCode).AnyAsync()) + { + await _dbContext.InsertAsync(new UserBalanceTmpEntity + { + UserCode = userCode, + Balance = 0, + Version = 0 + }); + } + + var balance = await _dbContext.Queryable() + .Where(x => x.UserCode == userCode) + .Where(x => x.Status == BalanceRecordStatusEnumType.Valid) + .Where(x => !BalanceRecordSourceConstants.IgnoreSources.Contains(x.Source)) + .Where(x => x.CostDate <= d) + .SumAsync(x => x.Amount); + + await _dbContext.Updateable() + .SetColumns(x => x.Balance == balance) + .SetColumns(x => x.ModifiedTime == DateTime.Now) + .Where(x => x.UserCode == userCode) + .ExecuteCommandAsync(); + } + } } \ No newline at end of file