98 lines
4.1 KiB
C#
98 lines
4.1 KiB
C#
using F9WebAiTest.Application.Common;
|
||
using F9WebAiTest.Application.Dtos.SYS.Zq;
|
||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.Zq;
|
||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||
using F9WebAiTest.Domain.Entities.SYS.Zq;
|
||
using F9WebAiTest.Domain.Interfaces.SYS.Zq;
|
||
using MediatR;
|
||
|
||
namespace F9WebAiTest.Application.CQRS.SYS.Zq.Queries;
|
||
|
||
/// <summary>账期分页查询(按年度筛选)</summary>
|
||
public record GetZhangqiPagedQuery(int? Year = null, string Keyword = "", int PageIndex = 1, int PageSize = 20)
|
||
: IRequest<PageResult<ZhangqiReadDto_View>>;
|
||
|
||
public class GetZhangqiPagedQueryHandler(IZhangqiReadRepository readRepository)
|
||
: IRequestHandler<GetZhangqiPagedQuery, PageResult<ZhangqiReadDto_View>>
|
||
{
|
||
public async Task<PageResult<ZhangqiReadDto_View>> Handle(GetZhangqiPagedQuery query, CancellationToken ct)
|
||
=> await readRepository.GetPagedAsync(query.Year, query.Keyword, query.PageIndex, query.PageSize);
|
||
}
|
||
|
||
/// <summary>按 Id 查询账期(编辑回显)</summary>
|
||
public record GetZhangqiByIdQuery(long Id) : IRequest<ZhangqiReadDto_Edit>;
|
||
|
||
public class GetZhangqiByIdQueryHandler(IZhangqiReadRepository readRepository)
|
||
: IRequestHandler<GetZhangqiByIdQuery, ZhangqiReadDto_Edit>
|
||
{
|
||
public async Task<ZhangqiReadDto_Edit> Handle(GetZhangqiByIdQuery query, CancellationToken ct)
|
||
=> await readRepository.GetByIdForEditAsync(query.Id);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 按日期取所属账期:不存在时按"每月 28 日"划分规则自动创建
|
||
/// (日期 ≤ 28 号:上月 29 日 ~ 本月 28 日;日期 > 28 号:本月 29 日 ~ 下月 28 日)。
|
||
/// </summary>
|
||
public record GetZhangqiByDateQuery(DateTime Date) : IRequest<ZhangqiReadDto_Edit>;
|
||
|
||
public class GetZhangqiByDateQueryHandler(
|
||
IZhangqiReadRepository readRepository,
|
||
IZhangqiWriteRepository writeRepository,
|
||
ISnowflakeIdGenerator snowflakeIdGenerator) : IRequestHandler<GetZhangqiByDateQuery, ZhangqiReadDto_Edit>
|
||
{
|
||
public async Task<ZhangqiReadDto_Edit> Handle(GetZhangqiByDateQuery query, CancellationToken ct)
|
||
{
|
||
var exist = await readRepository.GetByDateAsync(query.Date);
|
||
if (exist != null) return exist;
|
||
|
||
var (startDate, endDate) = CalcRange(query.Date);
|
||
|
||
var entity = new Zhangqi(snowflakeIdGenerator.CreateId(), endDate.Year, endDate.Month, startDate, endDate);
|
||
await writeRepository.AddAsync(entity);
|
||
|
||
return new ZhangqiReadDto_Edit(entity.Id, entity.Year, entity.Month, entity.StartDate, entity.EndDate, entity.IsZhaZhang);
|
||
}
|
||
|
||
/// <summary>按 28 日边界推算账期区间(截止日期取当天 23:59:59)</summary>
|
||
private static (DateTime StartDate, DateTime EndDate) CalcRange(DateTime date)
|
||
{
|
||
DateTime startDate;
|
||
DateTime endDay;
|
||
|
||
if (date.Day <= 28)
|
||
{
|
||
var lastMonth = date.AddMonths(-1);
|
||
startDate = new DateTime(lastMonth.Year, lastMonth.Month, 28).AddDays(1);
|
||
endDay = new DateTime(date.Year, date.Month, 28);
|
||
}
|
||
else
|
||
{
|
||
var nextMonth = date.AddMonths(1);
|
||
startDate = new DateTime(date.Year, date.Month, 28).AddDays(1);
|
||
endDay = new DateTime(nextMonth.Year, nextMonth.Month, 28);
|
||
}
|
||
|
||
return (startDate, endDay.Date.AddDays(1).AddSeconds(-1));
|
||
}
|
||
}
|
||
|
||
/// <summary>指定年月是否已扎帐</summary>
|
||
public record GetZhangqiIsZhaZhangQuery(int Year, int Month) : IRequest<bool>;
|
||
|
||
public class GetZhangqiIsZhaZhangQueryHandler(IZhangqiReadRepository readRepository)
|
||
: IRequestHandler<GetZhangqiIsZhaZhangQuery, bool>
|
||
{
|
||
public async Task<bool> Handle(GetZhangqiIsZhaZhangQuery query, CancellationToken ct)
|
||
=> await readRepository.GetIsZhaZhangAsync(query.Year, query.Month);
|
||
}
|
||
|
||
/// <summary>指定日期所属账期是否已扎帐</summary>
|
||
public record GetZhangqiIsZhaZhangByDateQuery(DateTime Date) : IRequest<bool>;
|
||
|
||
public class GetZhangqiIsZhaZhangByDateQueryHandler(IZhangqiReadRepository readRepository)
|
||
: IRequestHandler<GetZhangqiIsZhaZhangByDateQuery, bool>
|
||
{
|
||
public async Task<bool> Handle(GetZhangqiIsZhaZhangByDateQuery query, CancellationToken ct)
|
||
=> await readRepository.GetIsZhaZhangByDateAsync(query.Date);
|
||
}
|