using System.Text.Json; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using WiBLL.Common; using WiDAL.CustomCode.LineProductionPlanAllocation; using WiDAL.DTOs; namespace WiBLL.LineProductionPlanAllocation; public class LineProductionPlanAllocationBLL : ILineProductionPlanAllocationBLL { private readonly ILineProductionPlanAllocationDAL _dal; private readonly AutoNumber _autoNumber; private readonly IWiHubNotifier _hubNotifier; public LineProductionPlanAllocationBLL(ILineProductionPlanAllocationDAL dal, AutoNumber autoNumber, IWiHubNotifier hubNotifier) { _dal = dal; _autoNumber = autoNumber; _hubNotifier = hubNotifier; } public async Task GetByLineDateShift( int productionLineId, DateOnly allocationDate, int shiftId, LoginDTO login, CancellationToken ct) { var rows = await _dal.GetByLineDateShift(productionLineId, allocationDate, shiftId, login, ct) .ConfigureAwait(false); return JsonSerializer.Serialize(rows); } public async Task SaveAllocation(LineProductionPlanAllocationDTO dto, LoginDTO login, CancellationToken ct) { if (dto.ProductionPlanDetailId == 0) throw new InvalidOperationException("ProductionPlanDetailId is required."); if (dto.ProductionLineId == 0) throw new InvalidOperationException("ProductionLineId is required."); if (dto.ShiftId == 0) throw new InvalidOperationException("ShiftId is required."); if (dto.AllocationDate == default) throw new InvalidOperationException("AllocationDate is required."); if (dto.StartMinutes is not null && dto.EndMinutes is not null && dto.StartMinutes >= dto.EndMinutes) throw new InvalidOperationException("StartMinutes must be less than EndMinutes."); dto.TenantId = login.ClientId; dto.ModifiedById = login.UserId; bool isNew = dto.LineAllocationId == 0; if (!isNew) { await _dal.UpdateAllocation(dto, login, ct).ConfigureAwait(false); await _hubNotifier.NotifyAllocationChangedAsync(dto.ProductionLineId, dto.AllocationDate, dto.ShiftId, ct) .ConfigureAwait(false); return $"{SuccessResponse.UpdateSuccessMessage} {dto.LineAllocationId}"; } dto.CreatedById = login.UserId; var an = await _autoNumber.GetNumberAsync(1, "LINEPRODUCTIONPLANALLOCATION", login).ConfigureAwait(false); dto.LineAllocationId = an.StartNumber; try { await _dal.InsertAllocation(dto, login, ct).ConfigureAwait(false); await _hubNotifier.NotifyAllocationChangedAsync(dto.ProductionLineId, dto.AllocationDate, dto.ShiftId, ct) .ConfigureAwait(false); return $"{SuccessResponse.SaveSuccessMessage} {dto.LineAllocationId}"; } catch { await _autoNumber.RollbackAutoNumber("LINEPRODUCTIONPLANALLOCATION", dto.LineAllocationId, login) .ConfigureAwait(false); throw; } } public async Task DeleteAllocation(int lineAllocationId, LoginDTO login, CancellationToken ct) { var context = await _dal.GetLineDateShiftForAllocation(lineAllocationId, login, ct).ConfigureAwait(false); var rows = await _dal.DeleteAllocation(lineAllocationId, login, ct).ConfigureAwait(false); if (rows == 0) return ErrorResponse.DeleteNotFoundMessage; if (context is not null) await _hubNotifier.NotifyAllocationChangedAsync(context.ProductionLineId, context.AllocationDate, context.ShiftId, ct) .ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }