@@ -13,9 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HKControl", "HKControl\HKCo | |||
EndProject | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HKHelper", "HKHelper\HKHelper.csproj", "{CFA68AF9-1C11-41A3-8A73-34E004660CFC}" | |||
EndProject | |||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UHFHelper", "UHFHelper\UHFHelper.csproj", "{F7A3683F-B767-427C-BCB8-7C5DA507D663}" | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UHFHelper", "UHFHelper\UHFHelper.csproj", "{F7A3683F-B767-427C-BCB8-7C5DA507D663}" | |||
EndProject | |||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{3AD252E2-6878-4ABC-8A3E-22CCFCE2E075}" | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "test", "test\test.csproj", "{3AD252E2-6878-4ABC-8A3E-22CCFCE2E075}" | |||
EndProject | |||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HKLib", "HKLib\HKLib.csproj", "{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}" | |||
EndProject | |||
Global | |||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | |||
@@ -81,6 +83,14 @@ Global | |||
{3AD252E2-6878-4ABC-8A3E-22CCFCE2E075}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{3AD252E2-6878-4ABC-8A3E-22CCFCE2E075}.Release|x86.ActiveCfg = Release|x86 | |||
{3AD252E2-6878-4ABC-8A3E-22CCFCE2E075}.Release|x86.Build.0 = Release|x86 | |||
{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}.Debug|x86.ActiveCfg = Debug|Any CPU | |||
{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}.Debug|x86.Build.0 = Debug|Any CPU | |||
{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}.Release|x86.ActiveCfg = Release|Any CPU | |||
{471F95A5-B9E9-41CF-A8A3-E4B36FFDE84C}.Release|x86.Build.0 = Release|Any CPU | |||
EndGlobalSection | |||
GlobalSection(SolutionProperties) = preSolution | |||
HideSolutionNode = FALSE | |||
@@ -32,10 +32,10 @@ | |||
<ItemGroup> | |||
<PackageReference Include="HandyControls" Version="3.4.1" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> | |||
<PackageReference Include="Stylet" Version="1.3.6" /> | |||
<PackageReference Include="SqlSugarCore" Version="5.1.2.7" /> | |||
<PackageReference Include="XExten.Advance" Version="1.2.4.2-preview" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\HKLog\HKLog.csproj" /> | |||
@@ -31,9 +31,9 @@ namespace HKCardOUT | |||
bool ping = PingHelper.PingTest(); | |||
//1.检测网络上下线 | |||
bool network = HKHelpers.GetInstance().GetNetworkState(); | |||
if (ping&& network!= DataBus.NetWordState) | |||
if (ping && network != DataBus.NetWordState) | |||
{ | |||
DataBus.NetWordState = network&& ping; | |||
DataBus.NetWordState = network && ping; | |||
if (DataBus.NetWordState) HandyControl.Controls.Growl.InfoGlobal("网络连接成功"); | |||
else HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络!!!"); | |||
} | |||
@@ -69,6 +69,8 @@ namespace HKCardOUT | |||
DataBus.PM = configer["PM"]; | |||
DataBus.Count = configer["Count"].AsInt(); | |||
DataBus.AT = configer["AT"]; | |||
HKLib.Configer.SaasRoute = DataBus.SaasRoute; | |||
HKLib.Configer.MqAddress = configer["MQ"]; | |||
DbContext.InitTable(); | |||
RemoteService.PullShopInfo(); | |||
base.Configure(); | |||
@@ -53,6 +53,7 @@ | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\HKControl\HKControl.csproj" /> | |||
<ProjectReference Include="..\HKLib\HKLib.csproj" /> | |||
<ProjectReference Include="..\HKLog\HKLog.csproj" /> | |||
<ProjectReference Include="..\UHFHelper\UHFHelper.csproj" /> | |||
</ItemGroup> | |||
@@ -0,0 +1,23 @@ | |||
using HKCardOUT.Logic; | |||
using SqlSugar; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace Logic.Model | |||
{ | |||
[SugarTable("CardStutas")] | |||
public class CardStutas: BaseEntity | |||
{ | |||
/// <summary> | |||
/// 卡号 | |||
/// </summary> | |||
[SugarColumn(IsNullable = false)] | |||
public string CardNo { get; set; } | |||
/// <summary> | |||
/// 卡状态 0.禁用 1.正常 2.挂失 3.作废 | |||
/// </summary> | |||
[SugarColumn(IsNullable = false)] | |||
public int Stutas { get; set; } | |||
} | |||
} |
@@ -19,16 +19,15 @@ namespace HKCardOUT.Logic | |||
public class RemoteService | |||
{ | |||
/// <summary> | |||
/// 判断卡状态 | |||
/// 获取所有卡状态和卡号 | |||
/// </summary> | |||
/// <param name="input"></param> | |||
public static int CheckCardStuatas(string CardNo) | |||
public static int CheckCardStuatas() | |||
{ | |||
var data = IHttpMultiClient.HttpMulti.AddNode(t => | |||
{ | |||
t.NodePath = ApiRoute.CheckCardStatus; | |||
t.ReqType = MultiType.POST; | |||
t.JsonParam = (new { cardNum = CardNo }).ToJson(); | |||
t.ReqType = MultiType.GET; | |||
}).Build().RunStringFirst().ToModel<JObject>()["data"]["cardState"]; | |||
return data != null ? data.ToString().AsInt() : 0; | |||
} | |||
@@ -2,6 +2,7 @@ | |||
using HKCardOUT.Helper; | |||
using HKCardOUT.Logic.Model; | |||
using HKControl; | |||
using Logic.Model; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
@@ -27,6 +28,14 @@ namespace HKCardOUT.Logic.Service | |||
if (Old.Count > 3) return null; | |||
return Old; | |||
} | |||
bool GetCardStutas(string CardNo) | |||
{ | |||
var data = DbContext.Context.Queryable<CardStutas>().First(t => t.CardNo == CardNo); | |||
if (data == null) return false; | |||
if (data.Stutas != 1) return false; | |||
return true; | |||
} | |||
/// <summary> | |||
/// 早上消费 | |||
/// </summary> | |||
@@ -37,6 +46,7 @@ namespace HKCardOUT.Logic.Service | |||
{ | |||
try | |||
{ | |||
if(!GetCardStutas(input.CardNo)) return false; | |||
var Old = Commom(input); | |||
if (Old == null) return false; | |||
var AM = DataBus.AM.Split(",").Select(t => t.AsInt()).ToList(); | |||
@@ -52,29 +62,30 @@ namespace HKCardOUT.Logic.Service | |||
var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity(); | |||
if (entity.Id != Guid.Empty) | |||
{ | |||
//有网络直接同步 | |||
if (DataBus.NetWordState) | |||
{ | |||
//先出餐后推数据 | |||
if (DataBus.StartDevice) | |||
{ | |||
Main.GetInstance.Start(input.Location.AsInt()); | |||
UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
} | |||
var res = RemoteService.SyncSaleLog(input.CardNo, input.Location); | |||
if (res) | |||
DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand(); | |||
return res; | |||
} | |||
else | |||
////有网络直接同步 | |||
//if (DataBus.NetWordState) | |||
//{ | |||
// //先出餐后推数据 | |||
// if (DataBus.StartDevice) | |||
// { | |||
// Main.GetInstance.Start(input.Location.AsInt()); | |||
// UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
// } | |||
// var res = RemoteService.SyncSaleLog(input.CardNo, input.Location); | |||
// if (res) | |||
// DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand(); | |||
// return res; | |||
//} | |||
//else | |||
//{ | |||
//} | |||
if (DataBus.StartDevice) | |||
{ | |||
if (DataBus.StartDevice) | |||
{ | |||
Main.GetInstance.Start(input.Location.AsInt()); | |||
UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
} | |||
return true; | |||
Main.GetInstance.Start(input.Location.AsInt()); | |||
UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
} | |||
return true; | |||
} | |||
} | |||
return false; | |||
@@ -96,6 +107,7 @@ namespace HKCardOUT.Logic.Service | |||
{ | |||
try | |||
{ | |||
if (!GetCardStutas(input.CardNo)) return false; | |||
var Old = Commom(input); | |||
if (Old == null) return false; | |||
var PM = DataBus.PM.Split(",").Select(t => t.AsInt()).ToList(); | |||
@@ -110,29 +122,29 @@ namespace HKCardOUT.Logic.Service | |||
var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity(); | |||
if (entity.Id != Guid.Empty) | |||
{ | |||
//有网络直接同步 | |||
if (DataBus.NetWordState) | |||
{ | |||
//先出餐后推数据 | |||
if (DataBus.StartDevice) | |||
{ | |||
Main.GetInstance.Start(input.Location.AsInt()); | |||
UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
} | |||
var res = RemoteService.SyncSaleLog(input.CardNo, input.Location); | |||
if (res) | |||
DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand(); | |||
return res; | |||
} | |||
else | |||
{ | |||
////有网络直接同步 | |||
//if (DataBus.NetWordState) | |||
//{ | |||
// //先出餐后推数据 | |||
// if (DataBus.StartDevice) | |||
// { | |||
// Main.GetInstance.Start(input.Location.AsInt()); | |||
// UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
// } | |||
// var res = RemoteService.SyncSaleLog(input.CardNo, input.Location); | |||
// if (res) | |||
// DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand(); | |||
// return res; | |||
//} | |||
//else | |||
//{ | |||
if (DataBus.StartDevice) | |||
{ | |||
Main.GetInstance.Start(input.Location.AsInt()); | |||
UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
} | |||
return true; | |||
} | |||
//} | |||
} | |||
} | |||
return false; | |||
@@ -154,6 +166,7 @@ namespace HKCardOUT.Logic.Service | |||
{ | |||
try | |||
{ | |||
if (!GetCardStutas(input.CardNo)) return false; | |||
var Old = Commom(input); | |||
if (Old == null) return false; | |||
var AT = DataBus.AT.Split(",").Select(t => t.AsInt()).ToList(); | |||
@@ -168,29 +181,29 @@ namespace HKCardOUT.Logic.Service | |||
var entity = DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteReturnEntity(); | |||
if (entity.Id != Guid.Empty) | |||
{ | |||
//有网络直接同步 | |||
if (DataBus.NetWordState) | |||
{ | |||
//先出餐后推数据 | |||
if (DataBus.StartDevice) | |||
{ | |||
Main.GetInstance.Start(input.Location.AsInt()); | |||
UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
} | |||
var res = RemoteService.SyncSaleLog(input.CardNo, input.Location); | |||
if (res) | |||
DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand(); | |||
return res; | |||
} | |||
else | |||
{ | |||
////有网络直接同步 | |||
//if (DataBus.NetWordState) | |||
//{ | |||
// //先出餐后推数据 | |||
// if (DataBus.StartDevice) | |||
// { | |||
// Main.GetInstance.Start(input.Location.AsInt()); | |||
// UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
// } | |||
// var res = RemoteService.SyncSaleLog(input.CardNo, input.Location); | |||
// if (res) | |||
// DbContext.Context.Updateable<SaleLog>().SetColumns(t => t.IsSync == true).Where(t => t.Id == entity.Id).ExecuteCommand(); | |||
// return res; | |||
//} | |||
//else | |||
//{ | |||
if (DataBus.StartDevice) | |||
{ | |||
Main.GetInstance.Start(input.Location.AsInt()); | |||
UHF_RS485_Helper.GetInstance().OpenBeep(input.Location.AsInt()); | |||
} | |||
return true; | |||
} | |||
//} | |||
} | |||
} | |||
return false; | |||
@@ -0,0 +1,22 @@ | |||
using HKCardOUT.Logic; | |||
using HKLib.Dto; | |||
using HKLib.RabbitMQ.Config; | |||
using Logic.Model; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using XExten.Advance.LinqFramework; | |||
namespace HKCardOUT | |||
{ | |||
public class MqHandle : IAccept | |||
{ | |||
public void AcceptMQ<T>(string msg) | |||
{ | |||
var dto = msg.ToModel<CardStutasDto>(); | |||
DbContext.Context.Updateable<CardStutas>().SetColumns(t => t.Stutas == dto.Stutas).Where(t => t.CardNo == dto.CardNo).ExecuteCommand(); | |||
} | |||
} | |||
} |
@@ -1,7 +1,7 @@ | |||
{ | |||
//服务地址 | |||
"SaasRoute": "https://bpa.black-pa.com:21527/kitchen/", | |||
//启动设备调试 | |||
//启动设备调试 | |||
"StartDevice": true, | |||
//串口 | |||
"COM": "COM3", | |||
@@ -19,8 +19,10 @@ | |||
"PM": "11,14", | |||
//下午 | |||
"AT": "17,20", | |||
//每个时间段的刷卡次数 | |||
"Count":1, | |||
//每个时间段的刷卡次数 | |||
"Count": 1, | |||
//取消单卡刷卡限制 | |||
"Cancel":false | |||
"Cancel": false, | |||
//MQ地址 | |||
"MQ": "host=10.2.1.21:5672;username=guest;password=guest" | |||
} |
@@ -0,0 +1,12 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace HKLib | |||
{ | |||
public class Configer | |||
{ | |||
public static string MqAddress { get; set; } | |||
public static string SaasRoute { get; set; } | |||
} | |||
} |
@@ -0,0 +1,18 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace HKLib.Dto | |||
{ | |||
public class CardStutasDto | |||
{ | |||
/// <summary> | |||
/// 卡号 | |||
/// </summary> | |||
public string CardNo { get; set; } | |||
/// <summary> | |||
/// 状态 0.禁用 1.正常 2.挂失 3.作废 | |||
/// </summary> | |||
public int Stutas { get; set; } | |||
} | |||
} |
@@ -0,0 +1,22 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace HKLib.Dto | |||
{ | |||
public class TimeSpilte | |||
{ | |||
/// <summary> | |||
/// 早 例如 8,10 | |||
/// </summary> | |||
public string AM { get; set; } | |||
/// <summary> | |||
/// 中 例如 11,13 | |||
/// </summary> | |||
public string PM { get; set; } | |||
/// <summary> | |||
/// 晚 例如 17,19 | |||
/// </summary> | |||
public string AT { get; set; } | |||
} | |||
} |
@@ -0,0 +1,30 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace HKLib.Dto | |||
{ | |||
public class UserDto | |||
{ | |||
/// <summary> | |||
/// 机构 | |||
/// </summary> | |||
public string Org { get; set; } | |||
/// <summary> | |||
/// 姓名 | |||
/// </summary> | |||
public string Name { get; set; } | |||
/// <summary> | |||
/// 手机号 | |||
/// </summary> | |||
public string Phone { get; set; } | |||
/// <summary> | |||
/// 卡号 | |||
/// </summary> | |||
public string CardNo { get; set; } | |||
/// <summary> | |||
/// 身份证号 | |||
/// </summary> | |||
public string IdCard { get; set; } | |||
} | |||
} |
@@ -0,0 +1,13 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<TargetFramework>netstandard2.1</TargetFramework> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="EasyNetQ" Version="6.5.2" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> | |||
<PackageReference Include="Snowflake.Core" Version="2.0.0" /> | |||
<PackageReference Include="XExten.Advance" Version="1.2.4.2-preview" /> | |||
</ItemGroup> | |||
</Project> |
@@ -0,0 +1,66 @@ | |||
using HKLib.Dto; | |||
using HKLib.RabbitMQ.Config; | |||
using HKLib.RabbitMQ.Publisher; | |||
using XExten.Advance.HttpFramework.MultiCommon; | |||
using XExten.Advance.HttpFramework.MultiFactory; | |||
using XExten.Advance.LinqFramework; | |||
using static RabbitMQ.Client.Logging.RabbitMqClientEventSource; | |||
namespace HKLib.Interfaces | |||
{ | |||
public class HKLibHelper | |||
{ | |||
/// <summary> | |||
/// 设置刷卡时间端 | |||
/// </summary> | |||
public static void SetTime(TimeSpilte input) | |||
{ | |||
IHttpMultiClient.HttpMulti.AddNode(t => | |||
{ | |||
t.NodePath = $"{Configer.SaasRoute}"; | |||
t.JsonParam = input.ToJson(); | |||
t.ReqType = MultiType.POST; | |||
}).Build().RunStringFirst(); | |||
} | |||
/// <summary> | |||
/// 根据用户命获取下拉用户列表 | |||
/// </summary> | |||
/// <param name="keyword"></param> | |||
public static void GetUserList(string keyword) | |||
{ | |||
IHttpMultiClient.HttpMulti.AddNode(t => | |||
{ | |||
t.NodePath = $"{Configer.SaasRoute}/{keyword}"; | |||
t.ReqType = MultiType.GET; | |||
}).Build().RunStringFirst(); | |||
} | |||
/// <summary> | |||
/// 添加用户 | |||
/// </summary> | |||
/// <param name="input"></param> | |||
public static void AddUser(UserDto input) | |||
{ | |||
IHttpMultiClient.HttpMulti.AddNode(t => | |||
{ | |||
t.NodePath = $"{Configer.SaasRoute}"; | |||
t.ReqType = MultiType.POST; | |||
t.JsonParam = input.ToJson(); | |||
}).Build().RunStringFirst(); | |||
} | |||
/// <summary> | |||
/// 变更卡状态 | |||
/// </summary> | |||
/// <param name="input"></param> | |||
public static void CardStutasChange(CardStutasDto input) | |||
{ | |||
IHttpMultiClient.HttpMulti.AddNode(t => | |||
{ | |||
t.NodePath = $"{Configer.SaasRoute}"; | |||
t.ReqType = MultiType.POST; | |||
t.JsonParam = input.ToJson(); | |||
}).Build().RunStringFirst(); | |||
ServiceQueryPush.QueryPush.PushMQ(input, "CardStutasChanged", MQEnum.Top); | |||
} | |||
} | |||
} |
@@ -0,0 +1,28 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace HKLib.RabbitMQ.Config | |||
{ | |||
public class AcceptEntity | |||
{ | |||
/// <summary> | |||
/// 队列名称 | |||
/// </summary> | |||
public String QueeName { get; set; } | |||
/// <summary> | |||
/// 推送模式 | |||
/// </summary> | |||
public MQEnum SendType { get; set; } | |||
/// <summary> | |||
/// 管道名称 | |||
/// </summary> | |||
public String ExchangeName { get; set; } | |||
/// <summary> | |||
/// 路由名称 | |||
/// </summary> | |||
public String RouteName { get; set; } | |||
} | |||
} |
@@ -0,0 +1,13 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace HKLib.RabbitMQ.Config | |||
{ | |||
public interface IAccept | |||
{ | |||
void AcceptMQ<T>(string msg); | |||
} | |||
} |
@@ -0,0 +1,26 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel; | |||
using System.Text; | |||
namespace HKLib.RabbitMQ.Config | |||
{ | |||
public enum MQEnum | |||
{ | |||
/// <summary> | |||
/// 订阅模式 | |||
/// </summary> | |||
[Description("订阅模式")] | |||
Sub = 10, | |||
/// <summary> | |||
/// 推送模式 | |||
/// </summary> | |||
[Description("推送模式")] | |||
Push = 20, | |||
/// <summary> | |||
/// 主路由模式 | |||
/// </summary> | |||
[Description("主路由模式")] | |||
Top = 30 | |||
} | |||
} |
@@ -0,0 +1,117 @@ | |||
using EasyNetQ; | |||
using EasyNetQ.Topology; | |||
using HKLib.DataBus.数据缓存; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Configuration; | |||
using System.Linq.Expressions; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace HKLib.RabbitMQ.Config | |||
{ | |||
public class MQFactory | |||
{ | |||
private volatile static IBus Bus = null; | |||
/// <summary> | |||
/// 创建链接 | |||
/// </summary> | |||
/// <returns></returns> | |||
public static IBus CreateMQ() | |||
{ | |||
if (Bus == null) | |||
Bus = RabbitHutch.CreateBus(Configer.MqAddress); | |||
return Bus; | |||
} | |||
/// <summary> | |||
/// 释放链接 | |||
/// </summary> | |||
public static void DisposeBus() | |||
{ | |||
Bus?.Dispose(); | |||
} | |||
/// <summary> | |||
/// 同步执行 | |||
/// </summary> | |||
/// <typeparam name="T"></typeparam> | |||
/// <param name="Param"></param> | |||
/// <returns></returns> | |||
public static bool SendMQ<T>(PushEntity<T> Param) | |||
{ | |||
try | |||
{ | |||
if (Bus == null) | |||
CreateMQ(); | |||
new PushManage().SendMQ(Param, Bus); | |||
return true; | |||
} | |||
catch (Exception) | |||
{ | |||
return false; | |||
} | |||
} | |||
/// <summary> | |||
/// 推荐异步执行 | |||
/// </summary> | |||
/// <typeparam name="T"></typeparam> | |||
/// <param name="Param"></param> | |||
/// <returns></returns> | |||
public static async Task SendMQAsync<T>(PushEntity<T> Param) | |||
{ | |||
if (Bus == null) | |||
CreateMQ(); | |||
await new PushManage().SendMQAsync(Param, Bus); | |||
} | |||
/// <summary> | |||
/// 订阅消息 | |||
/// </summary> | |||
/// <typeparam name="TAccept"></typeparam> | |||
/// <param name="Args"></param> | |||
public static void Subscriber<TAccept, T>(AcceptEntity Args) where TAccept : IAccept, new() | |||
{ | |||
try | |||
{ | |||
if (Bus == null) | |||
CreateMQ(); | |||
if (string.IsNullOrEmpty(Args.ExchangeName)) | |||
return; | |||
Expression<Action<IAccept>> methodCall; | |||
IExchange EX = null; | |||
if (Args.SendType == MQEnum.Sub) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Args.ExchangeName, ExchangeType.Fanout); | |||
} | |||
if (Args.SendType == MQEnum.Push) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Args.ExchangeName, ExchangeType.Direct); | |||
} | |||
if (Args.SendType == MQEnum.Top) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Args.ExchangeName, ExchangeType.Topic); | |||
} | |||
IQueue queue = Bus.Advanced.QueueDeclare(Args.QueeName ?? null); | |||
Bus.Advanced.Bind(EX, queue, Args.RouteName); | |||
Bus.Advanced.Consume(queue, (body, properties, info) => Task.Factory.StartNew(() => | |||
{ | |||
try | |||
{ | |||
var message = Encoding.UTF8.GetString(body); | |||
//处理消息 | |||
methodCall = job => job.AcceptMQ<T>(message); | |||
methodCall.Compile()(new TAccept()); | |||
} | |||
catch (Exception ex) | |||
{ | |||
throw ex; | |||
} | |||
})); | |||
} | |||
catch (Exception ex) | |||
{ | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,26 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace HKLib.RabbitMQ.Config | |||
{ | |||
public class PushEntity<T> | |||
{ | |||
/// <summary> | |||
/// 推送内容 | |||
/// </summary> | |||
public T BodyData { get; set; } | |||
/// <summary> | |||
/// 推送模式 | |||
/// </summary> | |||
public MQEnum SendType { get; set; } | |||
/// <summary> | |||
/// 管道名称 | |||
/// </summary> | |||
public String ExchangeName { get; set; } | |||
/// <summary> | |||
/// 路由名称 | |||
/// </summary> | |||
public String RouteName { get; set; } | |||
} | |||
} |
@@ -0,0 +1,81 @@ | |||
using EasyNetQ; | |||
using EasyNetQ.Topology; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using Newtonsoft.Json; | |||
using System.Threading.Tasks; | |||
namespace HKLib.RabbitMQ.Config | |||
{ | |||
public class PushManage | |||
{ | |||
/// <summary> | |||
/// 发布消息队列异步 | |||
/// </summary> | |||
/// <typeparam name="T"></typeparam> | |||
/// <param name="Param"></param> | |||
/// <param name="Bus"></param> | |||
/// <returns></returns> | |||
public async Task SendMQAsync<T>(PushEntity<T> Param, IBus Bus) | |||
{ | |||
//one to one | |||
var msg = new Message<T>(Param.BodyData); | |||
IExchange EX = null; | |||
if (Param.SendType == MQEnum.Sub) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Fanout); | |||
} | |||
if (Param.SendType == MQEnum.Push) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Direct); | |||
} | |||
if (Param.SendType == MQEnum.Top) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Topic); | |||
} | |||
await Bus.Advanced.PublishAsync(EX, Param.RouteName, false, msg).ContinueWith(t => | |||
{ | |||
//消息投递失败 | |||
if (!t.IsCompleted && t.IsFaulted) | |||
{ | |||
// //将消息记录到数据库轮询 | |||
// IRepository Repository = new Repository(); | |||
// SystemLog Log = new SystemLog | |||
// { | |||
// LogName = "消息队列", | |||
// Source = "发布队列", | |||
// EventData = JsonConvert.SerializeObject(Param.BodyData) | |||
// }; | |||
// Repository.Insert(Log); | |||
} | |||
}); | |||
} | |||
/// <summary> | |||
/// 发布消息队列同步 | |||
/// </summary> | |||
/// <typeparam name="T"></typeparam> | |||
/// <param name="Param"></param> | |||
/// <param name="Bus"></param> | |||
public void SendMQ<T>(PushEntity<T> Param, IBus Bus) | |||
{ | |||
//one to one | |||
var msg = new Message<T>(Param.BodyData); | |||
IExchange EX = null; | |||
if (Param.SendType == MQEnum.Sub) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Fanout); | |||
} | |||
if (Param.SendType == MQEnum.Push) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Direct); | |||
} | |||
if (Param.SendType == MQEnum.Top) | |||
{ | |||
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Topic); | |||
} | |||
Bus.Advanced.Publish(EX, Param.RouteName, false, msg); | |||
} | |||
} | |||
} |
@@ -0,0 +1,74 @@ | |||
using EasyNetQ; | |||
using HKLib.RabbitMQ.Config; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace HKLib.RabbitMQ.Publisher | |||
{ | |||
public class ServiceQueryPush | |||
{ | |||
public static ServiceQueryPush QueryPush => new Lazy<ServiceQueryPush>().Value; | |||
public async Task<bool> PushMQAsync<T>(T Entity,string Key, MQEnum MQType = MQEnum.Push) | |||
{ | |||
//推送模式 | |||
//推送模式下需指定管道名称和路由键值名称 | |||
//消息只会被发送到指定的队列中去 | |||
//订阅模式 | |||
//订阅模式下只需指定管道名称 | |||
//消息会被发送到该管道下的所有队列中 | |||
//主题路由模式 | |||
//路由模式下需指定管道名称和路由值 | |||
//消息会被发送到该管道下,和路由值匹配的队列中去 | |||
PushEntity<T> entity = new PushEntity<T>(); | |||
entity.BodyData = Entity; | |||
entity.SendType = MQType; | |||
if (MQType == MQEnum.Push) | |||
{ | |||
entity.ExchangeName = "Message.Direct"; | |||
entity.RouteName = Key; | |||
} | |||
else if (MQType == MQEnum.Sub) | |||
{ | |||
entity.ExchangeName = "Message.Fanout"; | |||
} | |||
else | |||
{ | |||
entity.ExchangeName = "Message.Topic"; | |||
entity.RouteName = Key; | |||
} | |||
return await MQFactory.SendMQAsync(entity).ContinueWith(t => { return t.IsCompleted ? true : false; }); | |||
} | |||
public bool PushMQ<T>(T Entity, string Key, MQEnum MQType = MQEnum.Push) | |||
{ | |||
//推送模式 | |||
//推送模式下需指定管道名称和路由键值名称 | |||
//消息只会被发送到指定的队列中去 | |||
//订阅模式 | |||
//订阅模式下只需指定管道名称 | |||
//消息会被发送到该管道下的所有队列中 | |||
//主题路由模式 | |||
//路由模式下需指定管道名称和路由值 | |||
//消息会被发送到该管道下,和路由值匹配的队列中去 | |||
PushEntity<T> entity = new PushEntity<T>(); | |||
entity.BodyData = Entity; | |||
entity.SendType = MQType; | |||
if (MQType == MQEnum.Push) | |||
{ | |||
entity.ExchangeName = "Message.Direct"; | |||
entity.RouteName = Key; | |||
} | |||
else if (MQType == MQEnum.Sub) | |||
{ | |||
entity.ExchangeName = "Message.Fanout"; | |||
} | |||
else | |||
{ | |||
entity.ExchangeName = "Message.Topic"; | |||
entity.RouteName = Key; | |||
} | |||
return MQFactory.SendMQ(entity); | |||
} | |||
} | |||
} |
@@ -0,0 +1,42 @@ | |||
using EasyNetQ; | |||
using HKLib.RabbitMQ.Config; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace HKLib.RabbitMQ.Subscriber | |||
{ | |||
public class ServiceQueryExcute | |||
{ | |||
public static ServiceQueryExcute QueryExcute => new Lazy<ServiceQueryExcute>().Value; | |||
public void ExtuteMQ<TImpl,T>(string Key,MQEnum Type) where TImpl : IAccept, new() | |||
{ | |||
Task.Run(() => | |||
{ | |||
AcceptEntity accept = new AcceptEntity(); | |||
if (Type == MQEnum.Push) | |||
{ | |||
accept.SendType = MQEnum.Push; | |||
accept.ExchangeName = "Message.Direct"; | |||
accept.QueeName = "Meesage.DirectQuene"; | |||
accept.RouteName = Key; | |||
} | |||
else if (Type == MQEnum.Sub) | |||
{ | |||
accept.SendType = MQEnum.Sub; | |||
accept.ExchangeName = "Message.Fanout"; | |||
accept.QueeName = "Meesage.FanoutQuene"; | |||
} | |||
else | |||
{ | |||
accept.SendType = MQEnum.Top; | |||
accept.ExchangeName = "Message.Topic"; | |||
accept.QueeName = "Meesage.FanoutQuene"; | |||
accept.RouteName = Key; | |||
} | |||
MQFactory.Subscriber<TImpl, T>(accept); | |||
}); | |||
} | |||
} | |||
} |