old_flsystem/应用/KSRebate/MessageOperation.cs

409 lines
20 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Api.Framework;
using Api.Framework.Cps;
using Api.Framework.EntityTmp.KuaiShou;
using Api.Framework.Enums;
using Api.Framework.Events;
using Api.Framework.Model;
using Api.Framework.SDK;
using Api.Framework.Timers;
using Api.Framework.Tools;
using Chat.Framework.WXSdk.Events;
using Chat.Framework.WXSdk.Implement;
using CsharpHttpHelper;
using CsharpHttpHelper.Enum;
using KSRebate.Properties;
using Newtonsoft.Json;
namespace KSRebate
{
public class MessageOperation
{
ReciveIMEvent e = null;
Class1 plugin = null;
object sender = null;
public MessageOperation(object sender, ReciveIMEvent e, Class1 plugin)
{
this.e = e;
this.plugin = plugin;
this.sender = sender;
}
#region
public bool BindOrderId()
{
if (!Regex.IsMatch(e.Message.Trim(), @"^\d{16}$"))//2214500070274639
return false;
var db = ApiClient.GetSession();
var member = e.GetMemberinfo();
var orderid = e.Message.Trim();
//通过宝贝的id 和 订单号 进行查询 查看是否存在
var orders = db.Find<fl_order_kuaishou>("select * from fl_order_kuaishou where oid = @oid", new { oid = orderid });
if (orders != null && orders.Count != 0)
{
var notices = new List<OrderNoticeEvent>();
var downkuaishouTimer = new DownKuaiShouTimer();
foreach (var order in orders)
{
if (order.db_userid != 0 && order.db_userid != member.id)
{
e.SendMessage(Class1.Config.OccupyOrderErrorTip.Replace("[订单号]", orderid));
return true;
}
if (order.db_userid != 0)
{
var point = HttpHelper.JsonToObject<ItemPoint>(order.db_point) as ItemPoint;
member = e.GetMemberinfo(true);
if (order.db_status == SystemOrderStatus.)
e.SendMessage(new VariateReplace().CommonReplace(Class1.Config.OrderRepetBindTip, order, member, point));
else
e.SendMessage(Class1._GetOrderStateMess(order.db_status, order, member, point));
}
else
{
if (order.payTime < ApiClient.Setting.SystemConfig.allow_bind_create_order_time)
{
e.SendMessage($"订单:{orderid},已经过了有效绑定时间!");
return true;
}
if (order.db_status == SystemOrderStatus.)
order.db_endtime = DateTime.Now.AddMinutes(-5);//重新结算的话,需要将订单的冻结时间重新赋值,让后台处理
//用户绑定订单
order.db_robotname = e.RobotName;
order.db_robottype = e.ChatType;
order.msg_groupid = e.Groupid;
order.db_userid = member.id;
db.SaveOrUpdate(order);
member.bind_order++;
member = db.UpdateMemberGroup(member);
#region
var record = db.FindStatisticsRecord(member.id);
if (record == null)
{
record = new fl_statistics_record() { uid = member.id, ex2 = 0, ex4 = HttpExtend.GetTimeStamp(DateTime.Now) };
db.Saveable(record).ExecuteCommand();
}
else
{
if (record.ex2 == 0 && record.ex4 == 0)
{
record.ex4 = HttpExtend.GetTimeStamp(DateTime.Now);
db.Saveable(record).ExecuteCommand();
}
}
#endregion
downkuaishouTimer.UpdateOrder(order, db, notices, isFrontData: true);
}
}
#region
if (!ApiClient.Setting.SystemConfig.message_warning_switch && notices.Count != 0)
{
var tasks = TimerTask.GetTimer<Update_NoticeQueue>() as Update_NoticeQueue;
foreach (var item in notices)
{
//if (item.IsRewards)
tasks.Add(item);
//else
//SDK_OrderNoticeEvent(this, item);
}
}
#endregion
e.Cancel = true;
}
else
{
CpsClient.UpdateOrder(CpsType., DateTime.Now.AddMinutes(-30), DateTime.Now, orderid);
e.SendMessage(Class1.Config.NotFoundOrderErrorTip.Replace("[订单号]", orderid));
e.Cancel = true;
return true;
}
return false;
}
#endregion
private Dictionary<string, DateTime> debutTimes = new Dictionary<string, DateTime>();
public bool AnalyseFanLi()
{
var title = string.Empty;
Compute compute = null;
var msgid = Guid.NewGuid().ToString();
try
{
var db = ApiClient.GetSession();
var ksinfoTemps = db.FindKsInfoTempGroups();
var ksInfoTemp = ksinfoTemps.FirstOrDefault(f => f.name == e.RobotInfo.name && f.onoff == false);
if (ksInfoTemp == null) return false;
var memberTmp = e.GetMemberinfo();
var message = e.Message.Trim();
//推广位
var pid = string.Empty;
fl_cps_member ks_cps = null;
#region 使广 (广/广/广)
var isDefaultPid = true;
//判断消息是否为群消息并且判断群是否设置了群pid
if (!string.IsNullOrWhiteSpace(e.Groupid))//群pid
{
var groupAdzone = db.FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType. && f.custom_type == PrivateAdzoneCustomType.pid.ToString() && f.group_id == e.Groupid);
if (groupAdzone != null && !string.IsNullOrWhiteSpace(groupAdzone.adzone_pid))
{
pid = groupAdzone.adzone_pid;
ks_cps = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType. && f.username == groupAdzone.adzone_pid_cps_name);
isDefaultPid = false;
}
}
if (isDefaultPid)//私人pid
{
var memberAdzone = db.FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType. && f.custom_type == PrivateAdzoneCustomType.pid.ToString() && f.member_id == memberTmp.id);
if (memberAdzone != null && !string.IsNullOrWhiteSpace(memberAdzone.adzone_pid))//私人推广位
{
pid = memberAdzone.adzone_pid;
ks_cps = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType. && f.username == memberAdzone.adzone_pid_cps_name);
isDefaultPid = false;
}
}
if (isDefaultPid)//默认推广位
{
ks_cps = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType. && f.username == ((memberTmp.buy_point >= Class1.Config.Point && memberTmp.finish_order >= Class1.Config.OrderNum) ? ksInfoTemp.pid_chief_cps_name : ksInfoTemp.pid_deputy_cps_name));//通过判断用户的购物积分,来判断用户使用哪个推广位
pid = ((memberTmp.buy_point >= Class1.Config.Point && memberTmp.finish_order >= Class1.Config.OrderNum) ? ksInfoTemp.pid_chief : ksInfoTemp.pid_deputy);
}
if (ks_cps == null) throw new Exception("快手推广位设置异常,请检测后重试!");
#endregion
if (string.IsNullOrWhiteSpace(pid)) throw new Exception("推广位数据异常,请重新设置推广位");
var api = CpsClient.CreateKuaiShouRequest(ks_cps);
if (api == null) throw new Exception("@创建快手联盟API请求失败");
//商品id
var item_id = KSHelper.GetItemId(message, out title);
#region id搜索
if (string.IsNullOrEmpty(item_id))
return false;
if (!ApiClient.Setting.SystemConfig.message_warning_switch)
e.SendMessage(Class1.Config.SearchingTip);
//商品的详细信息
var itemInfo = api.GetItemInfo(item_id);
if (itemInfo == null || itemInfo.promotionRate == 0)
throw new Exception("商品无佣金");
//店铺ID
var sellerId = itemInfo.mallId.ToString();
if (Class1.Config.SellerIDRestrictlist.Contains(sellerId))
{
e.SendMessage(Class1.Config.SellerIDRestrictTip.Replace("[店铺ID]", sellerId));
return true;
}
//商品标题//&lt; &gt;&amp;&quot;&copy; <>&",©;
itemInfo.goodsTitle = itemInfo.goodsTitle.Replace("", "").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&").Replace("&quot;", "\"").Replace(" &copy;", "©");//这里有个看不见的字符,要将其替换掉
var price = Math.Round((itemInfo.goodsPrice / 100m), 2);
if (itemInfo.zkGoodsPrice != 0)
price = Math.Round((itemInfo.zkGoodsPrice / 100m), 2);
//最高佣金率
var promotionAmount = Math.Round((itemInfo.promotionAmount / 100d), 2);
//用券后 给用户的佣金
var commFee = db.FindItemPoint(memberTmp, promotionAmount * 0.9, 1, CpsType., out compute);
var guid = Guid.NewGuid().ToString("N");
//返利信息入库
var itempointId = db.Insertable(new fl_itempoint1_hist() { guidkey = guid, cpstype = e.RobotInfo.type, robotname = e.RobotInfo.name, uid = memberTmp.id, gid = e.Groupid, text = JsonConvert.SerializeObject(compute) }).ExecuteReturnBigIdentity();
var turnInfo = api.TurnUrl(item_id, pid, guid);
if (turnInfo == null)
throw new Exception("商品转链异常");
var itemUrl = turnInfo.linkUrl;
var kl = turnInfo.shareToken;
#region
var mess = string.Empty;
if (e.ChatType == ChatType. || e.ChatType == ChatType. || (e.ChatType == ChatType.QQ && string.IsNullOrWhiteSpace(Class1.Config.QQSearchSuccessWithoutCouponTip)))
mess = Class1.Config.SearchSuccessWithoutCouponTip;
else
mess = Class1.Config.QQSearchSuccessWithoutCouponTip;
e.SendMessage(mess
.Replace("[商品标题]", itemInfo.goodsTitle)
.Replace("[商品原价]", string.Format("{0:F}", price))
.Replace("[商品主图]", $"[图片={itemInfo.goodsImageUrl}]")
.Replace("[图片地址]", itemInfo.goodsImageUrl)
.Replace("[月销量]", itemInfo.salesTip.ToString())
.Replace("[店铺名称]", itemInfo.mallName)
.Replace("[返利积分]", string.Format("{0:F}", commFee.UserPoint))
.Replace("[购买地址]", itemUrl)
.Replace("[购买口令]", kl)
.Replace("[共节省]", string.Format("{0:F}", commFee.UserPoint))
.Replace("[商品图片]", mess.Contains("[商品图片]") ? "[图片=" + ApiClient.GetQRImage(itemInfo.goodsTitle, price.ToString(), "0", price.ToString(), itemInfo.goodsImageUrl, itemUrl, ApiClient.QrImageType.B, CpsType.) + "]" : string.Empty)
);
#endregion
#region ,,,
db.Insertable(new fl_query_hist()
{
crt_time = DateTime.Now,
type = CpsType.,
itemid = item_id,
groupid = e.Groupid,
robot_name = e.RobotName,
robot_type = e.ChatType,
userid = memberTmp.id,
title = itemInfo.goodsTitle,
adzoneid = pid,
mallid = sellerId,
compute_configdic = compute == null ? string.Empty : JsonConvert.SerializeObject(compute)
}).ExecuteCommand();
db.UpdateRecord(memberTmp.id);
var shared = new Dictionary<string, object>();
shared["msg_type"] = "查询宝贝";
shared["cps_type"] = CpsType.;
shared["msg_username"] = memberTmp.username;
shared["price"] = string.Format("{0:F}", price);
shared["title"] = itemInfo.goodsTitle;
shared["coupon_price"] = "0";
shared["user_point"] = string.Format("{0:F}", commFee.UserPoint);
shared["economize"] = string.Format("{0:F}", commFee.UserPoint);
var sharedEvent = new SharedEvents(shared);
EventClient.OnEvent(sender, sharedEvent);
#endregion
return true;
#endregion
}
catch (Exception ex)
{
if (ex.Message.StartsWith("商品"))
{
var searchUrl = string.Empty;
if (!string.IsNullOrWhiteSpace(title))
{
try
{
var memberTmp = e.GetMemberinfo();
//推广位
var pid = string.Empty;
#region 使广 (广/广/广)
var isDefaultPid = true;
#region pid
if (!string.IsNullOrWhiteSpace(e.Groupid))
{
var groupAdzone = ApiClient.GetSession().FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType. && f.custom_type == PrivateAdzoneCustomType.pid.ToString() && f.group_id == e.Groupid);
if (groupAdzone != null && !string.IsNullOrWhiteSpace(groupAdzone.adzone_pid))
{
pid = groupAdzone.adzone_pid;
isDefaultPid = false;
}
}
#endregion
#region pid
if (isDefaultPid)
{
var memberAdzone = ApiClient.GetSession().FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType. && f.custom_type == PrivateAdzoneCustomType.pid.ToString() && f.member_id == memberTmp.id);
if (memberAdzone != null && !string.IsNullOrWhiteSpace(memberAdzone.adzone_pid))//私人推广位
{
pid = memberAdzone.adzone_pid;
isDefaultPid = false;
}
}
#endregion
if (isDefaultPid)//默认推广位
{
var tgw = Tools.GetTuiguangwei(e.RobotInfo.name, memberTmp);
if (tgw == null || string.IsNullOrWhiteSpace(tgw.Pid)) throw new Exception("@快手推广位异常.请检测后重试!!");
pid = tgw.Pid;
}
#endregion
var pids = pid.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
if (pids == null || pids.Length == 0) throw new Exception("推广位数据异常,请重新设置推广位");
var tb_cps = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType. && f.username == pids[1]);
if (tb_cps == null) throw new Exception("@快手推广位异常,请检测后重试!");
//关键词/标题
var keyword = string.Empty;
//是否是标题
var IsTitle = false;
var api = CpsClient.CreateAlimamaRequest(tb_cps);
if (api == null) throw new Exception("@创建快手API请求失败");
searchUrl = api.ComposeTbCms(pid, keyword);//内置的搜索机制
}
catch (Exception exception)
{ }
}
e.SendMessage(Class1.Config.SearchNoCommissionTip.Replace("[购买地址]", searchUrl)); //提示没有返利提示
var session = ApiClient.GetSession();
session.UpdateRecord(e.GetMemberinfo().id);
var shared = new Dictionary<string, object>();
shared["msg_type"] = "查询宝贝";
shared["cps_type"] = CpsType.;
shared["msg_username"] = e.GetMemberinfo().username;
shared["price"] = "未知";
shared["title"] = "未知";
shared["coupon_price"] = "未知";
shared["user_point"] = "未知";
shared["economize"] = "未知";
var sharedEvent = new SharedEvents(shared);
EventClient.OnEvent(sender, sharedEvent);
return true;
}
if (ex.Message.StartsWith("@"))
plugin.OnLog($"E ({e.RobotInfo.nick}【{e.RobotInfo.name}】){ex.Message.Replace("@", "")}");
else
plugin.OnLog($"E({e.RobotInfo.nick}【{e.RobotInfo.name}】){ex.Message} - {ex.StackTrace}");
plugin.OnLog(ApiClient.Setting.SystemConfig.msg_error);
}
return false;
}
}
}