153 lines
6.9 KiB
C#
153 lines
6.9 KiB
C#
using System.Reflection;
|
|
using Dlhis.Canteen.Order.Dto;
|
|
using Dlhis.Canteen.Order.Request;
|
|
using Dlhis.Canteen.Order.Response;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Kurisu.AspNetCore.CustomClass;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Canteen.Order.Services.Implements;
|
|
|
|
public class BalanceSummaryService : IBalanceSummaryService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
|
|
public BalanceSummaryService(IDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<BalanceRecordAnalysisSummaryResponse> BalanceRecordAnalysisAsync(BalanceRecordAnalysisRequest request)
|
|
{
|
|
var list = await _dbContext.Queryable<BalanceRecordEntity>()
|
|
.WhereIF(request.SearchDateTimePeriod.HasValue(),
|
|
record => record.CostDate >= request.SearchDateTimePeriod.Start && record.CostDate <= request.SearchDateTimePeriod.End)
|
|
.WhereIF(request.BusinessPlaceCode.HasValue, record => record.BusinessPlaceCode == request.BusinessPlaceCode)
|
|
.Where(record => record.Type == BalanceRecordTypeEnumType.Out && record.Status == BalanceRecordStatusEnumType.Valid)
|
|
.ToListAsync();
|
|
|
|
var sum = list.Sum(x => Math.Abs(x.Amount));
|
|
|
|
var shifts = Enum.GetValues<ShiftEnumType>().ToList();
|
|
|
|
var page = list.GroupBy(record => record.CostDate.Date).Select(grp =>
|
|
{
|
|
return new BalanceRecordAnalysisResponse
|
|
{
|
|
Date = DateOnly.FromDateTime(grp.Key),
|
|
Items = shifts.Select(shift =>
|
|
{
|
|
return new KeyValueDto<ShiftEnumType, int>
|
|
{
|
|
Key = shift,
|
|
Value = grp.Where(x => x.ShiftType == shift).Select(x => x.UserNo).Distinct().Count()
|
|
};
|
|
}).ToList(),
|
|
Total = grp.Where(x => x.CostDate == grp.Key).Select(x => x.UserNo).Distinct().Count()
|
|
};
|
|
}).OrderByDescending(x => x.Date).ToPage(request);
|
|
|
|
return new BalanceRecordAnalysisSummaryResponse
|
|
{
|
|
Amount = sum,
|
|
Pagination = page
|
|
};
|
|
}
|
|
|
|
|
|
public async Task<Pagination<BalanceRecordAnalysisByCostResponse>> BalanceRecordAnalysisByCostIdAsync()
|
|
{
|
|
var list = await _dbContext.Queryable<BalanceRecordEntity>()
|
|
// .WhereIF(request.SearchDateTimePeriod?.Start.HasValue == true && request.SearchDateTimePeriod?.End.HasValue == true,
|
|
// record => record.CostDate >= request.SearchDateTimePeriod.Start && record.CostDate <= request.SearchDateTimePeriod.End)
|
|
//.WhereIF(request.BusinessPlaceCode.HasValue, record => record.BusinessPlaceCode == request.BusinessPlaceCode)
|
|
.Where(record => record.Type == BalanceRecordTypeEnumType.Out && record.Status == BalanceRecordStatusEnumType.Valid)
|
|
.ToListAsync();
|
|
|
|
var shifts = Enum.GetValues<ShiftEnumType>().ToList();
|
|
var page = list.GroupBy(record => record.CostDate.Date).Select(grp =>
|
|
{
|
|
return new BalanceRecordAnalysisResponse
|
|
{
|
|
Date = DateOnly.FromDateTime(grp.Key),
|
|
Items = shifts.Select(shift =>
|
|
{
|
|
return new KeyValueDto<ShiftEnumType, int>
|
|
{
|
|
Key = shift,
|
|
Value = grp.Where(x => x.ShiftType == shift).Select(x => x.UserNo).Distinct().Count()
|
|
};
|
|
}).ToList(),
|
|
Total = grp.Where(x => x.CostDate == grp.Key).Select(x => x.UserNo).Distinct().Count()
|
|
};
|
|
}).OrderByDescending(x => x.Date);
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
public async Task<BalanceRecordAnalysisShiftAmountResponse> BalanceRecordShiftAnalysisAmountAsync(BalanceRecordAnalysisByShiftRequest request)
|
|
{
|
|
var queryable = _dbContext.Queryable<BalanceRecordEntity>()
|
|
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid && x.Type == BalanceRecordTypeEnumType.Out)
|
|
.WhereIF(request.SearchDateTimePeriod.HasValue(), x => x.CostDate >= request.SearchDateTimePeriod.Start && x.CostDate <= request.SearchDateTimePeriod.End)
|
|
.WhereIF(request.BusinessPlaceCodes?.Any() == true, x => request.BusinessPlaceCodes.Contains(x.BusinessPlaceCode.Value));
|
|
|
|
var amount = await queryable.SumAsync(x => SqlFunc.Abs(x.Amount));
|
|
|
|
var count = typeof(ShiftEnumType).GetFields(BindingFlags.Public | BindingFlags.Static).Length;
|
|
|
|
var result = await queryable.GroupBy(x => new { x.CostDate, x.ShiftType })
|
|
.Select(x => new { x.CostDate, x.ShiftType, Amount = SqlFunc.AggregateSum(SqlFunc.Abs(x.Amount)) })
|
|
.ToPageListAsync(request.PageIndex, count * request.PageSize);
|
|
|
|
return new BalanceRecordAnalysisShiftAmountResponse
|
|
{
|
|
Amount = amount,
|
|
Pagination = new Pagination<BalanceRecordAnalysisShiftPageResponse>
|
|
{
|
|
PageIndex = request.PageIndex,
|
|
PageSize = request.PageSize,
|
|
Total = result.Count,
|
|
Data = result.GroupBy(x => x.CostDate).Select(x => new BalanceRecordAnalysisShiftPageResponse
|
|
{
|
|
Date = x.Key,
|
|
Shifts = x.Select(item => new ShiftSummaryDto<decimal>
|
|
{
|
|
Shift = item.ShiftType,
|
|
Value = item.Amount
|
|
}).ToList()
|
|
}).ToList()
|
|
}
|
|
};
|
|
}
|
|
|
|
public async Task<BalanceRecordAnalysisShiftAmountAndCountResponse> BalanceRecordShiftAnalysisAmountAndCountAsync(BalanceRecordAnalysisByShiftRequest request)
|
|
{
|
|
var queryable = _dbContext.Queryable<BalanceRecordEntity>()
|
|
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid && x.Type == BalanceRecordTypeEnumType.Out)
|
|
.WhereIF(request.SearchDateTimePeriod.HasValue(), x => x.CostDate >= request.SearchDateTimePeriod.Start && x.CostDate <= request.SearchDateTimePeriod.End)
|
|
.WhereIF(request.BusinessPlaceCodes?.Any() == true, x => request.BusinessPlaceCodes.Contains(x.BusinessPlaceCode.Value));
|
|
|
|
var amount = await queryable.SumAsync(x => SqlFunc.Abs(x.Amount));
|
|
|
|
var result = await queryable.GroupBy(x => new { x.ShiftType })
|
|
.Select(x => new { x.ShiftType, Amount = SqlFunc.AggregateSum(SqlFunc.Abs(x.Amount)), Count = SqlFunc.AggregateDistinctCount(x.UserCode) })
|
|
.ToListAsync();
|
|
|
|
return new BalanceRecordAnalysisShiftAmountAndCountResponse
|
|
{
|
|
Amount = amount,
|
|
Items = result.Select(x => new BalanceRecordAnalysisShiftAmountAndCountItemResponse
|
|
{
|
|
ShiftType = x.ShiftType,
|
|
Amount = x.Amount,
|
|
Count = x.Count
|
|
}).ToList()
|
|
};
|
|
}
|
|
} |