347 lines
17 KiB
C#
347 lines
17 KiB
C#
|
using Common.Models.Enums;
|
|||
|
using Common.Models.SubTables;
|
|||
|
using Common.Models.UnqTables;
|
|||
|
using Common.Requests.Lianmengs;
|
|||
|
using Common.Utils;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using SqlSugar;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.IO;
|
|||
|
using System.Xml.Linq;
|
|||
|
using System.Xml;
|
|||
|
using Common.DbExtends.Extends;
|
|||
|
using Common.Models.SubValueTables;
|
|||
|
using Server.Utils;
|
|||
|
using Server.Configs;
|
|||
|
using Server.MyClass.Caches;
|
|||
|
|
|||
|
namespace Server.Timers
|
|||
|
{
|
|||
|
|
|||
|
public class TbUpdateorderTimer : MyTimer
|
|||
|
{
|
|||
|
|
|||
|
private Dictionary<int, bool> IsUpdateing = new Dictionary<int, bool>();
|
|||
|
private Client client = Client.SingleClient;
|
|||
|
SqlSugar.SqlSugarClient Db { get { return client.Db; } }
|
|||
|
public TbUpdateorderTimer()
|
|||
|
{
|
|||
|
TaobaoRequest.OrderNoticeEvent += TaobaoRequest_OrderNoticeEvent;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private void TaobaoRequest_OrderNoticeEvent(TaobaoRequest req, Newtonsoft.Json.Linq.JToken Orders, TBOrderScene OrderScene, bool IsNext, DateTime EndTime)
|
|||
|
{
|
|||
|
if (Orders != null && Orders.Count() > 0)
|
|||
|
{
|
|||
|
var Db = Client.SingleClient.Db;
|
|||
|
try
|
|||
|
{
|
|||
|
Db.BeginTran();
|
|||
|
foreach (var item in Orders)
|
|||
|
{
|
|||
|
var tk_create_time = DateTime.Parse(item["tk_create_time"].ToString());
|
|||
|
var table_name = Db.GetTableName<TbOrder>(tk_create_time);
|
|||
|
var order = Db.Queryable<TbOrder>()
|
|||
|
.Where(f => f.trade_id == item["trade_id"].ToString() && f.trade_parent_id == item["trade_parent_id"].ToString())
|
|||
|
.SplitTable(t => t.InTableNames(table_name))
|
|||
|
.First();
|
|||
|
|
|||
|
//新订单
|
|||
|
if (order == null)
|
|||
|
{
|
|||
|
order = new TbOrder()
|
|||
|
{
|
|||
|
trade_id = item["trade_id"].ToString(),
|
|||
|
trade_parent_id = item["trade_parent_id"].ToString(),
|
|||
|
SystemOrderStatus = SystemOrderStatus.订单创建
|
|||
|
};
|
|||
|
}
|
|||
|
//跳过已更新的订单
|
|||
|
else if (order.modified_time == item["modified_time"].ToString()) continue;
|
|||
|
//跳过已完成订单(防止重复结算)
|
|||
|
else if ((int)order.SystemOrderStatus > 2000) continue;
|
|||
|
|
|||
|
order.LianmengId = req.Lianmeng.Id;
|
|||
|
Util.CopyToObj(item, order);
|
|||
|
|
|||
|
//查找用户对应关系,并查找用户的查询记录
|
|||
|
if (order.tk_status == TkStatus.订单付款)
|
|||
|
{
|
|||
|
if (order.UserId == 0)
|
|||
|
{
|
|||
|
if (order.special_id != 0)
|
|||
|
{
|
|||
|
var tb_special_table_name = Db.GetTableName<TbSpecial>(req.Lianmeng.Id);
|
|||
|
var tbSpecial = Db.Queryable<TbSpecial>().Where(f => f.SpecialId == order.special_id).SplitTable(t => t.InTableNames(tb_special_table_name)).First();
|
|||
|
if (tbSpecial != null)
|
|||
|
{
|
|||
|
order.UserId = tbSpecial.UserId;
|
|||
|
var pid = $"{order.site_id}_{order.adzone_id}";
|
|||
|
var cache = Db.GetQueryHist(order.UserId, order.item_id, order.LianmengId, pid);
|
|||
|
if (cache != null)//找到查询记录直接复制
|
|||
|
{
|
|||
|
order.QueryCache = cache;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var lastId = order.trade_parent_id.Substring(order.trade_parent_id.Length - 6);
|
|||
|
//通过聊天记录查一次
|
|||
|
var pid = $"{order.site_id}_{order.adzone_id}";
|
|||
|
var cache = Db.GetQueryHist(f => f.ItemId == order.item_id && f.TaobaoLastid == lastId && f.Pid == pid && f.LianmegnId == req.Lianmeng.Id && f.ItemType == LianmengType.淘宝联盟);
|
|||
|
if (cache != null)
|
|||
|
{
|
|||
|
order.QueryCache = cache;
|
|||
|
order.UserId = cache.UserId;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//如果查询出来,只有一条记录
|
|||
|
var cacheTmp = Db.GetQueryHist(f => f.ItemId == order.item_id && f.TaobaoLastid == "" && f.Pid == pid && f.LianmegnId == req.Lianmeng.Id && f.ItemType == LianmengType.淘宝联盟);
|
|||
|
if (cacheTmp != null)
|
|||
|
{
|
|||
|
order.QueryCache = cacheTmp;
|
|||
|
order.UserId = cacheTmp.UserId;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
//首次找到后用户,对他的付款订单数增量
|
|||
|
if (order.UserId != 0)
|
|||
|
{
|
|||
|
var user = Db.GetUserData(order.UserId);
|
|||
|
user.PayOrderCount++;
|
|||
|
Db.Save(user);
|
|||
|
|
|||
|
//可是查询记录没找到,自动创建一条查询记录(不入库),直接保存到ORDER里面,便于记录最后佣金
|
|||
|
if (order.QueryCache == null)
|
|||
|
{
|
|||
|
var userData = Db.GetUserData(order.UserId);
|
|||
|
var robotData = Db.GetRobot(userData.RobotId);
|
|||
|
if (robotData != null)
|
|||
|
{
|
|||
|
var rConfig = Db.GetRebateConfigById(robotData.ConfigRebateId);
|
|||
|
var query = new QueryHist(userData, order.item_id, LianmengType.淘宝联盟, order.LianmengId, $"{order.site_id}_{order.adzone_id}", order.pub_share_pre_fee);
|
|||
|
if (Db.CalculationData(query, rConfig))
|
|||
|
{
|
|||
|
order.QueryCache = query;
|
|||
|
var bConfig = Db.GetBaseConfigById(robotData.ConfigBaseId);
|
|||
|
if (bConfig != null)
|
|||
|
{
|
|||
|
Db.CalculationData(query, bConfig);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else if (order.tk_status == TkStatus.订单失效)
|
|||
|
{
|
|||
|
order.SystemOrderStatus = SystemOrderStatus.订单失效;
|
|||
|
if (order.UserId > 0)
|
|||
|
{
|
|||
|
var UserData = Db.GetUserData(order.UserId);
|
|||
|
UserData.PayOrderCount++;
|
|||
|
Db.Save(UserData);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//对数据库更新
|
|||
|
var orderId = order.Id > 0 ? order.Id : Util.CreateID(order.tk_create_time);
|
|||
|
if (order.Id == 0)
|
|||
|
{
|
|||
|
order.Id = orderId;
|
|||
|
Db.Insertable(order).SplitTable().ExecuteCommand();
|
|||
|
|
|||
|
}
|
|||
|
else Db.Updateable<TbOrder>(order).SplitTable(it => it.InTableNames(table_name)).ExecuteCommand();
|
|||
|
|
|||
|
|
|||
|
|
|||
|
FinishOrder frozen = null;
|
|||
|
//改变状态
|
|||
|
switch (order.tk_status)
|
|||
|
{
|
|||
|
case TkStatus.订单成功:
|
|||
|
case TkStatus.订单结算:
|
|||
|
order.SystemOrderStatus = SystemOrderStatus.订单结算;
|
|||
|
if (order.UserId > 0)
|
|||
|
{
|
|||
|
if (order.PayStatus == OrderPayStatus.未支付)
|
|||
|
{
|
|||
|
order.PayStatus = OrderPayStatus.支付中;
|
|||
|
//时间截取
|
|||
|
var TimeConsuming = DateTime.Parse(order.tk_earning_time) - DateTime.Parse(order.tk_paid_time);
|
|||
|
DateTime ThawingTime = DateTime.Now;
|
|||
|
if (TimeConsuming.TotalDays < 1)
|
|||
|
{
|
|||
|
var pubConfig = Db.GetPubConfig();
|
|||
|
if (pubConfig != null && pubConfig.ExtendDay > 0)
|
|||
|
{
|
|||
|
ThawingTime = DateTime.Parse(order.tk_earning_time).AddDays(pubConfig.ExtendDay);
|
|||
|
}
|
|||
|
}
|
|||
|
frozen = new FinishOrder() { CreateTime = DateTime.Now, LianmengType = LianmengType.淘宝联盟, OrderId = order.Id, ThawingTime = ThawingTime, UserId = order.UserId };
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
break;
|
|||
|
case TkStatus.订单付款:
|
|||
|
order.SystemOrderStatus = SystemOrderStatus.订单付款;
|
|||
|
|
|||
|
break;
|
|||
|
case TkStatus.订单失效:
|
|||
|
order.SystemOrderStatus = SystemOrderStatus.订单失效;
|
|||
|
break;
|
|||
|
case TkStatus.未知:
|
|||
|
break;
|
|||
|
default:
|
|||
|
break;
|
|||
|
}
|
|||
|
Db.Updateable<TbOrder>(order).SplitTable(it => it.InTableNames(table_name)).ExecuteCommand();
|
|||
|
|
|||
|
if (frozen != null) Db.Insertable(frozen).ExecuteCommand();
|
|||
|
|
|||
|
if (order.UserId > 0 && order.QueryCache != null)
|
|||
|
{
|
|||
|
bool IsFreeze = (frozen != null && frozen.ThawingTime > DateTime.Now);
|
|||
|
client.SendClientMsg(order.QueryCache.RobotId, DeviceMessageType.淘宝订单更新, new { OrderId = order.Id, TableName = table_name, IsFreeze = IsFreeze });
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
Db.CommitTran();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Db.RollbackTran();
|
|||
|
Db.OnLog("淘宝订单同步", ex);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (!IsNext)
|
|||
|
{
|
|||
|
var tbConfig = Client.SingleClient.Config.RuntimeCache.TbDatas.FirstOrDefault(f => f.LianmengId == req.Lianmeng.Id);
|
|||
|
switch (OrderScene)
|
|||
|
{
|
|||
|
case TBOrderScene.常规订单:
|
|||
|
tbConfig.UpdateTime1 = EndTime;
|
|||
|
break;
|
|||
|
case TBOrderScene.渠道订单:
|
|||
|
break;
|
|||
|
case TBOrderScene.会员运营订单:
|
|||
|
tbConfig.UpdateTime3 = EndTime;
|
|||
|
break;
|
|||
|
default:
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
public RuntimeCache updateOrderCache { get { return Client.SingleClient.Config.RuntimeCache; } }
|
|||
|
protected override void Run(object state, bool timedOut)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
//第一步:获取可以更新的联盟集合
|
|||
|
var List = Db.Queryable<Lianmeng>().WithCache().Where(f => f.LianmengType == Common.Models.Enums.LianmengType.淘宝联盟 && f.ExpirationTime > DateTime.Now).ToList();
|
|||
|
foreach (var item in List)
|
|||
|
{
|
|||
|
//更新中跳过
|
|||
|
if (IsUpdateing.ContainsKey(item.Id) && IsUpdateing[item.Id]) continue;
|
|||
|
|
|||
|
|
|||
|
//读取配置
|
|||
|
TaobaoUpdateCacheInfo tbConfig;
|
|||
|
tbConfig = Client.SingleClient.Config.RuntimeCache.TbDatas.FirstOrDefault(f => f.LianmengId == item.Id);
|
|||
|
if (tbConfig == null)
|
|||
|
{
|
|||
|
tbConfig = new Configs.TaobaoUpdateCacheInfo() { LianmengId = item.Id };
|
|||
|
updateOrderCache.TbDatas.Add(tbConfig);
|
|||
|
}
|
|||
|
|
|||
|
if (IsUpdateing.ContainsKey(item.Id))
|
|||
|
{
|
|||
|
////60内秒内更新过,跳过
|
|||
|
if (tbConfig.UpdateTimer7.AddSeconds(60) > DateTime.Now) continue;
|
|||
|
}
|
|||
|
|
|||
|
IsUpdateing[item.Id] = true;
|
|||
|
|
|||
|
Task.Factory.StartNew(() =>
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
tbConfig.UpdateTimer7 = DateTime.Now; ;
|
|||
|
|
|||
|
|
|||
|
//第三步:开始更新,并从最后一次更新时间中更新 - 普通订单和会员运营订单
|
|||
|
TaobaoRequest req = new TaobaoRequest(item);
|
|||
|
|
|||
|
foreach (TBOrderScene em in Enum.GetValues(typeof(TBOrderScene)))
|
|||
|
{
|
|||
|
if (em == TBOrderScene.常规订单)
|
|||
|
{
|
|||
|
if (tbConfig.UpdateTime1 > tbConfig.UpdateTimer7)
|
|||
|
{
|
|||
|
//throw new Exception("开始时间不能大于结束时间");
|
|||
|
tbConfig.UpdateTime1= tbConfig.UpdateTimer7;
|
|||
|
}
|
|||
|
var ErrorMsg = req.UpdateOrder(tbConfig.UpdateTime1, tbConfig.UpdateTimer7, em);
|
|||
|
if (!string.IsNullOrEmpty(ErrorMsg))
|
|||
|
{
|
|||
|
Db.OnLog("淘宝订单更新", ErrorMsg, LogType.错误);
|
|||
|
}
|
|||
|
}
|
|||
|
else if (em == TBOrderScene.会员运营订单)
|
|||
|
{
|
|||
|
if (tbConfig.UpdateTime3 > tbConfig.UpdateTimer7)
|
|||
|
{
|
|||
|
//throw new Exception("开始时间不能大于结束时间");
|
|||
|
tbConfig.UpdateTime3 = tbConfig.UpdateTimer7;
|
|||
|
}
|
|||
|
var ErrorMsg = req.UpdateOrder(tbConfig.UpdateTime3, tbConfig.UpdateTimer7, em);
|
|||
|
if (!string.IsNullOrEmpty(ErrorMsg))
|
|||
|
{
|
|||
|
Db.OnLog("淘宝订单更新", ErrorMsg, LogType.错误);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
//client.Event.OnLog($"更新淘宝订单({item.Nickname}({item.Username}))发生错误:{ex.Message}", "淘宝", ex);
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
IsUpdateing[item.Id] = false;
|
|||
|
}
|
|||
|
});
|
|||
|
Thread.Sleep(1000);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|