| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Threading.Tasks;
- using ConsoleHttp;
- using ConsoleHttp.Model;
- using MqttMsgServer.Dao;
- using MqttMsgServer.HttpService.dto;
- using MqttMsgServer.Model;
- using MqttMsgServer.Redis;
- using MqttMsgServer.Service.Client.Dto;
- using MqttMsgServer.Tools;
- using MQTTnet.Protocol;
- using StackExchange.Redis;
- namespace MqttMsgServer.Service.Client
- {
- public interface IClientInfoService
- {
- ResponseResult AddOrUpdateClient(ClientDto input);
- ResponseResult RegisterClient(ClientDto input);
- ResponseResult UpdateClients(ClientDto input);
- void RefreshClientCache(ClientInfos input);
- ResponseResult ConnectServer(ConnectDto input);
- }
- public class ClientInfoService : IClientInfoService
- {
- //private readonly IRepository<string, ClientInfos> ClientsRepository;
- //private readonly IRepository<String,ReceiveMessageRecord> ReceiveMessageRecordRepository;
- protected RedisHelpers redisCache;
- public ClientInfoService()//IRepository<string, ClientInfos> clientsRepository, IRepository<string, ReceiveMessageRecord> receiveMessageRecordRepository
- {
- //ClientsRepository = clientsRepository;
- //ReceiveMessageRecordRepository = receiveMessageRecordRepository;
- this.redisCache = new RedisHelpers();
- }
- public ResponseResult ConnectServer(ConnectDto input)
- {
- ResponseResult result = new ResponseResult();
- if (string.IsNullOrEmpty(input.ClientName) || string.IsNullOrEmpty(input.ClientId) ||
- string.IsNullOrEmpty(input.Password))
- {
- return StringHelper.CheckError("请求字段为空!");
- }
- //var rs = RedisHelper.Instance.GetStringKey<ClientInfos>(CacheName.ClientCachePrefix + input.ClientId);
- var rs = redisCache.StringGet<ClientInfos>(CacheName.ClientCachePrefix + input.ClientId);
- if (rs == null)
- {
- string lcSql = $"select * from Clients where Id='{input.ClientId}';";
- DataTable dt = SqlDbHelper.ExecuteDataTable(lcSql);
- if (dt != null && dt.Rows.Count > 0)
- {
- rs= StringHelper.ToList<ClientInfos>(dt).FirstOrDefault();
- }
- }
- if (rs == null)
- {
- return StringHelper.CheckError("用户不存在!");
- }
- if (rs.ClientState == DefineConfig.ClientStateClosed)
- {
- return StringHelper.CheckError("账户已被锁定!");
- }
- var pwd = StringHelper.GenerateMD5(input.Password);
- if (pwd != rs.Password)
- {
- return StringHelper.CheckError("密码不正确!");
- }
- // RedisHelper.Instance.SetStringKey(CacheName.ClientCachePrefix + input.ClientId, rs, TimeSpan.FromHours(24));
- redisCache.StringSet(CacheName.ClientCachePrefix + input.ClientId, rs, TimeSpan.FromHours(24));
- return result;
- }
- public void RefreshClientCache(ClientInfos input)
- {
- //RedisHelper.Instance.SetStringKey(CacheName.ClientCachePrefix + input.Id, input, TimeSpan.FromHours(input.Hours));
- redisCache.StringSet(CacheName.ClientCachePrefix + input.Id, input, TimeSpan.FromHours(input.Hours));
- }
- public ResponseResult AddOrUpdateClient(ClientDto input)
- {
- ResponseResult result = new ResponseResult();
- if (string.IsNullOrEmpty(input.ClientName) || string.IsNullOrEmpty(input.Id) ||
- string.IsNullOrEmpty(input.Password))
- {
- return StringHelper.CheckError("编号,名称,密码请求字段为空!");
- }
- var check = GetClientInfo(input.Id);
- //typeof(SqlDbHelper).LogError("1.--查询对象");
- if (check == null)
- {
- ClientInfos c = new ClientInfos()
- {
- Id = input.Id,
- ClientName = input.ClientName,
- ClientState = input.ClientState,
- CreatorDate = DateTime.Now,
- CreatorUserId = 1,
- Password = StringHelper.GenerateMD5(input.Password),
- SystemDesc = input.SystemDesc,
- Hours = input.Hours
- };
- SqlDbHelper.ExecuteNonQuery(c.InsertSql());
- RefreshClientCache(c);
- }
- else
- {
- //typeof(SqlDbHelper).LogError("2.--查询到对象");
- ClientInfos c = new ClientInfos()
- {
- Id = input.Id,
- ClientName = string.IsNullOrEmpty(input.ClientName) ? check.ClientName : input.ClientName,
- ClientState = input.ClientState,
- Password = string.IsNullOrEmpty(input.Password) ? check.Password : StringHelper.GenerateMD5(input.Password),
- SystemDesc = string.IsNullOrEmpty(input.SystemDesc) ? check.SystemDesc : input.SystemDesc,
- Hours = input.Hours,
- CreatorDate = check.CreatorDate,
- CreatorUserId = check.CreatorUserId,
- };
- //typeof(SqlDbHelper).LogError("3.--创建到对象");
- SqlDbHelper.ExecuteNonQuery(c.UpdateSql());
- //typeof(SqlDbHelper).LogError("4.--更新数据库对象");
- RefreshClientCache(c);
- //typeof(SqlDbHelper).LogError("5.--更新缓存");
- }
- return result;
- }
- private ClientInfos GetClientInfo(string id)
- {
- ClientInfos check = null;
- string lcSql = $"select * from Clients where Id='{id}';";
- DataTable dt = SqlDbHelper.ExecuteDataTable(lcSql);
- if (dt != null && dt.Rows.Count > 0)
- {
- check = StringHelper.ToList<ClientInfos>(dt).FirstOrDefault();
- }
- return check;
- }
- public ResponseResult RegisterClient(ClientDto input)
- {
- ResponseResult result = new ResponseResult();
- if (string.IsNullOrEmpty(input.ClientName) || string.IsNullOrEmpty(input.Id) ||
- string.IsNullOrEmpty(input.Password))
- {
- return StringHelper.CheckError("编号,名称,密码请求字段为空!");
- }
- var check = GetClientInfo(input.Id);
-
- if (check != null)
- {
- return StringHelper.CheckError("clientId已经存在!");
- }
- ClientInfos c = new ClientInfos()
- {
- Id = input.Id,
- ClientName = input.ClientName,
- ClientState = input.ClientState,
- CreatorDate = DateTime.Now,
- CreatorUserId = 1,
- Password = StringHelper.GenerateMD5(input.Password),
- SystemDesc = input.SystemDesc,
- Hours = input.Hours
- };
- SqlDbHelper.ExecuteNonQuery(c.InsertSql());
- RefreshClientCache(c);
- return result;
- }
- public ResponseResult UpdateClients(ClientDto input)
- {
- ResponseResult result = new ResponseResult();
- if ( string.IsNullOrEmpty(input.Id) )
- {
- return StringHelper.CheckError("编码字段为空!");
- }
- var check = GetClientInfo(input.Id);
- if (check == null)
- {
- return StringHelper.CheckError("clientId不存在!");
- }
- ClientInfos c = new ClientInfos()
- {
- Id = input.Id,
- ClientName = string.IsNullOrEmpty(input.ClientName)?check.ClientName:input.ClientName,
- ClientState = input.ClientState,
- Password = string.IsNullOrEmpty(input.Password)?check.Password:StringHelper.GenerateMD5(input.Password),
- SystemDesc = string.IsNullOrEmpty(input.SystemDesc)?check.SystemDesc:input.SystemDesc,
- Hours = input.Hours,
- CreatorDate = check.CreatorDate,
- CreatorUserId = check.CreatorUserId,
- };
- SqlDbHelper.ExecuteNonQuery(c.UpdateSql());
- RefreshClientCache(c);
- return result;
- }
- public void WriteCache(string clientId, string topic, string msg)
- {
- var entity = new ReceiveMessageRecord()
- {
- Id = Guid.NewGuid().ToString("N"),
- ClientId = clientId,
- Topic = topic,
- Payload = msg,
- QualityOfServiceLevel= MqttQualityOfServiceLevel.AtLeastOnce,
- CreatorDate = DateTime.Now,
- CreatorUserId = 1
- };
- WriteData(entity);
- }
- public void WriteData(ReceiveMessageRecord pReceiveMessageRecord)
- {
- WriteCache(pReceiveMessageRecord);
- Action ac = () => { WriteRecordToDb(pReceiveMessageRecord); };
- ac.Invoke();
- }
- public void WriteCache(ReceiveMessageRecord pReceiveMessageRecord)
- {
- //List<ReceiveMessageRecord> rs = redisCache.StringGet<List<ReceiveMessageRecord>>(CacheName.TopicMsgCachePrefix + pReceiveMessageRecord.Topic) ?? new List<ReceiveMessageRecord>();
- //if (rs.Any())
- //{
- // DateTime currentDay = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
- // rs = rs.Where(i => i.CreatorDate > currentDay).Take(100).ToList();
- //}
- //rs.Add(pReceiveMessageRecord);
- //redisCache.StringSet(CacheName.TopicMsgCachePrefix + pReceiveMessageRecord.Topic, rs, TimeSpan.FromDays(1));
- int retainMinute = AppSetting.GetInt("redisMsgTTL");
- redisCache.ListRightPush(CacheName.TopicMsgCachePrefix + pReceiveMessageRecord.Topic, pReceiveMessageRecord, TimeSpan.FromMinutes(retainMinute));
- //List<ReceiveMessageRecord> rs2 = redisCache.StringGet<List<ReceiveMessageRecord>>(CacheName.ClientMsgCachePrefix + pReceiveMessageRecord.ClientId) ?? new List<ReceiveMessageRecord>();
- //if (rs2.Any())
- //{
- // DateTime currentDay = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
- // rs2 = rs2.Where(i => i.CreatorDate > currentDay).Take(100).ToList();
- //}
- //rs2.Add(pReceiveMessageRecord);
- //redisCache.StringSet(CacheName.ClientMsgCachePrefix + pReceiveMessageRecord.ClientId, rs2, TimeSpan.FromDays(1));
- redisCache.ListRightPush(CacheName.ClientMsgCachePrefix + pReceiveMessageRecord.ClientId, pReceiveMessageRecord, TimeSpan.FromMinutes(retainMinute));
- }
- public void WriteRecordToDb(ReceiveMessageRecord pReceiveMessageRecord)
- {
- try
- {
- SqlDbHelper.ExecuteNonQuery(pReceiveMessageRecord.InsertSql());
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
-
- }
- }
- }
- }
|