package models import ( "github.com/beego/beego/v2/client/orm" "time" ) type UserRecord struct { UserRecordId int `orm:"column(user_record_id);pk"` OpenId string `description:"用户openid,最大长度:32"` UnionId string `description:"用户unionid,最大长度:64"` Subscribe int `description:"是否关注"` NickName string `descritpion:"用户昵称,最大长度:32"` RealName string `descritpion:"用户实际名称,最大长度:32"` BindAccount string `descritpion:"绑定时的账号,最大长度:128"` Sex int `descritpion:"普通用户性别,1为男性,2为女性"` Province string `description:"普通用户个人资料填写的省份,最大长度:30"` City string `description:"普通用户个人资料填写的城市,最大长度:30"` Country string `description:"国家,如中国为CN,最大长度:30"` Headimgurl string `description:"用户第三方(微信)头像,最大长度:512"` CreateTime time.Time `description:"创建时间,关系添加时间、用户授权时间"` CreatePlatform int `description:"注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1"` SessionKey string `description:"微信小程序会话密钥,最大长度:255"` UserId int `description:"用户id"` } // 添加用户关系 func AddUserRecord(record *UserRecord) (recordId int64, err error) { o := orm.NewOrmUsingDB("weekly_report") recordId, err = o.Insert(record) return } // 根据openid获取用户关系 // 12是买方研选 create_platform func GetUserRecordByUnionId(unionId string) (item *UserRecord, err error) { o := orm.NewOrmUsingDB("weekly_report") sql := `SELECT * FROM user_record WHERE union_id=? ORDER BY user_id DESC LIMIT 1 ` err = o.Raw(sql, unionId).QueryRow(&item) return } // 获取数量 func GetUserRecordCount(openId string) (count int, err error) { o := orm.NewOrmUsingDB("weekly_report") sqlCount := ` SELECT COUNT(1) AS count FROM user_record WHERE open_id=? ` err = o.Raw(sqlCount, openId).QueryRow(&count) return } // 根据用户id和平台id获取用户关系 func GetUserRecordByUserOpenId(open_id string) (item *UserRecord, err error) { o := orm.NewOrmUsingDB("weekly_report") sql := `SELECT u.* FROM JOIN user_record AS u WHERE u.open_id = ? AND u.create_platform = 4 AND u.bind_account != '' GROUP BY u.union_id` err = o.Raw(sql, open_id).QueryRow(&item) return } func GetUserRecordList(condition string, pars []interface{}) (items []*UserRecord, err error) { o := orm.NewOrmUsingDB("weekly_report") sql := ` SELECT * FROM user_record WHERE 1= 1 ` if condition != "" { sql += condition } _, err = o.Raw(sql, pars).QueryRows(&items) return } // 根据openid解除绑定用户关系 func UnBindUserRecordByOpenid(openId string) (err error) { o := orm.NewOrmUsingDB("weekly_report") msql := ` UPDATE user_record SET user_id = 0,bind_account="" WHERE open_id = ? ` _, err = o.Raw(msql, openId).Exec() return }