From aa981c60d68d3410d4ea580ca6f24abab43cb30f Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期三, 12 八月 2020 17:15:11 +0800
Subject: [PATCH] fix sync person from hik and update phoneNum if change

---
 service/carService.go  |   10 +++++-----
 service/userService.go |    9 ++++-----
 models/user.go         |   10 ++++------
 3 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/models/user.go b/models/user.go
index ea773c9..bab5e41 100644
--- a/models/user.go
+++ b/models/user.go
@@ -55,17 +55,15 @@
 	return all,nil
 }
 
-func (u *User) GetAllMapByPhone() (map[string]User, map[string]User) {
-	m := make(map[string]User)
+func (u *User) GetAllMap() (map[string]User) {
 	idM := make(map[string]User)
 	all, _ := u.GetAllUsers()
 	if all !=nil {
 		for _,p := range all {
-			m[p.PhoneNum] = p
 			idM[p.Id] = p
 		}
 	}
-	return m, idM
+	return idM
 }
 
 func (u *User) Update() (int64, error) {
@@ -73,9 +71,9 @@
 	return o.Update(u)
 }
 
-func (u *User) SyncHikPersonId(phoneNum string, hikPersonId string) (int64, error) {
+func (u *User) UpdatePhoneNum(phoneNum string, hikPersonId string) (int64, error) {
 	o := orm.NewOrm()
-	res, err := o.Raw("update sys_user set id=? where phoneNum=?", hikPersonId, phoneNum).Exec()
+	res, err := o.Raw("update sys_user set phoneNum=? where id=?", phoneNum, hikPersonId).Exec()
 	if err != nil {
 		return 0, err
 	}
diff --git a/service/carService.go b/service/carService.go
index a3c45e0..3fdff76 100644
--- a/service/carService.go
+++ b/service/carService.go
@@ -309,13 +309,13 @@
 	hikPersons := sv.GetHikPersonList()
 	if hikPersons != nil && len(hikPersons) >0 {
 		var u models.User
-		uMap,idMap := u.GetAllMapByPhone()
+		idMap := u.GetAllMap()
 		hikPM := make(map[string]string)
 		for _,hp := range hikPersons {
-			hikPM[hp.PersonId] = hp.PersonId
-			if sp,ok := uMap[hp.PhoneNo];ok {
-				if sp.Id != hp.PersonId {
-					u.SyncHikPersonId(hp.PhoneNo, hp.PersonId)
+			hikPM[hp.PersonId] = hp.PhoneNo
+			if sp,ok := idMap[hp.PersonId];ok {
+				if sp.PhoneNum != hp.PhoneNo {
+					u.UpdatePhoneNum(hp.PhoneNo, hp.PersonId)
 					syncCount++
 				}
 			}
diff --git a/service/userService.go b/service/userService.go
index 71bf37f..dc2f08b 100644
--- a/service/userService.go
+++ b/service/userService.go
@@ -37,7 +37,7 @@
 		}
 
 		var tmpUser models.User
-		err := tmpUser.SelectByPhoneNum(phoneNum)
+		err := tmpUser.SelectById(hikPersonId)
 		fmt.Println("login err:", err)
 		if err != nil { //鐢ㄦ埛涓嶅瓨鍦�,鍒欐柊澧�
 			u := models.User{
@@ -88,12 +88,11 @@
 				return false, nil, errors.New("娉ㄥ唽澶辫触")
 			}
 		} else { //鐢ㄦ埛宸插瓨鍦�
-			if hikPersonId != tmpUser.Id {
-				tmpUser.Id = hikPersonId
-				tmpUser.SyncHikPersonId(tmpUser.PhoneNum, hikPersonId)
+			if phoneNum != tmpUser.PhoneNum {
+				tmpUser.UpdatePhoneNum(tmpUser.PhoneNum, hikPersonId)
 			}
 			var plateNos = make([]string, 0)
-			hikVehicles := carSv.GetVehicleListByPerson(tmpUser.Id)
+			hikVehicles := carSv.GetVehicleListByPerson(hikPersonId)
 
 			if hikVehicles != nil {
 				for _,up := range hikVehicles {

--
Gitblit v1.8.0