480 lines
25 KiB
C#
480 lines
25 KiB
C#
using Api.Framework;
|
||
using Api.Framework.Model;
|
||
using Api.Framework.SDK;
|
||
using Api.Framework.Tools;
|
||
using Chat.Framework;
|
||
using Chat.Framework.WXSdk.Implement;
|
||
using SignContacts.Entitys;
|
||
using SignContacts.Enums;
|
||
using SignContacts.Properties;
|
||
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace SignContacts
|
||
{
|
||
public class Class1 : Plugin
|
||
{
|
||
|
||
public Class1()
|
||
{
|
||
this.Name = Resources.PluginName;
|
||
this.Note = Resources.PluginNote;
|
||
this.Logo = Resources.标记;
|
||
}
|
||
|
||
#region 自定义变量
|
||
public static Config Config;
|
||
private MainForm mainForm = null;
|
||
#endregion
|
||
|
||
public override void Start()
|
||
{
|
||
try
|
||
{
|
||
var session = ApiClient.GetSession();
|
||
#region 判断表是否存在,不存在创建表
|
||
if (!session.TableExist<fl_plugin_signcontacts_sign_info>())
|
||
{
|
||
session.CreateTable<fl_plugin_signcontacts_sign_info>();
|
||
session.AddUnique<fl_plugin_signcontacts_sign_info>("signtype", "amount");
|
||
|
||
session.Insertable(new List<fl_plugin_signcontacts_sign_info>(){
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.好友天数,amount = 5,sign = "C"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.好友天数,amount = 15,sign = "B"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.好友天数,amount = 99999,sign = "A"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.推荐人数,amount = 5,sign = "▲"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.推荐人数,amount = 15,sign = "★"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.推荐人数,amount = 99999,sign = "◆"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.查询次数,amount = 5,sign = "。"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.查询次数,amount = 50,sign = "o"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.查询次数,amount = 99999,sign = "O"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.订单总数,amount = 5,sign = "M"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.订单总数,amount = 50,sign = "K"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.订单总数,amount = 99999,sign = "P"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.退款总数,amount = 20,sign = "Υ"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.退款总数,amount = 100,sign = "γ"},
|
||
new fl_plugin_signcontacts_sign_info() { signtype = SignType.退款总数,amount = 99999,sign = "Ψ"}
|
||
}).ExecuteCommand();
|
||
}
|
||
#endregion
|
||
|
||
//创建配置文件
|
||
Config = this.ReadConfig<Config>();
|
||
SDK.WXNewFriendEvent += SDK_WXNewFriendEvent;
|
||
SDK.OrderNoticeEvent += SDK_OrderNoticeEvent;
|
||
SDK.SharedEvent += SDK_SharedEvent;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog(ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新加好友
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void SDK_WXNewFriendEvent(object sender, Chat.Framework.WXSdk.Events.WXNewFriend e)
|
||
{
|
||
var session = ApiClient.GetSession();
|
||
var member = session.Find<fl_member_info>("select * from fl_member_info where username = @username", new { username = e.Friend.UserName }).FirstOrDefault();
|
||
if (member == null)
|
||
{
|
||
member = new fl_member_info() { crt_time = DateTime.Now, usernick = e.Friend.NickName, realnick = e.Friend.NickName, robot_name = e.Client.WeixinHao, robot_type = e.Client.WeixinType == WeixinType.QYHook微信 ? ChatType.企业微信 : ChatType.微信, username = e.Friend.UserName, alipay_name = string.Empty, alipay_num = string.Empty, remark = string.Empty };
|
||
member = session.Saveable(member).ExecuteReturnEntity();
|
||
}
|
||
NewUsername(e.Client, member, UserType.新用户);
|
||
}
|
||
|
||
private void SDK_SharedEvent(object sender, Api.Framework.Events.SharedEvents e)
|
||
{
|
||
try
|
||
{
|
||
if (Config.NotesSwitch == Api.Framework.Enums.SwitchType.开启)
|
||
{
|
||
if (e.Shareds != null && e.Shareds.ContainsKey("msg_type") && e.Shareds["msg_type"].ToString() == "查询宝贝")
|
||
{
|
||
var wx = sender as WeixinBase;
|
||
if (wx == null) return;
|
||
|
||
var session = ApiClient.GetSession();
|
||
string username = e.Shareds["msg_username"].ToString();
|
||
|
||
var member = session.Find<fl_member_info>("select * from fl_member_info where username = @username", new { username = username }).FirstOrDefault();
|
||
if (member == null) return;
|
||
|
||
//if (!string.IsNullOrWhiteSpace(Config.NotesFormat_QueryNotOrder))
|
||
//{
|
||
// #region 首次查询
|
||
// var incomeResult = session.FindSingle<fl_statistics_record>(@"select * from fl_statistics_record where uid = @userid and querynum = 1", new { userid = member.id });
|
||
// if (incomeResult != null)
|
||
// {
|
||
// if (member.bind_order != 0 || member.finish_order != 0) return;
|
||
// var orderCount = 0;
|
||
// orderCount += session.Find<fl_order_alimama>("select * from fl_order_alimama where db_userid = @db_userid", new { db_userid = member.id }).Count;
|
||
// if (orderCount == 0)
|
||
// {
|
||
// orderCount += session.Find<fl_order_jingdong>("select * from fl_order_jingdong where db_userid = @db_userid", new { db_userid = member.id }).Count;
|
||
// if (orderCount == 0)
|
||
// orderCount += session.Find<fl_order_pinduoduo>("select * from fl_order_pinduoduo where db_userid = @db_userid", new { db_userid = member.id }).Count;
|
||
// }
|
||
// if (orderCount == 0)
|
||
// {
|
||
// NewUsername(wx, member, UserType.查询未下单);
|
||
// return;
|
||
// }
|
||
// }
|
||
// #endregion
|
||
//}
|
||
|
||
NewUsername(wx, member, UserType.未知);
|
||
|
||
//long inviter_id = 0;
|
||
//string inviter_username = string.Empty;
|
||
|
||
//var notes = _ProcessStr(out inviter_id, out inviter_username, uid: member.id);
|
||
//if (!string.IsNullOrWhiteSpace(notes) && member.usernick != notes)
|
||
//{
|
||
// wx.EditContacts(e.Shareds["msg_username"].ToString(), EditContactsType.备注, notes);
|
||
// member.usernick = notes;
|
||
// session.SaveOrUpdate(member);
|
||
//}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog($"{ex.Message} - {ex.StackTrace}.");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 订单通知接口
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void SDK_OrderNoticeEvent(object sender, Api.Framework.Events.OrderNoticeEvent e)
|
||
{
|
||
try
|
||
{
|
||
if (Config.NotesSwitch == Api.Framework.Enums.SwitchType.开启)
|
||
{
|
||
if (e.Customer == null && e.Member != null)
|
||
{
|
||
var wx = ChatClient.WXClient.FirstOrDefault(f => f.Key == e.Member.robot_name).Value;
|
||
if (wx == null || wx.Status != Chat.Framework.WXSdk.WxStatus.在线)
|
||
return;
|
||
|
||
if (e.Member.bind_order == 1)
|
||
NewUsername(wx, e.Member, UserType.首单用户);
|
||
else
|
||
{
|
||
var session = ApiClient.GetSession();
|
||
|
||
var systemOrderStatus = -1;
|
||
if (e.Order is fl_order_alimama)
|
||
systemOrderStatus = (int)(e.Order as fl_order_alimama).db_status;
|
||
if (e.Order is fl_order_pinduoduo)
|
||
systemOrderStatus = (int)(e.Order as fl_order_pinduoduo).db_status;
|
||
if (e.Order is fl_order_jingdong)
|
||
systemOrderStatus = (int)(e.Order as fl_order_jingdong).db_status;
|
||
if (e.Order is fl_order_weipinhui)
|
||
systemOrderStatus = (int)(e.Order as fl_order_weipinhui).db_status;
|
||
if (systemOrderStatus == 1002 || systemOrderStatus == 998 || systemOrderStatus == 999 || systemOrderStatus == 1003)
|
||
{
|
||
NewUsername(wx, e.Member, UserType.常规用户);
|
||
//long inviter_id = 0;
|
||
//string inviter_username = string.Empty;
|
||
//var notes = _ProcessStr(out inviter_id, out inviter_username, username: e.Member.username);
|
||
//if (!string.IsNullOrWhiteSpace(notes) && e.Member.usernick != notes)
|
||
//{
|
||
// wx.EditContacts(e.Member.username, EditContactsType.备注, notes);
|
||
// e.Member.usernick = notes;
|
||
// session.SaveOrUpdate(e.Member);
|
||
//}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog($"{ex.Message} - {ex.StackTrace}");
|
||
}
|
||
}
|
||
|
||
public void NewUsername(WeixinBase wx, fl_member_info member, UserType userType)
|
||
{
|
||
try
|
||
{
|
||
if (Config.NotesSwitch == Api.Framework.Enums.SwitchType.开启 && !(string.IsNullOrWhiteSpace(Config.NotesFormat) && string.IsNullOrWhiteSpace(Config.NotesFormat_FirstOrder) && string.IsNullOrWhiteSpace(Config.NotesFormat_NewFirend) && string.IsNullOrWhiteSpace(Config.NotesFormat_QueryNotOrder)))
|
||
{
|
||
var session = ApiClient.GetSession();
|
||
var format = string.Empty;
|
||
switch (userType)
|
||
{
|
||
case UserType.新用户:
|
||
format = Config.NotesFormat_NewFirend;
|
||
break;
|
||
case UserType.查询未下单:
|
||
format = Config.NotesFormat_QueryNotOrder;
|
||
break;
|
||
case UserType.首单用户:
|
||
format = Config.NotesFormat_FirstOrder;
|
||
break;
|
||
case UserType.常规用户:
|
||
format = Config.NotesFormat;
|
||
break;
|
||
case UserType.未知:
|
||
{
|
||
#region 判断是查询过
|
||
var incomeResult = session.FindSingle<fl_statistics_record>(@"select * from fl_statistics_record where uid = @userid", new { userid = member.id });
|
||
if (incomeResult != null)
|
||
{
|
||
if (incomeResult.querynum == 0)
|
||
format = Config.NotesFormat_NewFirend;
|
||
else
|
||
{
|
||
if (member.bind_order <= 0)
|
||
format = Config.NotesFormat_QueryNotOrder;
|
||
else if (member.bind_order == 1)
|
||
format = Config.NotesFormat_FirstOrder;
|
||
else
|
||
format = Config.NotesFormat;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (member.bind_order <= 0)
|
||
format = Config.NotesFormat_NewFirend;
|
||
else if (member.bind_order == 1)
|
||
format = Config.NotesFormat_FirstOrder;
|
||
else if (member.bind_order > 1)
|
||
format = Config.NotesFormat;
|
||
//else
|
||
// format = Config.NotesFormat_QueryNotOrder;
|
||
}
|
||
#endregion
|
||
}
|
||
break;
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(format) && !string.IsNullOrWhiteSpace(Config.NotesFormat))
|
||
format = Config.NotesFormat;
|
||
|
||
//if (string.IsNullOrWhiteSpace(format))
|
||
// return;
|
||
|
||
long inviter_id = 0;
|
||
string inviter_username = string.Empty;
|
||
|
||
var notes = string.Empty;
|
||
//var member = session.Find<fl_member_info>("select * from fl_member_info where username = @username", new { username = username }).FirstOrDefault();
|
||
if (member != null)
|
||
{
|
||
notes = _ProcessStr(format, out inviter_id, out inviter_username, uid: member.id);
|
||
if (!string.IsNullOrWhiteSpace(notes) && member.usernick != notes)
|
||
{
|
||
this.OnLog($"{member.usernick}({member.username}) => {notes}");
|
||
wx.EditContacts(member.username, EditContactsType.备注, notes);
|
||
member.usernick = notes;
|
||
session.SaveOrUpdate(member);
|
||
Thread.Sleep(1000);
|
||
}
|
||
else
|
||
{
|
||
this.OnLog($"{member.usernick}({member.username}) => {notes},和之前一样");
|
||
}
|
||
}
|
||
|
||
//if (inviter_id != 0 && !string.IsNullOrWhiteSpace(inviter_username))
|
||
//{
|
||
// var inviter_member = session.Find<fl_member_info>("select * from fl_member_info where username = @username", new { username = inviter_username }).FirstOrDefault();
|
||
// if (inviter_member == null) return;
|
||
|
||
// notes = _ProcessStr(format, out inviter_id, out inviter_username, uid: inviter_member.id);
|
||
// if (string.IsNullOrWhiteSpace(notes) || inviter_member.usernick == notes)
|
||
// return;
|
||
// Thread.Sleep(1000);
|
||
// wx.EditContacts(inviter_username, EditContactsType.备注, notes);
|
||
// inviter_member.usernick = notes;
|
||
// session.SaveOrUpdate(inviter_member);
|
||
//}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog($"{ex.Message} . {ex.StackTrace}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 标签/备注 字符串替换方法
|
||
/// </summary>
|
||
/// <param name="format">标签/备注 模板</param>
|
||
/// <param name="username">用户wxid</param>
|
||
/// <returns></returns>
|
||
private string _ProcessStr(string format, out long inviter_id, out string inviter_username, string username = "", long uid = 0)
|
||
{
|
||
inviter_id = 0;
|
||
inviter_username = string.Empty;
|
||
if (string.IsNullOrWhiteSpace(format))
|
||
return string.Empty;
|
||
|
||
try
|
||
{
|
||
var session = ApiClient.GetSession();
|
||
fl_member_info member = null;
|
||
if (!string.IsNullOrWhiteSpace(username))
|
||
member = session.Find<fl_member_info>("select * from fl_member_info where username = @username", new { username = username }).FirstOrDefault();
|
||
else if (uid != 0)
|
||
member = session.Find<fl_member_info>("select * from fl_member_info where id = @id", new { id = uid }).FirstOrDefault();
|
||
if (member == null)
|
||
return string.Empty;
|
||
|
||
var days = (DateTime.Now.Date - member.crt_time.Date).Days;
|
||
|
||
#region 查询次数/推荐人数/完成订单数(3)/退款数量(3)
|
||
|
||
decimal query_amount = 0m;//查询次数
|
||
decimal invite_amount = 0m;//推荐人数
|
||
decimal order_amount = 0m;//完成订单数
|
||
decimal refund_amount = 0m;//退款订单数
|
||
|
||
var incomeResult = session.FindTable(
|
||
//@"select querynum as num from fl_statistics_record where uid = @userid
|
||
@"select COALESCE(sum(querynum),0) as num from fl_statistics_record where uid = @userid
|
||
UNION ALL
|
||
select count(id) as num from fl_member_info where inviter_id = @userid
|
||
UNION ALL
|
||
select COALESCE(sum(item_num),0) as num from fl_order_alimama where db_userid = @userid and db_status not in (1006,1004)
|
||
UNION ALL
|
||
select COALESCE(sum(skuNum),0) as num from fl_order_jingdong where db_userid = @userid and db_status not in (1006,1004)
|
||
UNION ALL
|
||
select COALESCE(sum(goods_quantity),0) as num from fl_order_pinduoduo where db_userid = @userid and db_status not in (1006,1004)
|
||
UNION ALL
|
||
select COALESCE(sum(item_num),0) as num from fl_order_alimama where db_userid = @userid and db_status in (999,998,1003)
|
||
UNION ALL
|
||
select COALESCE(sum(skuNum),0) as num from fl_order_jingdong where db_userid = @userid and db_status in (999,998,1003)
|
||
UNION ALL
|
||
select COALESCE(sum(goods_quantity),0) as num from fl_order_pinduoduo where db_userid = @userid and db_status in (999,998,1003) ", new { userid = member.id });
|
||
if (incomeResult.Rows.Count != 0)
|
||
{
|
||
query_amount = decimal.Parse(incomeResult.Rows[0]["num"].ToString());
|
||
invite_amount = decimal.Parse(incomeResult.Rows[1]["num"].ToString());
|
||
|
||
order_amount = decimal.Parse(incomeResult.Rows[2]["num"].ToString()) + decimal.Parse(incomeResult.Rows[3]["num"].ToString()) + decimal.Parse(incomeResult.Rows[4]["num"].ToString());
|
||
var ss = incomeResult.Rows[7];
|
||
refund_amount = decimal.Parse(incomeResult.Rows[5]["num"].ToString()) + decimal.Parse(incomeResult.Rows[6]["num"].ToString()) + decimal.Parse(incomeResult.Rows[7]["num"].ToString());
|
||
}
|
||
#endregion
|
||
|
||
//查询次数/推荐人数/完成订单数/退款数量
|
||
var signQuery = session.FindSignInfo(SignType.查询次数, (long)query_amount);
|
||
var signInvite = session.FindSignInfo(SignType.推荐人数, (long)invite_amount);
|
||
var signOrder = session.FindSignInfo(SignType.订单总数, (long)order_amount);
|
||
var signRefund = session.FindSignInfo(SignType.退款总数, (long)refund_amount);
|
||
var signDays = session.FindSignInfo(SignType.好友天数, days);
|
||
inviter_id = member.inviter_id;
|
||
inviter_username = member.username;
|
||
|
||
//var format = string.Empty;
|
||
|
||
//if (Config.NotesSwitch == Api.Framework.Enums.SwitchType.开启 && !(string.IsNullOrWhiteSpace(Config.NotesFormat) && string.IsNullOrWhiteSpace(Config.NotesFormat_FirstOrder) && string.IsNullOrWhiteSpace(Config.NotesFormat_NewFirend) && string.IsNullOrWhiteSpace(Config.NotesFormat_QueryNotOrder)))
|
||
|
||
|
||
//if (order_amount <= 0 && !string.IsNullOrWhiteSpace(Config.NotesFormat_NotOrder))
|
||
// format = Config.NotesFormat_NotOrder;
|
||
//if (string.IsNullOrWhiteSpace(format) && !string.IsNullOrWhiteSpace(Config.NotesFormat))
|
||
// format = Config.NotesFormat;
|
||
|
||
//if (string.IsNullOrWhiteSpace(format))
|
||
// return string.Empty;
|
||
|
||
|
||
|
||
#region 最后下单时间
|
||
var last_buy_time = DateTime.MinValue;
|
||
if (format.Contains("[最后下单"))
|
||
{
|
||
var tb_data = session.Queryable<fl_order_alimama>().OrderBy(f => f.create_time, SqlSugar.OrderByType.Desc).Where(f => f.db_userid == member.id).First();
|
||
var jd_data = session.Queryable<fl_order_jingdong>().OrderBy(f => f.orderTime, SqlSugar.OrderByType.Desc).Where(f => f.db_userid == member.id).First();
|
||
var pdd_data = session.Queryable<fl_order_pinduoduo>().OrderBy(f => f.order_create_time, SqlSugar.OrderByType.Desc).Where(f => f.db_userid == member.id).First();
|
||
var wph_data = session.Queryable<fl_order_weipinhui>().OrderBy(f => f.orderTime, SqlSugar.OrderByType.Desc).Where(f => f.db_userid == member.id).First();
|
||
if (tb_data != null)
|
||
last_buy_time = last_buy_time < tb_data.create_time ? tb_data.create_time : last_buy_time;
|
||
if (jd_data != null)
|
||
last_buy_time = last_buy_time < jd_data.orderTime ? jd_data.orderTime : last_buy_time;
|
||
if (pdd_data != null)
|
||
last_buy_time = last_buy_time < pdd_data.order_create_time ? pdd_data.order_create_time : last_buy_time;
|
||
if (wph_data != null)
|
||
last_buy_time = last_buy_time < Util.GetDateTime(wph_data.orderTime) ? Util.GetDateTime(wph_data.orderTime) : last_buy_time;
|
||
}
|
||
#endregion
|
||
|
||
return format
|
||
.Replace("[编号]", member.id.ToString())
|
||
.Replace("[天数]", signDays.sign)
|
||
.Replace("[查询]", signQuery.sign)
|
||
.Replace("[订单]", signOrder.sign)
|
||
.Replace("[退款]", signRefund.sign)
|
||
.Replace("[推荐]", signInvite.sign)
|
||
.Replace("[昵称]", member.realnick)
|
||
.Replace("[上级编码]", member.inviter_id.ToString())
|
||
.Replace("[最后下单时间]", (last_buy_time == DateTime.MinValue ? "" : last_buy_time.ToString("yyyy-MM-dd HH:mm:ss")))
|
||
.Replace("[最后下单年]", (last_buy_time == DateTime.MinValue ? "" : last_buy_time.ToString("yyyy")))
|
||
.Replace("[最后下单月]", (last_buy_time == DateTime.MinValue ? "" : last_buy_time.ToString("MM")))
|
||
.Replace("[最后下单日]", (last_buy_time == DateTime.MinValue ? "" : last_buy_time.ToString("dd")))
|
||
.Replace("[好友注册时间]", (member.crt_time == DateTime.MinValue ? "" : member.crt_time.ToString("yyyy-MM-dd HH:mm:ss")))
|
||
.Replace("[好友注册年]", (member.crt_time == DateTime.MinValue ? "" : member.crt_time.ToString("yyyy")))
|
||
.Replace("[好友注册月]", (member.crt_time == DateTime.MinValue ? "" : member.crt_time.ToString("MM")))
|
||
.Replace("[好友注册日]", (member.crt_time == DateTime.MinValue ? "" : member.crt_time.ToString("dd")));
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
return string.Empty;
|
||
}
|
||
|
||
public override void ShowForm()
|
||
{
|
||
try
|
||
{
|
||
if (mainForm == null || mainForm.IsDisposed)
|
||
{
|
||
mainForm = new MainForm();
|
||
mainForm.Show();
|
||
}
|
||
mainForm.TopMost = true;
|
||
mainForm.TopMost = false;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog(ex.Message);
|
||
}
|
||
}
|
||
|
||
public override void Stop()
|
||
{
|
||
try
|
||
{
|
||
if (mainForm != null)
|
||
{
|
||
mainForm.CloseForm();
|
||
mainForm = null;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog(ex.Message);
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|