From dbc843d0b37f786fb816131bcc7ebca86dbe72e9 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期四, 06 八月 2020 09:58:46 +0800
Subject: [PATCH] add crossRecord

---
 service/userService.go |  155 ++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 121 insertions(+), 34 deletions(-)

diff --git a/service/userService.go b/service/userService.go
index 24db8c1..731ee51 100644
--- a/service/userService.go
+++ b/service/userService.go
@@ -1,15 +1,15 @@
 package service
 
 import (
-	"car-service/cache"
 	"car-service/extend/util"
 	"car-service/models"
 	"car-service/vo"
 	"errors"
 	"fmt"
 	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
-	"github.com/gomodule/redigo/redis"
 	"github.com/satori/go.uuid"
+	"sync"
+	"time"
 )
 
 type UserService struct {
@@ -18,28 +18,57 @@
 
 func (sv *UserService) Login(phoneNum, code string) (bool,*vo.UserInfo,error) {
 	if verifyCode(phoneNum, code) {
+		carSv := NewCarService()
+
 		var tmpUser models.User
-		if err := tmpUser.SelectByPhoneNum(phoneNum); err != nil { //鐢ㄦ埛涓嶅瓨鍦�,鍒欐柊澧�
+		err := tmpUser.SelectByPhoneNum(phoneNum)
+		fmt.Println("login err:", err)
+		if err != nil { //鐢ㄦ埛涓嶅瓨鍦�,鍒欐柊澧�
+			//鍏堝垽鏂鎵嬫満鍙锋槸鍚﹀湪娴峰悍骞冲彴涓�
+			hikPersons := carSv.GetHikPersonList()
+			found := false
+			hikPersonId := ""
+			if hikPersons != nil {
+				for _,hikP := range hikPersons {
+					if hikP.PhoneNo == phoneNum {
+						found = true
+						hikPersonId = hikP.PersonId
+					}
+				}
+			}
+			if !found {
+				return false, nil, errors.New("璇疯仈绯诲仠杞﹀満绠$悊鍛�")
+			}
+
 			u := models.User{
-				Id: uuid.NewV4().String(),
+				Id: hikPersonId,
 				PhoneNum: phoneNum,
 				IsDelete: false,
 			}
-			if i, e := u.Insert();e ==nil && i >0 {
+			if _, e := u.Insert();e ==nil {
+				var plateNos = make([]string, 0)
+				hikVehicles := carSv.GetVehicleListByPerson(u.Id)
+
+				if hikVehicles != nil {
+					for _,up := range hikVehicles {
+						plateNos = append(plateNos, up.PlateNo)
+					}
+				}
 				return true, &vo.UserInfo{
 					UserId: u.Id,
 					PhoneNum: phoneNum,
-					PlateNos: []string{},
+					PlateNos: plateNos,
 				}, nil
 			} else {
+				fmt.Println("u.Insert err:", e)
 				return false, nil, errors.New("娉ㄥ唽澶辫触")
 			}
 		} else { //鐢ㄦ埛宸插瓨鍦�
-			var userCar models.UserCar
-			var plateNos []string
-			all, e := userCar.GetByUserId(tmpUser.Id)
-			if e == nil && all != nil {
-				for _,up := range all {
+			var plateNos = make([]string, 0)
+			hikVehicles := carSv.GetVehicleListByPerson(tmpUser.Id)
+
+			if hikVehicles != nil {
+				for _,up := range hikVehicles {
 					plateNos = append(plateNos, up.PlateNo)
 				}
 			}
@@ -55,10 +84,43 @@
 }
 
 func (sv *UserService) AddPlateNo(userId, plateNo string) bool {
+	var uc models.UserCar
+	if uc.Exist(userId, plateNo) {
+		return true
+	}
+	tmp := models.UserCar{
+		Id: uuid.NewV4().String(),
+		UserId: userId,
+		PlateNo: plateNo,
+	}
+	_, err := tmp.Insert()
+	if err == nil {
+		return true
+	}
 	return false
 }
 
 func NewVerifyCode(phoneNum string) error {
+	var tmpUser models.User
+	err := tmpUser.SelectByPhoneNum(phoneNum)
+	if err != nil { //鐢ㄦ埛涓嶅瓨鍦�,娉ㄥ唽鑾峰彇楠岃瘉鐮侊紝姝ゆ墜鏈哄彿蹇呴』鍦ㄨ偛鑻辨捣搴峰钩鍙颁腑
+		carSv := NewCarService()
+		personList := carSv.GetHikPersonList()
+		found := false
+		if personList ==nil {
+			return errors.New("鎵嬫満鍙蜂笉瀛樺湪锛岃鑱旂郴鍋滆溅鍦虹鐞嗗憳")
+		} else {
+			for _,p := range personList {
+				if p.PhoneNo == phoneNum {
+					found = true
+				}
+			}
+		}
+		if !found {
+			return errors.New("鎵嬫満鍙蜂笉瀛樺湪锛岃鑱旂郴鍋滆溅鍦虹鐞嗗憳")
+		}
+	}
+
 	regionId := "cn-hangzhou"
 	accessKeyId := "LTAIkHFaStA1JKk5"
 	AccessSecret := "oE7LhSqBWWUBzV0B7l1G9aVmgHPddM"
@@ -85,35 +147,60 @@
 }
 
 func verifyCode(phoneNum string, cod string) bool {
-	return true
-	//if b,r := existCode(phoneNum);b && r == cod {
-	//	return true
-	//} else {
-	//	fmt.Println("verifyCode false,cod:",cod, "r:",r,"b:",b)
-	//}
-	//return false
+	if b,r := existCode(phoneNum);b && r == cod {
+		return true
+	} else {
+		fmt.Println("verifyCode false,cod:",cod, "r:",r,"b:",b)
+	}
+	return false
 }
 
+type CodCache struct {
+	phoneNum    string
+	cod 		string
+	ex 			time.Time
+}
+var codMap = make(map[string]CodCache)
+var codLock sync.RWMutex
 func add2Cache(phoneNum string, code string) {
 	//鍐欏埌redis缂撳瓨涓�
-	c := cache.Get()
-	defer c.Close()
-
-	reply, err := c.Do("SET", phoneNum, code, "EX", 5 * 60)//瓒呮椂浜斿垎閽�
-	if err != nil {
-		fmt.Println("鍐欏叆redis澶辫触")
-	} else {
-		fmt.Println("鍐欏叆redis鎴愬姛,reply:", reply)
+	//c := cache.Get()
+	//defer c.Close()
+	//
+	//reply, err := c.Do("SET", phoneNum, code, "EX", 5 * 60)//瓒呮椂浜斿垎閽�
+	//if err != nil {
+	//	fmt.Println("鍐欏叆redis澶辫触")
+	//} else {
+	//	fmt.Println("鍐欏叆redis鎴愬姛,reply:", reply)
+	//}
+	codLock.Lock()
+	defer codLock.Unlock()
+	expireTime := time.Now().Add(time.Minute * 5)
+	codMap[phoneNum] = CodCache{
+		phoneNum:phoneNum,
+		cod: code,
+		ex:expireTime,
 	}
 }
 func existCode(phoneNum string) (bool,string) {
-	c := cache.Get()
-	defer c.Close()
-
-	r, err := redis.String(c.Do("GET", phoneNum))
-	if err != nil {
-		fmt.Println("existCode err:", err)
-		return false,""
+	//c := cache.Get()
+	//defer c.Close()
+	//
+	//r, err := redis.String(c.Do("GET", phoneNum))
+	//if err != nil {
+	//	fmt.Println("existCode err:", err)
+	//	return false,""
+	//}
+	//return true,r
+	codLock.Lock()
+	defer codLock.Unlock()
+	if cache,exist := codMap[phoneNum];exist {
+		if cache.ex.Before(time.Now()) {//宸茶繃鏈�
+			return false, ""
+		} else {
+			return true, cache.cod
+		}
+	} else {
+		return false, ""
 	}
-	return true,r
 }
\ No newline at end of file

--
Gitblit v1.8.0