From 43b0d5870d528f23ecd6aeceb6cfd4325188b46f Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期四, 01 七月 2021 15:50:43 +0800
Subject: [PATCH] Revert "1"

---
 HDL_ON/Entity/Room.cs |  396 +++++++++++++++++++++++++++-----------------------------
 1 files changed, 192 insertions(+), 204 deletions(-)

diff --git a/HDL_ON/Entity/Room.cs b/HDL_ON/Entity/Room.cs
index 389c764..609e788 100644
--- a/HDL_ON/Entity/Room.cs
+++ b/HDL_ON/Entity/Room.cs
@@ -46,9 +46,10 @@
         public SpatialInfo(string spatialType)
         {
             roomType = "FLOOR";
-            parentId = DB_ResidenceData.residenceData.CurReginID;
+            parentId = DB_ResidenceData.Instance.CurrentRegion.id;
         }
 
+        public string homeId = "";
         public string roomId = "";
         public string roomName = "";
         //public string roomImage = "";
@@ -56,6 +57,7 @@
         public string roomType = "";
         public string parentId = "";
         public string uid = Guid.NewGuid().ToString();
+        public string floorRoomName = "";
         public string createTime = "";
         public string modifyTime = "";
         ///// <summary>
@@ -65,124 +67,36 @@
         ///// </summary>
         //public bool DeleteSign = false;
 
-
-        protected ResponsePackNew SaveSpatialInfo()
-        {
-            var pm = new HttpServerRequest();
-            var revPack = new ResponsePackNew();
-            if (string.IsNullOrEmpty(createTime) && string.IsNullOrEmpty(modifyTime))
-            {
-                revPack = pm.AddRoom(new List<SpatialInfo>() { this });
-            }
-            else if (string.IsNullOrEmpty(createTime) && !string.IsNullOrEmpty(modifyTime))
-            {
-                revPack = pm.DeleteRoom(new List<string>() { roomId });
-            }
-            else if (!string.IsNullOrEmpty(createTime) && !string.IsNullOrEmpty(modifyTime))
-            {
-                revPack = pm.UpdateRoom(new List<SpatialInfo>() { this });
-            }
-            return revPack;
-        }
-
-        /// <summary>
-        /// 涓嬭浇浜戠鎴块棿鏁版嵁
-        /// </summary>
-        public void DownloadRoomList()
-        {
-            new System.Threading.Thread(() =>
-            {
-                var pm = new HttpServerRequest();
-                var pack = pm.GetRoomList();
-                if (pack.Code == StateCode.SUCCESS)
-                {
-                    var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<SpatialApiPack>(pack.Data.ToString());
-                    if (revData != null)
-                    {
-                        CurrentSpatial.UpdateSpatialList(revData.list, OptionType.Cover);
-                    }
-                }
-                else
-                {
-                    MainPage.Log($"璇诲彇鎴块棿鏁版嵁澶辫触:\r\nCode:{pack.Code}; msg:{pack.message}");
-                }
-            })
-            { IsBackground = true }.Start();
-        }
-
         /// <summary>
         /// 鏇存柊绌洪棿淇℃伅
         /// </summary>
         /// <param name="spatials">绌洪棿淇℃伅</param>
         /// <param name="optionType">鎿嶄綔绫诲瀷锛欰DD/UPDATE/DELETE</param>
