public History(UndercarriageContext undercarriageContext, int historyRecordId) { _context = undercarriageContext; _history = _context.ACTION_TAKEN_HISTORY.Find(historyRecordId); }
public IEquipmentActionRecord UpdateEquipmentByAction(IEquipmentActionRecord actionRecord, ref string OperationResult) { //Steps in this method: //1- Create a record in ACTION_TAKEN_HISTORY Table with no data in EquipmentSMU and EquipmentLTD //2- Update Equipment, Systems and Components life //3- Update ACTION_TAKEN_HISTORY Table with New EquipmentSMU and EquipmentLTD //4- Return an IEquipmentActionRecord to use in the happening action //Step1 OperationResult += "Start insert into -> ACTION_TAKEN_HISTORY" + System.Environment.NewLine; var k = new ACTION_TAKEN_HISTORY { action_type_auto = (int)ActionType.NoActionTakenYet, cmu = 0, event_date = actionRecord.ActionDate, entry_date = DateTime.Now, entry_user_auto = actionRecord.ActionUser.Id, equipmentid_auto = actionRecord.EquipmentId, cost = (long)actionRecord.Cost, equipment_ltd = 0, //Will be updated in Step3 equipment_smu = 0, //Will be updated in Step3 comment = actionRecord.Comment }; _context.ACTION_TAKEN_HISTORY.Add(k); try { _context.SaveChanges(); OperationResult += "Succeded" + System.Environment.NewLine; } catch (Exception ex) { OperationResult += "Error :" + ex.Message + System.Environment.NewLine; return(null); } if (base.longNullableToint(k.history_id) == 0) { OperationResult += "Error: Returned Id is not valid" + System.Environment.NewLine; return(null); } //End of Step 1 //Step2 OperationResult += "Updating Equipment, Systems and Components life -> " + System.Environment.NewLine; if (!UpdateEquipmentLife(actionRecord.EquipmentId, actionRecord.ReadSmuNumber, actionRecord.ActionUser.Id, base.longNullableToint(k.history_id), actionRecord.ActionDate, ref OperationResult)) { return(null); } int EquipmentActualLife = GetEquipmentLife(actionRecord.EquipmentId, actionRecord.ActionDate); int EquipmentCurrentSMU = GetEquipmentSerialMeterUnit(actionRecord.EquipmentId, actionRecord.ActionDate); if (EquipmentActualLife < 0 || EquipmentCurrentSMU < 0) { OperationResult += "Equipment Actual Life OR Equipment Current SMU is invalid" + System.Environment.NewLine; return(null); } //Step3 k.equipment_ltd = EquipmentActualLife; k.equipment_smu = EquipmentCurrentSMU; _context.Entry(k).State = System.Data.Entity.EntityState.Modified; //Step4 actionRecord.Id = (int)k.history_id; actionRecord.EquipmentActualLife = EquipmentActualLife; return(actionRecord); }
protected IEquipmentActionRecord UpdateEquipmentByAction(IEquipmentActionRecord actionRecord, ref string OperationResult) { //Steps in this method: //1- Create a record in ACTION_TAKEN_HISTORY Table with no data in EquipmentSMU and EquipmentLTD //2- Update Equipment, Systems and Components life //3- Update ACTION_TAKEN_HISTORY Table with New EquipmentSMU and EquipmentLTD //4- Return an IEquipmentActionRecord to use in the occuring action actionLifeUpdate = new ActionLifeUpdate(); DALEquipment = _context.EQUIPMENTs.Find(actionRecord.EquipmentId); if (DALEquipment == null) { return(null); } //Step1 int aType = 0; try { aType = (int)actionRecord.TypeOfAction; } catch { aType = (int)ActionType.NoActionTakenYet; } OperationResult += "Start insert into ACTION_TAKEN_HISTORY" + System.Environment.NewLine; var k = new ACTION_TAKEN_HISTORY { action_type_auto = aType, cmu = 0, event_date = actionRecord.ActionDate, entry_date = DateTime.Now, entry_user_auto = actionRecord.ActionUser.Id, equipmentid_auto = actionRecord.EquipmentId, cost = (long)actionRecord.Cost, equipment_ltd = 0, //Will be updated in Step3 equipment_smu = 0, //Will be updated in Step3 comment = actionRecord.Comment, GETActionHistoryId = actionRecord.GETHistoryId, recordStatus = (int)RecordStatus.MiddleOfAction }; _context.ACTION_TAKEN_HISTORY.Add(k); try { _context.SaveChanges(); OperationResult += "Succeded" + System.Environment.NewLine; DALRecord = k; } catch (Exception ex) { OperationResult += "Error :" + ex.Message + System.Environment.NewLine; Message = "Saving action history failed!"; return(null); } if (longNullableToint(k.history_id) == 0) { OperationResult += "Error: Returned Id is not valid" + System.Environment.NewLine; Message = "Action history has not been saved successfully"; return(null); } actionLifeUpdate.ActionTakenHistory = k; int EquipmentActualLife = actionRecord.EquipmentActualLife; int EquipmentCurrentSMU = actionRecord.ReadSmuNumber; //End of Step 1 if (k.action_type_auto != (int)ActionType.UpdateSetupEquipment) { //Step2 OperationResult += "Updating Equipment, Systems and Components life " + System.Environment.NewLine; if (!UpdateEquipmentLife(actionRecord.EquipmentId, actionRecord.ReadSmuNumber, actionRecord.ActionUser.Id, longNullableToint(k.history_id), actionRecord.ActionDate, ref OperationResult)) { return(null); } EquipmentActualLife = GetEquipmentLife(actionRecord.EquipmentId, actionRecord.ActionDate, true); EquipmentCurrentSMU = GetEquipmentSerialMeterUnit(actionRecord.EquipmentId, actionRecord.ActionDate, true); if (EquipmentActualLife < 0 || EquipmentCurrentSMU < 0) { OperationResult += "Equipment Actual Life OR Equipment Current SMU is invalid" + System.Environment.NewLine; Message = "Stored Life or SMU of the equipment is less than zero and not correct!"; return(null); } } //Step3 k.equipment_ltd = EquipmentActualLife; k.equipment_smu = EquipmentCurrentSMU; _context.Entry(k).State = System.Data.Entity.EntityState.Modified; try { OperationResult += "Updating Action Taken History..." + Environment.NewLine; _context.SaveChanges(); OperationResult += "Succeded" + Environment.NewLine; } catch (Exception ex) { OperationResult += "Error :" + ex.Message + Environment.NewLine; Message = "Updating action history failed!"; return(null); } //Step4 actionRecord.Id = (int)k.history_id; actionRecord.EquipmentActualLife = EquipmentActualLife; return(actionRecord); }
public ActionStatus Start() { if (Status == ActionStatus.Close) { if (Params.EquipmentInspection != null) { var ti = _context.TRACK_INSPECTION.Find(Params.EquipmentInspection.inspection_auto); if (ti != null) { if (ti.ActionHistoryId != null && ti.ActionTakenHistory != null) { //We need to keep previous action record data and if this action is not valid bring that one back removingActionHistoryRecord = ti.ActionTakenHistory; removingActionRecord = new EquipmentActionRecord { Id = longNullableToint(removingActionHistoryRecord.history_id), ActionDate = removingActionHistoryRecord.event_date, ActionUser = new User { Id = longNullableToint(removingActionHistoryRecord.entry_user_auto) }, EquipmentId = longNullableToint(removingActionHistoryRecord.equipmentid_auto), ReadSmuNumber = removingActionHistoryRecord.equipment_smu, TypeOfAction = ActionType.InsertInspection, Comment = removingActionHistoryRecord.comment, Cost = removingActionHistoryRecord.cost }; _context.ACTION_TAKEN_HISTORY.Remove(removingActionHistoryRecord); try { _context.SaveChanges(); } catch (Exception e) { Message = "Error on removing previous action history: " + e.Message; return(ActionStatus.Close); } } } else { Message = "Inspection not found to be updated!"; return(ActionStatus.Close); } string log = ""; _actionRecord = UpdateEquipmentByAction(_actionRecord, ref log); ActionLog += log; Message = base.Message; if (_actionRecord != null && _actionRecord.Id != 0) { //Step3 Update action record to have component fields ActionLog += "Updating Action History ..." + Environment.NewLine; var dalActionRecord = _context.ACTION_TAKEN_HISTORY.Find(_actionRecord.Id); //TRACK_ACTION_TYPE Table should be updated to show actions related to the new actions and previous ones were unusable dalActionRecord.action_type_auto = (int)ActionType.UpdateInspection; _context.Entry(dalActionRecord).State = System.Data.Entity.EntityState.Modified; Status = ActionStatus.Started; Message = "Opened successfully"; } else { Status = ActionStatus.Close; return(Status); } } else { Status = ActionStatus.Close; Message = "Parameters are not set correctly"; } } return(Status); }