123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package bizhi
- import (
- models2 "duoduo/apis/bizhi/models"
- orm "duoduo/database"
- "pmo/pkg/tools"
- )
- type User struct {
- ID int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
- Phone string `gorm:"column:phone;type:varchar(24)" json:"phone"` // 手机号
- OpenID1 string `gorm:"column:open_id_1;type:varchar(255)" json:"openId1"` // 上级open_id
- NickName string `gorm:"column:nick_name;type:varchar(255)" json:"nickName"` // 微信用户名
- AvatarUrl string `gorm:"column:avatar_url;type:varchar(255)" json:"avatarUrl"` // 头像url
- Admin int `gorm:"column:admin;type:int(11)" json:"admin"` // 1-管理员
- OpenID string `gorm:"column:open_id;type:varchar(255)" json:"openId"` // open_id
- CreateTime string `gorm:"column:create_time;type:datetime" json:"createTime"` // 创建时间
- UpdateTime string `gorm:"column:update_time;type:datetime" json:"updateTime"` // 更新时间
- Score int `gorm:"column:score;type:int(11)" json:"score"` // 积分
- }
- func (m *User) TableName() string {
- return "user"
- }
- func (u *User) GetNum() int {
- var count int
- tableCount := orm.BzMysql.Table(u.TableName()).Where("open_id = ? ", u.OpenID)
- tableCount.Count(&count)
- return count
- }
- func (u *User) Create() (User, error) {
- var doc User
- result := orm.Eloquent.Table(u.TableName()).Create(&u)
- if result.Error != nil {
- err := result.Error
- return doc, err
- }
- doc = *u
- return doc, nil
- }
- func (u *User) GetScore() (User, error) {
- var doc User
- if err := orm.BzMysql.Table(u.TableName()).Where("open_id = ?", u.OpenID).Select("score").First(&doc).Error; err != nil {
- return doc, err
- }
- return doc, nil
- }
- func (u *User) SubScore(req models2.SubUserScoreRequest) error {
- var score ScoreLog
- var err error
- tx := orm.BzMysql.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- score.Score = req.Score
- score.OpenID = req.OpenId
- score.CreateTime = tools.GetCurrentTimeStr()
- scoreStr := tools.IntToString(req.Score)
- if err := tx.Table(u.TableName()).Model(&u).Where("open_id = ? ", u.OpenID).Updates(
- map[string]interface{}{
- "score": "score + " + scoreStr}).Error; err != nil {
- return err
- }
- if err := tx.Table(score.TableName()).Create(&score).Error; err != nil {
- return err
- }
- return nil
- }
|