From 0a74c490a894b5b8d8ed2dc2add5176380f13bb9 Mon Sep 17 00:00:00 2001 From: xxe Date: Wed, 12 Oct 2022 16:04:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HKCard.sln | 14 +- HKCardIN/HKCardIN.csproj | 2 +- HKCardOUT/Bootstrapper.cs | 6 +- HKCardOUT/HKCardOUT.csproj | 1 + HKCardOUT/Logic/Model/CardStutas.cs | 23 ++++ HKCardOUT/Logic/RemoteService.cs | 7 +- HKCardOUT/Logic/Service/HKCore.cs | 123 ++++++++++-------- HKCardOUT/MqHandle.cs | 22 ++++ HKCardOUT/options.json | 10 +- HKLib/Configer.cs | 12 ++ HKLib/Dto/CardStutasDto.cs | 18 +++ HKLib/Dto/TimeSpilte.cs | 22 ++++ HKLib/Dto/UserDto.cs | 30 +++++ HKLib/HKLib.csproj | 13 ++ HKLib/Interfaces/HKLibHelper.cs | 66 ++++++++++ HKLib/RabbitMq/Config/AcceptEntity.cs | 28 ++++ HKLib/RabbitMq/Config/IAccept.cs | 13 ++ HKLib/RabbitMq/Config/MQEnum.cs | 26 ++++ HKLib/RabbitMq/Config/MQFactory.cs | 117 +++++++++++++++++ HKLib/RabbitMq/Config/PushEntity.cs | 26 ++++ HKLib/RabbitMq/Config/PushManage.cs | 81 ++++++++++++ HKLib/RabbitMq/Publisher/ServiceQueryPush.cs | 74 +++++++++++ .../RabbitMq/Subscriber/ServiceQueryExcute.cs | 42 ++++++ 23 files changed, 708 insertions(+), 68 deletions(-) create mode 100644 HKCardOUT/Logic/Model/CardStutas.cs create mode 100644 HKCardOUT/MqHandle.cs create mode 100644 HKLib/Configer.cs create mode 100644 HKLib/Dto/CardStutasDto.cs create mode 100644 HKLib/Dto/TimeSpilte.cs create mode 100644 HKLib/Dto/UserDto.cs create mode 100644 HKLib/HKLib.csproj create mode 100644 HKLib/Interfaces/HKLibHelper.cs create mode 100644 HKLib/RabbitMq/Config/AcceptEntity.cs create mode 100644 HKLib/RabbitMq/Config/IAccept.cs create mode 100644 HKLib/RabbitMq/Config/MQEnum.cs create mode 100644 HKLib/RabbitMq/Config/MQFactory.cs create mode 100644 HKLib/RabbitMq/Config/PushEntity.cs create mode 100644 HKLib/RabbitMq/Config/PushManage.cs create mode 100644 HKLib/RabbitMq/Publisher/ServiceQueryPush.cs create mode 100644 HKLib/RabbitMq/Subscriber/ServiceQueryExcute.cs diff --git a/HKCard.sln b/HKCard.sln index bfe92d2..b287ba7 100644 --- a/HKCard.sln +++ b/HKCard.sln @@ -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 diff --git a/HKCardIN/HKCardIN.csproj b/HKCardIN/HKCardIN.csproj index 8e122f9..49f47cb 100644 --- a/HKCardIN/HKCardIN.csproj +++ b/HKCardIN/HKCardIN.csproj @@ -32,10 +32,10 @@ + - diff --git a/HKCardOUT/Bootstrapper.cs b/HKCardOUT/Bootstrapper.cs index 5dc6577..a74312f 100644 --- a/HKCardOUT/Bootstrapper.cs +++ b/HKCardOUT/Bootstrapper.cs @@ -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(); diff --git a/HKCardOUT/HKCardOUT.csproj b/HKCardOUT/HKCardOUT.csproj index 46b050f..02192a8 100644 --- a/HKCardOUT/HKCardOUT.csproj +++ b/HKCardOUT/HKCardOUT.csproj @@ -53,6 +53,7 @@ + diff --git a/HKCardOUT/Logic/Model/CardStutas.cs b/HKCardOUT/Logic/Model/CardStutas.cs new file mode 100644 index 0000000..0884c14 --- /dev/null +++ b/HKCardOUT/Logic/Model/CardStutas.cs @@ -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 + { + /// + /// 卡号 + /// + [SugarColumn(IsNullable = false)] + public string CardNo { get; set; } + /// + /// 卡状态 0.禁用 1.正常 2.挂失 3.作废 + /// + [SugarColumn(IsNullable = false)] + public int Stutas { get; set; } + } +} diff --git a/HKCardOUT/Logic/RemoteService.cs b/HKCardOUT/Logic/RemoteService.cs index a727815..e3245cc 100644 --- a/HKCardOUT/Logic/RemoteService.cs +++ b/HKCardOUT/Logic/RemoteService.cs @@ -19,16 +19,15 @@ namespace HKCardOUT.Logic public class RemoteService { /// - /// 判断卡状态 + /// 获取所有卡状态和卡号 /// /// - 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()["data"]["cardState"]; return data != null ? data.ToString().AsInt() : 0; } diff --git a/HKCardOUT/Logic/Service/HKCore.cs b/HKCardOUT/Logic/Service/HKCore.cs index 4c55175..b71a8a9 100644 --- a/HKCardOUT/Logic/Service/HKCore.cs +++ b/HKCardOUT/Logic/Service/HKCore.cs @@ -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().First(t => t.CardNo == CardNo); + if (data == null) return false; + if (data.Stutas != 1) return false; + return true; + } /// /// 早上消费 /// @@ -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().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().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().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().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().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().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; diff --git a/HKCardOUT/MqHandle.cs b/HKCardOUT/MqHandle.cs new file mode 100644 index 0000000..1e4a846 --- /dev/null +++ b/HKCardOUT/MqHandle.cs @@ -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(string msg) + { + var dto = msg.ToModel(); + + DbContext.Context.Updateable().SetColumns(t => t.Stutas == dto.Stutas).Where(t => t.CardNo == dto.CardNo).ExecuteCommand(); + + } + } +} diff --git a/HKCardOUT/options.json b/HKCardOUT/options.json index e11dd17..64b8e5d 100644 --- a/HKCardOUT/options.json +++ b/HKCardOUT/options.json @@ -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" } diff --git a/HKLib/Configer.cs b/HKLib/Configer.cs new file mode 100644 index 0000000..150c400 --- /dev/null +++ b/HKLib/Configer.cs @@ -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; } + } +} diff --git a/HKLib/Dto/CardStutasDto.cs b/HKLib/Dto/CardStutasDto.cs new file mode 100644 index 0000000..8f076e1 --- /dev/null +++ b/HKLib/Dto/CardStutasDto.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace HKLib.Dto +{ + public class CardStutasDto + { + /// + /// 卡号 + /// + public string CardNo { get; set; } + /// + /// 状态 0.禁用 1.正常 2.挂失 3.作废 + /// + public int Stutas { get; set; } + } +} diff --git a/HKLib/Dto/TimeSpilte.cs b/HKLib/Dto/TimeSpilte.cs new file mode 100644 index 0000000..6af9590 --- /dev/null +++ b/HKLib/Dto/TimeSpilte.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace HKLib.Dto +{ + public class TimeSpilte + { + /// + /// 早 例如 8,10 + /// + public string AM { get; set; } + /// + /// 中 例如 11,13 + /// + public string PM { get; set; } + /// + /// 晚 例如 17,19 + /// + public string AT { get; set; } + } +} diff --git a/HKLib/Dto/UserDto.cs b/HKLib/Dto/UserDto.cs new file mode 100644 index 0000000..21d4df9 --- /dev/null +++ b/HKLib/Dto/UserDto.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace HKLib.Dto +{ + public class UserDto + { + /// + /// 机构 + /// + public string Org { get; set; } + /// + /// 姓名 + /// + public string Name { get; set; } + /// + /// 手机号 + /// + public string Phone { get; set; } + /// + /// 卡号 + /// + public string CardNo { get; set; } + /// + /// 身份证号 + /// + public string IdCard { get; set; } + } +} diff --git a/HKLib/HKLib.csproj b/HKLib/HKLib.csproj new file mode 100644 index 0000000..3f18926 --- /dev/null +++ b/HKLib/HKLib.csproj @@ -0,0 +1,13 @@ + + + + netstandard2.1 + + + + + + + + + diff --git a/HKLib/Interfaces/HKLibHelper.cs b/HKLib/Interfaces/HKLibHelper.cs new file mode 100644 index 0000000..8963c6d --- /dev/null +++ b/HKLib/Interfaces/HKLibHelper.cs @@ -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 + { + /// + /// 设置刷卡时间端 + /// + public static void SetTime(TimeSpilte input) + { + IHttpMultiClient.HttpMulti.AddNode(t => + { + t.NodePath = $"{Configer.SaasRoute}"; + t.JsonParam = input.ToJson(); + t.ReqType = MultiType.POST; + }).Build().RunStringFirst(); + } + + /// + /// 根据用户命获取下拉用户列表 + /// + /// + public static void GetUserList(string keyword) + { + IHttpMultiClient.HttpMulti.AddNode(t => + { + t.NodePath = $"{Configer.SaasRoute}/{keyword}"; + t.ReqType = MultiType.GET; + }).Build().RunStringFirst(); + } + /// + /// 添加用户 + /// + /// + public static void AddUser(UserDto input) + { + IHttpMultiClient.HttpMulti.AddNode(t => + { + t.NodePath = $"{Configer.SaasRoute}"; + t.ReqType = MultiType.POST; + t.JsonParam = input.ToJson(); + }).Build().RunStringFirst(); + } + /// + /// 变更卡状态 + /// + /// + 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); + } + } +} diff --git a/HKLib/RabbitMq/Config/AcceptEntity.cs b/HKLib/RabbitMq/Config/AcceptEntity.cs new file mode 100644 index 0000000..444942f --- /dev/null +++ b/HKLib/RabbitMq/Config/AcceptEntity.cs @@ -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 + { + /// + /// 队列名称 + /// + public String QueeName { get; set; } + /// + /// 推送模式 + /// + public MQEnum SendType { get; set; } + /// + /// 管道名称 + /// + public String ExchangeName { get; set; } + /// + /// 路由名称 + /// + public String RouteName { get; set; } + } +} diff --git a/HKLib/RabbitMq/Config/IAccept.cs b/HKLib/RabbitMq/Config/IAccept.cs new file mode 100644 index 0000000..df09cb4 --- /dev/null +++ b/HKLib/RabbitMq/Config/IAccept.cs @@ -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(string msg); + } +} diff --git a/HKLib/RabbitMq/Config/MQEnum.cs b/HKLib/RabbitMq/Config/MQEnum.cs new file mode 100644 index 0000000..efe4eea --- /dev/null +++ b/HKLib/RabbitMq/Config/MQEnum.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Text; + +namespace HKLib.RabbitMQ.Config +{ + public enum MQEnum + { + /// + /// 订阅模式 + /// + [Description("订阅模式")] + Sub = 10, + /// + /// 推送模式 + /// + [Description("推送模式")] + Push = 20, + /// + /// 主路由模式 + /// + [Description("主路由模式")] + Top = 30 + } +} diff --git a/HKLib/RabbitMq/Config/MQFactory.cs b/HKLib/RabbitMq/Config/MQFactory.cs new file mode 100644 index 0000000..df084f0 --- /dev/null +++ b/HKLib/RabbitMq/Config/MQFactory.cs @@ -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; + /// + /// 创建链接 + /// + /// + public static IBus CreateMQ() + { + if (Bus == null) + Bus = RabbitHutch.CreateBus(Configer.MqAddress); + return Bus; + } + /// + /// 释放链接 + /// + public static void DisposeBus() + { + Bus?.Dispose(); + } + /// + /// 同步执行 + /// + /// + /// + /// + public static bool SendMQ(PushEntity Param) + { + try + { + if (Bus == null) + CreateMQ(); + new PushManage().SendMQ(Param, Bus); + return true; + } + catch (Exception) + { + return false; + } + } + /// + /// 推荐异步执行 + /// + /// + /// + /// + public static async Task SendMQAsync(PushEntity Param) + { + + if (Bus == null) + CreateMQ(); + await new PushManage().SendMQAsync(Param, Bus); + } + /// + /// 订阅消息 + /// + /// + /// + public static void Subscriber(AcceptEntity Args) where TAccept : IAccept, new() + { + try + { + if (Bus == null) + CreateMQ(); + if (string.IsNullOrEmpty(Args.ExchangeName)) + return; + Expression> 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(message); + methodCall.Compile()(new TAccept()); + } + catch (Exception ex) + { + throw ex; + } + })); + } + catch (Exception ex) + { + + } + + } + } +} diff --git a/HKLib/RabbitMq/Config/PushEntity.cs b/HKLib/RabbitMq/Config/PushEntity.cs new file mode 100644 index 0000000..4d9d61f --- /dev/null +++ b/HKLib/RabbitMq/Config/PushEntity.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace HKLib.RabbitMQ.Config +{ + public class PushEntity + { + /// + /// 推送内容 + /// + public T BodyData { get; set; } + /// + /// 推送模式 + /// + public MQEnum SendType { get; set; } + /// + /// 管道名称 + /// + public String ExchangeName { get; set; } + /// + /// 路由名称 + /// + public String RouteName { get; set; } + } +} diff --git a/HKLib/RabbitMq/Config/PushManage.cs b/HKLib/RabbitMq/Config/PushManage.cs new file mode 100644 index 0000000..2ccc01a --- /dev/null +++ b/HKLib/RabbitMq/Config/PushManage.cs @@ -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 + { + /// + /// 发布消息队列异步 + /// + /// + /// + /// + /// + public async Task SendMQAsync(PushEntity Param, IBus Bus) + { + //one to one + var msg = new Message(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); + + } + }); + } + /// + /// 发布消息队列同步 + /// + /// + /// + /// + public void SendMQ(PushEntity Param, IBus Bus) + { + //one to one + var msg = new Message(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); + } + } +} diff --git a/HKLib/RabbitMq/Publisher/ServiceQueryPush.cs b/HKLib/RabbitMq/Publisher/ServiceQueryPush.cs new file mode 100644 index 0000000..40fd66c --- /dev/null +++ b/HKLib/RabbitMq/Publisher/ServiceQueryPush.cs @@ -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().Value; + public async Task PushMQAsync(T Entity,string Key, MQEnum MQType = MQEnum.Push) + { + //推送模式 + //推送模式下需指定管道名称和路由键值名称 + //消息只会被发送到指定的队列中去 + //订阅模式 + //订阅模式下只需指定管道名称 + //消息会被发送到该管道下的所有队列中 + //主题路由模式 + //路由模式下需指定管道名称和路由值 + //消息会被发送到该管道下,和路由值匹配的队列中去 + PushEntity entity = new PushEntity(); + 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 Entity, string Key, MQEnum MQType = MQEnum.Push) + { + //推送模式 + //推送模式下需指定管道名称和路由键值名称 + //消息只会被发送到指定的队列中去 + //订阅模式 + //订阅模式下只需指定管道名称 + //消息会被发送到该管道下的所有队列中 + //主题路由模式 + //路由模式下需指定管道名称和路由值 + //消息会被发送到该管道下,和路由值匹配的队列中去 + PushEntity entity = new PushEntity(); + 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); + } + } +} diff --git a/HKLib/RabbitMq/Subscriber/ServiceQueryExcute.cs b/HKLib/RabbitMq/Subscriber/ServiceQueryExcute.cs new file mode 100644 index 0000000..80a4276 --- /dev/null +++ b/HKLib/RabbitMq/Subscriber/ServiceQueryExcute.cs @@ -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().Value; + public void ExtuteMQ(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(accept); + }); + } + } +}