예제 #1
0
        public void OrderPlanChange(int orderID, int instructionTypeID, int?instructionID, int vehicleResourceID, DateTime arrivalDateTime)
        {
            using (var uow = DIContainer.CreateUnitOfWork())
            {
                if (instructionID.HasValue)
                {
                    Facade.IInstruction facInstruction = new Facade.Instruction();
                    Facade.IJob         facJob         = new Facade.Job();

                    var instructionRepo = DIContainer.CreateRepository <IInstructionRepository>(uow);
                    var jobID           = instructionRepo.Find(instructionID.Value).JobID;

                    var job         = facJob.GetJob(jobID, true, true);
                    var instruction = job.Instructions.Find(i => i.InstructionID == instructionID.Value);

                    var changeTimes   = instruction.PlannedArrivalDateTime != arrivalDateTime;
                    var changeVehicle = instruction.Vehicle.ResourceId != vehicleResourceID;

                    var userName = HttpContext.Current.User.Identity.Name;

                    if (changeVehicle)
                    {
                        var result = facInstruction.PlanInstruction(new[] { instructionID.Value }, jobID, -1, vehicleResourceID, -1, job.LastUpdateDate, userName);

                        if (!result.Success)
                        {
                            throw new ApplicationException(
                                      string.Format(
                                          "Could not update vehicle on run {0} due to the following infringements: {1}",
                                          jobID,
                                          string.Join("\n", result.Infringements.Select(i => i.Description))));
                        }
                    }

                    if (changeTimes)
                    {
                        instruction.PlannedArrivalDateTime   = arrivalDateTime;
                        instruction.PlannedDepartureDateTime = arrivalDateTime.AddMinutes(15);
                        var result = facJob.UpdatePlannedTimes(jobID, job.Instructions, job.LastUpdateDate, userName);

                        if (!result.Success)
                        {
                            throw new ApplicationException(
                                      string.Format(
                                          "Could not update planned times on run {0} due to the following infringements: {1}",
                                          jobID,
                                          string.Join("\n", result.Infringements.Select(i => i.Description))));
                        }
                    }
                }
                else
                {
                    var repo = DIContainer.CreateRepository <IOrderRepository>(uow);
                    repo.AddUpdateOrderPrePlan(orderID, (eInstructionType)instructionTypeID, vehicleResourceID, arrivalDateTime);
                    uow.SaveChanges();
                }
            }
        }