//public Single SetRestWeight(int shipId, string tripId, int taskId, int? stateId, Single arrangedWeight)
        //{
        //    var jns = db.Journeys.FirstOrDefault(p => p.TripId == tripId && p.ShipId == shipId);
        //    Single result = (jns.Weight - arrangedWeight);
        //    return result;
        //}
        private void UpdateModels(UpdateViewModel upv, string tripId, int shipId, dynamic model, int kind, string userName)
        {
            model.ShipId = shipId;
            model.TripId = tripId;
            switch (kind)
            {
                case 0:
                    int taskId = model.TaskId;
                    int? stateId = model.StateId;
                    int? portId = model.PortId;
                    //if ((stateId == ConstId.LoadedStateId || stateId == ConstId.UnloadedStateId || stateId == ConstId.UnloadingStateId || stateId == ConstId.LoadingStateId) && (taskId == ConstId.LoadTaskId || taskId == ConstId.UnloadTaskId))
                    //{
                    //    model.RestWeight = SetRestWeight(shipId, tripId, taskId, stateId, model.ArrangedWeight);
                    //}
                    //JourneySupervision a = db.JourneySuppervision.Where(p => p.TripId == tripId && p.ShipId == shipId && p.TaskId == taskId && p.PortId == portId && p.StateId == stateId).FirstOrDefault();
                    //if (a == null)
                    //{
                    if (model.PortId != null || model.CreatedDate != null || model.Position != null || model.ArrangedWeight != 0)
                    {
                        var js = new JourneySupervision
                        {
                            ShipId = shipId,
                            TripId = tripId,
                            StateId = stateId,
                            PortId = model.PortId,
                            ArrangedWeight = model.ArrangedWeight,
                            CreatedDate = model.CreatedDate,
                            RestWeight = model.RestWeight,
                            Position = model.Position,
                            TaskId = taskId,
                            CategoryName = model.CategoryName,
                            Updator = userName,
                            UpdateTime = DateTime.Now,
                            NationName = model.NationName
                        };
                        db.JourneySuppervision.Add(js);
                    }
                    //}
                    //else
                    //{
                    //    if (model.StateId == ConstId.LoadingStateId || model.StateId == ConstId.UnloadingStateId)
                    //    {
                    //        var js = new JourneySupervision
                    //        {
                    //            ShipId = shipId,
                    //            TripId = tripId,
                    //            StateId = stateId,
                    //            PortId = model.PortId,
                    //            ArrangedWeight = model.ArrangedWeight,
                    //            CreatedDate = model.CreatedDate,
                    //            RestWeight = model.RestWeight,
                    //            Position = model.Position,
                    //            TaskId = taskId,
                    //            CategoryName = model.CategoryName,
                    //            Updator = userName,
                    //            UpdateTime = DateTime.Now
                    //        };
                    //        db.JourneySuppervision.Add(js);
                    //    }
                    //    else
                    //    {
                    //        a.ArrangedWeight = model.ArrangedWeight ?? 0;
                    //        a.RestWeight = model.RestWeight ?? 0;
                    //        a.CreatedDate = model.CreatedDate;
                    //        a.Note = model.Note;
                    //        a.PortId = model.PortId;
                    //        a.Position = model.Position;
                    //        a.StateId = stateId;
                    //        a.CategoryName = model.CategoryName;
                    //        a.UpdateTime = DateTime.Now;
                    //        a.Updator = userName;
                    //        db.Entry(a).State = EntityState.Modified;
                    //    }
                    //}
                    if (model.StateId != null)
                    {
                        var plan = db.Compares.FirstOrDefault(p => p.StateId == stateId);
                        if (plan != null)
                        {
                            string plid = plan.PlanId;

                            var jourpl = db.journeyPlans.FirstOrDefault(p => p.ShipId == shipId && p.TripId == tripId && p.PlanId == plid && p.PortId == portId);
                            if (jourpl != null)
                            {
                                var plTime = jourpl.PlanTime;
                                DateTime? jnTime = model.CreatedDate;
                                if (plTime != null && jnTime != null)
                                {
                                    var rs1 = db.Results.Where(p => p.TripId == tripId && p.ShipId == shipId && p.PlanId == plid && p.PortId == portId);
                                    if (rs1 != null)
                                    {
                                        foreach (var item in rs1)
                                        {
                                            item.StateId = stateId;
                                            item.JourneyDate = jnTime;
                                            var days = (item.PlanDate.Value - jnTime.Value).Days;
                                            if (days > 0)
                                            {
                                                upv.IsLate = 1;
                                                item.Remark = "Sớm hơn " + days + " ngày so với dự kiến";
                                            }
                                            if (days < 0)
                                            {
                                                item.Remark = "Trễ hơn " + -days + " ngày so với dự kiến";
                                                upv.IsLate = 2;
                                            }
                                            if (days == 0)
                                            {
                                                upv.IsLate = 0;
                                                item.Remark = "Đúng theo dự kiến";
                                            }
                                        }
                                        upv.rStateId = stateId;
                                        upv.rPlanId = plid;
                                        upv.rShipId = shipId;
                                    }
                                }
                            }
                        }

                    }
                    string ctripId = GenerateTripId(tripId);
                    var jn = db.Journeys.FirstOrDefault(p => p.ShipId == shipId && p.TripId == ctripId);
                    if (model.TaskId == ConstId.UnloadTaskId && model.StateId == ConstId.UnloadedStateId && jn == null)
                    {
                        var now = model.CreatedDate??DateTime.Now;
                        var updateJourney = db.Journeys.FirstOrDefault(p => p.ShipId == shipId && p.TripId == tripId);
                        updateJourney.EndDate = now;
                       // db.Entry(updateJourney).State = System.Data.EntityState.Modified;
                       // db.SaveChanges();
                        var journey = new Journey { ShipId = model.ShipId, TripId = ctripId, StartPortId = model.PortId, StartDate = now };
                        db.Journeys.Add(journey);
                    }
                    db.SaveChanges();

                    break;
                case 1:
                    string planId = model.PlanId;
                    //var b = db.journeyPlans.FirstOrDefault(p => p.TripId == tripId && p.ShipId == shipId && p.PlanId == planId);
                    //if (b != null)
                    //{
                    //    b.PlanTime = model.PlanTime;
                    //    b.PortId = model.PortId;
                    //    b.UpdateTime = DateTime.Now;
                    //    b.Updator = userName;
                    //}
                    //else
                    //{
                    if (model.PlanId != null || model.PlanTime != null && model.PortId)
                    {
                        var jp = new JourneyPlan
                        {
                            ShipId = shipId,
                            TripId = tripId,
                            PlanId = planId,
                            PlanTime = model.PlanTime,
                            PortId = model.PortId,
                            Updator = userName,
                            UpdateTime = DateTime.Now
                        };
                        var rs = new Result { PlanDate = model.PlanTime, PlanId = planId, ShipId = shipId, TripId = tripId, PortId = model.PortId };
                        db.journeyPlans.Add(jp);
                        db.Results.Add(rs);
                    }
                    //}
                    db.SaveChanges();
                    break;
                case 2:
                    bool isnull = string.IsNullOrEmpty(model.NoteContent);
                    if (!isnull)
                    {
                        var note = new Note { CreatedDate = model.CreatedDate, ShipId = shipId, TripId = tripId, NoteContent = model.NoteContent, Updator = userName, UpdateTime = DateTime.Now };
                        db.Notes.Add(note);
                        db.SaveChanges();
                    }
                    break;
                default:
                    break;
            }
        }
 public ActionResult NModify(Note model)
 {
     if (ModelState.IsValid)
     {
         model.Updator = User.Identity.Name;
         model.UpdateTime = DateTime.Now;
         model.CreatedDate = DateTime.Now;
         DbContext.Entry(model).State = EntityState.Modified;
         DbContext.SaveChanges();
     }
     return RedirectToAction("Detail", "Home", new { shipId = model.ShipId, tripId = model.TripId, all = true, currentTrip = true });
 }