示例#1
0
        public async Task <IActionResult> UpdateAmazonDB(string file)
        {
            var path = Path.Combine(
                Directory.GetCurrentDirectory(), "wwwroot",
                file + ".xlsx");

            //SetDictionariesAsync();

            //amazonList = _context.Amazon.ToList();

            //var tasks = new List<Task>();

            ////Task amazonListTask = new Task(() => amazonList = _context.Amazon.ToList());

            //Task amazonItemsTask = new Task(() => amazonItems = amazonList.ToDictionary(x => x.Asin, y => y.sku));

            //Task amazonList2Task = new Task(() => amazonList2 = _context.Amazon.ToList());

            ////tasks.Add(amazonListTask);

            //tasks.Add(amazonItemsTask);

            //tasks.Add(amazonList2Task);

            //Parallel.ForEach(tasks, task =>
            //{
            //    task.RunSynchronously();
            //});

            var azImporter = _context.AzImporter.ToDictionary(x => x.Sku, x => x);

            var perfumeWorldWide = _context.PerfumeWorldWide.ToDictionary(x => x.sku, x => x);

            var fragrancex = _context.Fragrancex.ToDictionary(x => x.ItemID, x => x);

            var amazon = _context.Amazon.ToDictionary(x => x.Asin, x => x);

            var shipping = _context.Shipping.ToDictionary(x => x.weightId, x => x.ItemPrice);

            AmazonDBUploader amazonDBUploader = new AmazonDBUploader(path, azImporter, fragrancex
                                                                     , perfumeWorldWide, amazon, shipping);

            try
            {
                amazonDBUploader.ExcelGenerator();
            }
            catch (Exception e)
            {
                return(null);
            }
            finally
            {
                //using (var con = _context.Database.GetDbConnection())
                //{
                //    using (var cmd = con.CreateCommand())
                //    {
                //        cmd.CommandText = "select f1, f2 from table";

                //        using (var rdr = cmd.ExecuteReader())
                //        {
                //            var f1 = rdr.GetInt32(0);
                //            var f2 = rdr.GetInt32(1`);
                //        }
                //    }
                //}

                // Upload to the DB

                DBModifierAmazon dBModifierAmazon = new DBModifierAmazon(amazonDBUploader.amazonList);

                dBModifierAmazon.TableExecutor();
            }

            var memory = new MemoryStream();

            using (var stream = new FileStream(path, FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }

            memory.Position = 0;

            FileStreamResult returnFile =
                File(memory, Helper.GetContentType(path), "Amazon"
                     + "_Converted_" + DateTime.Today.GetDateTimeFormats()[10]
                     + Path.GetExtension(path).ToLowerInvariant());

            System.IO.File.Delete(path);

            return(returnFile);
        }
示例#2
0
        public async Task <IActionResult> Upload(string file, MarketPlace marketPlace)
        {
            var path = Path.Combine(
                Directory.GetCurrentDirectory(), "wwwroot",
                file + ".xlsx");

            try
            {
                var azImporter = _context.Wholesaler_AzImporter.ToDictionary(x => x.Sku, x => x);

                //var perfumeWorldWide = _context.PerfumeWorldWide.ToDictionary(x => x.sku, x => x);

                var fragrancex = _context.Wholesaler_Fragrancex.ToDictionary(x => x.Sku, x => x);

                var amazon = _context.Amazon.Where(x => x.marketPlace == marketPlace.ToString()).ToList();

                var shipping = _context.Shipping.ToDictionary(x => x.weightId, x => x.ItemPrice);

                var amazonNumber = _context.Amazon.Count();

                AmazonDBUploader amazonDBUploader = new AmazonDBUploader(path, azImporter, fragrancex
                                                                         , amazon, shipping, marketPlace, amazonNumber);

                try
                {
                    amazonDBUploader.ExcelGenerator();
                }
                catch (Exception e)
                {
                    System.IO.File.Delete(path);
                    ViewData["Error"] = e.Message.ToString();

                    return(View());
                }
                try
                {
                    using (var tran = _context.Database.BeginTransaction())
                    {
                        await _context.BulkInsertAsync(amazonDBUploader.amazonList);

                        tran.Commit();
                    }
                }
                catch (Exception e)
                {
                    System.IO.File.Delete(path);
                    ViewData["Error"] = e.Message.ToString();
                    return(View());
                }

                var memory = new MemoryStream();

                using (var stream = new FileStream(path, FileMode.Open))
                {
                    await stream.CopyToAsync(memory);
                }

                memory.Position = 0;

                FileStreamResult returnFile =
                    File(memory, Helper.GetContentType(path), "Amazon"
                         + "_Converted_" + DateTime.Today.GetDateTimeFormats()[10]
                         + marketPlace.ToString() + Path.GetExtension(path).ToLowerInvariant());

                System.IO.File.Delete(path);

                return(returnFile);
            }
            catch (Exception)
            {
                System.IO.File.Delete(path);

                ViewData["Error"] = "The ASIN and/or Market Place is NOT in the database.";

                UploadUI();

                return(View());
            }
        }