From 23ca39219c5075d829d14137cb54896a24ae513d Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期五, 04 十二月 2020 13:57:08 +0800
Subject: [PATCH] 20201204

---
 HDL_ON/Entity/Room.cs |  357 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 282 insertions(+), 75 deletions(-)

diff --git a/HDL_ON/Entity/Room.cs b/HDL_ON/Entity/Room.cs
index 5c0cdcb..91e78cd 100644
--- a/HDL_ON/Entity/Room.cs
+++ b/HDL_ON/Entity/Room.cs
@@ -1,12 +1,13 @@
 锘縰sing System;
 using System.Collections.Generic;
+using HDL_ON.DAL.Server;
 
 namespace HDL_ON.Entity
 {
     /// <summary>
     /// 璇诲彇鏈嶅姟鍣ㄧ┖闂翠俊鎭繑鍥炲埌鏁版嵁鍖�
     /// </summary>
-    public class RoomApiPack
+    public class SpatialApiPack
     {
         public List<SpatialInfo> list = new List<SpatialInfo>();
 
@@ -25,19 +26,47 @@
     /// </summary>
     public class SpatialInfo
     {
+        static SpatialInfo _currentSpatial;
+        public static SpatialInfo CurrentSpatial
+        {
+            get
+            {
+                if (_currentSpatial == null)
+                {
+                    _currentSpatial = new SpatialInfo();
+                }
+                return _currentSpatial;
+            }
+        }
+        public SpatialInfo()
+        {
+        }
+
+        public SpatialInfo(string spatialType)
+        {
+            roomType = "FLOOR";
+            parentId = DB_ResidenceData.residenceData.CurReginID;
+        }
+
         public string roomId = "";
         public string roomName = "";
         public string roomImage = "";
         public string roomType = "";
         public string parentId = "";
         public string uid = Guid.NewGuid().ToString();
-
         public string createTime = "";
         public string modifyTime = "";
+        /// <summary>
+        /// 鍒犻櫎鏍囪
+        /// 闇�瑕佸垹闄ゆ暟鎹椂锛屾爣璁颁负锛歵rue
+        /// 鐢变簯绔垹闄ゆ垚鍔熶箣鍚庯紝杩斿洖鏁版嵁鍐嶆竻闄ゆ湰鍦版暟鎹�
+        /// </summary>
+        public bool DeleteSign = false;
+
 
         protected void SaveSpatialInfo()
         {
-            var pm = new DAL.Server.HttpServerRequest();
+            var pm = new HttpServerRequest();
             if (string.IsNullOrEmpty(createTime) && string.IsNullOrEmpty(modifyTime))
             {
                 pm.AddRoom(new List<SpatialInfo>() { this });
@@ -53,55 +82,111 @@
         }
 
         /// <summary>
-        /// 鏇存柊妤煎眰淇℃伅
+        /// 涓嬭浇浜戠鎴块棿鏁版嵁
         /// </summary>
-        public void UpdateSpatialInfoData()
+        public void DownloadRoomList()
         {
-            if (roomType == "ROOM")
+            new System.Threading.Thread(() =>
             {
-
-            }
-            else if (roomType == "FLOOR")
-            {
-                var tempFloor = Floor.InsFloor.Floors.Find((obj) => obj.roomId == roomId);
-                if (tempFloor == null)
+                var pm = new HttpServerRequest();
+                var pack = pm.GetRoomList();
+                if (pack.Code == StateCode.SUCCESS)
                 {
-                    Floor.InsFloor.Floors.Add(this as Floor);
+                    var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<SpatialApiPack>(pack.Data.ToString());
+                    if (revData != null)
+                    {
+                        CurrentSpatial.UpdateSpatialList(revData.list, OptionType.Cover);
+                    }
                 }
                 else
                 {
-                    tempFloor = this as Floor;
+                    MainPage.Log($"璇诲彇鎴块棿鏁版嵁澶辫触:\r\nCode:{pack.Code}; msg:{pack.message}");
                 }
-                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(Floor.InsFloor.Floors));
-                FileUtils.WriteFileByBytes(Floor.InsFloor.FloorDataSavePath, ssd);
-                MainPage.Log("鏇存柊妤煎眰淇℃伅");
-            }
+            })
+            { IsBackground = true }.Start();
         }
-    }
 
-    /// <summary>
-    /// 鎴块棿瀵硅薄
-    /// </summary>
-    [Serializable]
-    public class Floor : SpatialInfo
-    {
-        #region 妤煎眰鏁版嵁鐩稿叧
-        static Floor ins_Floor ;
-        public static Floor InsFloor {
-            get {
-                if(ins_Floor== null)
+        /// <summary>
+        /// 鏇存柊绌洪棿淇℃伅
+        /// </summary>
+        /// <param name="spatials">绌洪棿淇℃伅</param>
+        /// <param name="optionType">鎿嶄綔绫诲瀷锛欰DD/UPDATE/DELETE</param>
+        public void UpdateSpatialList(List<SpatialInfo> spatials,OptionType optionType)
+        {
+            var roomUpdateList = spatials.FindAll((obj) => obj.roomType == "ROOM");
+            var floorUpdateList = spatials.FindAll((obj) => obj.roomType == "FLOOR");
+            //澶勭悊鎴块棿鍒楄〃
+            if (roomUpdateList != null && roomUpdateList.Count > 0)
+            {
+                if (optionType == OptionType.Update)
                 {
-                    ins_Floor = new Floor();
+                    foreach (var updateTemp in roomUpdateList)
+                    {
+                        var localRoom = CurrentSpatial.RoomList.Find((obj) => obj.roomId == updateTemp.roomId);
+                        if (localRoom != null)
+                        {
+                            localRoom.roomName = updateTemp.roomName;
+                            localRoom.roomImage = updateTemp.roomImage;
+                            localRoom.parentId = updateTemp.parentId;
+                            localRoom.createTime = updateTemp.createTime;
+                            localRoom.modifyTime = updateTemp.modifyTime;
+                            localRoom.SaveRoomData();
+                        }else
+                        {
+                            updateTemp.SaveRoomData();
+                        }
+                    }
                 }
-                return ins_Floor;
+                else if (optionType == OptionType.Cover)
+                {
+                    CurrentSpatial.DeleteAllRoom();
+                    foreach(var newRoom in roomUpdateList)
+                    {
+                        newRoom.SaveRoomData();
+                    }
+                }
+            }
+            //妤煎眰鎴块棿鍒楄〃
+            if (floorUpdateList != null && floorUpdateList.Count > 0)
+            {
+                if (optionType == OptionType.Update)
+                {
+                    foreach(var updateTemp in floorUpdateList)
+                    {
+                        var localFloor = FloorList.Find((obj) => obj.roomId == updateTemp.roomId);
+                        if (localFloor == null)
+                        {
+                            FloorList.Add(updateTemp);
+                        }
+                        else
+                        {
+                            localFloor = updateTemp;
+                        }
+                    }
+                    CurrentSpatial.SaveFloorData();
+                }
+                if(optionType == OptionType.Cover)
+                {
+                    FloorList.Clear();
+                    foreach (var updateTemp in floorUpdateList)
+                    {
+                        FloorList.Add(updateTemp);
+                    }
+                    var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
+                    FileUtils.WriteFileByBytes(dataSavePath, ssd);
+                }
             }
         }
-        public string FloorDataSavePath = "FloorsData";
+
+        #region 妤煎眰鍒楄〃
         /// <summary>
         /// 妤煎眰鍒楄〃
         /// </summary>
-        List<Floor> floors;
-        public List<Floor> Floors
+        List<SpatialInfo> floors;
+        /// <summary>
+        /// 妤煎眰鍒楄〃
+        /// </summary>
+        public List<SpatialInfo> FloorList
         {
             get
             {
@@ -109,15 +194,15 @@
                 {
                     try
                     {
-                        var floorsDataBytes = FileUtils.ReadFile(FloorDataSavePath);
+                        var floorsDataBytes = FileUtils.ReadFile(dataSavePath);
                         if (floorsDataBytes != null)
                         {
                             var floorsDataString = System.Text.Encoding.UTF8.GetString(floorsDataBytes);
                             MainPage.Log(floorsDataString);
-                            floors = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Floor>>(floorsDataString);
+                            floors = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(floorsDataString);
                             if (floors == null)
                             {
-                                floors = new List<Floor>();
+                                floors = new List<SpatialInfo>();
                             }
                         }
                     }
@@ -129,50 +214,188 @@
                 return floors;
             }
         }
-
+        /// <summary>
+        /// 鏁版嵁淇濆瓨璺緞
+        /// </summary>
+        string dataSavePath
+        {
+            get
+            {
+                if(roomType == "ROOM")
+                {
+                    return "RoomPath_" + uid;
+                }
+                if(roomType == "FLOOR")
+                {
+                    return "FloorsData"; 
+                }
+                else
+                {
+                    return "SpatialInfo_"+uid;
+                }
+            }
+        }
         /// <summary>
         /// 澧炲姞妤煎眰
         /// </summary>
         /// <param name="floor"></param>
-        public void AddFloor(Floor floor)
+        public void AddFloor(SpatialInfo floor)
         {
-            ins_Floor.floors.Add(floor);
-            floor.SaveFloorData();
+            FloorList.Add(floor);
+            SaveFloorData();
+        }
+        /// <summary>
+        /// 鏇存柊妤煎眰淇℃伅
+        /// </summary>
+        /// <param name="floor"></param>
+        public void UpdateFloor()
+        {
+            SaveFloorData();
         }
         /// <summary>
         /// 鍒犻櫎妤煎眰
         /// </summary>
         /// <param name="floor"></param>
-        public void DelFloor(Floor floor)
+        public void DelFloor(SpatialInfo floor)
         {
-            var tRoom = ins_Floor.floors.Find((obj) => obj.uid == floor.uid);
+            var tRoom = FloorList.Find((obj) => obj.uid == floor.uid);
             if (tRoom != null)
             {
-                ins_Floor.floors.Remove(floor);
+                FloorList.Remove(floor);
                 floor.modifyTime = "";
             }
             SaveFloorData();
         }
 
-        #endregion
-
-        public Floor()
-        {
-            roomType = "FLOOR";
-            parentId = DB_ResidenceData.residenceData.CurReginID;
-        }
         /// <summary>
         /// 淇濆瓨妤煎眰鏁版嵁
         /// </summary>
-        public void SaveFloorData()
+        void SaveFloorData()
         {
-            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(ins_Floor.floors));
-            FileUtils.WriteFileByBytes(FloorDataSavePath, ssd);
+            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
+            FileUtils.WriteFileByBytes(dataSavePath, ssd);
             MainPage.Log($"Save floor Data");
-            base.SaveSpatialInfo();
+            SaveSpatialInfo();
         }
 
+        /// <summary>
+        /// 淇濆瓨鎴块棿鏁版嵁
+        /// </summary>
+        public void SaveRoomData()
+        {
+            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
+            FileUtils.WriteFileByBytes(dataSavePath, ssd);
+            MainPage.Log($"Save Room Data {roomName} : {uid}");
+            SaveSpatialInfo();
+        }
+        #endregion
+
+        #region 鎴块棿鍒楄〃
+        /// <summary>
+        /// 鎴块棿鍒楄〃
+        /// </summary>
+        List<Room> rooms;
+        /// <summary>
+        /// 鎴块棿鍒楄〃
+        /// </summary>
+        public List<Room> RoomList
+        {
+            get
+            {
+                if (rooms == null)
+                {
+                    try
+                    {
+                        rooms = new List<Room> { };
+                        var filePathList = FileUtils.ReadFiles();
+
+                        foreach (var filePath in filePathList)
+                        {
+                            if (filePath.StartsWith("RoomData_"))
+                            {
+                                var roomDataBytes = FileUtils.ReadFile(filePath);
+                                var roomDataString = System.Text.Encoding.UTF8.GetString(roomDataBytes);
+                                var room = Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(roomDataString);
+                                if (room != null)
+                                {
+                                    rooms.Add(room);
+                                }
+                            }
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        MainPage.Log($"鍔熻兘鏁版嵁鍒濆鍖栧け璐ワ細{ex.Message}");
+                    }
+                }
+                return rooms;
+            }
+        }
+        /// <summary>
+        /// 澧炲姞鎴块棿
+        /// </summary>
+        /// <param name="room"></param>
+        public void AddRoom(Room room)
+        {
+            RoomList.Add(room);
+            room.SaveRoomData();
+        }
+        /// <summary>
+        /// 鍒犻櫎鎴块棿
+        /// </summary>
+        /// <param name="room"></param>
+        public void DelRoom(Room room)
+        {
+            var tRoom = RoomList.Find((obj) => obj.uid == room.uid);
+            if (tRoom != null)
+            {
+                FileUtils.DeleteFile(room.dataSavePath);
+                RoomList.Remove(room);
+                room.DeleteSign = true;
+            }
+            room.SaveRoomData();
+        }
+        /// <summary>
+        /// 鍒犻櫎鎵�鏈夋埧闂�
+        /// </summary>
+        public void DeleteAllRoom()
+        {
+            foreach(var localRoom in RoomList)
+            {
+                FileUtils.DeleteFile(localRoom.dataSavePath);
+            }
+            RoomList.Clear();
+        }
+        /// <summary>
+        /// 鍒濆鍖栨瘡涓埧闂寸殑鍔熻兘鏁版嵁
+        /// </summary>
+        public void InitRoomFunction()
+        {
+            new System.Threading.Thread(() =>
+            {
+                try
+                {
+                    //鍒濆鍖栦綇瀹呮墍鏈夋埧闂村姛鑳芥暟鎹�
+                    foreach (var r in RoomList)
+                    {
+                        r.GetRoomFunctions(true);
+                        r.GetRoomScenes(true);
+                    }
+                }
+                catch (Exception ex)
+                {
+                    MainPage.Log($"Init room function error : {ex.Message}");
+                }
+            })
+            { IsBackground = true }.Start();
+        }
+
+
+        #endregion
+
     }
+
+
 
     [Serializable]
     public class Room : SpatialInfo
@@ -183,13 +406,7 @@
             roomName = "Room";
         }
         public string backgroundImage = "Classification/Room/Roombg.png";
-        public string roomSavePath
-        {
-            get
-            {
-                return "RoomPath_" + uid;
-            }
-        }
+
         /// <summary>
         /// 妤煎眰
         /// </summary>
@@ -198,9 +415,9 @@
         {
             get
             {
-                if (Floor.InsFloor.Floors.Count > 0)
+                if (FloorList.Count > 0)
                 {
-                    var f = Floor.InsFloor.Floors.Find((obj) => obj.uid == parentId);
+                    var f = FloorList.Find((obj) => obj.uid == parentId);
                     if (f != null)
                     {
                         return f.roomName + " ";
@@ -294,16 +511,6 @@
         public void RemoveRoomScene(Scene scene)
         {
             scenes.Remove(scenes.Find((obj) => obj.sid == scene.sid));
-        }
-        /// <summary>
-        /// 淇濆瓨鎴块棿鏁版嵁
-        /// </summary>
-        public void SaveRoomData()
-        {
-            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
-            FileUtils.WriteFileByBytes(roomSavePath, ssd);
-            MainPage.Log($"Save Room Data {roomName} : {this.uid}");
-            base.SaveSpatialInfo();
         }
     }
   

--
Gitblit v1.8.0