Files
Web_AuthorityManagement_Mvc…/Services/Manager/HostsServer.cs
2025-11-20 09:51:24 +08:00

167 lines
6.7 KiB
C#

using Models;
using Models.ModelItems;
using Services.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Services.Tool.HttpRequestHelp;
namespace Services.Manager
{
public class HostsServer
{
public static object LOCK = new object();
static string macsql = "select TOP 1 MAC,Id,RoomStatusID,HotelID,[RoomNumber],[Status] = convert(int, convert(nvarchar(50),[Status])) ,[Desc] = [remark],[CreateTime] =[registerdate] from BLW.CRICS.[dbo].tb_Hosts where MAC = @MAC";
static string macup = @"
--参数 @MAC 新的MAC @ID 为需要更新mac的id
begin tran
DECLARE @HostsId int;
DECLARE @oldmac nvarchar(50);
DECLARE @HostsIdoldmac nvarchar(50);
SET @HostsId = -1;
SET @oldmac = '-1';
SET @HostsIdoldmac = '-1';
SELECT TOP 1 @HostsId = id, @oldmac= MAC FROM tb_Hosts where MAC = @MAC
SELECT TOP 1 @HostsIdoldmac = MAC FROM tb_Hosts where Id = @ID and mac is not null and mac!=''
PRINT convert(varchar,@HostsId) +'&'+ @oldmac+'&'+@HostsIdoldmac
update tb_Hosts set MAC='' where id = @HostsId
update tb_Hosts set MAC=@MAC where Id = @ID
if @@ERROR>0
begin
rollback tran
end
ELSE
BEGIN
COMMIT TRAN
END
";
static string sql = " select MAC,Id,RoomStatusID,HotelID,[RoomNumber],[Status] = convert(int, convert(nvarchar(50),[Status])) ,[Desc] = [remark],[CreateTime] =[registerdate] from BLW.CRICS.[dbo].tb_Hosts where HotelID = @HotelID";
/// <summary>
/// 获取房间信息
/// </summary>
/// <param name="HotelID">酒点id</param>
/// <returns></returns>
public static List<HostsAdd> GetHostsInfo(int HotelID)
{
return HttpRequestHelp.GetHosts(HotelID);
}
/// <summary>
/// 检查MAC是否已经绑定
/// </summary>
/// <param name="MAC">MAC</param>
/// <returns></returns>
public static List<Hosts> CheckMAC(string MAC = "")
{
string newmac = string.Empty;
for (int i = 0; i < MAC.Length; i += 2)
{
newmac += MAC.Substring(i, 2) + "-";
}
newmac = newmac.Substring(0, newmac.Length - 1);
var res = new List<Hosts>();
res = HttpRequestHelp.GetHostByMAC(newmac);
if (res.Count == 0)
return null;
else
return res;
}
public static bool GetMAC(string MAC = "", int HotelID = 0, string roomID = "", string roomNumber = "", UserInfo userinfo = null, string loc = null)
{
var Ip = IPHelper.GetIP();
string newmac = string.Empty;
if (!string.IsNullOrEmpty(MAC)) {
for (int i = 0; i < MAC.Length; i += 2)
{
newmac += MAC.Substring(i, 2) + "-";
}
newmac = newmac.Substring(0, newmac.Length - 1);
}
List<MACLogs> logs = new List<MACLogs>();
logs.Add(new MACLogs()
{
AppType = 1,
HotelID = HotelID,
MAC = newmac,
roomNumber = roomNumber,
roomID = roomID,
userid = userinfo.Id,
type = MAC==""?1:0
});
int Status = 0;
if (HttpRequestHelp.SetHostMAC(HotelID, newmac, roomNumber)) { Status = 1; };
Task.Run(() =>
{
try
{
if (loc == null)
{
Data locs = HttpRequestHelp.GetIp($@"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={Ip}&co=&resource_id=6006&oe=utf8");
if (locs != null && locs.status == 0)
{
if (locs.data.Count > 0)
{
loc = locs.data[0].location;
}
}
}
}
catch (Exception ex)
{
LogHelp.WriteExceptionLog(ex, "日志错误");
}
finally
{
lock (LOCK)
{
try
{
var maclogs = SqlSugarBase.Db.Queryable<MACLogs>().Select(x => x.ActionId).ToList();
int ActionId = (maclogs.Count > 0 ? maclogs.Max():0) + 1;
foreach (var item in logs)
{
item.Ip = Ip;
item.location = loc;
item.ActionId = ActionId;
item.createtime = DateTime.Now;
item.Status = Status;
SqlSugarBase.Db.Insertable(item).ExecuteCommand();
}
}
catch (Exception ex)
{
LogHelp.WriteExceptionLog(ex);
}
}
}
});
return Status == 1;
}
/// <summary>
/// 写入用户反馈的错误
/// </summary>
public static bool AddErrorInfo(int userid, string MAC, int HotelID, int type,string roomNumber)
{
try
{
SqlSugarBase.Db.Insertable(new ErrorInfo() { createtime = DateTime.Now, type = type, status = 0, userid = userid, roomNumber = roomNumber, HotelID = HotelID, MAC = MAC }).ExecuteCommand();
}
catch (Exception ex)
{
LogHelp.WriteExceptionLog(ex, "反馈信息错误");
return false;
}
return true;
}
}
}