using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using Newtonsoft.Json; using WiDAL.DTOs; using WiDAL.Query.Wi; namespace WiDAL.CustomCode.LineRoster; public class LineRosterDAL : ILineRosterDAL { private readonly IQueryExecutor _qe; public LineRosterDAL(IQueryExecutor qe) => _qe = qe; public async Task GetRosterHeaderAsync( int productionLineId, DateOnly rosterDate, int shiftId, byte rosterType, LoginDTO login, CancellationToken ct) { return await _qe.QuerySingleAsync(login, LineRosterQB.GET_ROSTER_HEADER, new { ProductionLineId = productionLineId, RosterDate = rosterDate, ShiftId = shiftId, RosterType = rosterType, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } public async Task GetRosterByIdAsync(int rosterId, LoginDTO login, CancellationToken ct) { return await _qe.QuerySingleAsync(login, LineRosterQB.GET_ROSTER_BY_ID, new { LineShiftRosterId = rosterId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } public async Task InsertRosterAsync(LineShiftRosterDTO dto, LoginDTO login, CancellationToken ct) { return await _qe.ExecuteAsync(login, LineRosterQB.INSERT_ROSTER, new { dto.LineShiftRosterId, dto.ProductionLineId, dto.ShiftId, dto.RosterDate, dto.RosterType, dto.RosterStatus, dto.InChargeEmployeeId, dto.PlannedRosterId, dto.Remarks, UserId = login.UserId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } public async Task UpdateRosterStatusAsync(int rosterId, byte rosterStatus, int inChargeEmployeeId, LoginDTO login, CancellationToken ct) { return await _qe.ExecuteAsync(login, LineRosterQB.UPDATE_ROSTER_STATUS, new { LineShiftRosterId = rosterId, RosterStatus = rosterStatus, InChargeEmployeeId = inChargeEmployeeId, UserId = login.UserId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } public async Task GetStaffingByRosterAsync(int rosterId, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync(login, LineRosterQB.GET_STAFFING_BY_ROSTER, new { LineShiftRosterId = rosterId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); return JsonConvert.SerializeObject(result); } public async Task InsertStaffingAsync(LineShiftStaffingDTO dto, LoginDTO login, CancellationToken ct) { return await _qe.ExecuteAsync(login, LineRosterQB.INSERT_STAFFING, new { dto.LineShiftRosterId, dto.WorkstationId, dto.EmployeeId, dto.StaffingRole, IsPrimary = dto.IsPrimary ? 1 : 0, dto.PlannedStartTime, dto.PlannedEndTime, IsAbsent = dto.IsAbsent ? 1 : 0, dto.AbsenceReason, dto.ReplacedByEmployeeId, dto.Remarks, UserId = login.UserId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } public async Task MarkAbsentAsync(int staffingId, string? absenceReason, int? replacedByEmployeeId, LoginDTO login, CancellationToken ct) { return await _qe.ExecuteAsync(login, LineRosterQB.MARK_ABSENT, new { LineShiftStaffingId = staffingId, AbsenceReason = absenceReason, ReplacedByEmployeeId = replacedByEmployeeId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } public async Task GetPlannedVsActualAsync(int productionLineId, DateOnly rosterDate, int shiftId, int plannedInChargeEmployeeId, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync(login, LineRosterQB.GET_PLANNED_VS_ACTUAL, new { ProductionLineId = productionLineId, RosterDate = rosterDate, ShiftId = shiftId, PlannedInChargeEmployeeId = plannedInChargeEmployeeId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); return JsonConvert.SerializeObject(result); } public async Task GetCopyForwardSourceAsync( int productionLineId, int shiftId, byte rosterType, DateOnly newRosterDate, LoginDTO login, CancellationToken ct) { return await _qe.QuerySingleAsync(login, LineRosterQB.GET_COPY_FORWARD_SOURCE, new { ProductionLineId = productionLineId, ShiftId = shiftId, RosterType = rosterType, NewRosterDate = newRosterDate, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } public async Task GetRosterIdForStaffing(int lineShiftStaffingId, LoginDTO login, CancellationToken ct) { var rows = await _qe.QueryAsync(login, LineRosterQB.GET_ROSTERID_FOR_STAFFING, new { LineShiftStaffingId = lineShiftStaffingId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); var list = rows.ToList(); return list.Count > 0 ? list[0] : null; } }