/// <summary> /// Creates and returns a new object for the GET_EVENTS_IMPLEMENT table. /// </summary> /// <param name="getAuto"></param> /// <param name="ltd"></param> /// <param name="eventsAuto"></param> /// <returns></returns> public GET_EVENTS_IMPLEMENT createImplementEvent(int getAuto, int ltd, long eventsAuto) { GET_EVENTS_IMPLEMENT implementEvent = new GET_EVENTS_IMPLEMENT(); implementEvent.get_auto = getAuto; implementEvent.ltd = ltd; implementEvent.events_auto = eventsAuto; return(implementEvent); }
/// <summary> /// Find and return the previous implement event given the implement (GET) auto /// and an event date. /// </summary> /// <param name="_gContext"></param> /// <param name="getAuto"></param> /// <param name="eventDate"></param> /// <returns></returns> public GET_EVENTS_IMPLEMENT findPreviousImplementEvent(GETContext _gContext, long GETAuto, DateTime eventDate) { GET_EVENTS_IMPLEMENT result = new GET_EVENTS_IMPLEMENT(); DateTime eventDate2 = new DateTime(eventDate.Year, eventDate.Month, eventDate.Day, 23, 59, 59); result = (from ie in _gContext.GET_EVENTS_IMPLEMENT join ge in _gContext.GET_EVENTS on ie.events_auto equals ge.events_auto where ie.get_auto == GETAuto && ge.event_date <= eventDate2 && ge.recordStatus == 0 orderby ge.event_date descending, ge.events_auto descending select ie).FirstOrDefault(); return(result); }
public ActionStatus Start() { if (Status == ActionStatus.Close) { int result = -1; int changesSaved = 0; // Save a new row in GET_EVENTS. GET_EVENTS getEvent = new GET_EVENTS { user_auto = Params.UserAuto, action_auto = (int)Params.ActionType, recorded_date = Params.RecordedDate, event_date = Params.EventDate, comment = Params.Comment, cost = Params.Cost }; _gContext.GET_EVENTS.Add(getEvent); _gContext.SaveChanges(); // Check whether there are any inspections against this implement. var isExists_inspection = _gContext.GET_IMPLEMENT_INSPECTION .Where(gii => gii.get_auto == Params.GetAuto) .OrderBy(o => o.inspection_date) .ThenBy(t => t.inspection_auto) .Select(s => new { s.inspection_auto, s.ltd }).FirstOrDefault(); // Check whether there are any inventory actions against this implement. var isExists_inventoryAction = (from inv in _gContext.GET_EVENTS_INVENTORY join gei in _gContext.GET_EVENTS_IMPLEMENT on inv.implement_events_auto equals gei.implement_events_auto where gei.get_auto == Params.GetAuto select new { inv.inventory_events_auto, inv.implement_event.ltd }).FirstOrDefault(); // Check whether there are any implement setup events for this implement. var isExists_implementSetup = (from ge in _gContext.GET_EVENTS join ga in _gContext.GET_ACTIONS on ge.action_auto equals ga.actions_auto join gei in _gContext.GET_EVENTS_IMPLEMENT on ge.events_auto equals gei.events_auto where (ga.actions_auto == (int)GETActionType.Implement_Setup || ga.actions_auto == (int)GETActionType.Implement_Updated) && gei.get_auto == Params.GetAuto orderby ge.events_auto descending select new { ge.events_auto, gei.implement_events_auto }).ToArray(); // Now update the GET_EVENTS_IMPLEMENT table. GET_EVENTS_IMPLEMENT getImplementEvent = new GET_EVENTS_IMPLEMENT { events_auto = getEvent.events_auto, get_auto = Params.GetAuto, ltd = Params.ImplementLTD }; _gContext.GET_EVENTS_IMPLEMENT.Add(getImplementEvent); // Determine the maximum valid range for the specified implement's LTD at setup. int maxValidLTD = 0; if (isExists_inspection != null) { maxValidLTD = isExists_inspection.ltd; } if (isExists_inventoryAction != null) { if (isExists_inventoryAction.ltd < maxValidLTD) { maxValidLTD = isExists_inventoryAction.ltd; } } var recentImplement = ((isExists_inspection == null) && (isExists_inventoryAction == null)); // Check that inspections and inventory actions have not been done for this implement. if (recentImplement) { // Update the GET_EVENTS_COMPONENT table. if (isExists_implementSetup.Count() > 0) { getEvent.action_auto = (int)GETActionType.Implement_Updated; var componentInfo = (from gc in _gContext.GET_COMPONENT where gc.get_auto == Params.GetAuto && gc.active == true orderby gc.get_component_auto select new { gc.get_component_auto, gc.cmu }); var componentInfoArray = componentInfo.ToArray(); for (int i = 0; i < componentInfoArray.Count(); i++) { _gContext.GET_EVENTS_COMPONENT.Add( new GET_EVENTS_COMPONENT { events_auto = getEvent.events_auto, get_component_auto = componentInfoArray[i].get_component_auto, ltd = componentInfoArray[i].cmu }); } } try { changesSaved = _gContext.SaveChanges(); } catch (Exception ex1) { } result = changesSaved > 0 ? Params.ImplementLTD : -1; } // Else if this is an existing implement, but LTD or date needs to be updated. else if (!recentImplement && (Params.ImplementLTD <= maxValidLTD)) { if (isExists_implementSetup.Count() > 0) { getEvent.action_auto = (int)GETActionType.Implement_Updated; var componentInfo = (from gc in _gContext.GET_COMPONENT where gc.get_auto == Params.GetAuto && gc.active == true orderby gc.get_component_auto select new { gc.get_component_auto, gc.cmu }); var componentInfoArray = componentInfo.ToArray(); for (int i = 0; i < componentInfoArray.Count(); i++) { _gContext.GET_EVENTS_COMPONENT.Add( new GET_EVENTS_COMPONENT { events_auto = getEvent.events_auto, get_component_auto = componentInfoArray[i].get_component_auto, ltd = componentInfoArray[i].cmu }); } } try { changesSaved = _gContext.SaveChanges(); } catch (Exception ex1) { } result = changesSaved > 0 ? Params.ImplementLTD : -1; } // Else, rollback to previous LTD else { if (isExists_implementSetup.Count() > 0) { getEvent.action_auto = (int)GETActionType.Implement_Updated; var prevImplementSetup = isExists_implementSetup[0].implement_events_auto; var prevLTD = _gContext.GET_EVENTS_IMPLEMENT.Find(prevImplementSetup).ltd; var get = _gContext.GET.Find(Params.GetAuto); get.impsetup_hours = prevLTD; getImplementEvent.ltd = prevLTD; try { changesSaved = _gContext.SaveChanges(); } catch (Exception ex2) { } result = changesSaved > 0 ? prevLTD : -1; } } Status = ActionStatus.Started; Message = result.ToString(); } return(Status); }