From 7eda9be6f0021f2df46a9cb66d87fe23d5d3f904 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期三, 12 八月 2020 10:19:01 +0800 Subject: [PATCH] push to users those who bind clientId --- service/msgPush.go | 10 ++++- models/userClient.go | 53 ++++++++++++++++++++++++++ models/db.go | 2 service/userService.go | 20 ++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/models/db.go b/models/db.go index 5ad930c..72258a9 100644 --- a/models/db.go +++ b/models/db.go @@ -13,6 +13,6 @@ dbPath := rootPath +"/"+ dbUrl orm.RegisterDriver("sqlite", orm.DRSqlite) _ = orm.RegisterDataBase("default", "sqlite3", dbPath) - orm.RegisterModel(new(User), new(UserCar), new(SysUpgrade)) + orm.RegisterModel(new(User), new(UserCar), new(SysUpgrade), new(UserClient)) _ = orm.RunSyncdb("default", false, true) } diff --git a/models/userClient.go b/models/userClient.go index 2640e7f..fafebeb 100644 --- a/models/userClient.go +++ b/models/userClient.go @@ -1 +1,54 @@ package models + +import ( + "fmt" + "github.com/astaxie/beego/orm" +) + +//cid鍜屽埆鍚嶇粦瀹氳褰� +type UserClient struct { + Id string `orm:"pk;size(50);column(id)" json:"id"` + PhoneNum string `orm:"column(phoneNum)" json:"phoneNum"` //鎵嬫満鍙� + ClientId string `orm:"column(clientId)" json:"clientId"` //鎵嬫満瀹㈡埛绔痠d + BindTime string `orm:"column(bindTime)" json:"bindTime"` +} + +func (uc *UserClient) TableName() string { + return "user_client" +} + +func (uc *UserClient) Insert() (int64,error) { + o := orm.NewOrm() + return o.Insert(uc) +} + +func (uc *UserClient) Exist(phoneNum string) bool { + var list []UserClient + o := orm.NewOrm() + o.Raw("select * from ? where phoneNum=?", uc.TableName(), phoneNum).QueryRows(&list) + if len(list) >0 { + return true + } + return false +} + +func (uc *UserClient) ExistByCid(phoneNum string, cid string) bool { + var list []UserClient + o := orm.NewOrm() + o.Raw("select * from ? where phoneNum=? and clientId=?", uc.TableName(), phoneNum, cid).QueryRows(&list) + if len(list) >0 { + return true + } + return false +} + + +func (uc *UserClient) Delete(phoneNum string, clientId string) (int64, error) { + o := orm.NewOrm() + sql := fmt.Sprintf("delete from "+uc.TableName()+" where phoneNum='%s' and clientId='%s'", phoneNum, clientId) + result, err := o.Raw(sql).Exec() + if err != nil { + return 0, err + } + return result.RowsAffected() +} \ No newline at end of file diff --git a/service/msgPush.go b/service/msgPush.go index a74701f..03e036e 100644 --- a/service/msgPush.go +++ b/service/msgPush.go @@ -259,10 +259,13 @@ } } fmt.Println("len(carPersonM):", len(carPersonM), "len(pushUserM):", len(pushUserM)) + var uc models.UserClient for _,personId := range carPersonM { if phoneNum,ok := pushUserM[personId]; ok { //姝や汉宸叉敞鍐屽埌绯荤粺,骞朵笖杞︿笉鍦ㄥ仠杞﹀簱鍐� if _,in := delPersonIdM[personId];!in { - aliasArr = append(aliasArr, phoneNum) + if uc.Exist(phoneNum) { + aliasArr = append(aliasArr, phoneNum) + } } } } @@ -387,11 +390,14 @@ } } spaceNos := csv.FindSpaceNo("") + var uc models.UserClient for _,sn := range spaceNos { if sn.State == 1 && sn.PlateNo != "" { //宸茬粡鎶婅溅鍋滃埌鍋滆溅鍦虹殑杞︿富锛屼笉鍐嶆帹閫佹秷鎭� if personId,ok := carPersonM[sn.PlateNo];ok { if phoneNum,ok := pushUserM[personId]; ok { //姝や汉宸叉敞鍐屽埌绯荤粺,骞朵笖杞︿笉鍦ㄥ仠杞﹀簱鍐� - aliasArr = append(aliasArr, phoneNum) + if uc.Exist(phoneNum) { + aliasArr = append(aliasArr, phoneNum) + } } } } diff --git a/service/userService.go b/service/userService.go index 0bd2356..8f66c41 100644 --- a/service/userService.go +++ b/service/userService.go @@ -57,6 +57,16 @@ //瀹㈡埛绔痗id缁戝畾鍒悕 if cid != "" { go func() { + var uc models.UserClient + if !uc.ExistByCid(phoneNum, cid) { + new := models.UserClient{ + Id:uuid.NewV4().String(), + PhoneNum: phoneNum, + ClientId: cid, + BindTime: time.Now().Format("2006-01-02 15:04:05"), + } + new.Insert() + } bindR, bindE := BindAlias(cid, phoneNum) fmt.Println("bind cid:",cid, "phoneNum:",phoneNum,"result:", bindR, "err:", bindE) }() @@ -86,6 +96,16 @@ //瀹㈡埛绔痗id缁戝畾鍒悕 if cid != "" { go func() { + var uc models.UserClient + if !uc.ExistByCid(phoneNum, cid) { + new := models.UserClient{ + Id:uuid.NewV4().String(), + PhoneNum: phoneNum, + ClientId: cid, + BindTime: time.Now().Format("2006-01-02 15:04:05"), + } + new.Insert() + } bindR, bindE := BindAlias(cid, phoneNum) fmt.Println("bind cid:",cid, "phoneNum:",phoneNum,"result:", bindR, "err:", bindE) }() -- Gitblit v1.8.0