From dce6c3481a37216292724013ff9d2b75ceb82f86 Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期三, 10 六月 2020 15:47:28 +0800
Subject: [PATCH] 添加小度的代码

---
 ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlResidenceLogic.cs |  399 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 393 insertions(+), 6 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlResidenceLogic.cs b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlResidenceLogic.cs
index 997240b..c60d868 100755
--- a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlResidenceLogic.cs
+++ b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlResidenceLogic.cs
@@ -1,4 +1,5 @@
-锘縰sing System;
+锘縰sing Shared.Common;
+using System;
 using System.Collections.Generic;
 using System.Text;
 
@@ -32,6 +33,272 @@
 
         #endregion
 
+        #region 鈻� 鍒涘缓鏂颁綇瀹卂________________________
+
+        /// <summary>
+        /// 鍒涘缓鏂扮殑浣忓畢(杩斿洖浣忓畢id,null浠h〃澶辫触)
+        /// </summary>
+        /// <param name="residenceName">浣忓畢鍚嶇О</param>
+        public async System.Threading.Tasks.Task<string> CreatNewResidence(string residenceName)
+        {
+            var Pra = new AddResidencePra();
+            Pra.Name = residenceName;
+            //娣诲姞浣忓畢
+            string resultData = await UserCenterLogic.GetResponseDataByRequestHttps("App/AddHome", false, Pra);
+            if (resultData == null)
+            {
+                return null;
+            }
+            var newInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NewResidenceInfo>>(resultData);
+
+            //娣诲姞浣忓畢鍒扮紦瀛�
+            this.AddHouseToMemmory(newInfo[0].Id, residenceName);
+
+            return newInfo[0].Id;
+        }
+
+        /// <summary>
+        /// 娣诲姞浣忓畢鍒扮紦瀛�
+        /// </summary>
+        /// <param name="houseId">浣忓畢id</param>
+        /// <param name="houseName">浣忓畢鍚嶇О.</param>
+        /// <param name="isOthreShare">鏄惁涓哄叾浠栦富鐢ㄦ埛鍒嗕韩杩囨潵鐨勪綇瀹�</param>
+        /// <param name="accountType">浠呭瓙璐﹀彿鐧婚檰鐨勬椂鍊欎娇鐢�,褰撱�怚sOthreShare銆戜负"true"锛屽苟涓斻�怉ccountType銆戜负"1"鏃讹紝璇ヨ处鍙锋嫢鏈夌鐞嗗憳鏉冮檺</param>
+        public void AddHouseToMemmory(string houseId, string houseName,
+            bool isOthreShare = false, int accountType = 0)
+        {
+            var home = new Common.House();
+            home.Id = houseId;
+            home.Name = houseName;
+            home.IsOthreShare = isOthreShare;
+            home.AccountType = accountType;
+
+            //鍒涘缓鏂囦欢澶�
+            Common.Global.CreateHomeDirectory(houseId);
+            home.Save();
+            Common.Config.Instance.HomeFilePathList.Add(home.FileName);
+            Common.Config.Instance.Save();
+            //濡傛灉褰撳墠娌℃湁浣忓畢鐨勮瘽
+            if (Common.Config.Instance.Home.Id == string.Empty)
+            {
+                Common.Config.Instance.Home = this.GetHouseByFilePath(home.FileName);
+                Common.Config.Instance.HomeId = home.Id;
+            }
+        }
+
+
+        #endregion
+
+        #region 鈻� 鍒囨崲浣忓畢___________________________
+
+        /// <summary>
+        /// 鍒囨崲浣忓畢(娉�:瀹冨彧鍒囨崲鍐呭瓨,鐣岄潰骞舵湭澶勭悊)
+        /// </summary>
+        /// <param name="residenceId"></param>
+        /// <returns></returns>
+        public async System.Threading.Tasks.Task SwitchResidence(string residenceId)
+        {
+            //鎵撳紑杩涘害鏉�
+            ProgressBar.Show();
+
+            Config.Instance.HomeId = residenceId;
+            //閲嶆柊鍒濆鍖栦綇瀹呭璞�
+            Config.Instance.Home = this.GetHouseByHouseId(residenceId);
+            Config.Instance.Save();
+            //鍒锋柊涓汉涓績鐨勫唴瀛樺強绾跨▼
+            await UserCenterLogic.InitUserCenterMenmoryAndThread(false);
+
+            //鍏抽棴杩涘害鏉�
+            ProgressBar.Close();
+        }
+
+        #endregion
+
+        #region 鈻� 缂栬緫浣忓畢___________________________
+
+        /// <summary>
+        /// 缂栬緫浣忓畢
+        /// </summary>
+        /// <param name="residenceId">浣忓畢id</param>
+        /// <param name="residenceName">浣忓畢鍚嶇О</param>
+        /// <returns></returns>
+        public async System.Threading.Tasks.Task<bool> EditorResidenceName(string residenceId, string residenceName)
+        {
+            var Pra = new EditorResidencePra();
+            Pra.HomeId = residenceId;
+            Pra.Name = residenceName;
+            Pra.IsOtherAccountCtrl = false;
+            Pra.LoginAccessToken = Config.Instance.Token;
+
+            //缂栬緫浣忓畢
+            bool flage = await UserCenterLogic.GetResultStatuByRequestHttps("App/EditHome", false, Pra);
+            if (flage == true)
+            {
+                //鍒锋柊鍐呭瓨鐨勪綇瀹呭悕
+                this.EditorHouseByHouseId(residenceId, residenceName);
+            }
+            return flage;
+        }
+
+        /// <summary>
+        /// 缂栬緫缂撳瓨浣忓畢
+        /// </summary>
+        /// <param name="residenceId">浣忓畢id</param>
+        /// <param name="residenceName">浣忓畢鍚嶇О</param>
+        public void EditorHouseByHouseId(string residenceId, string residenceName)
+        {
+            if (Config.Instance.Home.Id == residenceId)
+            {
+                Config.Instance.Home.Name = residenceName;
+            }
+
+            var home = GetHouseByHouseId(residenceId);
+            if (home == null)
+            {
+                return;
+            }
+            home.Name = residenceName;
+            home.Save();
+            //浣忓畢淇敼鍚嶇О鐨勮瘽,涓婚〉闇�瑕侀噸鏂板埛鏂�
+            UserView.UserPage.Instance.RefreshForm = true;
+        }
+
+
+        #endregion
+
+        #region 鈻� 鑾峰彇浣忓畢___________________________
+
+        /// <summary>
+        /// 閫氳繃銆恑d銆戣幏鍙栦綇瀹�
+        /// </summary>
+        /// <returns>The house by house identifier.</returns>
+        /// <param name="houseId">浣忓畢id</param>
+        public House GetHouseByHouseId(string houseId)
+        {
+            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId, $"House_{houseId}.json");
+            var file = Shared.IO.FileUtils.ReadFile(path);
+            if (file == null)
+            {
+                return null;
+            }
+            return Newtonsoft.Json.JsonConvert.DeserializeObject<House>(System.Text.Encoding.UTF8.GetString(file));
+        }
+
+        /// <summary>
+        /// 閫氳繃銆愭枃浠惰矾寰勩�戣幏鍙栦綇瀹�
+        /// </summary>
+        /// <returns>The house by file path.</returns>
+        /// <param name="filePath">鏂囦欢璺緞</param>
+        public House GetHouseByFilePath(string filePath)
+        {
+            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, GetHouseIdByFilePath(filePath), filePath);
+            var file = Shared.IO.FileUtils.ReadFile(path);
+            if (file == null)
+            {
+                return null;
+            }
+            return Newtonsoft.Json.JsonConvert.DeserializeObject<House>(System.Text.Encoding.UTF8.GetString(file));
+        }
+
+        #endregion
+
+        #region 鈻� 鑾峰彇浜戠浣忓畢鍒楄〃___________________
+        /// <summary>
+        /// 鑾峰彇浜戠浣忓畢鍒楄〃
+        /// </summary>
+        public async System.Threading.Tasks.Task<List<string>> GetHomeListsFromDb()
+        {
+            var pageSetting = new SendDataToServer.ResidenceListPageSettingObj()
+            {
+                PageSize = CommonPage.PageSize
+            };
+
+            var reqDto = new SendDataToServer.ResidenceListObj()
+            {
+                LoginAccessToken = Config.Instance.Token,
+                PageSetting = pageSetting
+            };
+            var requestObj = new SendDataToServer.ResidenceListReqDto()
+            {
+                ReqDto = reqDto,
+                RequestVersion = CommonPage.RequestVersion,
+            };
+            try
+            {
+                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestObj);
+                var revertObj = await CommonPage.Instance.RequestHttpsZigbeeAsync("App/GetHomePager", System.Text.Encoding.UTF8.GetBytes(requestJson));
+                if (revertObj == null)
+                {
+                    return null;
+                }
+                if (revertObj.StateCode.ToUpper() == "SUCCESS")
+                {
+                    var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.ResidenceRes>(revertObj.ResponseData.ToString());
+                    if(responseDataObj.TotalCount>0)
+                    {
+                        //娓呯┖褰撳墠浣忓畢鍒楄〃
+                        Config.Instance.HomeFilePathList.Clear();
+                        var listHouse = new List<House>();
+                        foreach (var residence in responseDataObj.PageData)
+                        {
+                            Config.Instance.HomeFilePathList.Add($"House_{residence.Id}.json");
+                            var house = GetHouseByHouseId(residence.Id);
+                            if (house == null)
+                            {
+                                house = new House
+                                {
+                                    Id = residence.Id,
+                                    Name = residence.Name,
+                                    IsOthreShare = residence.IsOthreShare,
+                                    AccountType = residence.AccountType,
+                                    MainUserDistributedMark = residence.MainUserDistributedMark,
+                                    Longitude = residence.Longitude,
+                                    Latitude = residence.Latitude
+                                };
+                            }
+                            else
+                            {
+                                house.Id = residence.Id;
+                                house.Name = residence.Name;
+                                house.IsOthreShare = residence.IsOthreShare;
+                                house.AccountType = residence.AccountType;
+                                house.MainUserDistributedMark = residence.MainUserDistributedMark;
+                                house.Longitude = residence.Longitude;
+                                house.Latitude = residence.Latitude;
+                            }
+                            Global.CreateHomeDirectory(residence.Id);
+                            house.Save(false);
+                            listHouse.Add(house);
+                        }
+                        //濡傛灉鍒囨崲浜嗚处鍙�,鎴栬�呭師鏉ョ殑id涓嶅瓨鍦�,鍒欓噸缃綇瀹匢D
+                        if (Config.Instance.TheSameLoginAccount == false ||
+                            Config.Instance.HomeFilePathList.Find((obj) => obj == $"House_{Config.Instance.HomeId}.json") == null)
+                        {
+                            Config.Instance.HomeId = listHouse[0].Id;
+                            foreach (var house in listHouse)
+                            {
+                                //鍒濆閫夋嫨瀹冭嚜宸辩殑浣忓畢
+                                if (house.IsOthreShare == false)
+                                {
+                                    Config.Instance.HomeId = house.Id;
+                                    Config.Instance.Home = GetHouseByHouseId(house.Id);
+                                    break;
+                                }
+                            }
+                        }
+                        Config.Instance.Save();
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                HdlLogLogic.Current.WriteLog(ex);
+            }
+            return Config.Instance.HomeFilePathList;
+        }
+
+        #endregion
+
         #region 鈻� 鑾峰彇鏈湴浣忓畢鍒楄〃___________________
 
         /// <summary>
@@ -50,7 +317,7 @@
             var listHome = new List<Common.House>();
             foreach (var housePath in Common.Config.Instance.HomeFilePathList)
             {
-                var home = Common.House.GetHouseByFilePath(housePath);
+                var home =this.GetHouseByFilePath(housePath);
                 if (home == null)
                 {
                     continue;
@@ -85,14 +352,71 @@
                 {
                     //璇诲彇鏂囦欢鍐呭
                     var textValue = UserCenterLogic.LoadFileContent(System.IO.Path.Combine(nowPath, arryHouse[0]));
-                    if (textValue != null)
+                    if (textValue == null)
                     {
-                        var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.House>(textValue);
-                        listHome.Add(myHouse);
+                        continue;
                     }
+                    var myHouse = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.House>(textValue);
+                    listHome.Add(myHouse);
                 }
             }
             return listHome;
+        }
+
+        #endregion
+
+        #region 鈻� 鍒犻櫎浣忓畢___________________________
+
+        /// <summary>
+        /// 鍒犻櫎浣忓畢
+        /// </summary>
+        /// <param name="filePath">File path.</param>
+        public void DeleteHouseMemmory(string houseId)
+        {
+            string filePath = this.GetHouseFilePathByHouseId(houseId);
+            var delPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, GetHouseIdByFilePath(filePath));
+            if (System.IO.Directory.Exists(delPath) == false)
+            {
+                return;
+            }
+            var fileList = GetHouseFileListByHomeId(houseId);
+            foreach (var file in fileList)
+            {
+                //鍒犻櫎鏂囦欢
+                System.IO.File.Delete(System.IO.Path.Combine(delPath, file));
+            }
+            //鍒犻櫎鏂囦欢澶�
+            System.IO.Directory.Delete(delPath, true);
+            //HomeFilePathList涓垹闄よ鍒楄〃
+            Common.Config.Instance.HomeFilePathList.RemoveAll((obj) => obj == filePath);
+            Config.Instance.Save();
+        }
+
+        #endregion
+
+        #region 鈻� 鑾峰彇璇ヤ綇瀹呯殑鏂囦欢鍒楄〃_______________
+
+        /// <summary>
+        /// 閫氳繃銆恏ouseId銆戣幏鍙栬浣忓畢鐨勬枃浠跺垪琛�
+        /// </summary>
+        /// <returns>The house file list by home identifier.</returns>
+        /// <param name="houseId">House identifier.</param>
+        public List<string> GetHouseFileListByHomeId(string houseId)
+        {
+            var list = new List<string> { };
+            var path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, houseId);
+            if (!System.IO.Directory.Exists(path))
+            {
+                return new List<string> { };
+            }
+            var files = System.IO.Directory.GetFiles(path);
+            foreach (var file in files)
+            {
+                var f = file.Substring(path.Length + 1);
+                System.Console.WriteLine(f);
+                list.Add(f);
+            }
+            return list;
         }
 
         #endregion
