193 lines
7.5 KiB
C#
193 lines
7.5 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.Basic.Entity;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Dlhis.Sys.Api;
|
|
using Kurisu.AspNetCore.CustomClass;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Canteen.Order.Services.Implements;
|
|
|
|
public class BalanceSummaryService : IBalanceSummaryService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ITenantApi _tenantApi;
|
|
|
|
public BalanceSummaryService(IDbContext dbContext, ITenantApi tenantApi)
|
|
{
|
|
_dbContext = dbContext;
|
|
_tenantApi = tenantApi;
|
|
}
|
|
|
|
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<BalanceRecordAnalysisDResponse>> BalanceRecordAnalysisByDepartmentAsync(SummaryCommonDto request)
|
|
{
|
|
return await GetQueryable(request)
|
|
.LeftJoin<DepartmentEntity>((x, department) => x.DepartmentNo == department.No)
|
|
.GroupBy(x => x.DepartmentNo)
|
|
.Select((x, department) => new BalanceRecordAnalysisDResponse
|
|
{
|
|
No = x.DepartmentNo,
|
|
Name = department.Name,
|
|
Count = SqlFunc.AggregateDistinctCount(x.UserCode),
|
|
Amount = SqlFunc.AggregateSum(SqlFunc.Abs(x.Amount))
|
|
})
|
|
.ToPageListAsync(request);
|
|
}
|
|
|
|
public async Task<Pagination<BalanceRecordAnalysisDResponse>> BalanceRecordAnalysisByCostAsync(SummaryCommonDto request)
|
|
{
|
|
return await GetQueryable(request)
|
|
.GroupBy(x => x.CostId)
|
|
.Select(x => new BalanceRecordAnalysisDResponse
|
|
{
|
|
No = x.CostId,
|
|
Count = SqlFunc.AggregateDistinctCount(x.UserCode),
|
|
Amount = SqlFunc.AggregateSum(SqlFunc.Abs(x.Amount))
|
|
})
|
|
.ToPageListAsync(request);
|
|
}
|
|
|
|
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()
|
|
};
|
|
}
|
|
|
|
public async Task<Pagination<BalanceRecordAnalysisFactoryAmountAndCountResponse>> BalanceRecordAnalysisByFactoryAsync(SummaryCommonDto request)
|
|
{
|
|
var queryable = GetQueryable(request);
|
|
|
|
var page = await queryable
|
|
.GroupBy(x => new { x.TenantId })
|
|
.Select(x => new BalanceRecordAnalysisFactoryAmountAndCountResponse
|
|
{
|
|
Factory = x.TenantId,
|
|
Amount = SqlFunc.AggregateSum(SqlFunc.Abs(x.Amount)),
|
|
UserCount = SqlFunc.AggregateDistinctCount(x.UserCode)
|
|
})
|
|
.ToPageListAsync(request);
|
|
|
|
try
|
|
{
|
|
var tenants = await _tenantApi.GetTenantsInternalAsync();
|
|
page.Data.ForEach(item => { item.Factory = tenants.FirstOrDefault(x => x.Code == item.Factory)?.Name; });
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//ignore
|
|
}
|
|
|
|
return page;
|
|
}
|
|
|
|
//**************************************************************help method***********************************************************************
|
|
|
|
/// <summary>
|
|
/// 获取默认查询queryable
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
private ISugarQueryable<BalanceRecordEntity> GetQueryable(SummaryCommonDto request)
|
|
{
|
|
return _dbContext.Queryable<BalanceRecordEntity>()
|
|
.WhereIF(request.SearchDateTimePeriod.HasValue(), x => x.CostDate >= request.SearchDateTimePeriod.Start && x.CostDate <= request.SearchDateTimePeriod.End) //成本日期
|
|
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid && x.Type == BalanceRecordTypeEnumType.Out) //有效,消费数据
|
|
.WhereIF(request.BusinessPlaceCodes?.Any() == true, x => request.BusinessPlaceCodes.Contains(x.BusinessPlaceCode.Value)); //营业点
|
|
}
|
|
} |