316 lines
12 KiB
C#
316 lines
12 KiB
C#
|
using System;
|
|||
|
using System.Collections.Concurrent;
|
|||
|
using System.Collections.Generic;
|
|||
|
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 SuningHuiTimer : MyTimer
|
|||
|
{
|
|||
|
public SuningHuiTimer()
|
|||
|
{
|
|||
|
SuningRequest.OrderNoticeEvent += WeipinhuiRequest_OrderNoticeEvent;
|
|||
|
}
|
|||
|
|
|||
|
private void WeipinhuiRequest_OrderNoticeEvent(Lianmeng lianmeng, DateTime start, DateTime end)
|
|||
|
{
|
|||
|
HandleOrder(lianmeng, new SuningRequest(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);
|
|||
|
//HandleOrderLianmeng(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 SuningRequest(lianmeng);
|
|||
|
//上次读取时间
|
|||
|
var lastDateTime = GetLastDateTime(lianmeng.Id + "");
|
|||
|
if (lastDateTime == DateTime.MinValue)
|
|||
|
{
|
|||
|
//如果没有时间,读取最近1个月的数据
|
|||
|
lastDateTime = DateTime.Now.AddDays(-30);
|
|||
|
}
|
|||
|
//本次读取时间
|
|||
|
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, SuningRequest 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 orderCode = item["orderCode"].Value<string>();
|
|||
|
var orderDetailToken = item["orderDetail"].Children().FirstOrDefault();
|
|||
|
//goodsId 商品ID
|
|||
|
var orderId = orderCode;
|
|||
|
var orderTime = item["orderSubmitTime"].Value<DateTime>();
|
|||
|
var lastUpdateTime = item["orderLineStatusChangeTime"].Value<DateTime>();
|
|||
|
var tablenames = db.GetTableName<SnOrder>(orderTime);
|
|||
|
var info = db.Queryable<SnOrder>()
|
|||
|
.Where(w => w.orderCode == orderId)
|
|||
|
.SplitTable(t => t.InTableNames(tablenames))
|
|||
|
.First();
|
|||
|
if (info != null)
|
|||
|
{
|
|||
|
if (lastUpdateTime == info.orderLineStatusChangeTime)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
else if ((int)info.SystemOrderStatus > 3000)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
//Util.CopyToObj(item, info);
|
|||
|
Util.CopyToObj(orderDetailToken, info);
|
|||
|
SetOrderState(db, lianmeng, info.orderLineStatus, info);
|
|||
|
//修改数据
|
|||
|
db.Updateable(info).SplitTable(t => t.InTableNames(tablenames)).ExecuteCommand();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
info = new SnOrder();
|
|||
|
info.Id = Util.CreateID(orderTime);
|
|||
|
info.orderCode = orderId;
|
|||
|
info.LianmengId = lianmeng.Id;
|
|||
|
Util.CopyToObj(orderDetailToken, info);
|
|||
|
SetOrderState(db, lianmeng, info.orderLineStatus, info);
|
|||
|
db.Insertable(info).SplitTable().ExecuteCommand();
|
|||
|
}
|
|||
|
}
|
|||
|
}, ex => Db.OnLog("苏宁订单同步", ex));
|
|||
|
return false;
|
|||
|
});
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 设置订单状态
|
|||
|
/// </summary>
|
|||
|
/// <param name="lianmeng"></param>
|
|||
|
/// <param name="orderLineStatus"></param>
|
|||
|
/// <param name="info"></param>
|
|||
|
private void SetOrderState(SqlSugarClient db, Lianmeng lianmeng, string orderLineStatus, SnOrder info)
|
|||
|
{
|
|||
|
//转成小写
|
|||
|
var arr = orderLineStatus.ToLower().Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
|
|||
|
foreach (var state in arr)
|
|||
|
{
|
|||
|
switch (state)
|
|||
|
{
|
|||
|
case "c"://支付完成
|
|||
|
DocumentaryUser(db, info);
|
|||
|
break;
|
|||
|
case "r"://退款
|
|||
|
SalesReturn(db, lianmeng, info);
|
|||
|
break;
|
|||
|
case "d"://确认收货
|
|||
|
CreateComplete(db, info);
|
|||
|
break;
|
|||
|
case "wb"://待付尾款
|
|||
|
break;
|
|||
|
case "rd"://已退定金
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 跟单用户
|
|||
|
/// </summary>
|
|||
|
/// <param name="info"></param>
|
|||
|
private void DocumentaryUser(SqlSugarClient db, SnOrder info)
|
|||
|
{
|
|||
|
if (info.UserId > 0)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
//通过扩展ID,查到了用户
|
|||
|
if (long.TryParse(info.childAccountId, out var queryId))
|
|||
|
{
|
|||
|
var queryTabName = db.GetTableName<QueryHist>(queryId);
|
|||
|
var 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="lianmeng"></param>
|
|||
|
/// <param name="info"></param>
|
|||
|
private void SalesReturn(SqlSugarClient db, Lianmeng lianmeng, SnOrder info)
|
|||
|
{
|
|||
|
if (info.UserId <= 0)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (info.PayStatus != OrderPayStatus.已支付)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
db.PayPoint(info, info.QueryCache, info.Id, info.productName, true);
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 生成订单完成信息
|
|||
|
/// </summary>
|
|||
|
/// <param name="info"></param>
|
|||
|
private void CreateComplete(SqlSugarClient db, SnOrder info)
|
|||
|
{
|
|||
|
if (info.UserId <= 0)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrWhiteSpace(info.confirmTime))
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (!DateTime.TryParse(info.confirmTime, out var cDateTime))
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (info.payTime <= DateTime.MinValue)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (info.PayStatus == OrderPayStatus.未支付)
|
|||
|
{
|
|||
|
info.PayStatus = OrderPayStatus.支付中;
|
|||
|
var startTime = DateTime.Parse(info.confirmTime);
|
|||
|
var endTime = info.payTime;
|
|||
|
//时间截取
|
|||
|
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<SnOrder>(info.orderSubmitTime);
|
|||
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|