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;
/// 账期分页查询(按年度筛选)
public record GetZhangqiPagedQuery(int? Year = null, string Keyword = "", int PageIndex = 1, int PageSize = 20)
: IRequest>;
public class GetZhangqiPagedQueryHandler(IZhangqiReadRepository readRepository)
: IRequestHandler>
{
public async Task> Handle(GetZhangqiPagedQuery query, CancellationToken ct)
=> await readRepository.GetPagedAsync(query.Year, query.Keyword, query.PageIndex, query.PageSize);
}
/// 按 Id 查询账期(编辑回显)
public record GetZhangqiByIdQuery(long Id) : IRequest;
public class GetZhangqiByIdQueryHandler(IZhangqiReadRepository readRepository)
: IRequestHandler
{
public async Task Handle(GetZhangqiByIdQuery query, CancellationToken ct)
=> await readRepository.GetByIdForEditAsync(query.Id);
}
///
/// 按日期取所属账期:不存在时按"每月 28 日"划分规则自动创建
/// (日期 ≤ 28 号:上月 29 日 ~ 本月 28 日;日期 > 28 号:本月 29 日 ~ 下月 28 日)。
///
public record GetZhangqiByDateQuery(DateTime Date) : IRequest;
public class GetZhangqiByDateQueryHandler(
IZhangqiReadRepository readRepository,
IZhangqiWriteRepository writeRepository,
ISnowflakeIdGenerator snowflakeIdGenerator) : IRequestHandler
{
public async Task 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);
}
/// 按 28 日边界推算账期区间(截止日期取当天 23:59:59)
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));
}
}
/// 指定年月是否已扎帐
public record GetZhangqiIsZhaZhangQuery(int Year, int Month) : IRequest;
public class GetZhangqiIsZhaZhangQueryHandler(IZhangqiReadRepository readRepository)
: IRequestHandler
{
public async Task Handle(GetZhangqiIsZhaZhangQuery query, CancellationToken ct)
=> await readRepository.GetIsZhaZhangAsync(query.Year, query.Month);
}
/// 指定日期所属账期是否已扎帐
public record GetZhangqiIsZhaZhangByDateQuery(DateTime Date) : IRequest;
public class GetZhangqiIsZhaZhangByDateQueryHandler(IZhangqiReadRepository readRepository)
: IRequestHandler
{
public async Task Handle(GetZhangqiIsZhaZhangByDateQuery query, CancellationToken ct)
=> await readRepository.GetIsZhaZhangByDateAsync(query.Date);
}