using Api.Framework; using Api.Framework.Cps; using Api.Framework.EntityTmp.SuNing; using Api.Framework.EntityTmp.SuNing4; using Api.Framework.Enums; using Api.Framework.Model; using Api.Framework.Tools; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Weixin.SNCirclePromotion.Entitys; namespace Weixin.SNCirclePromotion { /// /// 解析商品 /// public class AnalyzeGoods { /// /// 通过商品的id查询返回获取到自己返利商品信息 /// /// /// /// /// public fl_plugin_sncirclepromotion_goodsinfos FindGoodsInfoToGoodsId(SuNingApi api, string goodinfo, GoodsSourceType goods_source) { try { fl_plugin_sncirclepromotion_goodsinfos goodsinfo = null; var snGInfo = api.QueryGoodInfo(goodinfo); if (snGInfo.sn_responseContent.sn_error != null) throw new Exception(snGInfo.sn_responseContent.sn_error.error_msg);//查询没结果 else if (snGInfo.sn_responseContent.sn_body == null || snGInfo.sn_responseContent.sn_body.queryCommoditydetail == null || snGInfo.sn_responseContent.sn_body.queryCommoditydetail.Count == 0) throw new Exception("查询没结果2"); foreach (var goods in snGInfo.sn_responseContent.sn_body.queryCommoditydetail) { var goodsname = (goods.commodityInfo.commodityName ?? string.Empty).ToString(); //商品标题 var goodsdesc = (goods.commodityInfo.sellingPoint ?? string.Empty).ToString(); //商品描述 var picurl = (goods.commodityInfo.pictureUrl != null && goods.commodityInfo.pictureUrl.Count != 0) ? goods.commodityInfo.pictureUrl[0].picUrl : string.Empty; //商品图片链接 var goodsid = (goods.commodityInfo.commodityCode ?? string.Empty).ToString(); //商品id //已售卖件数 var sold_quantity = goods.commodityInfo.monthSales; var vipPrice = -1d; if (!string.IsNullOrWhiteSpace(goods.commodityInfo.snPrice)) vipPrice = double.Parse(goods.commodityInfo.snPrice); if (vipPrice == -1 && !string.IsNullOrWhiteSpace(goods.commodityInfo.commodityPrice)) vipPrice = double.Parse(goods.commodityInfo.commodityPrice); var buyUrl = goods.commodityInfo.productUrl; if (goods.pgInfo != null) { if (!string.IsNullOrWhiteSpace(goods.pgInfo.pgUrl)) { buyUrl = goods.pgInfo.pgUrl; if (!string.IsNullOrWhiteSpace(goods.pgInfo.pgUrl)) { vipPrice = double.Parse(goods.pgInfo.pgPrice); } } } if (vipPrice == -1) throw new Exception("当前价格不明"); //优惠券金额 var coupon_price = 0.00d; //怕两个地址都没有链接.用自己拼的 if (string.IsNullOrWhiteSpace(buyUrl)) buyUrl = $@"https://product.suning.com/{goods.commodityInfo.supplierCode}/{goodsid}.html"; //优惠券地址 var couponUrl = (goods.couponInfo != null && !string.IsNullOrWhiteSpace(goods.couponInfo.couponUrl)) ? goods.couponInfo.couponUrl : string.Empty; //优惠券地址 //var coupon_click_url = string.Empty; if (!string.IsNullOrWhiteSpace(couponUrl))//有券 { //优惠券没有过期 if (goods.couponInfo.couponEndTime >= DateTime.Now && ((!string.IsNullOrWhiteSpace(goods.couponInfo.bounsLimit) && double.Parse(goods.couponInfo.bounsLimit) <= vipPrice) || (string.IsNullOrWhiteSpace(goods.couponInfo.bounsLimit)))) coupon_price = double.Parse(goods.couponInfo.couponValue); } //商品标题//< >&"© <,>,&,",©; var goodsName = goods.commodityInfo.commodityName.Replace("‮", "").Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace(""", "\"").Replace(" ©", "©"); var endPrice = vipPrice; if (coupon_price != 0) endPrice = (double)((decimal)vipPrice - (decimal)coupon_price); //佣金比例 var commissionRate = double.Parse(goods.commodityInfo.rate); var supplierName = goods.commodityInfo.supplierName; //店铺名称 var coupon_min_order_amount = Math.Round(string.IsNullOrWhiteSpace(goods.couponInfo.bounsLimit) ? 0 : double.Parse(goods.couponInfo.bounsLimit), 2); //优惠券门槛价格,单位为元 var coupon_discount = Math.Round(goods.couponInfo.couponValue == null ? 0 : double.Parse(goods.couponInfo.couponValue), 2); //优惠券面额,单位为元 goodsinfo = new fl_plugin_sncirclepromotion_goodsinfos() { couponUrl = couponUrl, buyUrl = buyUrl, goodsid = goodsid, supplierCode = goods.commodityInfo.supplierCode, goodsname = goodsname, goodsdesc = goodsdesc.Substring(0, goodsdesc.Length < 200 ? goodsdesc.Length : 200), picurl = picurl, sold_quantity = sold_quantity, vipPrice = vipPrice, supplierName = supplierName, coupon_min_order_amount = coupon_min_order_amount, coupon_discount = coupon_discount, commissionRate = commissionRate, cur_time = DateTime.Today, state = StateType.未推广, goods_source = goods_source }; } return goodsinfo; } catch (Exception ex) { return null; } } private static int DownPage = 1; #region 采集数据 /// /// 数据采集 /// public void GatherFactory() { for (int i = 0; i < 2; i++) { var session = ApiClient.GetSession(); try { var member = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.苏宁易购);//随机获取一个苏宁的cps if (member != null) { var api = CpsClient.CreateSuNingRequest(member); object data = null; var opt_ids = Class1.Config.Category.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in opt_ids) { var result = api.QueryGuideGoodsInfo(Util.ConvertEnum(item), DownPage); GatherAction(result); } } } catch (Exception ex) { DownPage = 1; EventClient.OnEvent(this, $"苏宁采集A:{ex.Message} - {ex.StackTrace}"); } return; } } #endregion /// /// 苏宁商品采集 /// /// SN_GoodInfo对象 private void GatherAction(SN_Recommend_Commodity snGInfo) { DownPage++; var session = ApiClient.GetSession(); try { if (snGInfo.sn_responseContent.sn_error != null) throw new Exception(snGInfo.sn_responseContent.sn_error.error_msg);//查询没结果 else if (snGInfo.sn_responseContent.sn_body == null || snGInfo.sn_responseContent.sn_body.querySelectrecommendcommodity == null || snGInfo.sn_responseContent.sn_body.querySelectrecommendcommodity == null && snGInfo.sn_responseContent.sn_body.querySelectrecommendcommodity.commodityList == null && snGInfo.sn_responseContent.sn_body.querySelectrecommendcommodity.commodityList.Count == 0) throw new Exception("查询没结果2"); session.BeginTransaction(); List goodsinfos = new List(); foreach (var goods in snGInfo.sn_responseContent.sn_body.querySelectrecommendcommodity.commodityList) { try { var goodsid = (goods.commodityInfo.commodityCode ?? string.Empty).ToString(); //商品id if (session.FindSingle("select * from fl_plugin_sncirclepromotion_goodsinfos where goodsid = @goodsid", new { goodsid = goodsid }) != null) continue; var goodsname = (goods.commodityInfo.commodityName ?? string.Empty).ToString(); //商品标题 var goodsdesc = (goods.commodityInfo.sellingPoint ?? string.Empty).ToString(); //商品描述 var picurl = (goods.commodityInfo.pictureUrl != null && goods.commodityInfo.pictureUrl.Count != 0) ? goods.commodityInfo.pictureUrl[0].picUrl : string.Empty; //商品图片链接 //已售卖件数 var sold_quantity = goods.commodityInfo.monthSales; var vipPrice = -1d; if (!string.IsNullOrWhiteSpace(goods.commodityInfo.snPrice)) vipPrice = double.Parse(goods.commodityInfo.snPrice); if (vipPrice == -1 && !string.IsNullOrWhiteSpace(goods.commodityInfo.commodityPrice)) vipPrice = double.Parse(goods.commodityInfo.commodityPrice); var buyUrl = goods.commodityInfo.productUrl; if (goods.pgInfo != null) { if (!string.IsNullOrWhiteSpace(goods.pgInfo.pgUrl)) { buyUrl = goods.pgInfo.pgUrl; if (!string.IsNullOrWhiteSpace(goods.pgInfo.pgUrl)) { vipPrice = double.Parse(goods.pgInfo.pgPrice); } } } if (vipPrice == -1) throw new Exception("当前价格不明"); //优惠券金额 var coupon_price = 0.00d; //怕两个地址都没有链接.用自己拼的 if (string.IsNullOrWhiteSpace(buyUrl)) buyUrl = $@"https://product.suning.com/{goods.commodityInfo.supplierCode}/{goodsid}.html"; //优惠券地址 var couponUrl = (goods.couponInfo != null && !string.IsNullOrWhiteSpace(goods.couponInfo.couponUrl)) ? goods.couponInfo.couponUrl : string.Empty; //优惠券地址 //var coupon_click_url = string.Empty; if (!string.IsNullOrWhiteSpace(couponUrl))//有券 { //优惠券没有过期 if (goods.couponInfo.couponEndTime >= DateTime.Now && ((!string.IsNullOrWhiteSpace(goods.couponInfo.bounsLimit) && double.Parse(goods.couponInfo.bounsLimit) <= vipPrice) || (string.IsNullOrWhiteSpace(goods.couponInfo.bounsLimit)))) coupon_price = double.Parse(goods.couponInfo.couponValue); } //商品标题//< >&"© <,>,&,",©; var goodsName = goods.commodityInfo.commodityName.Replace("‮", "").Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace(""", "\"").Replace(" ©", "©"); //var vipPrice = double.Parse(item.commodityInfo.snPrice); var endPrice = vipPrice; if (coupon_price != 0) endPrice = (double)((decimal)vipPrice - (decimal)coupon_price); //佣金比例 var commissionRate = double.Parse(goods.commodityInfo.rate); var supplierName = goods.commodityInfo.supplierName; //店铺名称 var coupon_min_order_amount = Math.Round(string.IsNullOrWhiteSpace(goods.couponInfo.bounsLimit) ? 0 : double.Parse(goods.couponInfo.bounsLimit), 2); //优惠券门槛价格,单位为元 var coupon_discount = Math.Round(goods.couponInfo.couponValue == null ? 0 : double.Parse(goods.couponInfo.couponValue), 2); //优惠券面额,单位为元 var ss = new fl_plugin_sncirclepromotion_goodsinfos() { couponUrl = couponUrl, buyUrl = buyUrl, goodsid = goodsid, supplierCode = goods.commodityInfo.supplierCode, goodsname = goodsName, goodsdesc = goodsdesc.Substring(0, goodsdesc.Length < 200 ? goodsdesc.Length : 200), picurl = picurl, sold_quantity = sold_quantity, vipPrice = vipPrice, supplierName = supplierName, coupon_min_order_amount = coupon_min_order_amount, coupon_discount = coupon_discount, commissionRate = commissionRate, cur_time = DateTime.Today, state = StateType.未推广, goods_source = GoodsSourceType.接口采集 }; goodsinfos.Add(ss); } catch (Exception ex) { } } if (goodsinfos.Count != 0) session.Insertable(goodsinfos).ExecuteCommand(); session.Commit(); } catch (Exception ex) { DownPage = 1; session.Rollback(); throw ex; } } } }