Beispiel #1
0
 public int[] GetLastProductId(ProductLocator locator, int page, int pageSize, DateTime date)
 {
     return(_context.GetProducts(locator)
            .Where(x => x.Updated > date)
            .OrderBy(x => x.DpcId).Skip(page * pageSize).Take(pageSize)
            .Select(x => x.DpcId).ToArray());
 }
Beispiel #2
0
        public ServiceResult DeleteProduct(ProductLocator locator, int id, string data)
        {
            return(RunAction(new UserContext(), null, () =>
            {
                var p = _context.GetProduct(locator, id);
                if (p != null)
                {
                    _context.Database.BeginTransaction();

                    var isPg = _context is NpgSqlDpcModelDataContext;
                    var sql = isPg
                        ? $"SELECT * from Products where id = {p.Id} FOR UPDATE "
                        : $"SELECT * from Products with(rowlock, xlock) where id = {p.Id}";
                    _context.Database.ExecuteSqlRaw(sql);

                    foreach (var pr in p.ProductRegions)
                    {
                        _context.Remove(pr);
                    }
                    _context.Remove(p);

                    if (locator.UseProductVersions)
                    {
                        UpdateDeletedProductVersion(p, data);
                    }

                    _context.SaveChanges();
                    _context.Database.CommitTransaction();
                }
            }));
        }
Beispiel #3
0
 public ServiceResult <bool> HasProductChanged(ProductLocator locator, int id, string data)
 {
     return(Run(new UserContext(), null, () =>
     {
         var p = _context.GetProduct(locator, id);
         if (p?.Hash == null)
         {
             return true;
         }
         return !p.Hash.Equals(GetHash(data), StringComparison.Ordinal);
     }));
 }
Beispiel #4
0
        private ProductInfo ParseInternal(ProductLocator locator, string data)
        {
            try
            {
                return(locator.GetSerialiser().Deserialize(data));
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
            }

            return(null);
        }
Beispiel #5
0
        public ProductData GetProductVersionData(ProductLocator locator, int id, DateTime date)
        {
            ProductData result = null;
            var         p      = _context.GetProductVersion(locator, id, date);

            if (p != null)
            {
                result = new ProductData
                {
                    Product = p.Data,
                    Created = p.Created,
                    Updated = p.Updated
                };
            }
            return(result);
        }
Beispiel #6
0
        public int[] GetAllProductVersionId(ProductLocator locator, int page, int pageSize, DateTime date)
        {
            using (new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
            {
                IsolationLevel = IsolationLevel.ReadUncommitted
            }))
            {
                var productVersions = _context.GetProductVersions(locator, date);

                return(productVersions
                       .Where(x => !productVersions.Any(y => x.DpcId == y.DpcId && x.Id < y.Id) && !x.Deleted)
                       .OrderBy(x => x.DpcId)
                       .Skip(page * pageSize).Take(pageSize)
                       .Select(x => x.DpcId)
                       .ToArray());
            }
        }
Beispiel #7
0
        public ServiceResult UpdateProduct(ProductLocator locator, Product product, string data, string userName, int userId)
        {
            return(RunAction(new UserContext(), null, () =>
            {
                var p = _context.GetProduct(locator, product.Id);
                var isNew = p == null;
                var now = DateTime.Now;
                if (isNew)
                {
                    p = new DAL.Product
                    {
                        Created = now,
                        DpcId = product.Id
                    };
                    _context.FillProduct(locator, p);
                }

                if (product.MarketingProduct != null)
                {
                    p.Alias = product.MarketingProduct.Alias;
                    p.Title = product.MarketingProduct.Title;
                    p.MarketingProductId = product.MarketingProduct.Id;
                }

                if (string.IsNullOrEmpty(p.Title))
                {
                    p.Title = product.Title;
                }

                if (string.IsNullOrEmpty(p.Alias))
                {
                    p.Alias = product.Alias;
                }

                p.Updated = now;

                p.Data = data;

                p.Hash = GetHash(data);
                p.ProductType = product.ProductType;

                p.UserUpdated = userName;
                p.UserUpdatedId = userId;

                var regionIds = new List <int>();
                if (product.Regions != null)
                {
                    if (p.Id != 0)
                    {
                        foreach (var pr in p.ProductRegions)
                        {
                            if (!product.Regions.Select(x => x.Id).Contains(pr.RegionId))
                            {
                                regionIds.Add(pr.RegionId);
                                _context.ProductRegions.Remove(pr);
                            }
                        }

                        foreach (var r in product.Regions.Select(x => x.Id))
                        {
                            if (!p.ProductRegions.Select(x => x.RegionId).Contains(r))
                            {
                                var pr = new ProductRegion
                                {
                                    RegionId = r,
                                    Product = p
                                };

                                _context.ProductRegions.Add(pr);
                            }
                        }
                    }
                    else
                    {
                        foreach (var r in product.Regions.Select(x => x.Id))
                        {
                            var pr = new ProductRegion
                            {
                                RegionId = r,
                                Product = p
                            };

                            _context.ProductRegions.Add(pr);
                        }
                    }
                }

                p.DpcId = product.Id;

                if (isNew)
                {
                    _context.Products.Add(p);
                }

                if (locator.UseProductVersions)
                {
                    UpdateExistingProductVersion(p, product.Regions?.Select(r => r.Id), now);
                }

                _context.SaveChanges();
                regionIds.AddRange(p.ProductRegions.Select(x => x.RegionId));
                foreach (var rid in regionIds)
                {
                    var ru = _context.RegionUpdates.SingleOrDefault(n => n.RegionId == rid) ?? new RegionUpdate()
                    {
                        RegionId = rid
                    };
                    ru.Updated = DateTime.Now;
                    if (ru.Id <= 0)
                    {
                        _context.Add(ru);
                    }
                }
            }));
        }
Beispiel #8
0
 public ServiceResult <ProductInfo> Parse(ProductLocator locator, string data)
 {
     return(Run(new UserContext(), null, () => ParseInternal(locator, data)));
 }
Beispiel #9
0
        public string GetProduct(ProductLocator locator, int id)
        {
            var p = _context.GetProduct(locator, id);

            return(p != null ? p.Data : "");
        }
Beispiel #10
0
 public int[] GetAllProductId(ProductLocator locator, int page, int pageSize)
 {
     return(_context.GetProducts(locator)
            .OrderBy(x => x.DpcId).Skip(page * pageSize).Take(pageSize)
            .Select(x => x.DpcId).ToArray());
 }