From eef11eeadc679b1ad7bd23c983a67318d7cd3705 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 11 八月 2020 17:34:00 +0800
Subject: [PATCH] sync user from hik manual,ret syncCount

---
 controllers/user.go   |   13 +++++++++++++
 routers/router.go     |    1 +
 service/carService.go |   18 +++++++++++++++---
 models/user.go        |   15 +++++++++++++--
 4 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/controllers/user.go b/controllers/user.go
index 26ca81b..927a38f 100644
--- a/controllers/user.go
+++ b/controllers/user.go
@@ -186,4 +186,17 @@
 }
 
 
+// @router /sync [get]
+func (u *UserController) Sync() {
+	resp := code.Code{}
+	syncCount := service.SyncHikPerson()
 
+	resp.Success = true
+	resp.Status = http.StatusOK
+	resp.Data = map[string]interface{}{
+		"syncCount": syncCount,
+	}
+
+	u.Data["json"] = resp
+	u.ServeJSON()
+}
diff --git a/models/user.go b/models/user.go
index df2b4e9..ea773c9 100644
--- a/models/user.go
+++ b/models/user.go
@@ -55,15 +55,17 @@
 	return all,nil
 }
 
-func (u *User) GetAllMapByPhone() map[string]User {
+func (u *User) GetAllMapByPhone() (map[string]User, map[string]User) {
 	m := make(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
+	return m, idM
 }
 
 func (u *User) Update() (int64, error) {
@@ -89,3 +91,12 @@
 	o := orm.NewOrm()
 	return o.Update(u, "isDelete")
 }
+
+func (u *User) Delete(id string) (int64, error) {
+	o := orm.NewOrm()
+	res, err := o.Raw("delete from sys_user where id=?", id).Exec()
+	if err != nil {
+		return 0, err
+	}
+	return res.RowsAffected()
+}
\ No newline at end of file
diff --git a/routers/router.go b/routers/router.go
index 105255c..4fd8880 100644
--- a/routers/router.go
+++ b/routers/router.go
@@ -56,6 +56,7 @@
 	beego.Router(preApi+"/user/delPlateNo", &controllers.UserController{}, "*:DelPlateNo")
 
 	beego.Router(preApi+"/user/all", &controllers.UserController{}, "*:GetUserAll")
+	beego.Router(preApi+"/user/sync", &controllers.UserController{}, "*:Sync")
 
 	beego.Router(preApi+"/car/crossRecord", &controllers.CarController{}, "*:CrossRecord")
 }
diff --git a/service/carService.go b/service/carService.go
index 964c706..a3c45e0 100644
--- a/service/carService.go
+++ b/service/carService.go
@@ -303,20 +303,32 @@
 	return list
 }
 
-func SyncHikPerson() {
+func SyncHikPerson() int {
+	syncCount := 0
 	sv := NewCarService()
 	hikPersons := sv.GetHikPersonList()
-	if hikPersons != nil {
+	if hikPersons != nil && len(hikPersons) >0 {
 		var u models.User
-		uMap := u.GetAllMapByPhone()
+		uMap,idMap := u.GetAllMapByPhone()
+		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)
+					syncCount++
 				}
 			}
 		}
+		for k,_ := range idMap {
+			if _,in := hikPM[k];!in {
+				u.Delete(k)
+				syncCount++
+			}
+		}
+
 	}
+	return syncCount
 }
 
 func (sv *CarService) getHikPageResult(url string, reqBody map[string]interface{}) *vo.HikPageResult {

--
Gitblit v1.8.0