309 lines
12 KiB
C#
309 lines
12 KiB
C#
|
using System;
|
|||
|
using System.Collections.Concurrent;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Globalization;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Common.DbExtends.Extends;
|
|||
|
using Common.Models.Enums;
|
|||
|
using Common.Models.SubTables;
|
|||
|
using Common.Models.UnqTables;
|
|||
|
using Common.Requests.Lianmengs;
|
|||
|
using Common.Utils;
|
|||
|
using Newtonsoft.Json.Linq;
|
|||
|
using Server.MyClass.Caches;
|
|||
|
using SqlSugar;
|
|||
|
|
|||
|
namespace Server.Timers
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 拼多多
|
|||
|
/// </summary>
|
|||
|
public class PDDOrderTimer : MyTimer
|
|||
|
{
|
|||
|
public PDDOrderTimer()
|
|||
|
{
|
|||
|
PinduoRequest.OrderNoticeEvent += PinduoRequest_OrderNoticeEvent;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 主动刷新订单
|
|||
|
/// </summary>
|
|||
|
/// <param name="lianmeng"></param>
|
|||
|
/// <param name="start"></param>
|
|||
|
/// <param name="end"></param>
|
|||
|
private void PinduoRequest_OrderNoticeEvent(Lianmeng lianmeng, DateTime start, DateTime end)
|
|||
|
{
|
|||
|
this.HandleOrder(lianmeng, new PinduoRequest(lianmeng), start, end);
|
|||
|
}
|
|||
|
|
|||
|
readonly Client Client = Client.SingleClient;
|
|||
|
private SqlSugar.SqlSugarClient Db => Client.Db;
|
|||
|
|
|||
|
private readonly ConcurrentDictionary<int, bool> IsUpdateing = new ConcurrentDictionary<int, bool>();
|
|||
|
|
|||
|
protected override void Run(object state, bool timedOut)
|
|||
|
{
|
|||
|
var lianmengs = this.Db.Queryable<Lianmeng>().Where(w => w.LianmengType == Common.Models.Enums.LianmengType.拼多多联盟 && w.ExpirationTime > DateTime.Now).ToList(); foreach (var lianmeng in lianmengs)
|
|||
|
{
|
|||
|
//防止重复读取
|
|||
|
if (IsUpdateing.ContainsKey(lianmeng.Id))
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
var lastDateTime = GetLastDateTime(lianmeng.Id + "");
|
|||
|
if ((DateTime.Now - lastDateTime).TotalSeconds <= 60)//60秒间隔读取
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
//启动线程读取
|
|||
|
ThreadPool.QueueUserWorkItem((o) =>
|
|||
|
{
|
|||
|
HandleOrderLianmeng(o as Lianmeng);
|
|||
|
}, lianmeng);
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 读取订单
|
|||
|
/// </summary>
|
|||
|
/// <param name="lianmeng"></param>
|
|||
|
private void HandleOrderLianmeng(Lianmeng lianmeng)
|
|||
|
{
|
|||
|
//防止重复读取
|
|||
|
if (IsUpdateing.ContainsKey(lianmeng.Id))
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
//标记防止重复读取
|
|||
|
IsUpdateing[lianmeng.Id] = true;
|
|||
|
try
|
|||
|
{
|
|||
|
var req = new PinduoRequest(lianmeng);
|
|||
|
//上次读取时间
|
|||
|
var lastDateTime = GetLastDateTime(lianmeng.Id + "");
|
|||
|
if (lastDateTime == DateTime.MinValue)
|
|||
|
{
|
|||
|
//如果没有时间,读取最近1个月的数据
|
|||
|
lastDateTime = DateTime.Now.AddDays(-30);
|
|||
|
}
|
|||
|
//req.CreatePosition("测试推广位");
|
|||
|
//本次读取时间
|
|||
|
var datetime = DateTime.Now;
|
|||
|
//处理订单
|
|||
|
HandleOrder(lianmeng, req, lastDateTime, datetime);
|
|||
|
//记录本次读取时间
|
|||
|
SetLastDateTime(lianmeng.Id + "", datetime);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Db.OnLog("拼多多订单同步2", ex);
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
//删除防止重复读取标记
|
|||
|
|
|||
|
IsUpdateing.TryRemove(lianmeng.Id, out _);
|
|||
|
if (lianmeng.ExpirationTime < DateTime.Now)
|
|||
|
{
|
|||
|
Db.Updateable(lianmeng).UpdateColumns(s => new { s.ExpirationTime }).ExecuteCommand();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 处理订单
|
|||
|
/// </summary>
|
|||
|
/// <param name="lianmeng"></param>
|
|||
|
/// <param name="req"></param>
|
|||
|
/// <param name="lastDateTime"></param>
|
|||
|
/// <param name="datetime"></param>
|
|||
|
private void HandleOrder(Lianmeng lianmeng, PinduoRequest req, DateTime lastDateTime, DateTime datetime)
|
|||
|
{
|
|||
|
lastDateTime = lastDateTime.AddMinutes(-1);
|
|||
|
req.OrderQuery(lastDateTime, datetime, (items) =>
|
|||
|
{
|
|||
|
var db = Db;
|
|||
|
db.UseTran(() =>
|
|||
|
{
|
|||
|
foreach (var item in items)
|
|||
|
{
|
|||
|
var orderId = item["order_id"].Value<string>();
|
|||
|
var createtime = Util.TimespanToDatatime(item["order_create_time"].Value<string>());
|
|||
|
var modifyTime = item["order_modify_at"].Value<long>();
|
|||
|
var orderStatus = (PDDOrderStatusEnum)item["order_status"].Value<int>();
|
|||
|
var tablenames = db.GetTableName<PddOrder>(createtime);
|
|||
|
var info = db.Queryable<PddOrder>().Where(w => w.order_id == orderId).SplitTable(t => t.InTableNames(tablenames)).First();
|
|||
|
if (info != null)
|
|||
|
{
|
|||
|
if (info.order_modify_at == modifyTime)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
else if ((int)info.SystemOrderStatus > 3000)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
//复制对象
|
|||
|
Util.CopyToObj(item, info);
|
|||
|
//设置订单状态
|
|||
|
SetOrderState(db, lianmeng, orderStatus, info);
|
|||
|
//修改数据
|
|||
|
db.Updateable(info).SplitTable(t => t.InTableNames(tablenames)).ExecuteCommand();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
info = new PddOrder();
|
|||
|
info.Id = Util.CreateID(createtime);
|
|||
|
info.LianmengId = lianmeng.Id;
|
|||
|
Util.CopyToObj(item, info);
|
|||
|
SetOrderState(db, lianmeng, orderStatus, info);
|
|||
|
db.Insertable(info).SplitTable().ExecuteCommand();
|
|||
|
}
|
|||
|
}
|
|||
|
}, ex => db.OnLog("拼多多订单同步", ex));
|
|||
|
return false;
|
|||
|
});
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 设置订单状态
|
|||
|
/// </summary>
|
|||
|
/// <param name="lianmeng"></param>
|
|||
|
/// <param name="orderStatus"></param>
|
|||
|
/// <param name="info"></param>
|
|||
|
private void SetOrderState(SqlSugarClient db, Lianmeng lianmeng, PDDOrderStatusEnum orderStatus, PddOrder info)
|
|||
|
{
|
|||
|
switch (orderStatus)
|
|||
|
{
|
|||
|
case PDDOrderStatusEnum.已支付:
|
|||
|
info.SystemOrderStatus = SystemOrderStatus.订单付款;
|
|||
|
DocumentaryUser(db, info);
|
|||
|
break;
|
|||
|
case PDDOrderStatusEnum.已成团:
|
|||
|
info.SystemOrderStatus = SystemOrderStatus.订单付款;
|
|||
|
DocumentaryUser(db, info);
|
|||
|
break;
|
|||
|
case PDDOrderStatusEnum.确认收货:
|
|||
|
info.SystemOrderStatus = SystemOrderStatus.订单收货;
|
|||
|
CreateComplete(db, info);
|
|||
|
break;
|
|||
|
case PDDOrderStatusEnum.审核成功:
|
|||
|
info.SystemOrderStatus = SystemOrderStatus.订单收货;
|
|||
|
CreateComplete(db, info);
|
|||
|
break;
|
|||
|
case PDDOrderStatusEnum.审核失败:
|
|||
|
info.SystemOrderStatus = SystemOrderStatus.订单失效;
|
|||
|
break;
|
|||
|
case PDDOrderStatusEnum.已经结算:
|
|||
|
info.SystemOrderStatus = SystemOrderStatus.订单结算;
|
|||
|
CreateComplete(db, info);
|
|||
|
break;
|
|||
|
case PDDOrderStatusEnum.已处罚:
|
|||
|
info.SystemOrderStatus = SystemOrderStatus.订单失效;
|
|||
|
break;
|
|||
|
case PDDOrderStatusEnum.没有佣金:
|
|||
|
info.SystemOrderStatus = SystemOrderStatus.订单失效;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 跟单用户
|
|||
|
/// </summary>
|
|||
|
/// <param name="info"></param>
|
|||
|
private void DocumentaryUser(SqlSugarClient db, PddOrder info)
|
|||
|
{
|
|||
|
if (info.UserId > 0)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
//通过扩展ID,查到了用户
|
|||
|
if (long.TryParse(info.custom_parameters, out var queryId))
|
|||
|
{
|
|||
|
var queryTabName = db.GetTableName<QueryHist>(queryId);
|
|||
|
QueryHist cache = db.Queryable<QueryHist>().SplitTable(tab => tab.InTableNames(queryTabName))
|
|||
|
.Single(f => f.Id == queryId);
|
|||
|
if (cache != null)
|
|||
|
{
|
|||
|
info.UserId = cache.UserId;
|
|||
|
info.QueryCache = cache;
|
|||
|
var user = db.GetUserData(info.UserId);
|
|||
|
user.PayOrderCount++;
|
|||
|
db.Save(user);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 生成订单完成信息
|
|||
|
/// </summary>
|
|||
|
/// <param name="info"></param>
|
|||
|
private void CreateComplete(SqlSugarClient db, PddOrder info)
|
|||
|
{
|
|||
|
if (info.UserId <= 0)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (info.order_receive_time <= 0)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (info.order_pay_time <= DateTime.MinValue)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (info.PayStatus == OrderPayStatus.未支付)
|
|||
|
{
|
|||
|
info.PayStatus = OrderPayStatus.支付中;
|
|||
|
var startTime = Util.TimespanToDatatime(info.order_receive_time + "");
|
|||
|
var endTime = Util.TimespanToDatatime(info.order_pay_time + "");
|
|||
|
//时间截取
|
|||
|
var timeConsuming = startTime - endTime;
|
|||
|
DateTime thawingTime = DateTime.Now;
|
|||
|
if (timeConsuming.TotalDays < 1)
|
|||
|
{
|
|||
|
var pubConfig = Db.GetPubConfig();
|
|||
|
if (pubConfig != null && pubConfig.ExtendDay > 0)
|
|||
|
{
|
|||
|
thawingTime = startTime.AddDays(pubConfig.ExtendDay);
|
|||
|
}
|
|||
|
}
|
|||
|
var frozen = new FinishOrder()
|
|||
|
{
|
|||
|
CreateTime = DateTime.Now,
|
|||
|
LianmengType = LianmengType.拼多多联盟,
|
|||
|
OrderId = info.Id,
|
|||
|
ThawingTime = thawingTime,
|
|||
|
UserId = info.UserId
|
|||
|
};
|
|||
|
db.Insertable(frozen).ExecuteCommand();
|
|||
|
//发送通知
|
|||
|
var tablenames = db.GetTableName<PddOrder>(info.order_create_time);
|
|||
|
if (info.UserId > 0 && info.QueryCache != null)
|
|||
|
{
|
|||
|
bool IsFreeze = (frozen.ThawingTime > DateTime.Now);
|
|||
|
Client.SendClientMsg(info.QueryCache.RobotId, DeviceMessageType.拼多多订单更新, new { OrderId = info.Id, TableName = tablenames, IsFreeze = IsFreeze });
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 获取上次时间
|
|||
|
/// </summary>
|
|||
|
/// <param name="id"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private DateTime GetLastDateTime(string id)
|
|||
|
{
|
|||
|
return Client.Config.RuntimeCache.GetPublicValue("订单同步" + id, () => DateTime.MinValue);
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 记录上次时间
|
|||
|
/// </summary>
|
|||
|
/// <param name="id"></param>
|
|||
|
/// <param name="now"></param>
|
|||
|
private void SetLastDateTime(string id, DateTime now)
|
|||
|
{
|
|||
|
Client.Config.RuntimeCache.SetPublicValue("订单同步" + id, now);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|