2022-09-20 03:10:29 +00:00
using Api.Framework ;
using Api.Framework.Enums ;
using Api.Framework.Model ;
using Api.Framework.SDK ;
using Api.Framework.Tools ;
using PDDCircleFriends.Entitys ;
using SqlSugar ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Text.RegularExpressions ;
using Weixin.PDDCirclePromotion ;
using Weixin.PDDCirclePromotion.Properties ;
using static PDDCircleFriends . Enums ;
namespace PDDCircleFriends
{
public class SendCircleFriendThread : TimerTask
{
PDDHelper pddHelper = new PDDHelper ( ) ;
AnalyzeGoods analyzeGoods = new AnalyzeGoods ( ) ;
StringBuilder strb = new StringBuilder ( ) ;
bool isSend = false ;
bool isSendTime = false ; //这里是用来判断是否在任务时间段里面,如果满足这会赋值为true
SqlSugarClient session = ApiClient . GetSession ( ) ;
private static object objLock = new object ( ) ;
public override void Run ( object state , bool timedOut )
{
#region 定 时 清 空 采 集 的 数 据
if ( ! string . IsNullOrWhiteSpace ( Class1 . Config . AutoClearTaskTimes ) )
{
if ( Class1 . Config . Auto_IsClear = = SwitchType . 开 启 )
{
try
{
if ( DateTime . Now . AddSeconds ( - 3 ) . ToString ( "HHmm" ) = = DateTime . Parse ( Class1 . Config . AutoClearTaskTimes ) . ToString ( "HHmm" ) )
{
session . ExcuteSQL ( "Delete from fl_plugin_pddcirclepromotion_goodsinfos" , new { } ) ;
}
}
catch ( Exception ex )
{
EventClient . OnEvent ( this , "定时清理异常" + ex . Message + "-" + ex . StackTrace ) ;
}
}
}
#endregion
isSendTime = false ;
var now = DateTime . Now ;
for ( int i = 0 ; i < Class1 . Config . SendTaskTimes . Length ; i + + )
{
if ( ! string . IsNullOrWhiteSpace ( Class1 . Config . SendTaskTimes [ i ] ) )
{
var times = Class1 . Config . SendTaskTimes [ i ] . Trim ( ) . Split ( '-' ) ;
if ( times . Length = = 2 )
{
var begin = DateTime . Parse ( times [ 0 ] . Trim ( ) ) ;
var end = DateTime . Parse ( times [ 1 ] . Trim ( ) ) ;
if ( begin < = now & & now < = end )
{
isSendTime = true ;
break ;
}
}
}
}
if ( ! isSendTime ) return ;
if ( ( DateTime . Now - Class1 . Config . RecordSendTime ) . TotalMinutes < Class1 . Config . Interval_Circle ) return ;
try
{
lock ( objLock )
{
session . ExcuteSQL ( "Delete from fl_plugin_pddcirclepromotion_goodsinfos where cur_time <= @time" , new { time = DateTime . Today . AddDays ( - 15 ) } ) ;
isSend = false ;
#region 采 集 数 据
if ( Class1 . Config . Switch = = SwitchType . 开 启 )
{
var num = session . Queryable < fl_plugin_pddcirclepromotion_goodsinfos > ( ) . Count ( f = > f . state = = StateType . 未 推 广 ) ;
if ( num < = Class1 . Config . GatherTouchNum )
analyzeGoods . GatherFactory ( ) ;
}
#endregion
var goods_infos_temp = new List < fl_plugin_pddcirclepromotion_goodsinfos > ( ) ;
var goods_infos = session . Queryable < fl_plugin_pddcirclepromotion_goodsinfos > ( ) . Where ( f = > f . state = = StateType . 未 推 广 ) . OrderBy ( f = > f . cur_time , OrderByType . Desc ) . ToPageList ( 1 , ( Class1 . Config . SendGoodsImageNum < = 0 ? 1 : Class1 . Config . SendGoodsImageNum ) ) ;
if ( goods_infos ! = null & & goods_infos . Count ! = 0 )
{
var clients = Chat . Framework . ChatClient . WXClient . Values . ToList ( ) ;
foreach ( var item in clients )
{
try
{
if ( ( item . WeixinType = = Chat . Framework . WXSdk . Implement . WeixinType . Grpc微信 | | item . WeixinType = = Chat . Framework . WXSdk . Implement . WeixinType . Hook微信 ) & & item . Status = = Chat . Framework . WXSdk . WxStatus . 在 线 )
{
var robot_info = session . FindRobotInfo ( item . WeixinHao , ChatType . 微 信 ) ;
if ( robot_info ! = null )
{
var pd_circle_promotion_temp = session . FindPddCirclePromotions ( ) . Where ( f = > f . onoff ) ;
if ( pd_circle_promotion_temp ! = null )
{
if ( pd_circle_promotion_temp . FirstOrDefault ( f = > f . name = = robot_info . name ) ! = null ) continue ;
}
var adzone = session . FindAdzoneInfos ( ) . FirstOrDefault ( f = > f . alliance_id = = ( int ) CpsType . 多 多 进 宝 & & f . custom_type = = Resources . SoftwareType & & f . robot_id = = robot_info . id ) ;
if ( adzone ! = null )
{
var member = CpsClient . Members . FirstOrDefault ( f = > f . cpstype = = CpsType . 多 多 进 宝 & & f . username = = adzone . adzone_pid_cps_name ) ;
if ( member ! = null )
{
//string param = $"fl_{item.WeixinHao}_{(int)ChatType.微信}_朋友圈推广";
var api = CpsClient . CreatePinduoduoRequest ( member ) ;
if ( api = = null ) { return ; }
strb . Length = 0 ;
strb . AppendLine ( Class1 . Config . Content_Circle ) ;
int count = 0 ;
2023-04-12 09:06:04 +00:00
var isBind = api . IsAuthority ( adzone . adzone_pid ) ;
if ( ! isBind )
2022-09-20 03:10:29 +00:00
{
2023-04-12 09:06:04 +00:00
return ;
2022-09-20 03:10:29 +00:00
}
for ( int i = 0 ; i < goods_infos . Count ; i + + )
{
try
{
goods_infos_temp . Add ( goods_infos [ i ] ) ;
var goodsinfo = goods_infos [ i ] ;
count = 0 ;
Next :
var result = analyzeGoods . FindGoodsInfoToGoodsId ( api , adzone . adzone_pid , goods_infos [ i ] . goods_id , GoodsSourceType . 对 象 ) ; //获取到自己返利链接等信息
if ( result = = null )
{
count + + ;
if ( count < = 5 )
{
goodsinfo = session . Queryable < fl_plugin_pddcirclepromotion_goodsinfos > ( ) . Where ( f = > f . state = = StateType . 未 推 广 ) . OrderBy ( f = > f . cur_time , OrderByType . Desc ) . First ( ) ;
goods_infos_temp . Add ( goodsinfo ) ;
goto Next ;
}
else
continue ;
}
else
{
var cprice = ( result . min_normal_price - result . coupon_discount ) . ToString ( ) ;
var shoppingUrl = pddHelper . GetShoppingUrl ( api , new { p_id = adzone . adzone_pid , goods_sign_list = $"[\" { goods_infos [ i ] . goods_id } \ "]" , multi_group = false , generate_short_url = true , generate_weapp_webview = true , force_duo_id = true , generate_authority_url = ! isBind , generate_schema_url = true } ) ;
var shoppingUrl2 = pddHelper . GetShoppingUrl ( api , new { p_id = adzone . adzone_pid , goods_sign_list = $"[\" { result . goods_id } \ "]" , multi_group = true , generate_short_url = true , generate_weapp_webview = true , force_duo_id = true , generate_authority_url = ! isBind , generate_schema_url = true } ) ;
var goods_desc = string . IsNullOrWhiteSpace ( Class1 . Config . Goods_Desc ) ? result . goods_desc : Class1 . Config . Goods_Desc
. Replace ( "[商品标题]" , result . goods_name )
. Replace ( "[商品描述]" , result . goods_desc )
. Replace ( "[已售数量]" , result . sold_quantity . ToString ( ) )
. Replace ( "[店铺名称]" , result . mall_name )
. Replace ( "[优惠券门槛金额]" , result . coupon_min_order_amount . ToString ( ) )
. Replace ( "[优惠券金额]" , result . coupon_discount . ToString ( ) )
. Replace ( "[优惠券数量]" , result . coupon_remain_quantity . ToString ( ) )
. Replace ( "[商品单买价]" , result . min_normal_price . ToString ( ) )
. Replace ( "[劵后单买价]" , cprice )
. Replace ( "[券后单买价]" , cprice )
. Replace ( "[单买地址]" , shoppingUrl )
. Replace ( "[商品拼团价]" , result . min_group_price . ToString ( ) )
. Replace ( "[劵后拼团价]" , ( ( decimal ) result . min_group_price - ( decimal ) result . coupon_discount ) . ToString ( ) )
. Replace ( "[券后拼团价]" , ( ( decimal ) result . min_group_price - ( decimal ) result . coupon_discount ) . ToString ( ) )
. Replace ( "[拼团地址]" , shoppingUrl2 ) ;
var image = ApiClient . GetQRImage ( result . goods_name , result . min_normal_price . ToString ( ) , result . coupon_discount . ToString ( ) , cprice , result . goods_thumbnail_url , shoppingUrl2 , Class1 . Config . Model , CpsType . 多 多 进 宝 , result . goods_desc ) ; //合成附带二维码等信息的宝贝图片
strb . Append ( $"[图片={image}]" ) ;
}
}
catch
{ }
}
var json = item . SendCircle ( strb . ToString ( ) ) ;
if ( ! string . IsNullOrWhiteSpace ( Class1 . Config . AdditionalComment ) )
{
var reg = Regex . Match ( json . Replace ( "<![CDATA[" , "" ) . Replace ( "]]>" , "" ) , @"<id>(?<id>\d{15,})</id>" ) ; //获取返回的朋友圈id
if ( reg . Success )
item . SendCircleComment ( item . User . Username , reg . Groups [ "id" ] . Value , Class1 . Config . AdditionalComment ) ;
}
isSend = true ; //认定发送过了...
}
}
}
}
}
catch ( Exception ex )
{
//EventClient.OnEvent(this, $"拼多多推广:{item.WeixinHao},朋友圈推广失败,{ex.Message}");
}
}
if ( isSend )
{
for ( int i = 0 ; i < goods_infos_temp . Count ; i + + )
{
session . ExcuteSQL ( "update fl_plugin_pddcirclepromotion_goodsinfos set state = @state where id = @id" , new { state = Enums . StateType . 已 推 广 , id = goods_infos_temp [ i ] . id } ) ;
}
Class1 . Config . RecordSendTime = DateTime . Now ;
Util . Save ( Class1 . Config ) ;
}
}
}
}
catch ( Exception ex )
{
EventClient . OnEvent ( this , $"拼多多推广:{ex.Message},{ex.StackTrace}" ) ;
}
}
}
}