262 lines
10 KiB
C#
262 lines
10 KiB
C#
using Common.Models;
|
||
using Common.Models.UnqTables;
|
||
using Common.Utils;
|
||
using Microsoft.Owin.Hosting;
|
||
using Server.MyClass.Class;
|
||
using Server.Timers;
|
||
using Server.Winforms;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Common.DbExtends;
|
||
using Common.Requests.Lianmengs;
|
||
using Common.DbExtends.Extends;
|
||
using System.Threading;
|
||
using Common.Models.PubClass;
|
||
using Common.Models.Enums;
|
||
using Server.MyClass.Caches;
|
||
using Server.Events;
|
||
using Server.Services.DataMigration;
|
||
|
||
namespace Server
|
||
{
|
||
internal class Client
|
||
{
|
||
public EventManager Events { get; private set; }
|
||
|
||
private static Client _client;
|
||
public static Client SingleClient { get { if (_client == null) _client = new Client(); return _client; } }
|
||
|
||
public string InternetIP { get; private set; }
|
||
public Config Config { get; private set; }
|
||
|
||
public OwinServer OwinServer { get; private set; }
|
||
public string Url { get; set; }
|
||
private SessionTimer DeviceSessionTimer = null;
|
||
private TbUpdateorderTimer TbUpdateorderTimer = null;
|
||
private TbSpecialTimer TbSpecialTimer = null;
|
||
private OldUserActivationTimer OldUserActivationTimer = null;
|
||
private FinishOrderTimer FinishOrderTimer = null;
|
||
private MtOrderTimer MtOrderTimer = null;
|
||
private JDOrderTimer JDOrderTimer = null;
|
||
private PDDOrderTimer PDDOrderTimer = null;
|
||
private TbUpdateRefundOrderTimer TbUpdateRefundOrderTimer = null;
|
||
private UpdateCookiesTimer UpdateCookiesTimer = null;
|
||
private WeiPinHuiTimer WeiPinHuiTimer = null;
|
||
private SystemTimer SystemTimer = null;
|
||
/// <summary>
|
||
/// 收到的消息
|
||
/// </summary>
|
||
public Dictionary<string, DeviceReplyMessage> ReplyMessages { get; set; }
|
||
private Client()
|
||
{
|
||
Config = new Config();
|
||
InternetIP = Util.QueryInternetIP();
|
||
}
|
||
public bool IsDebug { get; private set; }
|
||
|
||
public void InitSystem()
|
||
{
|
||
this.Db.SetDefaultConnection();
|
||
this.Db.InitTables();
|
||
this.Db.InitDefaultData();
|
||
this.Config.RuntimeCache = Db.GetMapValue<RuntimeCache>("系统缓存", () => new RuntimeCache());
|
||
this.Events = new EventManager(this);
|
||
OwinServer = new OwinServer();
|
||
//MtOrderTimer = MyTimer.NewTimer<MtOrderTimer>(60);
|
||
JDOrderTimer = MyTimer.NewTimer<JDOrderTimer>(60);
|
||
//PDDOrderTimer = MyTimer.NewTimer<PDDOrderTimer>(60);
|
||
//WeiPinHuiTimer = MyTimer.NewTimer<WeiPinHuiTimer>(60);
|
||
//OldUserActivationTimer = MyTimer.NewTimer<OldUserActivationTimer>(60 * 60);
|
||
ReplyMessages = new Dictionary<string, DeviceReplyMessage>();
|
||
//UpdateCookiesTimer = MyTimer.NewTimer<UpdateCookiesTimer>(60);
|
||
//TbUpdateRefundOrderTimer = MyTimer.NewTimer<TbUpdateRefundOrderTimer>(60 * 60);
|
||
//MyTimer.NewTimer<TbUpdateRefundOrderTimer>(1);
|
||
//MyTimer.NewTimer<SuningHuiTimer>(60);
|
||
//同步黑名单
|
||
//MyTimer.NewTimer<BlackTimer>(1);
|
||
//同步商品黑名单
|
||
//MyTimer.NewTimer<GoodsBlackTimer>(1);
|
||
//同步商店黑名单
|
||
//MyTimer.NewTimer<StoreBlackTimer>(60);
|
||
//同步抖音
|
||
//MyTimer.NewTimer<DyUpdateTimer>(60);
|
||
string baseAddress = $"http://*:{Config.Port}/";
|
||
|
||
WebApp.Start<OwinServer>(url: baseAddress);
|
||
|
||
var flag = false;
|
||
if (flag) //是否创建测试数据
|
||
{
|
||
Db.CreateTestData();
|
||
}
|
||
|
||
//var u = Db.GetUser(1);
|
||
//if (u == null) u = new User() { Username = "test", UserType = Common.Models.Enums.UserType.微信用户, Headurl = String.Empty };
|
||
//Db.Save(u);
|
||
EO.WebBrowser.Runtime.AddLicense(
|
||
"N4SOscufWbPw+g7kp+rp9um7aOPt9BDtrNzpz7iJWZeksefgpePzCOmMQ5ek" +
|
||
"scufWZekzQzjnZf4ChvkdpnJ4NnCoenz/hChWe3pAx7oqOXBs92wZ6emsdq9" +
|
||
"RoGkscufdabl/RfusLWRm8ufWZfAAB3jnunN/xHuWdvlBRC8W6i2wNywaKm0" +
|
||
"w9uhWer58/D3qeD29h7ArbSmwt2uaqizw9uxapmkBxDxrODz/+iha6emsdq9" +
|
||
"RoGkscufdert+Bngrez29unLn9D7BR7Wb8T66/nCm6y40hu2b8zFzui7aOrt" +
|
||
"+Bngrez29umMQ7Oz/RTinuX39umMQ3Xj7fQQ7azcwp61n1mXpM0X6Jzc8gQQ" +
|
||
"yJ21uur5sXGqusrz0XCwtMTis4+zs/0U4p7l9/b043c=");
|
||
|
||
this.Url = $"http://{InternetIP}";
|
||
#if DEBUG
|
||
IsDebug = true;
|
||
Url = $"http://127.0.0.1";
|
||
#endif
|
||
if (Config.Port != 80) Url += ":" + Config.Port;
|
||
|
||
Events.OrderChange += Events_OrderChange;
|
||
//new Thread(() =>
|
||
//{
|
||
// DataMigrationManageSerivce.Instance.Test();
|
||
//}).Start();
|
||
}
|
||
|
||
private void Events_OrderChange(object sender, OrderChangeEventArgs e)
|
||
{
|
||
if (e.UserId != 0)
|
||
{
|
||
try
|
||
{
|
||
var User = Db.GetUser(e.UserId);
|
||
if (User != null)
|
||
{
|
||
SendClientMsg(User.RobotId, DeviceMessageType.订单状态改变, e);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Db.OnLog("通知用户订单状态", ex);
|
||
}
|
||
}
|
||
}
|
||
|
||
public MainForm Form { get; set; }
|
||
public HttpRuntimeCache Cache { get; set; } = new HttpRuntimeCache();
|
||
|
||
public SqlSugarClient Db
|
||
{
|
||
get
|
||
{
|
||
var db = new SqlSugarClient(new ConnectionConfig()
|
||
{
|
||
ConnectionString = $"Data Source={Config.MysqlHost};Initial Catalog={Config.MysqlName};Persist Security Info=True;User ID={Config.MysqlUser};Password={Config.MysqlPass};Port={Config.MysqlPort};Min Pool Size = 3; Max Pool Size = 100;CharSet=utf8mb4;AllowLoadLocalInfile=true;Allow Zero Datetime=True;SslMode=none;Convert Zero Datetime=True;",//连接符字串
|
||
DbType = SqlSugar.DbType.MySql, //数据库类型
|
||
IsAutoCloseConnection = true,
|
||
MoreSettings = new ConnMoreSettings()
|
||
{
|
||
IsAutoRemoveDataCache = true,
|
||
DefaultCacheDurationInSeconds = 86400,
|
||
},
|
||
ConfigureExternalServices = new ConfigureExternalServices()
|
||
{
|
||
DataInfoCacheService = Cache, //配置我们创建的缓存类,
|
||
},
|
||
});
|
||
db.Aop.DataExecuting = (oldValue, entityInfo) =>
|
||
{
|
||
db.SetSplitTableService(entityInfo.EntityName);
|
||
};
|
||
|
||
db.Aop.OnError = (ex) =>
|
||
{
|
||
try
|
||
{
|
||
var temp = ex.Sql;
|
||
var time = db.Ado.SqlExecutionTime;
|
||
db.OnLog("SQL错误", $"{ex.Message},SQL => {ex.Sql}", Common.Models.Enums.LogType.错误);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
};
|
||
|
||
db.Aop.OnLogExecuted = (sql, pars) =>
|
||
{
|
||
if (db.Ado.SqlExecutionTime.TotalSeconds > 60)
|
||
{
|
||
try
|
||
{
|
||
var temp = sql;
|
||
|
||
var time = db.Ado.SqlExecutionTime;
|
||
|
||
}
|
||
catch (Exception)
|
||
{
|
||
// ignored
|
||
}
|
||
}
|
||
else if (IsDebug)
|
||
{
|
||
var temp = sql;
|
||
|
||
foreach (var item in pars) temp = temp.Replace(item.ParameterName, $"'{item.Value}'");
|
||
var time = db.Ado.SqlExecutionTime;
|
||
//Console.WriteLine($"SQL耗时:{time.TotalSeconds}、SQL => {sql}"); }
|
||
//WWW Console.WriteLine(sql,pars);
|
||
}
|
||
};
|
||
return db;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
public ConcurrentDictionary<string, DeviceSession> OnlineDevices { get { return Config.RuntimeCache.DeviceSessions; } }
|
||
public ConcurrentDictionary<int, UserSession> OnlineUsers { get { return Config.RuntimeCache.UserSessions; } }
|
||
public void SendAllClientMsg(DeviceMessageType Type, object Data)
|
||
{
|
||
var list = this.OnlineDevices.ToArray();
|
||
foreach (var item in list)
|
||
{
|
||
SendClientMsg(item.Key, Type, Data);
|
||
}
|
||
}
|
||
|
||
public DeviceMessage SendClientMsg(int RobotId, DeviceMessageType Type, Object Data = null)
|
||
{
|
||
var deviceId = OnlineDevices.FirstOrDefault(f => f.Value.RobotId == RobotId).Key;
|
||
if (!string.IsNullOrEmpty(deviceId))
|
||
{
|
||
return SendClientMsg(deviceId, Type, Data);
|
||
}
|
||
return null;
|
||
}
|
||
public DeviceMessage SendClientMsg(string DeviceId, DeviceMessageType Type, Object Data = null)
|
||
{
|
||
if (OnlineDevices.ContainsKey(DeviceId))
|
||
{
|
||
var msg = new DeviceMessage() { Type = Type, Content = Data, Time = Util.GetTimespan(DateTime.Now) };
|
||
OnlineDevices[DeviceId].Messages.Enqueue(msg);
|
||
return msg;
|
||
}
|
||
return null;
|
||
}
|
||
public Task<DeviceReplyMessage> GetClientMsg(string Msgid, int Timeout = 60)
|
||
{
|
||
return Task.Factory.StartNew<DeviceReplyMessage>(() =>
|
||
{
|
||
var ExpirationTime = DateTime.Now.AddSeconds(Timeout);
|
||
do
|
||
{
|
||
Thread.Sleep(1000);
|
||
if (ReplyMessages.ContainsKey(Msgid)) return ReplyMessages[Msgid];
|
||
} while (ExpirationTime > DateTime.Now);
|
||
return null;
|
||
});
|
||
}
|
||
|
||
}
|
||
}
|