using Models; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Models.ModelItems; using Services.Tool; using static Services.Tool.HttpRequestHelp; using SqlSugar; using UI.Lib; namespace Services.Manager { public class FaceServer { public static List CheckfaceSNHosts(string faceSN) { var RES = CheckfaceSN(faceSN); List RESDATA = new List(); List HOS = new List(); if (RES == null) { return null; } else { foreach (var item in RES) { int HOTELID = 0; int RoomId = 0; if (!int.TryParse(item.HotelCode, out HOTELID)) { HOTELID = 0; }; RoomId = int.Parse(item.RoomId == null ? 0.ToString() : item.RoomId.ToString()); // 已经分配酒店 if (HOTELID > 0 ) { //未分配房间 if (RoomId == 0) { RESDATA.Add(new Hosts { Id = RoomId,HotelID = HOTELID,HotelName = Cache.CacheHelp.cacheHotels.Single(x=> x.Id == HOTELID ).Name,RoomNumber = "暂无绑定房间" }); } else { var TEMP = HOS.FirstOrDefault(X => X.Id == RoomId); if (TEMP == null) { HOS.AddRange(HttpRequestHelp.GetHosts(int.Parse(item.HotelCode))); var str = JsonConvert.SerializeObject(HOS.Select(x=>x.Id)); TEMP = HOS.FirstOrDefault(X => X.Id == RoomId); } if (TEMP != null) RESDATA.Add(TEMP); else { RESDATA.Add(new Hosts { Id = RoomId, HotelID = HOTELID, HotelName = Cache.CacheHelp.cacheHotels.Single(x => x.Id == HOTELID).Name, RoomNumber = "绑定房间已经被删除" }); // 绑定的房间被删除了 //throw new Exception("人脸机绑定的房间ID没有在房间列表找到 ~"); } //return null; } } } } return RESDATA; } /// /// 检查人脸机是否已经被绑定的 /// /// /// public static List CheckfaceSN(string faceSN) { return Test_Feac.Db.Queryable().Where(X=>X.SerialNo == faceSN).ToList(); //($"select * from DeviceManage WHERE SerialNo = '{faceSN}'").ToList(); } public static DeviceManage SelfaceSN_RoomId(int RoomId) { return Test_Feac.Db.Queryable().Where(X => X.RoomId == RoomId).First(); } public static List SelfaceSN_HotelID(int HotelID) { return Test_Feac.Db.Queryable().Where(X => X.HotelCode == HotelID+"").ToList(); //using (AuthorityDB DB = new AuthorityDB()) //{ // SqlSugarBase.Db.Queryable() // return DB.Database.SqlQuery($"select * from Face.DBO.DeviceManage WHERE HotelCode = @HotelID", new SqlParameter("@HotelID", HotelID)).ToList(); //} } /// /// 根据房间号绑定人脸机 往人脸机表添加字段 /// RETURN /// 0 成功 /// 1 已经注册已经绑定酒店 /// 2 已经注册更新失败 /// 3 未注册为分配酒店 添加注册 添加酒店是啊比 /// 4 未能预计的结果-- /// 5 数据不符合 /// 6 解绑失败 /// /// /// /// /// public static int GetfaceSN(UserInfo userinfo = null, string faceSN = "", string roomNumber = "", int hotelID = 0, string faceAddress = "",string roomID = "", bool isjb = false) { int RES = 5; // 如果传入 酒店房间号 就是绑定 判断是否已经绑定 if (!isjb) { var olddata = CheckfaceSN(faceSN); //已经注册已经绑定酒店 if (olddata != null && olddata.Count > 0 && olddata[0].HotelCode != "0" && olddata[0].RoomId > 0) { //已经绑定 房间 RES = 1; } else { Test_Feac.Db.Ado.ExecuteCommand($"UPDATE DeviceManage SET bindingStatus = 0, RoomId = '0',faceAddress = @faceAddress, HotelCode = 0 WHERE RoomId = @roomID and HotelCode = @hotelID; ", new SugarParameter("@SerialNo", faceSN), new SugarParameter("@roomID", roomID), new SugarParameter("@hotelID", hotelID), new SugarParameter("@faceAddress", faceAddress)); //AuthorityDB DB = new AuthorityDB(); ////如果之前有 SN 绑定房间 直接解绑 //DB.Database.ExecuteSqlCommand($"UPDATE Face.DBO.DeviceManage SET bindingStatus = 'false', RoomId = '0',faceAddress = @faceAddress, HotelCode = 0 WHERE RoomId = @roomID and HotelCode = @hotelID; ", new SqlParameter("@SerialNo", faceSN), new SqlParameter("@roomID", roomID), new SqlParameter("@hotelID", hotelID), new SqlParameter("@faceAddress", faceAddress)); //已经注册 if (olddata != null && olddata.Count > 0) { if (Test_Feac.Db.Ado.ExecuteCommand($"UPDATE DeviceManage SET bindingStatus = 1,RoomId = @roomID ,faceAddress = @faceAddress, HotelCode = @hotelID WHERE SerialNo = @SerialNo", new SugarParameter("@SerialNo", faceSN), new SugarParameter("@roomID", roomID), new SugarParameter("@hotelID", hotelID), new SugarParameter("@faceAddress", faceAddress)) > 0) { RES = 0; } else { RES = 2; } } else { //未注册为分配酒店 添加注册 添加酒店 [Status], faceAddress, bindingStatus) values('6926895501368', 2, '968', 'false', if (olddata == null || olddata.Count <= 0) { if (Test_Feac.Db.Ado.ExecuteCommand($"insert DeviceManage(SerialNo, HotelCode, RoomId,`Status`, faceAddress, bindingStatus) values(@SerialNo, @hotelID, @roomID,0, @faceAddress, 1); ", new SugarParameter("@SerialNo", faceSN), new SugarParameter("@roomID", roomID), new SugarParameter("@hotelID", hotelID), new SugarParameter("@faceAddress", faceAddress)) > 0) { RES = 0; } else { RES = 3; } } else { LogHelp.WriteExceptionLog(new Exception("未能预计的结果--" + JsonConvert.SerializeObject(olddata))); RES = 4; } } }; } else { //执行解绑 if (!string.IsNullOrEmpty(faceSN) && roomID != "0" && hotelID != 0) { //using (AuthorityDB DB = new AuthorityDB()) //{ if (Test_Feac.Db.Ado.ExecuteCommand($"UPDATE DeviceManage SET RoomId = '0' ,faceAddress = @faceAddress, HotelCode ='0' WHERE SerialNo = @SerialNo and HotelCode = @hotelID and RoomId = @roomID", new SugarParameter("@SerialNo", faceSN), new SugarParameter("@roomID", roomID), new SugarParameter("@hotelID", hotelID), new SugarParameter("@faceAddress", faceAddress)) >= 0) { RES = 0; } else { RES = 6; } //} } } if (RES == 0) { List logs = new List(); var Ip = IPHelper.GetIP(); logs.Add(new MACLogs() { HotelID = hotelID, MAC = faceSN, roomNumber = roomNumber, userid = userinfo.Id, roomID = roomID, AppType = 1, type = isjb? 1 : 0 }); Task.Run(() => { try { if (faceAddress == 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) { faceAddress = locs.data[0].location; } } } } catch (Exception ex) { LogHelp.WriteExceptionLog(ex, "日志错误"); } finally { try { lock (HostsServer.LOCK) { 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 = faceAddress; item.ActionId = ActionId; item.createtime = DateTime.Now; item.Status = 0; SqlSugarBase.Db.Insertable(item).ExecuteCommand(); } } } catch (Exception ex) { LogHelp.WriteExceptionLog(ex); } } }); } return RES; } } }