From cd58c94a09b82528956c2607f580355b0c0cc912 Mon Sep 17 00:00:00 2001
From: yanjinhui <3306209981@qq.com>
Date: 星期五, 11 四月 2025 13:41:59 +0800
Subject: [PATCH] 优化更新人脸识别

---
 项目代码/WIDESEAWCS_Server 正式/WIDESEAWCS_SystemServices/Sys_UserService.cs |  123 ++++++++++++++++++++++++++++++-----------
 1 files changed, 90 insertions(+), 33 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_SystemServices/Sys_UserService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_SystemServices/Sys_UserService.cs"
index f7c5351..f959724 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_SystemServices/Sys_UserService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_SystemServices/Sys_UserService.cs"
@@ -421,7 +421,12 @@
             }
         }
 
-
+        /// <summary>
+        /// 鏍规嵁鐢ㄦ埛缁勬潵鏌ョ湅褰撳墠浣滀笟鐨勬潈闄�
+        /// </summary>
+        /// <param name="userId">鐢ㄦ埛id</param>
+        /// <param name="group">鐢ㄦ埛鍒嗙粍</param>
+        /// <returns></returns>
         public WebResponseContent PermissionView(int userId, string group)
         {
             try
@@ -739,55 +744,58 @@
         {
             try
             {
-
                 // 1. 鏌ヨ鐢ㄦ埛
-                var user = BaseDal.QueryData();
-                var sys = user.Where(x => x.User_Id == addUserDTO.id).FirstOrDefault();
+                var userList = BaseDal.QueryData();
+                var sys = userList.FirstOrDefault(x => x.User_Id == addUserDTO.id);
                 if (sys == null)
                 {
                     return new WebResponseContent { Status = false, Message = "娌℃壘鍒拌鐢ㄦ埛" };
                 }
 
-                // 2. 鏌ヨ浜鸿劯搴撳垎缁� ID
-                var faceGroupID = _faceRecognitionRepository.QueryData().Select(x => x.GroupID).FirstOrDefault();
-
-                // 3. 鍒ゆ柇鏄惁闇�瑕佹洿鏂颁汉鑴镐俊鎭�
-                string faceToken = null;
-                if (!string.IsNullOrEmpty(addUserDTO.path)) // 鍙湁浼犱簡鏂板浘鐗囷紝鎵嶆洿鏂颁汉鑴镐俊鎭�
+                // 2. 妫�鏌ヨ处鍙锋槸鍚︿慨鏀癸紝骞跺垽鏂槸鍚﹂噸澶�//StringComparison.OrdinalIgnoreCase锛氳〃绀哄拷鐣ュぇ灏忓啓杩涜姣旇緝锛屾瘮濡� "admin" 鍜� "ADMIN" 涔熻涓烘槸鐩哥瓑鐨�
+                if (!string.Equals(sys.UserName, addUserDTO.username))
                 {
-                    var baiduResult = BaiDuFaceHelper.AddUser(new BDUserInfo
+                    var existUser = userList.FirstOrDefault(a => a.UserName == addUserDTO.username);
+                    if (existUser != null)
                     {
-                        ImgPath = addUserDTO.path,
-                        user_id = addUserDTO.username,
-                        group_id = faceGroupID // 杩欓噷搴旇濉櫨搴︿汉鑴稿簱鍒嗙粍
-                    });
-
-                    // 4. 鍒ゆ柇鐧惧害 API 鏄惁鎴愬姛
-                    if (baiduResult == null || baiduResult.error_code != 0)
-                    {
-                        return new WebResponseContent { Status = false, Message = "璇ュ浘鐗囦篃瀛樺湪锛屼笉瑕侀噸澶嶄笂浼狅細" + baiduResult.error_msg };
+                        return new WebResponseContent { Status = false, Message = "璐﹀彿閲嶅锛岃閲嶆柊褰曞叆" };
                     }
-
-                    // 5. 鍙湁褰撲笂浼犱簡鏂板浘鐗囧苟鎴愬姛鏃讹紝鎵嶆洿鏂拌繖浜涘瓧娈�
-                    faceToken = baiduResult.result.face_token;
-                    sys.Log_id = baiduResult.log_id;
-                    sys.HeadImageUrl = addUserDTO.path;
+                    sys.UserName = addUserDTO.username; // 纭鍙互鏀规椂鎵嶄慨鏀�
                 }
 
-                // 6. 鏇存柊鐢ㄦ埛鍏朵粬淇℃伅
-                sys.UserName = addUserDTO.username;
+                // 3. 鏇存柊鍏朵粬鐢ㄦ埛淇℃伅
                 sys.UserTrueName = addUserDTO.usertruename;
-                sys.Face_token = faceToken ?? sys.Face_token; // 鍙湁褰� faceToken 鏈夊�兼椂鎵嶆洿鏂帮紝鍚﹀垯淇濇寔鍘熷��
                 sys.PhoneNo = addUserDTO.phoneno;
                 sys.Role_Id = addUserDTO.roleid;
                 sys.RoleName = addUserDTO.rolename;
                 sys.Enable = addUserDTO.enable;
-                var list = user.Where(a => a.UserName == addUserDTO.username).FirstOrDefault();
-                if (list != null)
+
+                // 4. 鏌ヨ浜鸿劯搴撳垎缁� ID
+                var faceGroupID = _faceRecognitionRepository.QueryData().Select(x => x.GroupID).FirstOrDefault();
+
+                // 5. 鍒ゆ柇鏄惁闇�瑕佹洿鏂颁汉鑴镐俊鎭疎quals 鏄� .NET 涓敤浜庢瘮杈冧袱涓璞℃槸鍚︾浉绛�
+                if (!string.Equals(sys.HeadImageUrl, addUserDTO.path)) // 鏈夋柊鍥剧墖鎵嶅鐞嗕汉鑴告敞鍐�=> 鍓嶉潰鐨勫啓娉曠瓑浜�=if (sys.HeadImageUrl!=addUserDTO.path)
                 {
-                    return new WebResponseContent { Status = false, Message = "璐﹀彿閲嶅锛岃閲嶆柊褰曞叆" };
+                    var baiduResult = BaiDuFaceHelper.AddUser(new BDUserInfo
+                    {
+                        ImgPath = addUserDTO.path,
+                        user_id = sys.UserName,
+                        group_id = faceGroupID
+                    });
+
+                    // 6. 鍒ゆ柇鐧惧害 API 鏄惁鎴愬姛
+                    if (baiduResult == null || baiduResult.error_code != 0)
+                    {
+                        return new WebResponseContent { Status = false, Message = "浜鸿劯鏇存柊澶辫触锛�" + baiduResult.error_msg };
+                    }
+
+                    // 7. 鏇存柊浜鸿劯鐩稿叧瀛楁
+                    sys.Log_id = baiduResult.log_id;
+                    sys.HeadImageUrl = addUserDTO.path;
+                    sys.Face_token = baiduResult.result.face_token;
                 }
-                // 7. 鏇存柊鏁版嵁搴�
+
+                // 8. 鏇存柊鏁版嵁搴�
                 BaseDal.UpdateData(sys);
 
                 return new WebResponseContent { Status = true, Data = sys };
@@ -797,6 +805,7 @@
                 return new WebResponseContent { Status = false, Message = "澶辫触锛�" + ex.Message };
             }
         }
+
 
 
 
@@ -818,7 +827,7 @@
                 deleteUserImg.group_id = faceGroupID;
                 deleteUserImg.user_id = account;
 
-                string apiResult = BaiDuFaceHelper.DeleteUser(deleteUserImg);//鍙缁檛oken鍜宭og_id
+                string apiResult = BaiDuFaceHelper.DeleteUser(deleteUserImg);//鍙缁欑敤鎴疯处鍙峰拰鐢ㄦ埛缁刬d
 
                 // 2. 瑙f瀽杩斿洖缁撴灉锛屽垽鏂垹闄ゆ槸鍚︽垚鍔�
                 dynamic resultObj = JsonConvert.DeserializeObject(apiResult);
@@ -932,6 +941,54 @@
             }
         }
 
