Files
Kayori_MedusaPluginChargeMa…/MedusaPluginChargeMachine/Handlers/LA9/GetRecentChargeEaChargeHandler.cs
T
Kayori fa610a9bc6 Add EF Core persistence for charge machine plugin
Introduce ChargeMachineDbContext (SQLite) with ArticleContent and
ChargeHistory entities, plus migrations, and wire it up via
ConfigurePluginServices/OnAppInitialize with startup migration and
article-content seeding.

- ChargeEaChargeHandler now records each charge to ChargeHistory
- GetConfigEaChargeHandler serves article config from the DB instead
  of hardcoded values, and adds multibills/readagreement fields
- GetLastLogEaChargeHandler returns real daily totals and recent
  charge items from history, keyed by session card
- Add GetRecentChargeEaChargeHandler (paginated charge history) and
  OpChPassEaChargeHandler (PIN change)
- Update handlers to the new HandleAsync/primary-constructor style,
  file-scoped namespaces
- Bump EF Core/Identity packages and Abstractions reference to
  net10.0/10.0.10
2026-08-09 17:28:14 +02:00

75 lines
2.4 KiB
C#

using Abstractions.Handlers;
using Abstractions.SerializationTypes;
using MedusaPluginChargeMachine.Models.Request;
using MedusaPluginChargeMachine.Models.Response;
using MedusaPluginChargeMachine.Services;
using Microsoft.EntityFrameworkCore;
namespace MedusaPluginChargeMachine.Handlers.LA9;
public class GetRecentChargeEaChargeHandler(ISessionService sessionService, ChargeMachineDbContext db)
: Handler<GetRecentChargeRequest, GetRecentChargeResponse>
{
public override void Configure()
{
Module("eacharge");
Method("getrecentcharge");
}
public override async Task<GetRecentChargeResponse> HandleAsync(GetRecentChargeRequest req, string model)
{
var cardId = ResolveCardId(req);
var page = int.TryParse(req.Page, out var p) ? Math.Max(1, p) : 1;
var perPage = int.TryParse(req.PerPage, out var pp) ? Math.Max(1, pp) : 10;
var query = db.ChargeHistories.AsQueryable();
if (cardId is not null)
query = query.Where(h => h.CardId == cardId);
if (req.TargetDate is not null && DateTime.TryParse(req.TargetDate, out var targetDate))
query = query.Where(h => h.OrderDate.Date == targetDate.Date);
var total = await query.CountAsync();
var rows = await query
.OrderByDescending(h => h.OrderDate)
.Skip((page - 1) * perPage)
.Take(perPage)
.ToListAsync();
var items = rows.Select(h => new GetRecentChargeItem
{
RcvTime = new XrpcString(h.OrderDate.ToString("yyyyMMddHHmmss")),
ChrgPrice = new XrpcInt(h.ChargeAmount),
CardNo = new XrpcString(MaskCardId(h.CardId)),
AccCode = new XrpcString(""),
RcvStatus = new XrpcInt(0),
FontColor = new XrpcString("#FFFFFF")
}).ToList();
return new GetRecentChargeResponse
{
Status = 0,
Num = new XrpcInt(total),
Items = items
};
}
private string? ResolveCardId(GetRecentChargeRequest req)
{
if (req.SessionId is not null)
{
try { return sessionService.GetSession(req.SessionId).refId; }
catch { /* fall through to cardid */ }
}
return req.CardId;
}
private static string MaskCardId(string cardId) =>
cardId.Length <= 4
? new string('*', cardId.Length)
: new string('*', cardId.Length - 4) + cardId[^4..];
}