309 lines
15 KiB
C#
309 lines
15 KiB
C#
using Api.Framework;
|
||
using Api.Framework.Cps;
|
||
using Api.Framework.Enums;
|
||
using Api.Framework.Model;
|
||
using Api.Framework.SDK;
|
||
using Api.Framework.Tools;
|
||
using Chat.Framework;
|
||
using Chat.Framework.WXSdk;
|
||
using CouponsSend.Entitys;
|
||
using CouponsSend.Properties;
|
||
using CsharpHttpHelper;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Linq;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using static Api.Framework.Tools.TBHelper;
|
||
using static CouponsSend.Entitys.Enum;
|
||
|
||
namespace CouponsSend
|
||
{
|
||
public class SendCouponThread : TimerTask
|
||
{
|
||
public DateTime quanTime;
|
||
SqlSugarClient session = ApiClient.GetSession();
|
||
AnalyzeGoods analyzeGoods = new AnalyzeGoods();
|
||
private bool IsRunning = false;
|
||
public override void Run(object state, bool timedOut)
|
||
{
|
||
if (IsRunning) return;
|
||
|
||
try
|
||
{
|
||
IsRunning = true;
|
||
|
||
#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_couponssend_goodsinfos", new { });
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, "定时清理异常" + ex.Message + "-" + ex.StackTrace);
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
var isSendTime = false; //是否是发送时间段内
|
||
|
||
if (Class1.Config.NotCheckTime == SwitchType.关闭)
|
||
{
|
||
//检查时间限制
|
||
if (Class1.Config.SendTaskTimes != null && Class1.Config.SendTaskTimes.Length != 0)
|
||
{
|
||
var now = DateTime.Now;
|
||
foreach (var item in Class1.Config.SendTaskTimes)
|
||
{
|
||
try
|
||
{
|
||
var times = item.Trim().Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
|
||
if (times.Length != 2) throw new Exception("设置的时间段格式不正确");
|
||
var begin = DateTime.Parse(times[0].Trim());
|
||
var end = DateTime.Parse(times[1].Trim());
|
||
if (begin <= now && now <= end)
|
||
{
|
||
isSendTime = true;
|
||
break;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"{ex.Message} - {ex.StackTrace}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else isSendTime = true;//不限发送开启
|
||
|
||
if (!isSendTime) return;//不在发送时间终止
|
||
|
||
//删除N天前的数据
|
||
session.ExcuteSQL("Delete from fl_plugin_couponssend_goodsinfos where cur_time <= @time", new { time = DateTime.Today.AddDays(-3) });
|
||
|
||
#region 采集数据
|
||
if (Class1.Config.Switch == SwitchType.开启)
|
||
{
|
||
var num = session.Find<fl_plugin_couponssend_goodsinfos>("select * from fl_plugin_couponssend_goodsinfos where state = @state", new { state = StateType.未推广 }).Count();
|
||
if (num <= Class1.Config.GatherTouchNum)
|
||
analyzeGoods.GatherFactory();
|
||
}
|
||
#endregion
|
||
|
||
#region 发送
|
||
//未选择发送的群
|
||
var adzones = session.Find<fl_adzone_info>("select * from fl_adzone_info where custom_type = @custom_type and onoff = @onoff", new { custom_type = Resources.SoftwareType, onoff = false });
|
||
var wxGroups = adzones.Where(f => f.group_id.Contains("@") || f.group_id.Contains("R:")).ToList();//微信发送列表
|
||
var qqGroups = adzones.Where(f => !f.group_id.Contains("@") && !f.group_id.Contains("R:")).ToList();//微信发送列表
|
||
|
||
if (qqGroups.Count == 0 && wxGroups.Count == 0) return;
|
||
|
||
var goods_info = session.FindSingle<fl_plugin_couponssend_goodsinfos>($"select * from fl_plugin_couponssend_goodsinfos where state = 1 order by cur_time desc");
|
||
|
||
if (goods_info == null) throw new Exception("没有可发送的商品");
|
||
|
||
if (!string.IsNullOrEmpty(Class1.Config.BlackContent))
|
||
{
|
||
var tempReg = Regex.Match(goods_info.goods_name, "(" + Class1.Config.BlackContent.Replace(",", ",").Replace(",", "|") + ")");
|
||
if (tempReg.Success) return;
|
||
}
|
||
if (!string.IsNullOrEmpty(Class1.Config.WhiteContent))
|
||
{
|
||
var tempReg = Regex.Match(goods_info.goods_name, "(" + Class1.Config.WhiteContent.Replace(",", ",").Replace(",", "|") + ")");
|
||
if (!tempReg.Success) return;
|
||
}
|
||
|
||
|
||
if (quanTime > DateTime.Now) return;
|
||
|
||
//商品介绍
|
||
var introduce = goods_info.goods_desc;
|
||
|
||
//随便找一个推广位
|
||
var adzone = session.FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.custom_type == Resources.SoftwareType && !string.IsNullOrWhiteSpace(f.adzone_pid));
|
||
if (adzone == null)
|
||
adzone = session.FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType.阿里妈妈 && !string.IsNullOrWhiteSpace(f.adzone_pid));
|
||
if (adzone == null) return;
|
||
var cps = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.阿里妈妈 && f.username == adzone.adzone_pid_cps_name);
|
||
if (cps == null) throw new Exception("群发优惠券推广位设置异常!");
|
||
AlimamaApi api = CpsClient.CreateAlimamaRequest(cps);
|
||
|
||
var pid_split = adzone.adzone_pid.Split('_');
|
||
|
||
//string adzone_id = pid_split[3];
|
||
//string site_id = pid_split[2];
|
||
|
||
try
|
||
{
|
||
#region 微信群发送
|
||
var wxTask = Task.Run(() =>
|
||
{
|
||
var _session = ApiClient.GetSession();
|
||
var wxClient = ChatClient.WXClient.Values.ToList().Where(f => f.Status == WxStatus.在线);
|
||
foreach (var item in wxGroups)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(item.adzone_pid)) continue;
|
||
var robot = _session.FindRobots().FirstOrDefault(f => f.id == item.robot_id);
|
||
if (robot == null) continue;
|
||
var wx = wxClient.FirstOrDefault(f => f.WeixinHao == robot.name);
|
||
if (wx == null) continue;
|
||
var ipad = wx; //as WXClientImpl_IPAD;
|
||
if (ipad == null) continue;
|
||
var group = ipad.GetContact(item.group_id);
|
||
if (group == null) continue;
|
||
try
|
||
{
|
||
var msg = GetSendMsg(api, pid_split, item, goods_info, Class1.Config.WXMsg);
|
||
|
||
if (string.IsNullOrWhiteSpace(msg)) return;
|
||
|
||
ApiClient.SendMessage(robot, string.Empty, msg, item.group_id);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"发送群:{item.group_id} - {ex.Message} - {ex.StackTrace}");
|
||
continue;
|
||
}
|
||
Thread.Sleep(Class1.Config.IntervalTime_Group * 1000 + new Random().Next(100, 1000));
|
||
}
|
||
});
|
||
#endregion
|
||
|
||
#region QQ群发送
|
||
var qqTask = Task.Run(() =>
|
||
{
|
||
var _session = ApiClient.GetSession();
|
||
var qqClient = ChatClient.QQClients.Values;
|
||
foreach (var item in qqGroups)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(item.adzone_pid)) continue;
|
||
var robot = _session.FindRobots().FirstOrDefault(f => f.id == item.robot_id);
|
||
if (robot == null) continue;
|
||
var qq = qqClient.FirstOrDefault(f => f.QQ.ToString() == robot.name);
|
||
if (qq == null) continue;
|
||
if (!qq.IsLogin) continue;
|
||
|
||
try
|
||
{
|
||
var msg = GetSendMsg(api, pid_split, item, goods_info, Class1.Config.QQMsg);
|
||
|
||
if (string.IsNullOrWhiteSpace(msg)) return;
|
||
|
||
ApiClient.SendMessage(robot, string.Empty, msg, item.group_id);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"发送群:{item.group_id} - {ex.Message} - {ex.StackTrace}");
|
||
continue;
|
||
}
|
||
Thread.Sleep(Class1.Config.IntervalTime_Group * 1000 + new Random().Next(100, 1000));
|
||
}
|
||
});
|
||
#endregion
|
||
|
||
Task.WaitAll(wxTask, qqTask);
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"发送失败:{ex.Message} - {ex.StackTrace}");
|
||
}
|
||
//}
|
||
quanTime = DateTime.Now.AddSeconds(Class1.Config.IntervalTime_Coupon);
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//EventClient.OnEvent(this, $"解析群发优惠券消息异常:{ex.Message} - {ex.StackTrace}");
|
||
}
|
||
finally
|
||
{
|
||
IsRunning = false;
|
||
}
|
||
}
|
||
|
||
private string GetSendMsg(AlimamaApi api, string[] pid_split, fl_adzone_info item, fl_plugin_couponssend_goodsinfos goods_info, string mess)
|
||
{
|
||
try
|
||
{
|
||
var _cps = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.阿里妈妈 && f.username == item.adzone_pid_cps_name);
|
||
if (_cps == null) throw new Exception("阿里妈妈异常请重新登录!");
|
||
AlimamaApi _api = CpsClient.CreateAlimamaRequest(_cps);
|
||
pid_split = item.adzone_pid.Split('_');
|
||
|
||
var turnItem = api.TurnItemId(goods_info.goods_id, goods_info.goods_quan, pid_split[3], pid_split[2], "2");
|
||
|
||
var coupon_price = goods_info.coupon_discount;
|
||
|
||
//券后 总佣金
|
||
var useCoupon_totalCommFee = (double)turnItem.CommissionUseCouponMoney;
|
||
//用券后 给用户的佣金
|
||
var useCoupon_commFee = session.FindItemPoint(null, useCoupon_totalCommFee, 1, CpsType.阿里妈妈);
|
||
|
||
var tbAnalysis = new TbAnalysis();
|
||
|
||
//淘口令的前/后符号有没有设置.有设置将替换掉
|
||
var tkl = tbAnalysis.ReplaceTklModifier(turnItem.BuyPass);
|
||
|
||
var composeUrl = api.ComposeTbClick(goods_info.goods_id, "https://img.alicdn.com/tfs/TB1MaLKRXXXXXaWXFXXXXXXXXXX-480-260.png", tkl, turnItem.BuyUrl, true, item.adzone_pid, ApiClient.Setting.SystemConfig.ZjyComposeType == ComposeType.有推荐商品, (int)Class1.Config.SearchDwzType);//中间页地址
|
||
|
||
//[机器人账号]、[机器人昵称]、[商品标题]、[商品原价]、[优惠券金额]、[券后价]、[商品主图]、[积分名称]、[月销量]、[返利积分]、[购买地址]、[购买中间页地址]、[购买淘口令]、[共节省]、[商品图片]、[文案] XML:[图片地址]
|
||
|
||
string QRPic = string.Empty;
|
||
if (Class1.Config.WXMsg.Contains("[商品图片]"))
|
||
{
|
||
QRPic = ApiClient.GetQRImage(goods_info.goods_name, goods_info.normal_price.ToString(), coupon_price.ToString(), goods_info.actual_price.ToString(), goods_info.goods_thumbnail_url, composeUrl, Class1.Config.qrImageType, CpsType.阿里妈妈, goods_info.goods_desc);
|
||
if (!string.IsNullOrWhiteSpace(QRPic))
|
||
QRPic = $"[图片={QRPic}]";
|
||
}
|
||
if (goods_info.state != StateType.已推广)
|
||
{
|
||
goods_info.state = StateType.已推广;
|
||
goods_info = session.Saveable(goods_info).ExecuteReturnEntity();
|
||
}
|
||
|
||
return mess.Replace("[商品标题]", goods_info.goods_name)
|
||
.Replace("[商品原价]", goods_info.normal_price.ToString("0.00"))
|
||
.Replace("[商品主图]", $"[图片={goods_info.goods_thumbnail_url}]")
|
||
.Replace("[月销量]", "0")
|
||
.Replace("[返利积分]", useCoupon_commFee.UserPoint.ToString("0.00"))
|
||
.Replace("[券后价]", goods_info.actual_price.ToString("0.00"))
|
||
.Replace("[优惠券金额]", goods_info.coupon_discount.ToString("0.00"))
|
||
.Replace("[购买地址]", turnItem.BuyUrl)
|
||
.Replace("[购买中间页地址]", composeUrl)
|
||
.Replace("[购买淘口令]", tkl)
|
||
.Replace("[共节省]", (goods_info.coupon_discount + useCoupon_commFee.UserPoint).ToString("0.00"))
|
||
.Replace("[商品图片]", QRPic)
|
||
.Replace("[图片地址]", goods_info.goods_thumbnail_url)
|
||
.Replace("[文案]", goods_info.goods_desc);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
return string.Empty;
|
||
}
|
||
|
||
public double QueryDsr(string sellerId, string itemId)
|
||
{
|
||
string html = new HttpHelper().GetHtml("https://dsr-rate.tmall.com/list_dsr_info.htm?itemId=" + itemId + "&sellerId=" + sellerId + "&_ksTS=" + HttpExtend.GetTimeStamp() + "_194&callback=jsonp195").Html;
|
||
var reg = Regex.Match(html, "gradeAvg\":([^,]+)");
|
||
if (reg.Success)
|
||
{
|
||
return double.Parse(reg.Groups[1].Value);
|
||
}
|
||
return 0.00;
|
||
}
|
||
|
||
}
|
||
}
|