package models import ( "fmt" "strings" "time" "github.com/beego/beego/v2/client/orm" ) type User struct { UserId int `orm:"pk" description:"用户id"` OpenId string `description:"openid"` UnionId string `description:"unionid"` RealName string `description:"姓名"` Phone string `description:"手机号"` AreaCode string `description:"区号"` Email string `description:"邮箱"` SellerId int `description:"销售id(SysUserId)"` Company string `description:"所属公司"` ValidStartTime time.Time `description:"有效期开始时间"` ValidEndTime time.Time `description:"有效期结束时间"` Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"` CreateTime time.Time `description:"系统中首次新增用户的时间"` ModifyTime time.Time `description:"系统中用户信息变更的时间"` RegisterTime time.Time `description:"用户首次登录小程序的时间"` IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"` IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"` } func (u *User) Insert() (insertId int64, err error) { o := orm.NewOrm() insertId, err = o.Insert(u) return } type UserItem struct { UserId int `description:"用户id"` OpenId string `description:"open_id"` UnionId string `description:"union_id"` NickName string `description:"用户昵称"` RealName string `description:"用户实际名称"` Phone string `description:"手机号码"` Componey string `description:"所属公司"` AreaCode string `description:"区号"` SellerId int `description:"销售id"` Email string `description:"邮箱"` Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"` ValidEndTime time.Time `description:"服务截至时间"` RegisterTime time.Time `description:"登录时间,用户首次登录小程序的时间"` CreateTime time.Time `description:"系统中新增用户的时间"` ModifyTime time.Time `description:"系统中用户信息更新的时间"` IsRegistered bool `description:"是否注册:1:已注册,0:未注册"` } // 变更联系人是否已注册状态 func ModifyUserRegisterStatus(userId int, status bool, registerTime, modifyTime time.Time) (err error) { o := orm.NewOrm() sql := `UPDATE user SET is_registered=?, register_time=?, modify_time=? WHERE user_id = ? ` _, err = o.Raw(sql, status, registerTime, modifyTime, userId).Exec() return } func GetUserBySubscribe() (user []*User, err error) { o := orm.NewOrm() sql := `SELECT * FROM user WHERE is_subscribed=1 AND is_registered=1 AND union_id IS NOT NULL ` _, err = o.Raw(sql).QueryRows(&user) return } // GetUserBySubscribeAndFormatUser 查询已经注册和关注公众号的正式用户 func GetUserBySubscribeAndFormatUser() (user []*User, err error) { o := orm.NewOrm() sql := `SELECT * FROM user WHERE is_subscribed=1 AND is_registered=1 AND union_id IS NOT NULL AND status=2` _, err = o.Raw(sql).QueryRows(&user) return } func GetUserById(userId int) (item *User, err error) { o := orm.NewOrm() sql := `SELECT * FROM user WHERE user_id=? ` err = o.Raw(sql, userId).QueryRow(&item) return } func GetUserList(condition string, pars []interface{}) (items []*User, err error) { o := orm.NewOrm() sql := `SELECT * FROM user WHERE 1=1 ` if condition != "" { sql += condition } _, err = o.Raw(sql, pars...).QueryRows(&items) return } func GetUserItemByPhone(phone string) (item *UserItem, err error) { o := orm.NewOrm() sql := `SELECT * FROM user WHERE phone=? ` err = o.Raw(sql, phone).QueryRow(&item) return } func GetUserItemByEmail(email string) (item *UserItem, err error) { sql := `SELECT * FROM user WHERE email=? ` err = orm.NewOrm().Raw(sql, email).QueryRow(&item) return } func GetUserItemByUserId(userId int) (item *UserItem, err error) { sql := `SELECT * FROM user WHERE user_id=? ` err = orm.NewOrm().Raw(sql, userId).QueryRow(&item) return } func GetUserUnionIdListByIds(userIds []int) (items []string, err error) { sql := `SELECT union_id FROM user WHERE user_id IN (%s) AND union_id IS NOT NULL` var idsStr string if len(userIds) > 0 { var userIdsStr []string for _, userId := range userIds { userIdsStr = append(userIdsStr, fmt.Sprint(userId)) } idsStr = strings.Join(userIdsStr, ",") sql = fmt.Sprintf(sql, idsStr) } _, err = orm.NewOrm().Raw(sql).QueryRows(&items) return }