コード例 #1
0
        public void UpdateFactory()
        {
            if (RouteData != null)
            {
                _tenant = (Tenant)RouteData.Values.SingleOrDefault(r => r.Key == "tenant").Value;
            }
            var dt = DateTime.Now;

            foreach (string path in Directory.EnumerateFiles(
                         Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\FileUploads" + "\\" + _tenant.HostName), "*", SearchOption.AllDirectories))
            {
                FileInfo fileInfo = new FileInfo(path);

                using (ExcelPackage package = new ExcelPackage(fileInfo))
                {
                    List <Inspection> inspectionList = new List <Inspection>();
                    int workSheetTotal = package.Workbook.Worksheets.Count;
                    try
                    {
                        for (int j = 1; j <= workSheetTotal; j++)
                        {
                            ExcelWorksheet workSheet = package.Workbook.Worksheets[j];
                            if (workSheet.Dimension != null)
                            {
                                int totalRows = workSheet.Dimension.Rows;

                                var factories = _factoryRepository.Factories;
                                for (int i = 2; i <= totalRows; i++)
                                {
                                    var inspection = new Inspection();
                                    inspection.FactoryName = AssignCell(i, workSheet, "Factory");
                                    inspection.OrderNumber = AssignCell(i, workSheet, "Order number");

                                    var factory = factories.FirstOrDefault(x => x.Name.Contains(inspection.FactoryName));
                                    if (factory != null)
                                    {
                                        inspection.FactoryId = factory.FactoryId;
                                    }
                                    else
                                    {
                                        var newFactory = new Factory()
                                        {
                                            Name = inspection.FactoryName
                                        };

                                        inspection.FactoryId = _factoryRepository.CreateFactory(newFactory);
                                    }
                                    inspectionList.Add(inspection);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }
                    }

                    _inspectionRepository.UpdateFactorty(inspectionList);
                }
            }
        }