liuxiaolong
2020-08-12 7eda9be6f0021f2df46a9cb66d87fe23d5d3f904
push to users those who bind clientId
4个文件已修改
81 ■■■■■ 已修改文件
models/db.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/userClient.go 53 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/msgPush.go 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/userService.go 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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)
}
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"` //手机客户端id
    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()
}
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 {
                if uc.Exist(phoneNum) {
                aliasArr = append(aliasArr, phoneNum)
                }
            }
        }
    }
@@ -387,15 +390,18 @@
        }
    }
    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 { //此人已注册到系统,并且车不在停车库内
                    if uc.Exist(phoneNum) {
                    aliasArr = append(aliasArr, phoneNum)
                }
            }
        }
    }
    }
    if len(aliasArr) == 0 {
        fmt.Println("没有推送目标,aliasArr is empty")
service/userService.go
@@ -57,6 +57,16 @@
                //客户端cid绑定别名
                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 @@
            //客户端cid绑定别名
            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)
                }()