140 lines
5.4 KiB
C#
140 lines
5.4 KiB
C#
using System.Reflection;
|
|
using Dlhis.Canteen.Order.Dto;
|
|
using Dlhis.Canteen.Order.Request;
|
|
using Dlhis.Canteen.Order.Request.Common;
|
|
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)
|
|
{
|
|
if (request.BusinessPlaceCode.HasValue)
|
|
{
|
|
request.BusinessPlaceCodes = new List<Guid> { request.BusinessPlaceCode.Value };
|
|
}
|
|
|
|
var queryable = GetQueryable(request);
|
|
var list =await queryable.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<BalanceRecordAnalysisDepartmentResponse>> BalanceRecordAnalysisByCostIdAsync(SummaryCommonDto request)
|
|
{
|
|
// var queryable = GetQueryable(request);
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task<BalanceRecordAnalysisShiftAmountResponse> BalanceRecordShiftAnalysisAmountAsync(BalanceRecordAnalysisByShiftRequest request)
|
|
{
|
|
var queryable = GetQueryable(request);
|
|
|
|
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 = GetQueryable(request);
|
|
|
|
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()
|
|
};
|
|
}
|
|
|
|
//**************************************************************help method***********************************************************************
|
|
|
|
/// <summary>
|
|
/// 获取默认查询queryable
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
private ISugarQueryable<BalanceRecordEntity> GetQueryable(SummaryCommonDto request)
|
|
{
|
|
return _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)); //营业点
|
|
}
|
|
} |