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

240 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Api.Framework;
using Api.Framework.Cps;
using Api.Framework.Enums;
using Api.Framework.Model;
using Api.Framework.Tools;
using PDDCircleFriends.Entitys;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static PDDCircleFriends.Enums;
namespace PDDCircleFriends
{
/// <summary>
/// 解析商品
/// </summary>
public class AnalyzeGoods
{
/// <summary>
/// 通过商品的id查询返回获取到自己返利商品信息
/// </summary>
/// <param name="goods_id"></param>
/// <returns></returns>
public fl_plugin_pddcirclepromotion_goodsinfos FindGoodsInfoToGoodsId(PinduoduoApi api, string pid, string goods_id, GoodsSourceType goods_source)
{
try
{
fl_plugin_pddcirclepromotion_goodsinfos goodsinfo = null;
ArrayList goods_details = null;
goods_details = PDDHelper.FindSendGuideGoodsInfo(api, pid, goods_id);
//没有查到返利提示
if (goods_details == null || goods_details.Count == 0)
throw new Exception("商品不存在");
if (goods_details != null && goods_details.Count != 0)
{
foreach (var goods_detail in goods_details)
{
var goods = goods_detail as Dictionary<string, object>;
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 = goods["sold_quantity"] == null ? 0 : int.Parse(goods["sold_quantity"].ToString());
var goods_sign = goods["goods_sign"].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.00), 2); //最小拼团价格,单位为元
var min_normal_price = Math.Round(goods["min_normal_price"] == null ? 0 : (double.Parse(goods["min_normal_price"].ToString()) / 100.00), 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.00), 2); //优惠券门槛价格,单位为元
var coupon_discount = Math.Round(goods["coupon_discount"] == null ? 0 : (double.Parse(goods["coupon_discount"].ToString()) / 100.00), 2); //优惠券面额,单位为元
if (coupon_discount == 0) return null;//直接不要没有优惠券的商品(拼多多上面好像有点问题.前面采集到的数据,已经添加了优惠券的条件,有时候数据中存在没有优惠券的数据)
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.00), 2); //佣金比例,百分比
goodsinfo = new fl_plugin_pddcirclepromotion_goodsinfos() { goods_id = goods_sign, goods_name = goods_name, goods_desc = goods_desc, 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 = goods_source };
}
}
return goodsinfo;
}
catch (Exception ex)
{
return null;
}
}
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 shopScore = Tools.FindShopScoreVlaue(Class1.Config.ShopScore); //店铺评分
var rate = (Class1.Config.PromotionRate ?? 0) * 10; //返佣比例
string priceStr = string.Empty;
if ((!(Class1.Config.FloorPrice == null && Class1.Config.CeilingPrice == null)))
{
var floor_price = Class1.Config.FloorPrice == null ? @"""""" : (Math.Round((double)Class1.Config.FloorPrice, 2) * 100).ToString();
var ceiling_price = Class1.Config.CeilingPrice == null ? @"""""" : (Math.Round((double)Class1.Config.CeilingPrice, 2) * 100).ToString();
priceStr = @",{""range_id"":0,""range_from"":" + floor_price + @",""range_to"":" + ceiling_price + @"}";
}
var range_list = @"[{""range_id"":7,""range_from"":" + shopScore + @",""range_to"":500},{""range_id"":2,""range_from"":" + rate + @",""range_to"":1000}" + priceStr + @"]";
object data = null;
if (string.IsNullOrEmpty(Class1.Config.Category))
{
data = new { page = DownPage, page_size = 100, sort_type = 0, with_coupon = true, range_list = range_list, custom_parameters = "{\"new\":1}" };
GatherAction(member, data);
}
else
{
var opt_ids = Class1.Config.Category.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in opt_ids)
{
if (item == "精选")
data = new { page = DownPage, page_size = 100, sort_type = 0, with_coupon = true, range_list = range_list };
else
data = new { page = DownPage, page_size = 100, opt_id = Tools.FindCategoryNum(item), sort_type = 0, with_coupon = true, range_list = range_list, custom_parameters = "{\"new\":1}" };
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
/// <summary>
/// 拼多多商品采集
/// </summary>
/// <param name="member">cps对象</param>
/// <param name="data">参数</param>
private void GatherAction(fl_cps_member member, object data)
{
DownPage++;
var session = ApiClient.GetSession();
try
{
PinduoduoApi api = CpsClient.CreatePinduoduoRequest(member);
var goods_list = PDDHelper.FindSendGuideGoodsInfo(api, data);
if (goods_list != null && goods_list.Count != 0)
{
session.BeginTransaction();
List<fl_plugin_pddcirclepromotion_goodsinfos> goodsinfos = new List<fl_plugin_pddcirclepromotion_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
var goods_sign = (goods["goods_sign"] ?? string.Empty).ToString(); //商品id
if (session.FindSingle<fl_plugin_pddcirclepromotion_goodsinfos>("select * from fl_plugin_pddcirclepromotion_goodsinfos where goods_id = @goods_id", new { goods_id = goods_sign }) != 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_pddcirclepromotion_goodsinfos() { goods_id = goods_sign, 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;
}
}
}
}