using Dlhis.Canteen.Tickets.Request; using Dlhis.Canteen.Voucher.Requests; using Dlhis.Canteen.Voucher.Response; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Canteen.Entities.Voucher; using Kurisu.AspNetCore.Authentication.Abstractions; using Kurisu.AspNetCore.CustomClass; using Kurisu.AspNetCore.DataAccess.Dto; using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils.Extensions; namespace Dlhis.Canteen.Tickets.Services.Implements; public abstract class BaseVoucherService : IVoucherService { private readonly ICurrentUser _currentUser; protected IDbContext DbContext { get; set; } protected BaseVoucherService(IServiceProvider serviceProvider) { DbContext = serviceProvider.GetService(); _currentUser = serviceProvider.GetService(); } public async Task> GetPageAsync(VoucherPageRequest request) { var tenantId = _currentUser.GetStringTenantId(); var pagination = await DbContext.Queryable() .LeftJoin((x, u) => x.No == u.VoucherNo) .LeftJoin((x, u, ga) => u.UserCode == ga.Code) .Where((x, u) => x.Type == request.Type && u.Factory == tenantId) .WhereIF(request.UserCode.IsPresent(), (x, u, ga) => u.UserCode == request.UserCode) .WhereIF(request.SearchDateTimePeriod.HasValue(), (x, u, ga) => x.CreateTime >= request.SearchDateTimePeriod.Start && x.CreateTime <= request.SearchDateTimePeriod.End) .WhereIF(request.DepartmentNoFirst.IsPresent(), (x, u, ga) => u.DepartmentNoFirst == request.DepartmentNoFirst) .WhereIF(request.DepartmentNoSecond.IsPresent(), (x, u, ga) => u.DepartmentNoSecond == request.DepartmentNoSecond) .OrderByDescending(x => x.ClaimDateTime) .Select((x, u, ga) => new VoucherPageResponse { No = x.No, Type = x.Type, UserCode = u.UserCode, UserName = ga.Name, OriginalAmount = x.OriginalBalance, Amount = x.Balance, Remark = x.Remark, Description = x.Description, CostId = u.CostId, DepartmentNo = u.DepartmentNo, DepartmentNoFirst = u.DepartmentNoFirst, DepartmentNoSecond = u.DepartmentNoSecond, Werks = u.Werks, Factory = u.Factory }) .ToPageListAsync(request); var searchNos = pagination.Data.SelectMany(x => { return new List { x.DepartmentNo, x.DepartmentNoFirst, x.DepartmentNoSecond }; }).Distinct().ToList(); var departments = await DbContext.Queryable().Where(x => searchNos.Contains(x.No)).ToListAsync(); pagination.Data.ForEach(x => { x.DepartmentName = departments.FirstOrDefault(d => d.No == x.DepartmentNo)?.Name; x.DepartmentNameFirst = departments.FirstOrDefault(d => d.No == x.DepartmentNoFirst)?.Name; x.DepartmentNameSecond = departments.FirstOrDefault(d => d.No == x.DepartmentNoSecond)?.Name; }); return pagination; } public abstract Task CreateAsync(Dictionary pairs); public abstract Task> GetVoucherBalancePageAsync(VoucherBalancePageRequest request); }