File,Status,Changes
WM_CMS_SERVER/backendmux.go,Modified,"diff --git a/WM_CMS_SERVER/backendmux.go b/WM_CMS_SERVER/backendmux.go
index 033a8ce..e9d5dcd 100644
--- a/WM_CMS_SERVER/backendmux.go
+++ b/WM_CMS_SERVER/backendmux.go
@@ -337,8 +337,8 @@ func TestGoRoutine(w http.ResponseWriter, r *http.Request) {
 				FunName:        ""使用者管理"",
 				GrantType:      ""系統授權"",
 				GrantDesc:      """",
-				WorkStatus:     ssoACInfo.AfterID,
-				WorkStatusDesc: ssoACInfo.AfterRemark,
+				WorkStatus:     ssoACInfo.WorkStatus,
+				WorkStatusDesc: ssoACInfo.WorkStatusDesc,
 				GrantUserID:    ""SYSTEM"",
 				GrantUserName:  ""SYSTEM"",
 				Remark:         """","
WM_CMS_SERVER/mongodb/dbHRDataCheck.go,Modified,"diff --git a/WM_CMS_SERVER/mongodb/dbHRDataCheck.go b/WM_CMS_SERVER/mongodb/dbHRDataCheck.go
index 8572bc2..5c32507 100644
--- a/WM_CMS_SERVER/mongodb/dbHRDataCheck.go
+++ b/WM_CMS_SERVER/mongodb/dbHRDataCheck.go
@@ -14,14 +14,14 @@ import (
 )
 
 type UserInfo struct {
-	Username    string
-	DisplayName string
-	CompID      string
-	CompName    string
-	BrID        string
-	BrName      string
-	AfterID     string
-	AfterRemark string
+	Username       string
+	DisplayName    string
+	CompID         string
+	CompName       string
+	BrID           string
+	BrName         string
+	WorkStatus     string
+	WorkStatusDesc string
 }
 
 func CheckHRISDBData() ([]UserInfo, error) {
@@ -69,10 +69,14 @@ func checkEmpShareLogData(sqlConn *sql.DB) ([]UserInfo, error) {
 		""CompName"":       1,
 		""BrID"":           1,
 		""BrName"":         1,
+		""WorkStatus"":     1,
+		""WorkStatusDesc"": 1,
 		""account_status"": 1,
 	})
+
 	// filter := bson.M{""account_status"": ""enabled""}
-	cursor, err := userCollection.Find(context.Background(), bson.M{}, opts)
+	filter := bson.M{}
+	cursor, err := userCollection.Find(context.Background(), filter, opts)
 	if err != nil {
 		return nil, fmt.Errorf(""[db.checkEmpShareData] 查詢 MongoDB users 失敗: %v"", err)
 	}
@@ -80,17 +84,17 @@ func checkEmpShareLogData(sqlConn *sql.DB) ([]UserInfo, error) {
 
 	// 定義用戶結構
 	type User struct {
-		UserId        string    `bson:""username""`
-		DisplayName   string    `bson:""display_name""`
-		CreateTime    time.Time `bson:""create_time""`
-		LastLogin     time.Time `bson:""last_login,omitempty""`
-		CompID        string    `bson:""CompID,omitempty""`
-		CompName      string    `bson:""CompName,omitempty""`
-		BrID          string    `bson:""BrID,omitempty""`
-		BrName        string    `bson:""BrName,omitempty""`
-		AccountStatus string    `bson:""account_status,omitempty""`
-		AfterID       string
-		AfterRemark   string
+		UserId         string    `bson:""username""`
+		DisplayName    string    `bson:""display_name""`
+		CreateTime     time.Time `bson:""create_time""`
+		LastLogin      time.Time `bson:""last_login,omitempty""`
+		CompID         string    `bson:""CompID,omitempty""`
+		CompName       string    `bson:""CompName,omitempty""`
+		BrID           string    `bson:""BrID,omitempty""`
+		BrName         string    `bson:""BrName,omitempty""`
+		WorkStatus     string    `bson:""WorkStatus,omitempty""`
+		WorkStatusDesc string    `bson:""WorkStatusDesc,omitempty""`
+		AccountStatus  string    `bson:""account_status,omitempty""`
 	}
 
 	var users []User
@@ -107,12 +111,7 @@ func checkEmpShareLogData(sqlConn *sql.DB) ([]UserInfo, error) {
 			switch user.AccountStatus {
 			case ""enabled"", """":
 				enabledUserList = append(enabledUserList, fmt.Sprintf(""'%s'"", user.UserId))
-				user.AfterID = ""1""
-				user.AfterRemark = ""在職""
 			case ""disabled"":
-				// disabledUserList = append(disabledUserList, fmt.Sprintf(""'%s'"", user.UserId))
-				user.AfterID = ""3""
-				user.AfterRemark = ""禁用""
 			}
 			userInfoMap[user.UserId] = user
 		}
@@ -120,7 +119,7 @@ func checkEmpShareLogData(sqlConn *sql.DB) ([]UserInfo, error) {
 
 	// 查詢 Daily_EmpShareLog 取得 EmpID, AfterID, AfterRemark
 	query := fmt.Sprintf(`
-		SELECT EmpID, AfterID, AfterRemark FROM Daily_EmpShareLog 
+		SELECT EmpID, ChgID, AfterID, AfterRemark FROM Daily_EmpShareLog 
 		WHERE LogDate = '%s' 
 		AND EmpID IN (%s)`,
 		time.Now().Format(""2006/01/02""),
@@ -135,52 +134,73 @@ func checkEmpShareLogData(sqlConn *sql.DB) ([]UserInfo, error) {
 	// 準備回傳的 UserInfo
 	ssoACInfoList := make([]UserInfo, 0)
 
-	// 存放離職者ID
-	var departureUsers []string
+	// 需要停用的用戶列表
+	var disabledUsers []string
 	for rows.Next() {
-		var empID, afterID, afterRemark string
-		if err := rows.Scan(&empID, &afterID, &afterRemark); err != nil {
+		var empID, chgID, afterID, afterRemark string
+		if err := rows.Scan(&empID, &chgID, &afterID, &afterRemark); err != nil {
 			return nil, fmt.Errorf(""[db.checkEmpShareData] 讀取行數據失敗: %v"", err)
 		}
 
-		if afterID == ""3"" {
-			log.Printf(""[db.checkEmpShareData] 使用者離職 - EmpID:%s, AfterID:%s, AfterRemark:%s\n"", empID, afterID, afterRemark)
-			departureUsers = append(departureUsers, empID)
-
-			_user := userInfoMap[empID]
-			ssoACInfoList = append(ssoACInfoList, UserInfo{
-				Username:    _user.UserId,
-				DisplayName: _user.DisplayName,
-				CompID:      _user.CompID,
-				CompName:    _user.CompName,
-				BrID:        _user.BrID,
-				BrName:      _user.BrName,
-				AfterID:     afterID,
-				AfterRemark: afterRemark,
-			})
-
-			delete(userInfoMap, empID)
+		_user := userInfoMap[empID]
+		switch chgID {
+		case ""01"":
+			if afterID == ""3"" {
+				log.Printf(""[db.checkEmpShareData] 使用者離職 - EmpID:%s, ChgID:%s, AfterID:%s, AfterRemark:%s\n"", empID, chgID, afterID, afterRemark)
+				_user.WorkStatus = ""2""
+			} else {
+				_user.WorkStatus = ""3""
+			}
+			_user.WorkStatusDesc = afterRemark
+			userInfoMap[empID] = _user
+			disabledUsers = append(disabledUsers, empID)
+
+		case ""21"":
+			log.Printf(""[db.checkEmpShareData] 新增人員 - EmpID:%s, ChgID:%s, AfterID:%s, AfterRemark:%s\n"", empID, chgID, afterID, afterRemark)
+			_user.WorkStatus = ""1""
+			_user.WorkStatusDesc = ""在職""
+			userInfoMap[empID] = _user
+
+		default:
+			continue
+		}
+
+		_update := bson.M{
+			""$set"": bson.M{
+				""WorkStatus"":     _user.WorkStatus,
+				""WorkStatusDesc"": _user.WorkStatusDesc,
+			},
 		}
+		_, err = userCollection.UpdateOne(context.Background(), bson.M{""username"": empID}, _update)
+		if err != nil {
+			return nil, fmt.Errorf(""[db.checkEmpShareData] 更新用戶狀態失敗: %v"", err)
+		}
+		log.Printf(""[db.checkEmpShareData] 使用者 [%s] 資料已更新:%+v\n"", empID, _user)
+	}
+
+	if err := rows.Err(); err != nil {
+		return nil, fmt.Errorf(""[db.checkEmpShareData] 遍歷 Daily_EmpShareLog 結果失敗: %v"", err)
 	}
 
-	// 刪除離職員工
-	if len(departureUsers) > 0 {
+	// 更新離職員工帳號狀態
+	if len(disabledUsers) > 0 {
 		filter := bson.M{
-			""username"": bson.M{""$in"": departureUsers},
-			""role"":     bson.M{""$ne"": SUPER_USER},
+			""username"": bson.M{""$in"": disabledUsers},
 		}
-
-		result, err := userCollection.DeleteMany(context.Background(), filter)
+		update := bson.M{
+			""$set"": bson.M{
+				""account_status"": ""disabled"",
+			},
+		}
+		result, err := userCollection.UpdateMany(context.Background(), filter, update)
 		if err != nil {
-			log.Println(""[db.checkEmpShareData] Failed to delete users: "", err)
-			return nil, err
+			return nil, fmt.Errorf(""[db.checkEmpShareData] 更新用戶狀態失敗: %v"", err)
 		}
-
-		log.Printf(""[db.checkEmpShareData] 刪除 %v 使用者: "", result.DeletedCount)
+		log.Printf(""[db.checkEmpShareData] 已停用 %d 個使用者"", result.ModifiedCount)
 	}
 
 	// 需要停用的用戶列表
-	var disabledUsers []string
+	var expiredUsers []string
 	// 檢查最近登入時間
 	twoMonthsAgo := time.Now().AddDate(0, -2, 0)
 	for userId, user := range userInfoMap {
@@ -193,18 +213,24 @@ func checkEmpShareLogData(sqlConn *sql.DB) ([]UserInfo, error) {
 			checkTime = user.CreateTime
 		}
 		if checkTime.Before(twoMonthsAgo) {
-			user.AfterID = ""2""
-			user.AfterRemark = ""禁用""
+			user.WorkStatus = ""3""
+			user.WorkStatusDesc = ""禁用""
 			userInfoMap[userId] = user
-			disabledUsers = append(disabledUsers, user.UserId)
+			expiredUsers = append(expiredUsers, user.UserId)
 			log.Printf(""[db.checkEmpShareData] 禁用使用者:%s , 上次登入時間:%v\n"", user.UserId, checkTime)
 		}
 	}
 
 	// 更新 MongoDB
-	if len(disabledUsers) > 0 {
-		filter := bson.M{""username"": bson.M{""$in"": disabledUsers}}
-		update := bson.M{""$set"": bson.M{""account_status"": ""disabled""}}
+	if len(expiredUsers) > 0 {
+		filter := bson.M{""username"": bson.M{""$in"": expiredUsers}}
+		update := bson.M{
+			""$set"": bson.M{
+				""account_status"": ""disabled"",
+				""WorkStatus"":     ""3"",
+				""WorkStatusDesc"": ""禁用"",
+			},
+		}
 		result, err := userCollection.UpdateMany(context.Background(), filter, update)
 		if err != nil {
 			return nil, fmt.Errorf(""[db.checkEmpShareData] 更新用戶狀態失敗: %v"", err)
@@ -215,14 +241,14 @@ func checkEmpShareLogData(sqlConn *sql.DB) ([]UserInfo, error) {
 	// 準備回傳的 UserInfo
 	for _, user := range userInfoMap {
 		ssoACInfoList = append(ssoACInfoList, UserInfo{
-			Username:    user.UserId,
-			DisplayName: user.DisplayName,
-			CompID:      user.CompID,
-			CompName:    user.CompName,
-			BrID:        user.BrID,
-			BrName:      user.BrName,
-			AfterID:     user.AfterID,
-			AfterRemark: user.AfterRemark,
+			Username:       user.UserId,
+			DisplayName:    user.DisplayName,
+			CompID:         user.CompID,
+			CompName:       user.CompName,
+			BrID:           user.BrID,
+			BrName:         user.BrName,
+			WorkStatus:     user.WorkStatus,
+			WorkStatusDesc: user.WorkStatusDesc,
 		})
 	}
 
@@ -233,15 +259,6 @@ func updateUserEmpShareData(sqlConn *sql.DB) error {
 	log.Println(""[db.updateUserEmpShareData] 更新使用者資料"")
 	userCollection := mongodb.Collection(""users"")
 
-	// 查找欄位為空字串或不存在的使用者
-	// filter := bson.M{
-	// 	""$or"": []bson.M{
-	// 		{""CompID"": bson.M{""$in"": []interface{}{"""", nil}}},
-	// 		{""CompName"": bson.M{""$in"": []interface{}{"""", nil}}},
-	// 		{""BrID"": bson.M{""$in"": []interface{}{"""", nil}}},
-	// 		{""BrName"": bson.M{""$in"": []interface{}{"""", nil}}},
-	// 	},
-	// }
 	filter := bson.M{
 		""username"": bson.M{""$ne"": SUPER_USER},
 	}
@@ -265,7 +282,7 @@ func updateUserEmpShareData(sqlConn *sql.DB) error {
 		if username, ok := doc[""username""].(string); ok {
 			if username != SUPER_USER {
 				usernames = append(usernames, username)
-				log.Println(""[DEBUG] 需更新人員："", userDocs)
+				log.Println(""[DEBUG] 需更新人員："", username)
 			}
 		}
 	}
@@ -278,7 +295,7 @@ func updateUserEmpShareData(sqlConn *sql.DB) error {
 			quoted[i] = fmt.Sprintf(""'%s'"", name)
 		}
 		query := fmt.Sprintf(`
-			SELECT EmpID, CompID, CompName, DeptID, DeptName FROM vw_EmpShare
+			SELECT EmpID, CompID, CompName, DeptID, DeptName, WorkStatus FROM vw_EmpShare
 			WHERE EmpID IN (%s)`, strings.Join(quoted, "",""))
 
 		log.Println(""[DEBUG] [db.updateUserEmpShareData] query: "", query)
@@ -290,18 +307,30 @@ func updateUserEmpShareData(sqlConn *sql.DB) error {
 		defer rows.Close()
 
 		for rows.Next() {
-			var empID, compID, compName, deptID, deptName string
-			if err := rows.Scan(&empID, &compID, &compName, &deptID, &deptName); err != nil {
+			var empID, compID, compName, deptID, deptName, workStatus string
+			if err := rows.Scan(&empID, &compID, &compName, &deptID, &deptName, &workStatus); err != nil {
 				return fmt.Errorf(""[db.updateUserEmpShareData] 讀取行數據失敗: %v"", err)
 			}
 
+			workStatusDesc := """"
+			switch workStatus {
+			case ""1"":
+				workStatusDesc = ""在職""
+			case ""3"":
+				workStatusDesc = ""離職""
+			default:
+				workStatusDesc = ""其他""
+			}
+
 			// 更新回 MongoDB
 			update := bson.M{
 				""$set"": bson.M{
-					""CompID"":   compID,
-					""CompName"": compName,
-					""BrID"":     deptID,
-					""BrName"":   deptName,
+					""CompID"":         compID,
+					""CompName"":       compName,
+					""BrID"":           deptID,
+					""BrName"":         deptName,
+					""WorkStatus"":     workStatus,
+					""WorkStatusDesc"": workStatusDesc,
 				},
 			}
 
@@ -314,6 +343,9 @@ func updateUserEmpShareData(sqlConn *sql.DB) error {
 
 			log.Printf(""[db.updateUserEmpShareData] 使用者 [%s] 資料已更新:%+v\n"", empID, update)
 		}
+		if err := rows.Err(); err != nil {
+			return fmt.Errorf(""[db.updateUserEmpShareData] 遍歷 vw_EmpShare 結果失敗: %v"", err)
+		}
 	}
 
 	return nil"
WM_CMS_SERVER/scheduled.go,Modified,"diff --git a/WM_CMS_SERVER/scheduled.go b/WM_CMS_SERVER/scheduled.go
index a3da74e..8d38b42 100644
--- a/WM_CMS_SERVER/scheduled.go
+++ b/WM_CMS_SERVER/scheduled.go
@@ -69,8 +69,8 @@ func UpdateSSOACInfoLogRoutine() {
 						FunName:        ""使用者管理"",
 						GrantType:      ""系統授權"",
 						GrantDesc:      """",
-						WorkStatus:     ssoACInfo.AfterID,
-						WorkStatusDesc: ssoACInfo.AfterRemark,
+						WorkStatus:     ssoACInfo.WorkStatus,
+						WorkStatusDesc: ssoACInfo.WorkStatusDesc,
 						GrantUserID:    ""SYSTEM"",
 						GrantUserName:  ""SYSTEM"",
 						Remark:         """","