+        /// <summary>
+        /// 浜鸿劯鍒犻櫎锛堝垹闄ょ敤鎴风殑鏌愪竴寮犱汉鑴革紝濡傛灉璇ョ敤鎴峰彧鏈変竴寮犱汉鑴稿浘鐗囷紝鍒欏悓鏃跺垹闄ょ敤鎴枫�傦級
+        /// </summary>
+        /// <param name="addUserDTO"></param>
+        /// <returns></returns>
+        /// <exception cref="NotImplementedException"></exception>
+        public WebResponseContent DeleteUserIsface(string account)
+        {
+            try
+            {
+
+                // 1. 鏌ヨ浜鸿劯搴撳垎缁� ID
+                var faceGroupID = _faceRecognitionRepository.QueryData().Select(x => x.GroupID).FirstOrDefault();
+                //2.鏌ヨ瑕佸垹閭d釜鐢ㄦ埛鐨�
+                var user = BaseDal.QueryData(x => x.UserName == account).FirstOrDefault();
+                if (user == null)
+                {
+                    return new WebResponseContent { Status = false, Message = "鐢ㄦ埛涓嶅瓨鍦紝璇锋鏌ヨ处鍙锋槸鍚﹁緭鍏ユ纭�" };
+                }
+
+                WIDESEAWCS_Common.DeleteUserImg deleteUserImg = new WIDESEAWCS_Common.DeleteUserImg();
+                deleteUserImg.group_id = faceGroupID;
+                deleteUserImg.user_id = account;
+                deleteUserImg.face_token = user.Face_token;
+                deleteUserImg.log_id=user.Log_id;
+
+                string apiResult = BaiDuFaceHelper.DeleteUserImg(deleteUserImg);//缁欑敤鎴疯处鍙�/鐢ㄦ埛缁刬d/Face_token/Log_id
+
+                // 2. 瑙f瀽杩斿洖缁撴灉锛屽垽鏂垹闄ゆ槸鍚︽垚鍔�
+                dynamic resultObj = JsonConvert.DeserializeObject(apiResult);
+                if (resultObj.error_code == 0)
+                {
+                    if (user != null)
+                    {
+                        BaseDal.DeleteData(user);
+                    }
+                    return WebResponseContent.Instance.OK("鍒犻櫎鎴愬姛");
+                }
+                else
+                {
+                    return WebResponseContent.Instance.Error("鐧惧害 API 鍒犻櫎澶辫触锛�" + resultObj.error_msg);
+                }
+            }
+            catch (Exception ex)
+            {
+                return WebResponseContent.Instance.Error("鍒犻櫎鐢ㄦ埛澶辫触锛�" + ex.Message);
+            }
+        }
     }
 }
 

--
Gitblit v1.9.3