private void CreateVirtualProductionItem(DTO.PlanningProductionDTO dtoPlanning, ref List <ProductionItem> dbProductionItems, ref List <ProductionItem> dbProductionItemResult, int?companyID, int?userId) { using (PlanningProductionEntities context = CreateContext()) { if (dtoPlanning.PlanningProductionID < 0) { var x = context.ProductionItem.Where(o => o.ProductionItemUD == dtoPlanning.ProductionItemUD).FirstOrDefault(); if (x == null) { ProductionItem dbProductionItem = new ProductionItem(); dbProductionItems.Add(dbProductionItem); dbProductionItemResult.Add(dbProductionItem); dbProductionItem.ProductionItemUD = dtoPlanning.ProductionItemUD; dbProductionItem.ProductionItemNM = dtoPlanning.ProductionItemNM; dbProductionItem.ProductionItemTypeID = 1; //component dbProductionItem.UnitID = 25; dbProductionItem.CompanyID = companyID; dbProductionItem.UpdatedBy = userId; dbProductionItem.UpdatedDate = DateTime.Now; } else { dbProductionItemResult.Add(x); } } } foreach (var item in dtoPlanning.PlanningProductionDTOs) { CreateVirtualProductionItem(item, ref dbProductionItems, ref dbProductionItemResult, companyID, userId); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.PlanningProductionDTO dtoPlanning = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PlanningProductionDTO>(); try { int?companyID = fw_factory.GetCompanyID(userId); using (PlanningProductionEntities context = CreateContext()) { //delete item is deleted context.PlanningProductionMng_function_DeleteIsDeletedItem(); //get bom PlanningProduction dbItem = null; if (id > 0) { dbItem = context.PlanningProduction.Where(o => o.PlanningProductionID == id).FirstOrDefault(); } else { dbItem = new PlanningProduction(); context.PlanningProduction.Add(dbItem); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { //create production item for workcenter which is virtual List <ProductionItem> dbVirualProductionItem = new List <ProductionItem>(); List <ProductionItem> dbProductionItemResult = new List <ProductionItem>(); CreateVirtualProductionItem(dtoPlanning, ref dbVirualProductionItem, ref dbProductionItemResult, companyID, userId); context.ProductionItem.AddRange(dbVirualProductionItem); context.SaveChanges(); //convert dto to db converter.DTO2DB_PlanningProduction(dtoPlanning, ref dbItem, dbProductionItemResult); //remove orphan context.PlanningProductionTeam.Local.Where(o => o.PlanningProduction == null).ToList().ForEach(o => context.PlanningProductionTeam.Remove(o)); //update workorder int?workOrderID = dtoPlanning.WorkOrderID; var dbWorkOrder = context.WorkOrder.Where(o => o.WorkOrderID == workOrderID).FirstOrDefault(); dbWorkOrder.StartDate = dtoPlanning.WorkOrderStartDate.ConvertStringToDateTime(); dbWorkOrder.FinishDate = dtoPlanning.WorkOrderFinishDate.ConvertStringToDateTime(); //save data context.SaveChanges(); //get return data dtoItem = GetData(dbItem.PlanningProductionID, out notification).Data; //delete item is deleted context.PlanningProductionMng_function_DeleteIsDeletedItem(); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Library.Helper.GetInnerException(ex).Message; return(false); } }
public void DTO2DB_PlanningProduction(DTO.PlanningProductionDTO dtoItem, ref PlanningProduction dbItem, List <ProductionItem> dbVirualProductionItem) { PlanningProduction dbPlanningProduction; PlanningProduction dbParentPlanningProduction; PlanningProductionTeam dbPlanningProductionTeam; if (dtoItem.ParentPlanningProductionID.HasValue) { if (dtoItem.PlanningProductionID < 0) { dbPlanningProduction = new PlanningProduction(); dbParentPlanningProduction = new PlanningProduction(); //mapping node is adding AutoMapper.Mapper.Map <DTO.PlanningProductionDTO, PlanningProduction>(dtoItem, dbPlanningProduction); dbPlanningProduction.StartDate = dtoItem.StartDate.ConvertStringToDateTime(); dbPlanningProduction.FinishDate = dtoItem.FinishDate.ConvertStringToDateTime(); dbPlanningProduction.ProductionItemID = dbVirualProductionItem.Where(o => o.ProductionItemUD == dtoItem.ProductionItemUD).FirstOrDefault().ProductionItemID; //get parent node and add to parent node GetPlanningProductionByID(dtoItem.ParentPlanningProductionID, dbItem, ref dbParentPlanningProduction); dbParentPlanningProduction.PlanningProduction1.Add(dbPlanningProduction); //mapping production team foreach (var dtoTeamItem in dtoItem.PlanningProductionTeamDTOs) { dbPlanningProductionTeam = new PlanningProductionTeam(); dbPlanningProduction.PlanningProductionTeam.Add(dbPlanningProductionTeam); AutoMapper.Mapper.Map <DTO.PlanningProductionTeamDTO, PlanningProductionTeam>(dtoTeamItem, dbPlanningProductionTeam); } } else { dbPlanningProduction = new PlanningProduction(); //get node is editing GetPlanningProductionByID(dtoItem.PlanningProductionID, dbItem, ref dbPlanningProduction); //mapping node is editing AutoMapper.Mapper.Map <DTO.PlanningProductionDTO, PlanningProduction>(dtoItem, dbPlanningProduction); dbPlanningProduction.StartDate = dtoItem.StartDate.ConvertStringToDateTime(); dbPlanningProduction.FinishDate = dtoItem.FinishDate.ConvertStringToDateTime(); //mapping production team foreach (var dbTeamItem in dbPlanningProduction.PlanningProductionTeam.ToArray()) { if (!dtoItem.PlanningProductionTeamDTOs.Select(s => s.PlanningProductionTeamID).Contains(dbTeamItem.PlanningProductionTeamID)) { dbPlanningProduction.PlanningProductionTeam.Remove(dbTeamItem); } } foreach (var dtoTeamItem in dtoItem.PlanningProductionTeamDTOs) { if (dtoTeamItem.PlanningProductionTeamID > 0) { dbPlanningProductionTeam = dbPlanningProduction.PlanningProductionTeam.Where(o => o.PlanningProductionTeamID == dtoTeamItem.PlanningProductionTeamID).FirstOrDefault(); } else { dbPlanningProductionTeam = new PlanningProductionTeam(); dbPlanningProduction.PlanningProductionTeam.Add(dbPlanningProductionTeam); } AutoMapper.Mapper.Map <DTO.PlanningProductionTeamDTO, PlanningProductionTeam>(dtoTeamItem, dbPlanningProductionTeam); } } } else { AutoMapper.Mapper.Map <DTO.PlanningProductionDTO, PlanningProduction>(dtoItem, dbItem); } foreach (var item in dtoItem.PlanningProductionDTOs) { DTO2DB_PlanningProduction(item, ref dbItem, dbVirualProductionItem); } }