-        public void UpdateSpatialList(List<SpatialInfo> spatials,OptionType optionType)
+        public void UpdateSpatialList(List<SpatialInfo> spatials)
         {
             var roomUpdateList = spatials.FindAll((obj) => obj.roomType == "ROOM");
             var floorUpdateList = spatials.FindAll((obj) => obj.roomType == "FLOOR");
+            CurrentSpatial.DeleteAllRoom();
+            FloorList.Clear();
             //澶勭悊鎴块棿鍒楄〃
             if (roomUpdateList != null && roomUpdateList.Count > 0)
             {
-                if (optionType == OptionType.Update)
+                foreach (var newRoom in roomUpdateList)
                 {
-                    foreach (var updateTemp in roomUpdateList)
-                    {
-                        var localRoom = CurrentSpatial.RoomList.Find((obj) => obj.roomId == updateTemp.roomId);
-                        if (localRoom != null)
-                        {
-                            if (localRoom.modifyTime != updateTemp.modifyTime)
-                            {
-                                localRoom.roomName = updateTemp.roomName;
-                                localRoom.roomImage = updateTemp.roomImage;
-                                localRoom.parentId = updateTemp.parentId;
-                                localRoom.createTime = updateTemp.createTime;
-                                localRoom.modifyTime = updateTemp.modifyTime;
-                                localRoom.SaveRoomData(false);
-                            }
-                        }else
-                        {
-                            updateTemp.SaveRoomData(false);
-                        }
-                    }
-                }
-                else if (optionType == OptionType.Cover)
-                {
-                    CurrentSpatial.DeleteAllRoom();
-                    foreach(var newRoom in roomUpdateList)
-                    {
-                        newRoom.SaveRoomData(false);
-                        RoomList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(
-                            Newtonsoft.Json.JsonConvert.SerializeObject(newRoom)));
-                    }
+                    newRoom.SaveRoomFile();
+                    RoomList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(
+                        Newtonsoft.Json.JsonConvert.SerializeObject(newRoom)));
                 }
             }
             //妤煎眰鎴块棿鍒楄〃
             if (floorUpdateList != null && floorUpdateList.Count > 0)
             {
-                if (optionType == OptionType.Update)
+                foreach (var updateTemp in floorUpdateList)
                 {
-                    foreach(var updateTemp in floorUpdateList)
-                    {
-                        var localFloor = FloorList.Find((obj) => obj.roomId == updateTemp.roomId);
-                        if (localFloor == null)
-                        {
-                            FloorList.Add(updateTemp);
-                        }
-                        else
-                        {
-                            localFloor = updateTemp;
-                        }
-                    }
-                }
-                if(optionType == OptionType.Cover)
-                {
-                    FloorList.Clear();
-                    foreach (var updateTemp in floorUpdateList)
-                    {
-                        FloorList.Add(updateTemp);
-                    }
+                    FloorList.Add(updateTemp);
                 }
                 var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
-                FileUtils.WriteFileByBytes(dataSavePath, ssd);
+                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
             }
         }
 
