public override Result <ServiceHistoryModel> Find(IFilter filter) { try { ServiceHistory serviceHistory = _serviceHistoryDAL.Find((ServiceHistoryFilter)filter); ServiceHistoryModel serviceHistoryModel = _mapper.Map <ServiceHistoryModel>(serviceHistory); return(Result <ServiceHistoryModel> .BuildSuccess(serviceHistoryModel)); } catch (Exception error) { return(Result <ServiceHistoryModel> .BuildError("Erro ao localizar o veículo empenhado na ocorrência.", error)); } }
public override Result <VehiclePositionHistory> Register(VehiclePositionHistoryModel model) { try { VehiclePositionHistory vehiclePositionHistory = _mapper.Map <VehiclePositionHistory>(model); var result = _vehiclePositionHistoryValidation.Validate(vehiclePositionHistory); if (!result.Success) { return(result); } var resultFind = _vehiclePositionHistoryDAL.Find(new VehiclePositionHistoryFilter { ServiceHistoryId = vehiclePositionHistory.ServiceHistoryId, VehiclePosition = vehiclePositionHistory.VehiclePosition }); if ((resultFind?.Id ?? 0) > 0) { resultFind.Date = vehiclePositionHistory.Date; _vehiclePositionHistoryDAL.Update(resultFind); var resultUpdate = _vehiclePositionHistoryDAL.Save(); if (!resultUpdate.Success) { return(Result <VehiclePositionHistory> .BuildError(resultUpdate.Messages)); } return(Result <VehiclePositionHistory> .BuildSuccess(null)); } if (vehiclePositionHistory.VehiclePosition == VehiclePosition.J10Local) { var resultJ = _vehiclePositionHistoryDAL.Find(new VehiclePositionHistoryFilter { ServiceHistoryId = vehiclePositionHistory.ServiceHistoryId, VehiclePosition = VehiclePosition.J9Local }); if ((resultJ?.Id ?? 0) == 0) { return(Result <VehiclePositionHistory> .BuildError("Para cadastrar o J10 no local da ocorrência é necessário informar o J9.")); } } if (vehiclePositionHistory.VehiclePosition == VehiclePosition.J9Hospital) { var resultJ = _vehiclePositionHistoryDAL.Find(new VehiclePositionHistoryFilter { ServiceHistoryId = vehiclePositionHistory.ServiceHistoryId, VehiclePosition = VehiclePosition.J10Local }); if ((resultJ?.Id ?? 0) == 0) { return(Result <VehiclePositionHistory> .BuildError("Para cadastrar o J9 para o hospital é necessário informar o J10 no local da ocorrência.")); } } if (vehiclePositionHistory.VehiclePosition == VehiclePosition.J10Hospital) { var resultJ = _vehiclePositionHistoryDAL.Find(new VehiclePositionHistoryFilter { ServiceHistoryId = vehiclePositionHistory.ServiceHistoryId, VehiclePosition = VehiclePosition.J9Hospital }); if ((resultJ?.Id ?? 0) == 0) { return(Result <VehiclePositionHistory> .BuildError("Para cadastrar o J10 no hospital é necessário informar o J9 para o Hospital.")); } } if (vehiclePositionHistory.VehiclePosition == VehiclePosition.J11) { var resultJ9H = _vehiclePositionHistoryDAL.Find(new VehiclePositionHistoryFilter { ServiceHistoryId = vehiclePositionHistory.ServiceHistoryId, VehiclePosition = VehiclePosition.J9Hospital }); if ((resultJ9H?.Id ?? 0) > 0) { var resultJ10H = _vehiclePositionHistoryDAL.Find(new VehiclePositionHistoryFilter { ServiceHistoryId = vehiclePositionHistory.ServiceHistoryId, VehiclePosition = VehiclePosition.J10Hospital }); if ((resultJ10H?.Id ?? 0) == 0) { return(Result <VehiclePositionHistory> .BuildError("Para cadastrar o J11 é necessário informar o J10 no Hospital.")); } } else { var resultJ = _vehiclePositionHistoryDAL.Find(new VehiclePositionHistoryFilter { ServiceHistoryId = vehiclePositionHistory.ServiceHistoryId, VehiclePosition = VehiclePosition.J10Local }); if ((resultJ?.Id ?? 0) == 0) { return(Result <VehiclePositionHistory> .BuildError("Para cadastrar o J11 é necessário informar o J10 no local.")); } } } if (vehiclePositionHistory.VehiclePosition == VehiclePosition.J12) { var resultJ = _vehiclePositionHistoryDAL.Find(new VehiclePositionHistoryFilter { ServiceHistoryId = vehiclePositionHistory.ServiceHistoryId, VehiclePosition = VehiclePosition.J11 }); if ((resultJ?.Id ?? 0) == 0) { return(Result <VehiclePositionHistory> .BuildError("Para cadastrar o J12 é necessário informar o J11.")); } } _vehiclePositionHistoryDAL.Insert(vehiclePositionHistory); vehiclePositionHistory.ServiceHistory = _serviceHistoryDAL.Find(new ServiceHistoryFilter { Id = vehiclePositionHistory.ServiceHistoryId }); if (vehiclePositionHistory.VehiclePosition == VehiclePosition.J11) { _vehicleDAL.Find(new VehicleFilter { Id = vehiclePositionHistory.ServiceHistory.VehicleId }); vehiclePositionHistory.ServiceHistory.Vehicle.VehicleStatus = VehicleStatus.Cleared; _vehicleDAL.Update(vehiclePositionHistory.ServiceHistory.Vehicle); } if (vehiclePositionHistory.VehiclePosition == VehiclePosition.J12) { vehiclePositionHistory.ServiceHistory.ServiceHistoryStatus = ServiceHistoryStatus.Finalized; _serviceHistoryDAL.Update(vehiclePositionHistory.ServiceHistory); } var resultSave = _vehiclePositionHistoryDAL.Save(); if (!resultSave.Success) { return(Result <VehiclePositionHistory> .BuildError(resultSave.Messages)); } return(Result <VehiclePositionHistory> .BuildSuccess(null)); } catch (Exception error) { return(Result <VehiclePositionHistory> .BuildError("Erro no momento de registar a posição do veículo.", error)); } }