private Component GetComponentBasedOnTypeAndPositionFromOldSystemToTheNewOne(long OldComponentId) { GENERAL_EQ_UNIT OldDALcmpnt = _context.GENERAL_EQ_UNIT.Find(OldComponentId); if (OldDALcmpnt == null) { return(null); } Component Newcmpnt = null; //Finding the component which was in the same type and place of the old component foreach (var geuNewComp in logicalNewSystem.Components) { if (Newcmpnt == null && geuNewComp.LU_COMPART.comparttype_auto == OldDALcmpnt.LU_COMPART.comparttype_auto && geuNewComp.pos == OldDALcmpnt.pos) { Newcmpnt = new Component(_context, longNullableToint(geuNewComp.equnit_auto)); } } return(Newcmpnt); }
public ActionStatus Commit() { ActionLog += "Commiting the Operation ..." + Environment.NewLine; if (Status != ActionStatus.Valid) { ActionLog += "Operation Status is not valid" + Environment.NewLine; Status = ActionStatus.Failed; return(Status); } //Including Steps: //Step1 Detach the old component //Step2 Create a new component attached to this Equipment and set system as well //Step3 Update ActionRecord //Step1 ActionLog += "Adding New Component ..." + Environment.NewLine; GENERAL_EQ_UNIT geuNew = new GENERAL_EQ_UNIT { equipmentid_auto = _actionRecord.EquipmentId, module_ucsub_auto = Params.oldgeuComponent.module_ucsub_auto, date_installed = _actionRecord.ActionDate, created_user = _actionRecord.ActionUser.userStrId, compartid_auto = Params.newComponent.CompartId, created_date = _actionRecord.ActionDate, comp_status = (byte)Params.newComponent.ComponentStatus, pos = Params.oldgeuComponent.pos, side = Params.oldgeuComponent.side, eq_ltd_at_install = _actionRecord.EquipmentActualLife, track_0_worn = 0, track_100_worn = 0, track_120_worn = 0, track_budget_life = Params.newComponent.BudgetedLife, cmu = Params.newComponent.ComponentLifeAtInstall, cost = Params.newComponent.Cost, eq_smu_at_install = _actionRecord.ReadSmuNumber, smu_at_install = _actionRecord.ReadSmuNumber, system_LTD_at_install = GetSystemLife(longNullableToint(Params.oldgeuComponent.equnit_auto), _actionRecord.ActionDate), component_current_value = 0, variable_comp = false, insp_uom = 0, make_auto = Params.newComponent.brandId == 0 ? null : (int?)Params.newComponent.brandId }; _context.GENERAL_EQ_UNIT.Add(geuNew); try { ActionLog += "Save new component to the database and get the new Id ..." + Environment.NewLine; _context.SaveChanges(); ActionLog += "Save new component succeed" + Environment.NewLine; } catch (Exception e) { ActionLog += "Save new component FAILED : " + e.Message; Status = ActionStatus.Failed; Message = "Save new component FAILED"; return(Status); } //Step3 Update action record to have component fields ActionLog += "Updating Action History ..." + Environment.NewLine; var dalActionRecord = _context.ACTION_TAKEN_HISTORY.Find(_actionRecord.Id); //Now the Action Taken History Table should be updated to show actions details dalActionRecord.compartid_auto = Params.oldgeuComponent.compartid_auto; if (new Compart(_context).isAChild(Params.oldgeuComponent.compartid_auto)) //TT-610 -> if is a child then don't reference the old component because it would cause duplicate records in history page { dalActionRecord.equnit_auto = -(Params.oldgeuComponent.equnit_auto); dalActionRecord.equnit_auto_new = -geuNew.equnit_auto; } else { dalActionRecord.equnit_auto = Params.oldgeuComponent.equnit_auto; dalActionRecord.equnit_auto_new = geuNew.equnit_auto; } dalActionRecord.action_type_auto = (int)ActionType.ReplaceComponentWithNew; dalActionRecord.cmu = (int)Params.oldgeuComponent.cmu; dalActionRecord.cost = (long)_actionRecord.Cost; dalActionRecord.system_auto_id = Params.oldgeuComponent.module_ucsub_auto; dalActionRecord.recordStatus = (int)RecordStatus.Available; _context.Entry(dalActionRecord).State = System.Data.Entity.EntityState.Modified; //Update all life records of the old component to be for the new component var componentLifesAfterChangeOut = _context.COMPONENT_LIFE.Where(m => m.ComponentId == Params.oldgeuComponent.equnit_auto && m.ActionDate > _actionRecord.ActionDate).ToList(); foreach (var life in componentLifesAfterChangeOut) { var athistoryFuture = _context.ACTION_TAKEN_HISTORY.Find(life.ActionId); int EqLifeInFutureAction = 0; int EqSMUInFutureAction = 0; if (athistoryFuture != null) { if (athistoryFuture.equnit_auto == Params.oldgeuComponent.equnit_auto) { athistoryFuture.equnit_auto = geuNew.equnit_auto; _context.Entry(athistoryFuture).State = EntityState.Modified; } EqLifeInFutureAction = athistoryFuture.equipment_ltd; EqSMUInFutureAction = athistoryFuture.equipment_smu; } int newComponentActualLife = 0; if (EqLifeInFutureAction != 0 && EqLifeInFutureAction > _actionRecord.EquipmentActualLife) { newComponentActualLife = Params.newComponent.ComponentLifeAtInstall + (EqLifeInFutureAction - _actionRecord.EquipmentActualLife); } else if (EqSMUInFutureAction != 0 && EqSMUInFutureAction > _actionRecord.ReadSmuNumber) { newComponentActualLife = Params.newComponent.ComponentLifeAtInstall + (EqSMUInFutureAction - _actionRecord.ReadSmuNumber); } if (newComponentActualLife == 0) { newComponentActualLife = Params.newComponent.ComponentLifeAtInstall; } life.ActualLife = newComponentActualLife; life.ComponentId = geuNew.equnit_auto; life.Title = "Updated because of component replacement in the past"; _context.Entry(life).State = EntityState.Modified; } //Step4 Update all inspections after this change out var inspections = _context.EQUIPMENTs.Find(_actionRecord.EquipmentId).TRACK_INSPECTION.Where(m => m.inspection_date > _actionRecord.ActionDate).ToList(); var logicalEquipment = new Equipment(_context, _actionRecord.EquipmentId); foreach (var inspect in inspections) { var details = inspect.TRACK_INSPECTION_DETAIL.Where(m => m.track_unit_auto == Params.oldgeuComponent.equnit_auto).ToList(); foreach (var d in details) { var cmpnt = new Component(_context, longNullableToint(geuNew.equnit_auto)); InspectionImpact impact = d.TRACK_INSPECTION.impact == 2 ? InspectionImpact.High : InspectionImpact.Low; int ToolId = d.tool_auto == null ? 0 : (int)d.tool_auto; if (cmpnt.Id != 0) { d.worn_percentage = cmpnt.CalcWornPercentage(ConvertFrom(MeasurementType.Milimeter, d.reading), ToolId, impact); char evl = '-'; if (cmpnt.GetEvalCodeByWorn(d.worn_percentage, out evl)) { d.eval_code = evl.ToString(); } d.hours_on_surface = cmpnt.GetComponentLife(inspect.inspection_date); } // Update images stored in the old table to reference the new compartid_auto if the component type has changed // with the new replacement. Otherwise the image will not be viewable in the image popup. var images = _context.COMPART_ATTACH_FILESTREAM.Where(f => f.compartid_auto == Params.oldgeuComponent.compartid_auto) .Where(f => f.position == Params.oldgeuComponent.pos) .Where(f => f.inspection_auto == d.inspection_auto); foreach (var i in images) { i.compartid_auto = geuNew.compartid_auto; _context.Entry(i).State = EntityState.Modified; } d.track_unit_auto = geuNew.equnit_auto; _context.Entry(d).State = System.Data.Entity.EntityState.Modified; } logicalEquipment.UpdateMiningShovelInspectionParentsFromChildren(inspect.inspection_auto); } Params.oldgeuComponent.equipmentid_auto = null; Params.oldgeuComponent.module_ucsub_auto = null; Params.oldgeuComponent.modified_user = _actionRecord.ActionUser.userStrId; _context.Entry(Params.oldgeuComponent).State = System.Data.Entity.EntityState.Modified; try { ActionLog += "Save all changes to the database and get the new Id ..." + Environment.NewLine; _context.SaveChanges(); ActionLog += "Save all succeeded" + Environment.NewLine; } catch (Exception e) { ActionLog += "Save all changes FAILED : " + e.Message; Status = ActionStatus.Failed; Message = "Save all changes FAILED "; return(Status); } //Add a life record for the new component ActionLog += "Adding Life of the new component"; DAL.ComponentLife NewComponentLife = new ComponentLife { ActionDate = _actionRecord.ActionDate, ActionId = _actionRecord.Id, ActualLife = longNullableToint(geuNew.cmu), ComponentId = geuNew.equnit_auto, Title = "New component added by replace Action", UserId = _actionRecord.ActionUser.Id }; _context.COMPONENT_LIFE.Add(NewComponentLife); try { ActionLog += "Adding Life of the new component"; _context.SaveChanges(); ActionLog += "Adding Life of the new component Succeed"; } catch (Exception e1) { ActionLog += "Adding Life of the new component Filed " + e1.Message; string Message = e1.Message; } UniqueId = geuNew.equnit_auto.LongNullableToInt(); Status = ActionStatus.Succeed; Message = "All done successfully"; return(Status); }