Exemplo n.º 1
0
 public void DTO2DB_ProductionStatistics(DTO.ProductionStatisticsDTO dtoItem, ref ProductionStatistics dbItem, int?userId, int?companyID)
 {
     if (dtoItem.ProductionStatisticsDetailDTOs != null)
     {
         //delete item in db that exist in dto but not exist in db
         foreach (var item in dbItem.ProductionStatisticsDetail.ToArray())
         {
             if (!dtoItem.ProductionStatisticsDetailDTOs.Select(s => s.ProductionStatisticsDetailID).Contains(item.ProductionStatisticsDetailID))
             {
                 dbItem.ProductionStatisticsDetail.Remove(item);
             }
         }
         //read from dto to db
         ProductionStatisticsDetail dbDetail;
         foreach (var item in dtoItem.ProductionStatisticsDetailDTOs)
         {
             if (item.ProductionStatisticsDetailID > 0)
             {
                 dbDetail = dbItem.ProductionStatisticsDetail.Where(o => o.ProductionStatisticsDetailID == item.ProductionStatisticsDetailID).FirstOrDefault();
             }
             else
             {
                 dbDetail = new ProductionStatisticsDetail();
                 dbItem.ProductionStatisticsDetail.Add(dbDetail);
             }
             //read purchase request detail dto to db
             if (dbDetail != null)
             {
                 AutoMapper.Mapper.Map <DTO.ProductionStatisticsDetailDTO, ProductionStatisticsDetail>(item, dbDetail);
                 if (!string.IsNullOrEmpty(item.StartingTime))
                 {
                     dbDetail.StartingTime = item.StartingTime.ConvertStringToDateTime();
                 }
                 if (!string.IsNullOrEmpty(item.FinishingTime))
                 {
                     dbDetail.FinishingTime = item.FinishingTime.ConvertStringToDateTime();
                 }
             }
         }
     }
     AutoMapper.Mapper.Map <DTO.ProductionStatisticsDTO, ProductionStatistics>(dtoItem, dbItem);
     dbItem.ProducedDate = dtoItem.ProducedDate.ConvertStringToDateTime();
     dbItem.CompanyID    = companyID;
     dbItem.UpdatedBy    = userId;
     dbItem.UpdatedDate  = DateTime.Now;
     if (dtoItem.ProductionStatisticsID == 0)
     {
         dbItem.CreatedBy   = userId;
         dbItem.CreatedDate = DateTime.Now;
     }
 }
Exemplo n.º 2
0
 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.ProductionStatisticsDTO dtoProductionStatistics = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ProductionStatisticsDTO>();
     try
     {
         int?companyID = fw_factory.GetCompanyID(userId);
         using (ProductionStatisticsMngEntities context = CreateContext())
         {
             ProductionStatistics dbItem = null;
             if (id == 0)
             {
                 dbItem = new ProductionStatistics();
                 context.ProductionStatistics.Add(dbItem);
             }
             else
             {
                 dbItem = context.ProductionStatistics.Where(o => o.ProductionStatisticsID == id).FirstOrDefault();
             }
             if (dbItem == null)
             {
                 notification.Message = "data not found!";
                 return(false);
             }
             else
             {
                 //convert dto to db
                 converter.DTO2DB_ProductionStatistics(dtoProductionStatistics, ref dbItem, userId, companyID);
                 //remove orphan
                 context.ProductionStatisticsDetail.Local.Where(o => o.ProductionStatistics == null).ToList().ForEach(o => context.ProductionStatisticsDetail.Remove(o));
                 //save data
                 context.SaveChanges();
                 //get return data
                 dtoItem = GetData(userId, dbItem.ProductionStatisticsID, null, out notification).Data;
                 return(true);
             }
         }
     }
     catch (Exception ex)
     {
         notification.Type    = Library.DTO.NotificationType.Error;
         notification.Message = Library.Helper.GetInnerException(ex).Message;
         return(false);
     }
 }