Beispiel #1
0
        public void DTO2DB_MaterialTestReport(DTO.MaterialTestReportDTO dtoItem, ref MaterialTestReport dbItem, string TmpFile, int userId)
        {
            foreach (MaterialTestReportFile dbFile in dbItem.MaterialTestReportFile.ToArray())
            {
                if (!dtoItem.MaterialTestReportFileDTOs.Select(o => o.MaterialTestReportFileID).Contains(dbFile.MaterialTestReportFileID))
                {
                    if (!string.IsNullOrEmpty(dbFile.FileUD))
                    {
                        // remove file
                        fwFactory.RemoveImageFile(dbFile.FileUD);
                    }
                    dbItem.MaterialTestReportFile.Remove(dbFile);
                }
            }
            foreach (DTO.MaterialTestReportFileDTO dtoFile in dtoItem.MaterialTestReportFileDTOs)
            {
                MaterialTestReportFile dbFile;
                if (dtoFile.MaterialTestReportFileID <= 0)
                {
                    dbFile = new MaterialTestReportFile();
                    dbItem.MaterialTestReportFile.Add(dbFile);
                }
                else
                {
                    dbFile = dbItem.MaterialTestReportFile.FirstOrDefault(o => o.MaterialTestReportFileID == dtoFile.MaterialTestReportFileID);
                }

                if (dbFile != null)
                {
                    // change or add file
                    if (dtoFile.ScanHasChange)
                    {
                        dtoFile.FileUD = fwFactory.CreateNoneImageFilePointer(TmpFile, dtoFile.ScanNewFile, dtoFile.FileUD, dtoFile.FriendlyName);
                    }
                    AutoMapper.Mapper.Map <DTO.MaterialTestReportFileDTO, MaterialTestReportFile>(dtoFile, dbFile);
                }
            }

            if (dtoItem.MaterialTestReportFileDTOs != null)
            {
                foreach (MaterialTestReportFile item in  dbItem.MaterialTestReportFile.ToList())
                {
                    if (!dtoItem.MaterialTestReportFileDTOs.Select(s => s.MaterialTestReportFileID).Contains(item.MaterialTestReportFileID))
                    {
                        dbItem.MaterialTestReportFile.Remove(item);
                    }
                }

                foreach (DTO.MaterialTestReportFileDTO dto in dtoItem.MaterialTestReportFileDTOs)
                {
                    MaterialTestReportFile item;

                    if (dto.MaterialTestReportFileID < 0)
                    {
                        item = new MaterialTestReportFile();

                        dbItem.MaterialTestReportFile.Add(item);
                    }
                    else
                    {
                        item = dbItem.MaterialTestReportFile.FirstOrDefault(s => s.MaterialTestReportFileID == dto.MaterialTestReportFileID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.MaterialTestReportFileDTO, MaterialTestReportFile>(dto, item);
                    }
                }
            }

            if (dtoItem.MaterialTestStandardDTOs != null)
            {
                foreach (var item in dbItem.MaterialTestUsingMaterialStandard.ToArray())
                {
                    if (!dtoItem.MaterialTestStandardDTOs.Select(o => o.MaterialTestUsingMaterialStandardID).Contains(item.MaterialTestUsingMaterialStandardID))
                    {
                        dbItem.MaterialTestUsingMaterialStandard.Remove(item);
                    }
                }

                foreach (var item in dtoItem.MaterialTestStandardDTOs)
                {
                    MaterialTestUsingMaterialStandard dbTestStandard = new MaterialTestUsingMaterialStandard();
                    if (item.MaterialTestUsingMaterialStandardID < 0)
                    {
                        dbItem.MaterialTestUsingMaterialStandard.Add(dbTestStandard);
                    }
                    else
                    {
                        dbTestStandard = dbItem.MaterialTestUsingMaterialStandard.Where(s => s.MaterialTestUsingMaterialStandardID == item.MaterialTestUsingMaterialStandardID).FirstOrDefault();
                    }
                    if (dbTestStandard != null)
                    {
                        AutoMapper.Mapper.Map <DTO.MaterialTestStandardDTO, MaterialTestUsingMaterialStandard>(item, dbTestStandard);
                    }
                }
            }

            //if (!string.IsNullOrEmpty(dtoItem.TestDate))
            //{
            //    if (DateTime.TryParse(dtoItem.TestDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
            //    {
            //    }
            //}
            //if (!string.IsNullOrEmpty(dtoItem.UpdatedDate))
            //{
            //    if (DateTime.TryParse(dtoItem.UpdatedDate, nl, System.Globalization.DateTimeStyles.None, out tmpUpdateDate))
            //    {
            //    }
            //}
            AutoMapper.Mapper.Map <DTO.MaterialTestReportDTO, MaterialTestReport>(dtoItem, dbItem);
            dbItem.TestDate = dtoItem.TestDate.ConvertStringToDateTime();
        }
        public bool Update(int userId, int id, ref object dto, out Notification notification)
        {
            DTO.MaterialTestReportDTO dtoItem = ((Newtonsoft.Json.Linq.JObject)dto).ToObject <DTO.MaterialTestReportDTO>();
            notification = new Notification()
            {
                Type = NotificationType.Success
            };
            try
            {
                using (MaterialTestingMngEntities context = CreateContext())
                {
                    MaterialTestReport dbItem = null;
                    using (DbContextTransaction scope = context.Database.BeginTransaction())
                    {
                        context.Database.ExecuteSqlCommand("SELECT * FROM MaterialTestReport WITH (TABLOCKX, HOLDLOCK)");
                        try
                        {
                            if (id > 0)
                            {
                                dbItem = context.MaterialTestReport.FirstOrDefault(s => s.MaterialTestReportID == id);

                                if (dbItem == null)
                                {
                                    notification = new Notification()
                                    {
                                        Type = NotificationType.Error, Message = "Can not find data"
                                    };
                                    return(false);
                                }
                            }
                            else
                            {
                                dbItem = new MaterialTestReport();
                                context.MaterialTestReport.Add(dbItem);
                            }
                            converter.DTO2DB_MaterialTestReport(dtoItem, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId);
                            dbItem.UpdatedBy   = userId;
                            dbItem.UpdatedDate = DateTime.Now;

                            context.MaterialTestReportFile.Local.Where(o => o.MaterialTestReport == null).ToList().ForEach(s => context.MaterialTestReportFile.Remove(s));
                            context.MaterialTestUsingMaterialStandard.Local.Where(o => o.MaterialTestReport == null).ToList().ForEach(o => context.MaterialTestUsingMaterialStandard.Remove(o));

                            context.SaveChanges();

                            //Create code of id
                            dbItem.MaterialTestReportUD = "M" + dbItem.MaterialTestReportID.ToString("D9");
                            context.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            scope.Commit();
                        }
                    }

                    dto = GetEdit(dbItem.MaterialTestReportID, out notification).Data;
                }
                return(true);
            }
            catch (Exception ex)
            {
                notification = new Notification()
                {
                    Type = NotificationType.Error, Message = ex.Message
                };
                return(false);
            }
        }