public void AddViewCount() { _LazyEntity.Value.viewCount = (_LazyEntity.Value.viewCount ?? 0) + 1; _Repository.SaveChanges(); try { //搜索引擎 var entityFromSearch = _SearchManager.SearchProductByPids(new[] { _LazyEntity.Value.pid }).FirstOrDefault(); if (entityFromSearch != null) { //每隔十次更新一下索引 if ((_LazyEntity.Value.viewCount ?? 0) - (entityFromSearch.viewCount ?? 0) >= 10) { _SearchManager.UpdateSearchIndex(_LazyEntity.Value); } } } catch (Exception ex) { Log.LoggerResolver.Current.Error(ex); } }
public void AddOrUpdateProducts(Data.Product[] products) { ExceptionHelper.ThrowIfNull(products, "products"); if (products.Any()) { foreach (var item in products) { VeriryEntity(item); } //更新已存在的产品 var updatingProducts = products.Where(p => p.pid > 0).ToArray(); foreach (var newProduct in updatingProducts) { var oldProduct = AllProducts.FirstOrDefault(p => p.pid == newProduct.pid); if (oldProduct != null) { using (var scope = new System.Transactions.TransactionScope()) { OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.name, (p, v) => p.name = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.enName, (p, v) => p.enName = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.description, (p, v) => p.description = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.enDescription, (p, v) => p.enDescription = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.size, (p, v) => p.size = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.enSize, (p, v) => p.enSize = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.color, (p, v) => p.color = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.enColor, (p, v) => p.enColor = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.material, (p, v) => p.material = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.enMaterial, (p, v) => p.enMaterial = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.technique, (p, v) => p.technique = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.enTechnique, (p, v) => p.enTechnique = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.keywords, (p, v) => p.keywords = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.enKeywords, (p, v) => p.enKeywords = v); OverrideIfNotNullNotWhiteSpace(oldProduct, newProduct, p => p.classNo, (p, v) => p.classNo = v); var newProductFileId = FileId.FromFileId(newProduct.imagePath); var newFileID = newProductFileId.ToStorageId(); var imgChange = oldProduct.imagePath != newProduct.imagePath && oldProduct.imagePath != newFileID.Id; if (imgChange) { oldProduct.imagePath = newFileID.Id; } oldProduct.minQuantity = newProduct.minQuantity; oldProduct.deliveryDay = newProduct.deliveryDay; oldProduct.unitPrice = newProduct.unitPrice; oldProduct.sortNo = newProduct.sortNo; oldProduct.updated = DateTime.Now; if (newProduct.updater > 0) { oldProduct.updater = newProduct.updater; } _Repository.SaveChanges(); UpdateSearchIndex(oldProduct.pid);//更新索引 if (newProductFileId.IsTempId) { _FileStore.Copy(newProductFileId, newFileID);//将临时文件复制到永久文件处 } scope.Complete(); } System.Threading.Thread.Sleep(300);//同时添加太多产品,搜索引擎更新太频繁会报错(阿里云限制每秒频率,除非加钱),这里添加一个产品后先休眠几百毫秒 } } //新增的产品 var addingProducts = products.Where(p => p.pid <= 0).ToArray(); foreach (var entity in addingProducts) { using (var scope = new System.Transactions.TransactionScope()) { entity.created = DateTime.Now; entity.updated = DateTime.Now; entity.enabled = true; var temFileId = FileId.FromFileId(entity.imagePath); var newFileID = temFileId.ToStorageId(); entity.imagePath = newFileID.Id; _Repository.Add(entity); _Repository.SaveChanges(); if (temFileId.IsTempId) { _FileStore.Copy(temFileId, newFileID); //将临时文件复制到永久文件处 } UpdateSearchIndex(entity.pid); //更新索引 scope.Complete(); } } } }