@@ -106,9 +430,72 @@
         /// <returns></returns>
         public string GetFloorNameById(string i_FloorId)
         {
-            return Common.Config.Instance.Home.GetFloorNameById(i_FloorId);
+            if (i_FloorId == null) { return string.Empty; }
+            if (Config.Instance.Home.FloorDics.ContainsKey(i_FloorId) == false)
+            {
+                return string.Empty;
+            }
+            return Config.Instance.Home.FloorDics[i_FloorId];
         }
 
+        /// <summary>
+        /// 閫氳繃銆愭枃浠惰矾寰勩�戣幏鍙栦綇瀹卛d
+        /// </summary>
+        /// <returns>The house identifier by file path.</returns>
+        /// <param name="filePath">鏂囦欢璺緞</param>
+        public string GetHouseIdByFilePath(string filePath)
+        {
+            string[] sArray = filePath.Split(new string[] { "House_", ".json" }, StringSplitOptions.RemoveEmptyEntries);
+            if (sArray.Length >= 1)
+            {
+                return sArray[0];
+            }
+            return null;
+        }
+
+        /// <summary>
+        /// 閫氳繃銆愪綇瀹卛d銆戣幏鍙栦綇瀹呰矾寰�
+        /// </summary>
+        /// <returns>The house identifier by file path.</returns>
+        /// <param name="houseId">浣忓畢id</param>
+        public string GetHouseFilePathByHouseId(string houseId)
+        {
+            return $"House_{houseId}.json";
+        }
+
+        #endregion
+
+        #region 鈻� 缁撴瀯浣揰____________________________
+
+        /// <summary>
+        ///  娣诲姞浣忓畢鐨勫惎鍔ㄥ弬鏁�
+        /// </summary>
+        private class AddResidencePra : IfacePraCommon
+        {
+            /// <summary>
+            /// RequestVersion
+            /// </summary>
+            public string RequestVersion = Common.CommonPage.RequestVersion;
+            /// <summary>
+            /// LoginAccessToken
+            /// </summary>
+            public string LoginAccessToken = Common.Config.Instance.Token;
+            /// <summary>
+            /// Name
+            /// </summary>
+            public string Name = string.Empty;
+        }
+
+        /// <summary>
+        /// 鏂颁綇瀹呯殑淇℃伅
+        /// </summary>
+        private class NewResidenceInfo
+        {
+            /// <summary>
+            /// ZigbeeHomeGuid
+            /// </summary>
+            public string Id = string.Empty;
+        }
         #endregion
     }
 }

--
Gitblit v1.8.0