Esempio n. 1
0
        private bool InsertToProductPool(List <PageNumResult> list)
        {
            using (var db = new JDProductContext())
            {
                try
                {
                    var tmp = (from c in list
                               select new ProductPool
                    {
                        ProductPoolId = Guid.NewGuid().ToString(),
                        Name = c.name,
                        PageNum = Convert.ToInt32(c.page_num),
                        CreateTime = DateTime.Now
                    }).ToList();

                    var conn = db.Database.Connection;
                    tmp.RemoveAll(item => item == null);

                    BulkInsertHelper.InsertBatch <ProductPool>(conn, tmp).GetAwaiter().GetResult();
                    return(true);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Esempio n. 2
0
        private async Task <bool> InsertToProductSkuDetail()
        {
            var sw = new Stopwatch();

            sw.Start();
            try
            {
                SynchronousDataService service = new SynchronousDataService();
                var token = await service.AccessToken();

                IDbConnection     conn        = null;
                IDbTransaction    transaction = null;
                List <ProductSku> list        = GetProductSkuList();
                var len = Math.Ceiling(list.Count() * 0.0001);
                using (var db = new JDProductContext())
                {
                    for (var i = 0; i < len; i++)
                    {
                        List <ProductDetail> insertList = new List <ProductDetail>();
                        ParallelOptions      po         = new ParallelOptions();
                        po.MaxDegreeOfParallelism = Environment.ProcessorCount;
                        Parallel.ForEach(list.Skip(i * 10000).Take(10000), po, (sku) => {
                            var x = service.GetDetail(token, (sku.SkuId.ToString()), true).GetAwaiter().GetResult();
                            if (x == null)
                            {
                                _logger.ErrorFormat("######################SKUId={0}返回值为null#######################", sku.SkuId);
                                return;
                            }

                            ProductDetail productDetail = new ProductDetail
                            {
                                ProductId    = Guid.NewGuid().ToString(),
                                Appintroduce = x.appintroduce,
                                BrandName    = x.brandName,
                                Category     = x.category,
                                EleGift      = x.eleGift,
                                ImagePath    = x.imagePath,
                                Introduction = x.introduction,
                                Name         = x.name,
                                Param        = x.param,
                                ProductArea  = x.productArea,
                                SaleUnit     = x.saleUnit,
                                Shouhou      = x.shouhou,
                                Sku          = x.sku,
                                State        = x.state,
                                Upc          = x.upc,
                                WareQD       = x.wareQD,
                                Weight       = x.weight,
                                CreateTime   = DateTime.Now
                            };
                            lock (obj)
                            {
                                insertList.Add(productDetail);
                            }
                            //Console.WriteLine("Insert {0} Success!", x.sku);
                        });

                        sw.Stop();
                        _logger.InfoFormat("调用{0}次商品明细接口耗时{1}毫秒", insertList.Count(), sw.ElapsedMilliseconds);
                        insertList.RemoveAll(item => item == null);

                        if (conn == null)
                        {
                            conn = db.Database.Connection;
                        }

                        if (conn.State != System.Data.ConnectionState.Open)
                        {
                            conn.Open();
                        }
                        if (transaction == null)
                        {
                            transaction = conn.BeginTransaction();
                        }

                        if (i == 0 && transaction != null)
                        {
                            conn.Execute("delete from [dbo].[ProductDetail]", null, transaction);
                        }
                        BulkInsertHelper.InsertBatch <ProductDetail>(conn, insertList, transaction).GetAwaiter().GetResult();
                        if (i == len - 1)
                        {
                            //提交事务
                            transaction.Commit();
                        }
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
        private bool InsertToProductSku()
        {
            var sw = new Stopwatch();

            sw.Start();

            SynchronousDataService service = new SynchronousDataService();
            var token = service.AccessToken().GetAwaiter().GetResult();

            using (var db = new JDProductContext())
            {
                try
                {
                    List <ProductPool> list = GetProductPoolList();
                    List <ProductSku>  skus = new List <ProductSku>();
                    Parallel.ForEach(list, (num) =>
                    {
                        _logger.InfoFormat("商品池编号:{0},商品池名称:{1}", num.PageNum, num.Name);
                        var skustringArray = service.GetSku(token, num.PageNum.ToString()).GetAwaiter().GetResult();
                        if (string.IsNullOrWhiteSpace(skustringArray))
                        {
                            _logger.InfoFormat("#######string.IsNullOrWhiteSpace(skustringArray)###########");
                            return;
                        }
                        if (skustringArray.Contains(','))
                        {
                            var skuList = skustringArray.Split(',');
                            //Console.WriteLine("商品池编号:{0}--商品池名称:{1}--有{2}个Sku", num.PageNum, num.Name, skuList.Count());
                            long x = 0;

                            if (skuList == null || !skuList.Any())
                            {
                                Console.WriteLine("*******************************************");
                                Debug.Assert(true);
                            }

                            var tmp = (from c in skuList
                                       select new ProductSku
                            {
                                ProductSkuId = Guid.NewGuid().ToString(),
                                ProductPoolId = num.ProductPoolId,
                                PageNum = num.PageNum,
                                SkuId = long.TryParse(c, out x) ? x : 0,
                                CreateTime = DateTime.Now
                            }).ToList();

                            if (tmp == null || !tmp.Any())
                            {
                                Console.WriteLine("----------------------------------------------------");
                                Debug.Assert(true);
                            }

                            //var jsonStr = JsonConvert.SerializeObject(tmp);
                            //Console.WriteLine("商品池编号:{0}--商品池名称:{1}--有{2}个Sku,序列化结果:{3}", num.PageNum, num.Name, skuList.Count(),jsonStr);
                            //log.Info(jsonStr);
                            skus.AddRange(tmp);
                        }
                        else
                        {
                            long x   = 0;
                            var flag = long.TryParse(skustringArray, out x);
                            var tmp  = new ProductSku
                            {
                                ProductSkuId  = Guid.NewGuid().ToString(),
                                ProductPoolId = num.ProductPoolId,
                                PageNum       = num.PageNum,
                                SkuId         = x,
                                CreateTime    = DateTime.Now
                            };

                            if (tmp == null)
                            {
                                Console.WriteLine("=====================================================");
                                Debug.Assert(true);
                            }
                            skus.Add(tmp);

                            //var jsonStr = JsonConvert.SerializeObject(tmp);
                            //Console.WriteLine("商品池编号:{0}--商品池名称:{1},序列化结果:{2}", num.PageNum, num.Name, jsonStr);
                            //log.Info(jsonStr);
                        }
                    });

                    sw.Stop();
                    _logger.InfoFormat("执行InsertToProductSku-All耗时:{0}毫秒。", sw.ElapsedMilliseconds);

                    var conn = db.Database.Connection;

                    skus.RemoveAll(item => item == null);
                    BulkInsertHelper.InsertBatch <ProductSku>(conn, skus).GetAwaiter().GetResult();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return(true);
            }
        }