@@ -198,24 +112,34 @@
         {
             get
             {
+                if (MainPage.NoLoginMode)
+                {
+                    if (floors == null)
+                    {
+                        floors = new List<SpatialInfo>();
+                    }
+                    return floors;
+                }
+
                 if (floors == null)
                 {
                     try
                     {
-                        var floorsDataBytes = FileUtils.ReadFile("FloorsData");
+                        var floorsDataBytes = Common.FileUtlis.Files.ReadFile("FloorsData");
                         if (floorsDataBytes != null)
                         {
                             var floorsDataString = System.Text.Encoding.UTF8.GetString(floorsDataBytes);
                             MainPage.Log(floorsDataString);
                             floors = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(floorsDataString);
-                            if (floors == null)
-                            {
-                                floors = new List<SpatialInfo>();
-                            }
+                        }
+                        if(floors == null)
+                        {
+                            floors = new List<SpatialInfo>();
                         }
                     }
                     catch (Exception ex)
                     {
+                            floors = new List<SpatialInfo>();
                         MainPage.Log($"妤煎眰鏁版嵁鍒濆鍖栧け璐ワ細{ex.Message}");
                     }
                 }
@@ -247,67 +171,74 @@
         /// 澧炲姞妤煎眰
         /// </summary>
         /// <param name="floor"></param>
-        public void AddFloor(SpatialInfo floor)
+        public string AddFloor(SpatialInfo floor, out SpatialInfo newFloor)
         {
-            if (floor.SaveFloorData() == StateCode.SUCCESS)
+            var pm = new HttpServerRequest();
+            var revPack = new ResponsePackNew();
+            revPack = pm.AddRoom(new List<SpatialInfo>() { floor });
+            if (revPack.Code == StateCode.SUCCESS)
             {
-                FloorList.Add(floor);
+                var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(revPack.Data.ToString());
+                if (revData.Count > 0)
+                {
+                    var addFloor = revData.Find((obj) => obj.uid == floor.uid);
+                    if (addFloor != null)
+                    {
+                        CurrentSpatial.FloorList.Add(addFloor);
+                        floor = addFloor;
+                    }
+                }
+                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(CurrentSpatial.FloorList));
+                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
             }
+            else
+            {
+                Utlis.ShowTip(Language.StringByID(StringId.AddFloorFail) + "\r\nCode:" + revPack.Code);
+            }
+            newFloor = floor;
+            return revPack.Code;
         }
         /// <summary>
         /// 鏇存柊妤煎眰淇℃伅
         /// </summary>
         /// <param name="floor"></param>
-        public void UpdateFloor()
+        public string UpdateFloor(SpatialInfo floor)
         {
-            SaveFloorData();
+            var pm = new HttpServerRequest();
+            var revPack = pm.UpdateRoom(new List<SpatialInfo>() { floor });
+            if (revPack.Code == StateCode.SUCCESS)
+            {
+                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
+                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
+            }
+            return revPack.Code;
         }
         /// <summary>
         /// 鍒犻櫎妤煎眰
         /// </summary>
         /// <param name="floor"></param>
-        public void DelFloor(SpatialInfo floor)
+        public string DelFloor(string floorId)
         {
-            if (floor == null)
-                return;
+            if (string.IsNullOrEmpty( floorId))
+                return "";
             var pm = new HttpServerRequest();
-            var code = pm.DeleteRoom(new List<string>() { floor.roomId }).Code;
+            var code = pm.DeleteRoom(new List<string>() { floorId }).Code;
             if (code == StateCode.SUCCESS)
             {
-                var tRoom = FloorList.Find((obj) => obj.uid == floor.uid);
+                var tRoom = CurrentSpatial.FloorList.Find((obj) => obj.roomId == floorId);
                 if (tRoom != null)
                 {
-                    FloorList.Remove(floor);
+                    CurrentSpatial.FloorList.Remove(tRoom);
                 }
                 var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
-                FileUtils.WriteFileByBytes(dataSavePath, ssd);
-                MainPage.Log($"Save floor Data");
+                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
             }
             else
             {
-                Utlis.ShowTip(Language.StringByID(StringId.DeleteFloorFail) + "Code:" + code);
+                Utlis.ShowTip(Language.StringByID(StringId.DeleteFloorFail) + "\r\nCode:" + code);
             }
+            return code;
         }
-
-        /// <summary>
-        /// 淇濆瓨妤煎眰鏁版嵁
-        /// </summary>
-        string SaveFloorData()
-        {
-            var pack = SaveSpatialInfo();
-            if (pack.Code == StateCode.SUCCESS)
-            {
-                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
-                FileUtils.WriteFileByBytes(dataSavePath, ssd);
-                MainPage.Log($"Save floor Data");
-            }
-            else
-            {
-                Utlis.ShowTip(Language.StringByID(StringId.EditRoomInfoFail) + "Code:" + pack.Code);
-            }
-            return pack.Code;
-        }
-
 
         #endregion
 
@@ -323,18 +254,26 @@
         {
             get
             {
+                if(MainPage.NoLoginMode )
+                {
+                    if (rooms == null)
+                    {
+                        rooms = new List<Room>();
+                    }
+                    return rooms;
+                }
                 if (rooms == null)
                 {
                     try
                     {
                         rooms = new List<Room> { };
-                        var filePathList = FileUtils.ReadFiles();
+                        var filePathList = Common.FileUtlis.Files.ReadFiles();
 
                         foreach (var filePath in filePathList)
                         {
                             if (filePath.StartsWith("RoomData_"))
                             {
-                                var roomDataBytes = FileUtils.ReadFile(filePath);
+                                var roomDataBytes = Common.FileUtlis.Files.ReadFile(filePath);
                                 var roomDataString = System.Text.Encoding.UTF8.GetString(roomDataBytes);
                                 var room = Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(roomDataString);
                                 if (room != null)
@@ -356,50 +295,84 @@
         /// 澧炲姞鎴块棿
         /// </summary>
         /// <param name="room"></param>
-        public void AddRoom(Room room)
+        public string AddRoom(Room room,out Room newRoom)
         {
-            if (room.SaveRoomData() == StateCode.SUCCESS)
+            var pm = new HttpServerRequest();
+            var pack = pm.AddRoom(new List<SpatialInfo>() { room });
+
+            if (pack.Code == StateCode.SUCCESS)
             {
-                RoomList.Add(room);
+                var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(pack.Data.ToString());
+                if (revData != null)
+                {
+                    if (revData.Count > 0)
+                    {
+                        var tempRoom = revData.Find((obj) => obj.uid == room.uid);
+                        if (tempRoom != null)
+                        {
+                            room.roomId = tempRoom.roomId;
+                            CurrentSpatial.RoomList.Add(room);
+                            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(room));
+                            Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
+                        }
+                    }
+                }
             }
+            newRoom = room;
+            return pack.Code;
         }
         /// <summary>
         /// 鍒犻櫎鎴块棿
         /// </summary>
         /// <param name="room"></param>
-        public void DelRoom(Room room)
+        public string DelRoom(Room room)
         {
-            new System.Threading.Thread(() =>
+            var pm = new HttpServerRequest();
+            var pack = pm.DeleteRoom(new List<string>() { room.roomId });
+            if (pack.Code == StateCode.SUCCESS)
             {
-                var pm = new HttpServerRequest();
-                var pack = pm.DeleteRoom(new List<string>() { room.roomId });
-                if (pack.Code == StateCode.SUCCESS)
+                var tRoom = RoomList.Find((obj) => obj.roomId == room.roomId);
+                if (tRoom != null)
                 {
-                    var tRoom = RoomList.Find((obj) => obj.uid == room.uid);
-                    if (tRoom != null)
-                    {
-                        RoomList.Remove(room);
-                    }
-                    FileUtils.DeleteFile(room.dataSavePath);
+                    CurrentSpatial.RoomList.Remove(room);
                 }
-            })
-            { IsBackground = true }.Start();
+                Common.FileUtlis.Files.DeleteFile(room.dataSavePath);
+            }
+            return pack.Code;
         }
         /// <summary>
         /// 鍒犻櫎鎵�鏈夋埧闂�
         /// </summary>
         public void DeleteAllRoom()
         {
-            foreach(var localRoom in RoomList)
+            for(int i=0;i< RoomList.Count;)
             {
-                FileUtils.DeleteFile(localRoom.dataSavePath);
+                Common.FileUtlis.Files.DeleteFile(RoomList[i].dataSavePath);
+                RoomList.RemoveAt(i);
             }
-            RoomList.Clear();
+            rooms = null;
         }
+
+        public void Clear()
+        {
+            if (_currentSpatial != null)
+            {
+                if (_currentSpatial.floors != null)
+                {
+                    _currentSpatial.floors.Clear();
+                }
+                if (_currentSpatial.rooms != null)
+                {
+                    _currentSpatial.rooms.Clear();
+                }
+                _currentSpatial = null;
+            }
+        }
+
         /// <summary>
         /// 鍒濆鍖栨瘡涓埧闂寸殑鍔熻兘鏁版嵁
         /// </summary>
-        public void InitRoomFunction()
+        public void InitRoomListFunctions()
         {
             new System.Threading.Thread(() =>
             {
@@ -408,8 +381,7 @@
                     //鍒濆鍖栦綇瀹呮墍鏈夋埧闂村姛鑳芥暟鎹�
                     foreach (var r in RoomList)
                     {
-                        r.GetRoomFunctions(true);
-                        r.GetRoomScenes(true);
+                        InitRoomFuntion(r);
                     }
                 }
                 catch (Exception ex)
@@ -419,26 +391,42 @@
             })
             { IsBackground = true }.Start();
         }
+        /// <summary>
+        /// 鍒濆鍖栨埧闂村姛鑳�
+        /// </summary>
+        /// <param name="tempRoom"></param>
+        public void InitRoomFuntion(Room tempRoom)
+        {
+            tempRoom.GetRoomFunctions(true);
+            tempRoom.GetRoomScenes(true);
+        }
 
         /// <summary>
-        /// 淇濆瓨鎴块棿鏁版嵁
+        /// 淇濆瓨鎴块棿鏁版嵁鏂囦欢
         /// </summary>
-        public string SaveRoomData(bool upServer = true)
+        public void SaveRoomFile()
         {
-            var packCode = StateCode.SUCCESS;
-            bool needSave = true;
-            if (upServer)
-            {
-                packCode = SaveSpatialInfo().Code;
-            }
-            if (needSave)
-            {
-                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
-                FileUtils.WriteFileByBytes(dataSavePath, ssd);
-                MainPage.Log($"Save Room Data {roomName} : {uid}");
-            }
-            return packCode;
+            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
+            Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
         }
+        /// <summary>
+        /// 鏇存柊鎴块棿鏁版嵁
+        /// </summary>
+        public void UpdataRoomInfo()
+        {
+            new System.Threading.Thread(() =>
+            {
+                var pm = new HttpServerRequest();
+                var pack = pm.UpdateRoom(new List<SpatialInfo>() { this });
+                this.SaveRoomFile();
+                if (pack.Code != StateCode.SUCCESS)
+                {
+                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
+                }
+            })
+            { IsBackground = true }.Start();
+        }
+      
         #endregion
 
     }
@@ -482,9 +470,9 @@
         {
             get
             {
-                if (FloorList.Count > 0)
+                if (CurrentSpatial.FloorList.Count > 0)
                 {
-                    var f = FloorList.Find((obj) => obj.uid == parentId);
+                    var f = CurrentSpatial.FloorList.Find((obj) => obj.uid == parentId);
                     if (f != null)
                     {
                         return f.roomName + " ";
@@ -494,10 +482,10 @@
             }
         }
 
-        /// <summary>
-        /// 鎴块棿鍔熻兘鍒楄〃
-        /// </summary>
-        List<Function> functions = new List<Function>();
+        ///// <summary>
+        ///// 鎴块棿鍔熻兘鍒楄〃
+        ///// </summary>
+        //List<Function> functions = new List<Function>();
         /// <summary>
         /// 鑾峰彇鎴块棿鍔熻兘鍒楄〃
         /// </summary>
@@ -505,13 +493,13 @@
         {
             if (needRefresh)
             {
-                functions = new List<Function>();
             }
+            var functions = new List<Function>();
             if (functions.Count == 0)
             {
-                foreach (var f in DB_ResidenceData.functionList.GetAllFunction())
+                foreach (var f in FunctionList.List.GetDeviceFunctionList())
                 {
-                    if (f.roomIds.Contains(uid) && uid != null)
+                    if (f.roomIds.Contains(roomId) && roomId != null)
                     {
                         functions.Add(f);
                     }
@@ -519,22 +507,22 @@
             }
             return functions;
         }
-        /// <summary>
-        /// 澧炲姞鎴块棿鍔熻兘
-        /// 鎿嶄綔鐨勬槸缂撳瓨鏁版嵁锛屼笉鐢ㄤ繚瀛�
-        /// </summary>
-        public void AddRoomFunction(Function function)
-        {
-            functions.Add(function);
-        }
-        /// <summary>
-        /// 鍒犻櫎鎴块棿鍔熻兘
-        /// 鎿嶄綔鐨勬槸缂撳瓨鏁版嵁锛屼笉鐢ㄤ繚瀛�
-        /// </summary>
-        public void RemoveRoomFunction(Function function)
-        {
-            functions.Remove(functions.Find((obj) => obj.sid == function.sid));
-        }
+        ///// <summary>
+        ///// 澧炲姞鎴块棿鍔熻兘
+        ///// 鎿嶄綔鐨勬槸缂撳瓨鏁版嵁锛屼笉鐢ㄤ繚瀛�
+        ///// </summary>
+        //public void AddRoomFunction(Function function)
+        //{
+        //    functions.Add(function);
+        //}
+        ///// <summary>
+        ///// 鍒犻櫎鎴块棿鍔熻兘
+        ///// 鎿嶄綔鐨勬槸缂撳瓨鏁版嵁锛屼笉鐢ㄤ繚瀛�
+        ///// </summary>
+        //public void RemoveRoomFunction(Function function)
+        //{
+        //    functions.Remove(functions.Find((obj) => obj.sid == function.sid));
+        //}
         /// <summary>
         /// 鎴块棿鍦烘櫙鍒楄〃
         /// </summary>
@@ -551,9 +539,9 @@
             }
             if (scenes.Count == 0)
             {
-                foreach (var s in DB_ResidenceData.functionList.scenes)
+                foreach (var s in FunctionList.List.scenes)
                 {
-                    if (s.roomIds.Contains(uid) && uid != null)
+                    if (s.roomIds.Contains(roomId) && roomId != null)
                     {
                         scenes.Add(s);
                     }

--
Gitblit v1.8.0