push to users those who bind clientId
| | |
| | | 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) |
| | | } |
| | |
| | | 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() |
| | | } |
| | |
| | | } |
| | | } |
| | | 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) |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | 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") |
| | |
| | | //客户端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) |
| | | }() |
| | |
| | | //客户端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) |
| | | }() |