old_flsystem/应用/Weixin.WPHCirclePromotion/AnalyzeGoods.cs

247 lines
14 KiB
C#
Raw Normal View History

2022-09-20 03:10:29 +00:00
using Api.Framework;
using Api.Framework.Cps;
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.WPHCirclePromotion.Entitys;
using static Weixin.WPHCirclePromotion.Enums;
namespace Weixin.WPHCirclePromotion
{
/// <summary>
/// 解析商品
/// </summary>
public class AnalyzeGoods
{
private static int DownPage = 1;
#region
/// <summary>
/// 数据采集
/// </summary>
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 data = new { request = new { channelType = (int)Class1.Config.ChannelType, page = DownPage, pageSize = 100, requestId = Util.GetUUID() } };
GatherAction(member, data);
}
}
catch (Exception ex)
{
if (ex.Message.Contains("只支持查询前10000个商品") || ex.Message.Contains("被禁止商品搜索深翻页请求"))
{
DownPage = 1;
continue;
}
EventClient.OnEvent(this, $"唯品会采集A{ex.Message} - {ex.StackTrace}");
}
return;
}
}
#endregion
private void GatherAction(fl_cps_member member, object data)
{
DownPage++;
var session = ApiClient.GetSession();
try
{
WeipinhuiApi api = CpsClient.CreateWeipinhuiRequest(member);
var gInfosResult = api.SendWeipinhui<GoodsInfoResponseResult>("com.vip.adp.api.open.service.UnionGoodsService-1.0.0#goodsListWithOauth", data);
if (gInfosResult == null || gInfosResult.returnCode == null || gInfosResult.returnCode != "0" || gInfosResult.result == null)
throw new Exception("唯品会采集异常");
if (gInfosResult.result.goodsInfoList == null || gInfosResult.result.goodsInfoList.Count == 0) return;
List<fl_plugin_wphcirclepromotion_goodsinfos> goodsinfos = new List<fl_plugin_wphcirclepromotion_goodsinfos>();
foreach (var item in gInfosResult.result.goodsInfoList)
{
try
{
if (session.FindSingle<fl_plugin_wphcirclepromotion_goodsinfos>("select * from fl_plugin_wphcirclepromotion_goodsinfos where goodsId = @goodsId", new { goodsId = item.goodsId }) != null)
continue;
var ss = new fl_plugin_wphcirclepromotion_goodsinfos()
{
goodsId = item.goodsId,
goodsName = item.goodsName,
goodsDesc = item.goodsDesc == null ? string.Empty : item.goodsDesc.Substring(0, item.goodsDesc.Length < 200 ? item.goodsDesc.Length : 200),
goodsMainPicture = item.goodsMainPicture,
buy = item.couponInfo == null ? 0d : double.Parse(item.couponInfo.buy),
fav = item.couponInfo == null ? 0d : double.Parse(item.couponInfo.fav),
commission = double.Parse(item.commission),
commissionRate = double.Parse(item.commissionRate),
marketPrice = double.Parse(item.marketPrice),
storeName = item.storeInfo == null ? string.Empty : item.storeInfo.storeName,
totalAmount = item.couponInfo == null ? 0 : item.couponInfo.totalAmount,
vipPrice = double.Parse(item.vipPrice),
cur_time = DateTime.Today,
state = StateType.广,
//goods_source = GoodsSourceType.接口采集
};
goodsinfos.Add(ss);
}
catch (Exception ex)
{ }
}
if (goodsinfos.Count != 0)
session.Insertable(goodsinfos).ExecuteCommand();
//解析商品,获取商品的信息
//var goods_search_response = obj["goods_search_response"] as Dictionary<string, object>;
//var goods_list = goods_search_response["goods_list"] as ArrayList;
//if (goods_list != null && goods_list.Count != 0)
//{
// session.BeginTransaction();
// List<fl_plugin_wphcirclepromotion_goodsinfos> goodsinfos = new List<fl_plugin_wphcirclepromotion_goodsinfos>();
// foreach (var item in goods_list)
// {
// try
// {
// var goods = item as Dictionary<string, object>;
// var goods_id = (goods["goods_id"] ?? string.Empty).ToString(); //商品id
// if (session.FindSingle<fl_plugin_wphcirclepromotion_goodsinfos>("select * from fl_plugin_wphcirclepromotion_goodsinfos where goods_id = @goods_id", new { goods_id = goods_id }) != null)
// continue;
// var goods_name = (goods["goods_name"] ?? string.Empty).ToString(); //商品标题
// var goods_desc = (goods["goods_desc"] ?? string.Empty).ToString(); //商品描述
// var goods_thumbnail_url = (goods["goods_thumbnail_url"] ?? string.Empty).ToString(); //商品图片链接
// //已售卖件数
// var sold_quantity = 0;
// try
// {
// if (goods.ContainsKey("sales_tip"))
// {
// var _sales_tip = goods["sales_tip"].ToString();
// _sales_tip = _sales_tip.Replace("+", "");
// if (_sales_tip.Contains("万"))
// {
// _sales_tip = _sales_tip.Replace("万", "");
// var temp = decimal.Parse(_sales_tip);
// sold_quantity = (int)(temp * 10000m);
// }
// else
// sold_quantity = int.Parse(_sales_tip);
// }
// }
// catch (Exception)
// { sold_quantity = 0; }
// var min_group_price = Math.Round(goods["min_group_price"] == null ? 0 : (double.Parse(goods["min_group_price"].ToString()) / 100), 2); //最小拼团价格,单位为元
// var min_normal_price = Math.Round(goods["min_normal_price"] == null ? 0 : (double.Parse(goods["min_normal_price"].ToString()) / 100), 2); //最小单买价格,单位为元
// var mall_name = (goods["mall_name"] ?? string.Empty).ToString(); //店铺名称
// var category_id = (goods["category_id"] ?? string.Empty).ToString(); //类目id
// var coupon_min_order_amount = Math.Round(goods["coupon_min_order_amount"] == null ? 0 : (double.Parse(goods["coupon_min_order_amount"].ToString()) / 100), 2); //优惠券门槛价格,单位为元
// var coupon_discount = Math.Round(goods["coupon_discount"] == null ? 0 : (double.Parse(goods["coupon_discount"].ToString()) / 100), 2); //优惠券面额,单位为元
// var coupon_remain_quantity = goods["coupon_remain_quantity"] == null ? 0 : int.Parse(goods["coupon_remain_quantity"].ToString()); //优惠券剩余数量
// var promotion_rate = Math.Round(goods["promotion_rate"] == null ? 0 : (double.Parse(goods["promotion_rate"].ToString()) / 10), 2); //佣金比例,百分比
// var ss = new fl_plugin_wphcirclepromotion_goodsinfos() { goods_id = goods_id, goods_name = goods_name, goods_desc = goods_desc.Substring(0, goods_desc.Length < 200 ? goods_desc.Length : 200), goods_thumbnail_url = goods_thumbnail_url, sold_quantity = sold_quantity, min_group_price = min_group_price, min_normal_price = min_normal_price, mall_name = mall_name, category_id = category_id, coupon_min_order_amount = coupon_min_order_amount, coupon_discount = coupon_discount, coupon_remain_quantity = coupon_remain_quantity, promotion_rate = promotion_rate, 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();
//}
//else
// DownPage = 1;
}
catch (Exception ex)
{
//session.Rollback();
throw ex;
}
}
internal fl_plugin_wphcirclepromotion_goodsinfos FindGoodsInfoToGoodsId(string goods_id, fl_cps_member member)
{
try
{
fl_plugin_wphcirclepromotion_goodsinfos goodsinfo = null;
member = (member == null ? CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.) : member);
if (member != null)
{
var api = CpsClient.CreateWeipinhuiRequest(member);
var wphGInfo = api.SendWeipinhui<WphGoodsInfo>("com.vip.adp.api.open.service.UnionGoodsService-1.0.0#getByGoodsIdsWithOauth", new { goodsIdList = new List<string> { goods_id }, requestId = Util.GetUUID() /*,chanTag = chanTag*/ });
if (wphGInfo == null || wphGInfo.result == null || wphGInfo.returnCode == null || wphGInfo.returnCode != "0")
return null;
foreach (var item in wphGInfo.result)
{
//优惠券金额
var coupon_price = 0.00d;
//优惠券地址
var coupon_click_url = string.Empty;
//优惠券没有过期
if (item.exclusiveCoupon != null && item.exclusiveCoupon.useEndTime > Util.GetTimeSpan(DateTime.Now, true))
{
coupon_price = double.Parse(item.exclusiveCoupon.fav);
coupon_click_url = item.exclusiveCoupon.receiveUrl;
}
//商品标题//&lt; &gt;&amp;&quot;&copy; <>&",©;
var goodsName = item.goodsName.Replace("", "").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&").Replace("&quot;", "\"").Replace(" &copy;", "©");
var vipPrice = double.Parse(item.vipPrice);
var endPrice = vipPrice;
if (coupon_price != 0)
endPrice = (double)((decimal)vipPrice - (decimal)coupon_price);
//佣金比例
var commissionRate = double.Parse(item.commissionRate);
goodsinfo = new fl_plugin_wphcirclepromotion_goodsinfos()
{
goodsId = goods_id,
goodsName = item.goodsName,
goodsDesc = item.goodsDesc,
goodsMainPicture = item.goodsMainPicture,
marketPrice = double.Parse(item.marketPrice),
vipPrice = double.Parse(item.vipPrice),
buy = item.couponInfo == null ? 0 : double.Parse(item.couponInfo.buy),
commission = double.Parse(item.commission),
commissionRate = double.Parse(item.commissionRate),
fav = item.couponInfo == null ? 0 : double.Parse(item.couponInfo.fav),
storeName = item.storeInfo == null ? string.Empty : item.storeInfo.storeName,
totalAmount = item.couponInfo == null ? 0 : item.couponInfo.totalAmount,
cur_time = DateTime.Today,
state = StateType.广,
//goods_source = goods_source
};
}
return goodsinfo;
}
}
catch (Exception ex)
{ }
return null;
}
}
}