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"; /// /// 获取房间信息 /// /// 酒点id /// public static List GetHostsInfo(int HotelID) { return HttpRequestHelp.GetHosts(HotelID); } /// /// 检查MAC是否已经绑定 /// /// MAC /// public static List 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(); 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 logs = new List(); 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().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; } /// /// 写入用户反馈的错误 /// 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; } } }