public static OrderComputeResult ComputeAmount(ProductEntity linq, int quantity) { return new OrderComputeResult() { TotalAmount= linq.Price *quantity, ExtendPrice = linq.Price * quantity, TotalFee = 0m, TotalPoints = 0, TotalQuantity = quantity }; }
/// <summary> /// 构建商品图片 /// </summary> /// <param name="entity"></param> /// <param name="request"></param> private void BuildProductImages(ProductEntity entity, ISyncRequest request) { #if !DEBUG using (var db = DbContextHelper.GetDbContext()) { int idx = 1; var moreImg = new StringBuilder(); // reset images for product for (int i = 0; i < 5; i++) { request.Put(string.Format("uploadPicInfo{0}", ++i), "null"); } request.Put("moreImg", "null"); var resource = db.Resources.Where( t => t.SourceId == entity.Id && t.Type == 1 && t.SourceType == (int) SourceType.Product && t.Status == (int) DataStatus.Normal && t.ColorId.HasValue) .GroupBy(t => t.ColorId, (key, resources) => new{color = key,images = resources}).OrderByDescending(x=>x.images.Count()).FirstOrDefault(); if (resource == null || !resource.images.Any()) { throw new WgwSyncException(string.Format("Product {0} has no images",entity.Id)); } foreach (var img in resource.images) { var url = string.Format("{0}/{1}_320x0.jpg", WgwConfigHelper.Image_BaseUrl, img.Name); if (idx > 5) { moreImg.Append(url).Append("|"); } else { request.Put(string.Format("uploadPicInfo{0}", idx), url); } idx += 1; } var moreImgStr = moreImg.ToString(); var len = moreImgStr.Length; if (len > 0) { request.Put("moreImg", moreImgStr.Substring(0, len - 1)); } } #else request.Put("uploadPicInfo1", "http://ec4.images-amazon.com/images/I/51yQ0l-qvkL._AA135_.jpg"); request.Put("uploadPicInfo2", "http://ec8.images-amazon.com/images/I/41dBoxFHlyL._AA135_.jpg"); #endif }
/// <summary> /// 构建商品主图,为防止一次上传图片过多导致微购物返回失败,商品只上传一张主图 /// </summary> /// <param name="entity"></param> private void BuildImages(ProductEntity entity) { using (var db = GetDbContext()) { var img = db.Resources.Where(t => t.SourceId == entity.Id && t.Type == 1 && t.SourceType == (int)SourceType.Product && t.Status == (int)DataStatus.Normal) .OrderByDescending(t => t.SortOrder) .Take(1).FirstOrDefault(); if (img == null) { throw new WgwSyncException(string.Format("No image for product: {0}", entity.Id)); } Request.Put("uploadPicInfo1", string.Format("{0}/{1}_320x0.jpg", WgwConfigHelper.Image_BaseUrl, img.Name)); } }
protected override void Build(ProductEntity item) { if (item == null) throw new ArgumentNullException("item"); var map4Product = GetMap4Product(item.Id); if (map4Product == null) { throw new WgwSyncException(string.Format("Unmapped product:{0}", item.Id)); } Request.Put(ParamName.Param_ItemId, map4Product.ChannelProductId); if (!map4Product.IsImageUpload.HasValue || map4Product.IsImageUpload.Value != 1) { new ImageBuilder().BuildImage(Request, item); } new StockBuilder(Request, item).BuildStockInfo("stockstr"); }
/// <summary> /// 构建库存和颜色图片 /// </summary> /// <param name="item"></param> /// <param name="request"></param> private void BuildColorImages(ProductEntity item, ISyncRequest request) { using (var db = DbContextHelper.GetDbContext()) { var inventories = db.Inventories.Where(i => i.ProductId == item.Id); var images = new List<dynamic>(); foreach (var inventory in inventories) { var colorValue = db.ProductPropertyValues.FirstOrDefault(t => t.Id == inventory.PColorId); var color = db.ProductProperties.FirstOrDefault(t => t.Id == colorValue.PropertyId); if (colorValue == null || color == null) { continue; } if (images.Any(x => x.value == colorValue.ValueDesc)) { continue; } var img = db.Resources.Where(r => r.SourceId == inventory.ProductId && r.ColorId == inventory.PColorId && r.SourceType == (int)SourceType.Product && r.Status == (int)DataStatus.Normal).OrderByDescending(t=>t.SortOrder).FirstOrDefault(); if (img == null) { continue; } images.Add(new { property = color.PropertyDesc, value = colorValue.ValueDesc, url = string.Format("{0}/{1}_120x0.jpg", WgwConfigHelper.Image_BaseUrl, img.Name) }); } #if !DEBUG var sb = new StringBuilder(); var colorImgStr = images.Aggregate(sb, (s, img) => sb.AppendFormat("{0}:{1}|{2};", img.property, img.value, img.url), imgs => sb.ToString()); if (colorImgStr.Length > 0) { request.Put("stockAttrImgs", colorImgStr.Substring(0, colorImgStr.Length - 1)); } #else request.Put("stockAttrImgs", "颜色:花色|http://ec4.images-amazon.com/images/I/51%2B6RQwwhkL._SS45_.jpg;颜色:黑色|http://g-ec4.images-amazon.com/images/G/28/fanting_3P/shoes/123._SS75_V363140790_.jpg;颜色:绿色|http://ec4.images-amazon.com/images/I/41NZt4fsPrL._SS45_.jpg"); #endif } }
private ISyncRequest BuildParameters(ProductEntity entity) { this.BuildLeafClassId(entity.Id); this.BuildLabel(entity.Brand_Id); this.BuildUpTime(); this.Build(entity); Request.Put("buyLimit", WgwConfigHelper.BuyLimit); Request.Put("city", WgwConfigHelper.City); Request.Put("price_bin", Math.Ceiling(entity.Price * 100)); Request.Put("province", WgwConfigHelper.Province); Request.Put("marketPrice", entity.UnitPrice.HasValue ? Math.Ceiling(entity.UnitPrice.Value * 100) : int.MaxValue); Request.Put("defStockId", entity.Id); Request.Put("title", entity.Name); Request.Put("sendType", "0"); Request.Put("transportType", "1"); Request.Put("desc", entity.Description); return Request; }
public ProductEntity ProductEntityMapping(ProductEntity source, ProductEntity target) { var result = Mapper.Map(source, target); return result; }
public FavoriteInfoResponse FavoriteInfoResponseMapping(FavoriteEntity source, ProductEntity entity) { var target = Mapper.Map<FavoriteEntity, FavoriteInfoResponse>(source); target.FavoriteSourceName = entity == null ? String.Empty : entity.Name; return target; }
public static CouponCodeResponse CouponCodeResponseMapping(CouponHistoryEntity source, ProductEntity product, PromotionEntity promotion) { if (source == null) { return null; } var target = Mapper.Map<CouponHistoryEntity, CouponCodeResponse>(source); var productname = String.Empty; var producttype = 0; var productid = 0; var productDescription = String.Empty; if (promotion != null) { productname = promotion.Name; producttype = (int)SourceType.Promotion; productid = promotion.Id; productDescription = promotion.Description; target.Stype = SourceType.Promotion; } else { if (product != null) { productname = product.Name; producttype = (int)SourceType.Product; productid = product.Id; productDescription = product.Description; target.Stype = SourceType.Product; } } target.ProductId = productid; target.ProductName = productname; target.ProductType = producttype; target.ProductDescription = productDescription; return target; }
public static bool SyncOne(SUPPLY_MIN_PRICE product) { EnsureProductContext(product); if (!product.SHOP_SID.HasValue) return false; using (var ts = new TransactionScope()) { using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var existProduct = db.Set<ProductMapEntity>().Where(b => b.ChannelPId == product.PRODUCT_SID).FirstOrDefault(); var tagEntity = db.Set<TagEntity>().Join(db.Set<CategoryMapEntity>().Where(cm => cm.ChannelCatId == product.PRO_CLASS_SID), o => o.Id, i => i.CatId, (o, i) => o) .FirstOrDefault(); var storeEntity = db.Set<StoreEntity>().Where(s => s.ExStoreId == product.SHOP_SID).FirstOrDefault(); if (storeEntity == null) { Log.Error(string.Format("product sid has no store:{0}", product.PRODUCT_SID)); return false; } var brandEntity = db.Set<BrandEntity>().Where(b => b.ChannelBrandId == product.BRAND_SID).FirstOrDefault(); if (brandEntity == null) { Log.Error(string.Format("product sid has no brand:{0}", product.PRODUCT_SID)); return false; } if (existProduct == null) { var newProduct = new ProductEntity() { CreatedDate = DateTime.Now, CreatedUser = DEFAULT_OWNER_ID, SkuCode = product.PRO_SKU, Brand_Id = brandEntity == null ? 0 : brandEntity.Id, Description = product.PRO_DESC ?? string.Empty, Is4Sale = false, Name = string.IsNullOrEmpty(product.PRODUCT_NAME)? string.Format("{0}-{1}", brandEntity.Name, product.PRO_SKU):product.PRODUCT_NAME, UnitPrice = product.ORIGINAL_PRICE ?? NULL_PRICE, RecommendedReason = product.PRO_DESC ?? string.Empty, RecommendUser = DEFAULT_OWNER_ID, SortOrder = 0, Status = (int)DataStatus.Default, Store_Id = storeEntity == null ? 0 : storeEntity.Id, Tag_Id = tagEntity == null ? int.Parse(DEFAULT_TAG_ID) : tagEntity.Id, Price = product.PROMOTION_PRICE ?? NULL_PRICE, UpdatedDate = product.OPT_UPDATE_TIME ?? DateTime.Now, UpdatedUser = DEFAULT_OWNER_ID, BarCode = product.BARCODE, Favorable = "1" }; db.Products.Add(newProduct); db.SaveChanges(); db.ProductMaps.Add(new ProductMapEntity() { Channel = "ERP", ChannelBrandId = (int)product.BRAND_SID, ChannelPId = (int)product.PRODUCT_SID, ChannelCatId = (int)(product.PRO_CLASS_SID??0m), ProductId = newProduct.Id, UpdateDate = product.OPT_UPDATE_TIME ?? DateTime.Now }); } else { var existProductEntity = db.Set<ProductEntity>().Find(existProduct.ProductId); existProductEntity.BarCode = product.BARCODE; existProductEntity.UpdatedDate = product.OPT_UPDATE_TIME ?? DateTime.Now; existProductEntity.Store_Id = storeEntity == null ? 0 : storeEntity.Id; existProductEntity.Brand_Id = brandEntity == null ? 0 : brandEntity.Id; existProductEntity.Tag_Id = tagEntity == null ? int.Parse(DEFAULT_TAG_ID) : tagEntity.Id; existProductEntity.SkuCode = product.PRO_SKU; existProductEntity.Name = string.IsNullOrEmpty(product.PRODUCT_NAME) ? string.Format("{0}-{1}", brandEntity.Name, product.PRO_SKU) : product.PRODUCT_NAME; existProductEntity.UnitPrice = product.ORIGINAL_PRICE ?? NULL_PRICE; existProductEntity.Price = product.PROMOTION_PRICE ?? NULL_PRICE; existProductEntity.Description = product.PRO_DESC ?? string.Empty; existProductEntity.RecommendedReason = product.PRO_DESC ?? string.Empty; } db.SaveChanges(); } ts.Complete(); } return true; }
private static ItemsInfoResponse ItemsInfoResponseMapping(ProductEntity source, StoreInfoResponse store, List<ResourceInfoResponse> resources) { return ItemsInfoResponseMapping(source, store, resources, null); }
protected virtual void Build(ProductEntity entity) { this.BuildImages(entity); var stock = new StockBuilder(Request, entity); stock.BuildStockInfo("stockstr"); }
private void SyncOne(ProductEntity item) { int productId = item.Id; using (var db = DbContextHelper.GetDbContext()) { var map = db.Map4Products.FirstOrDefault( m => m.ProductId == productId && m.Channel == ConstValue.WGW_CHANNEL_NAME); if (map == null) { _failedCount += 1; throw new WgwSyncException(string.Format("Unmapped product ID:({0})", item.Id)); } var idList = new List<string>() {map.ChannelProductId}; ISyncRequest reqeust = null; if (item.Is4Sale.HasValue && item.Is4Sale.Value && item.IsHasImage && item.Status == 1) { reqeust = new UpItemRequest(idList); var rsp = Client.Execute<dynamic>(reqeust); if (rsp.errorCode == 0) { _succeedCount += 1; map.Status = 1; map.UpdateDate = DateTime.Now; db.SaveChanges(); Logger.Error(string.Format("Succeed publish product {0}", item.Id)); } else { _failedCount += 1; Logger.Error(string.Format("Failed to publish product {0}", item.Id)); } } else { reqeust = new DownItemRequest(idList); var rsp = Client.Execute<dynamic>(reqeust); if (rsp.errorCode == 0) { _succeedCount += 1; map.Status = 0; map.UpdateDate = DateTime.Now; db.SaveChanges(); Logger.Error(string.Format("Succeed Down product {0}", item.Id)); } else { _failedCount += 1; Logger.Error(string.Format("Failed to Down product {0}", item.Id)); } } } }
/// <summary> /// 构建商品图片信息 /// </summary> /// <param name="request"></param> /// <param name="product"></param> public void BuildImage(ISyncRequest request, ProductEntity product) { this.BuildProductImages(product,request); this.BuildColorImages(product,request); }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="brandInfo"></param> /// <param name="storeInfo"></param> /// <param name="showCustomerInfo"></param> /// <param name="tagInfoResponse"></param> /// <param name="resourceInfoResponses"></param> /// <param name="promotions"></param> /// <returns></returns> public ProductInfoResponse ProductInfoResponseMapping(ProductEntity source, BrandInfoResponse brandInfo, StoreInfoResponse storeInfo, ShowCustomerInfoResponse showCustomerInfo, TagInfoResponse tagInfoResponse, List<ResourceInfoResponse> resourceInfoResponses, List<PromotionInfo> promotions = null) { if (source == null) { return null; } var target = Mapper.Map<ProductEntity, ProductInfoResponse>(source); target.BrandInfoResponse = brandInfo; target.StoreInfoResponse = storeInfo; target.RecommendUserInfoResponse = showCustomerInfo; target.TagInfoResponse = tagInfoResponse; target.ResourceInfoResponses = resourceInfoResponses; target.Promotions = promotions; return target; }
/// <summary> /// 更新已经映射至微购物的商品映射信息 /// </summary> /// <param name="item"></param> /// <returns></returns> private bool Update(ProductEntity item) { try { var builder = RequestParamsBuilderFactory.CreateBuilder(new UpdateItemRequest()); var result = Client.Execute<dynamic>(builder.BuildParameters(item)); if (result.errorCode == 0) { using (var db = DbContextHelper.GetDbContext()) { var map4Product = db.Map4Products.FirstOrDefault( m => m.ProductId == item.Id && m.Channel == ConstValue.WGW_CHANNEL_NAME); if (map4Product == null) { throw new WgwSyncException(string.Format("Unmapped product ID=({0})", item.Id)); } map4Product.Status = item.Is4Sale.HasValue && item.Is4Sale.Value && item.IsHasImage && item.Status == 1 ? 1 : 0; map4Product.UpdateDate = item.UpdatedDate; map4Product.IsImageUpload = 1; db.SaveChanges(); return true; } } Logger.Error(string.Format("Failed to upate product ({0}) Error Message: {1}", item.Id, result.errorMessage)); } catch (Exception ex) { Logger.Error(string.Format("Failed to update product ({0}) Error Message: {1}", item.Id, ex.Message)); } return false; }
public ProductInfoResponse ProductInfoResponseMapping(ProductEntity source) { if (source == null) { return null; } var brand = BrandInfoResponseMapping(_brandRepository.GetItem(source.Brand_Id)); var store = StoreResponseMapping(_storeRepository.GetItem(source.Store_Id)); var ruser = ShowCustomerInfoResponseMapping(_customerRepository.GetItem(source.RecommendUser)); var tag = TagInfoResponseMapping(_tagRepository.GetItem(source.Tag_Id)); var resources = ResourceInfoResponsesMapping(GetListResourceEntities(SourceType.Product, source.Id)); var pprs = GetPromotionForRelation(new List<int>(1) { source.Id }); var promotions = GetPromotionInfos4V(pprs.Select(v => v.ProId ?? 0).ToList()); return ProductInfoResponseMapping(source, brand, store, ruser, tag, resources.ToList(), promotions); }
private bool Upload(ProductEntity item) { try { var builder = RequestParamsBuilderFactory.CreateBuilder(new AddItemRequest()); var result = Client.Execute<dynamic>(builder.BuildParameters(item)); if (result.errorCode == 0) { if (result.warnMessage != null && result.warnMessage.ToString().Length > 0) { Logger.Error(string.Format("Succeed upload product,but some warnning message :{0}", result.warnMessage)); } var processor = ProcessorFactory.CreateProcessor<ItemResponseProcessor>(); if (processor.Process(result, item.Id)) { return true; } Logger.Error(string.Format("Failed to upload product to wgw {0}({1}) Error Message: {2}", item.Name, item.Id, processor.ErrorMessage)); } else //if (result.errorCode == 50005) //图片读取问题导致的失败实际上商品有可能已经上传成功需要补救下 { var request = new QueryItemListRequest(); request.Put("defStockId",item.Id); request.Put("startIndex",0); request.Put("pageSize",1); request.Put("orderType","7"); var rsp = Client.Execute<dynamic>(request); if (rsp.errorCode == 0) { var ps = ProcessorFactory.CreateProcessor<QueryItemListResponseProcessor>(); if (ps.Process(rsp,null)) { return true; } Logger.Error(ps.ErrorMessage); } else { Logger.Error(string.Format("Failed to upload product to wgw {0} Error Message: {1}",item.Id, result.errorMessage)); } } } catch (Exception ex) { Logger.Error(string.Format("Failed to upload product {0} Error Message: {1}", item.Id, ex.Message)); } return false; }
private static ItemsInfoResponse ItemsInfoResponseMapping(ProductEntity source, StoreInfoResponse store, List<ResourceInfoResponse> resources, List<PromotionInfo> promotionInfos) { var target = Mapper.Map<ProductEntity, ItemsInfoResponse>(source); target.SType = SourceType.Product; target.Store = store; target.Resources = resources; target.Promotions = promotionInfos; return target; }
public StockBuilder(ISyncRequest request, ProductEntity item) { this._request = request; this._item = item; }