123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package mysql
- import (
- orm "duoduo/database"
- "duoduo/tools"
- "errors"
- )
- type User struct {
- CreateTime string `gorm:"column:create_time" json:"createTime"`
- OpenID string `gorm:"column:open_id" json:"openId"`
- Phone string `gorm:"column:phone" json:"phone"`
- Pid string `gorm:"column:pid" json:"pid"`
- UpdateTime string `gorm:"column:update_time" json:"updateTime"`
- NickName string `gorm:"column:nick_name" json:"nickName"`
- AvatarUrl string `gorm:"column:avatar_url" json:"avatarUrl"`
- OpenIdOne string `gorm:"column:open_id_1" json:"openIdOne"`
- OpenIdTwo string `gorm:"column:open_id_2" json:"openIdTwo"`
- Admin int `gorm:"column:admin" json:"admin"` //1-管理员
- AliPay string `gorm:"column:ali_pay" json:"aliPay"` //
- WxPay string `gorm:"column:wx_pay" json:"wxPay"`
- }
- type UserName struct {
- CreateTime string `gorm:"column:create_time" json:"createTime"`
- OpenID string `gorm:"column:open_id" json:"openId"`
- Phone string `gorm:"column:phone" json:"phone"`
- Pid string `gorm:"column:pid" json:"pid"`
- UpdateTime string `gorm:"column:update_time" json:"updateTime"`
- NickName string `gorm:"column:nick_name" json:"nickName"`
- AvatarUrl string `gorm:"column:avatar_url" json:"avatarUrl"`
- OpenIdOne string `gorm:"column:open_id_1" json:"openIdOne"`
- OpenIdTwo string `gorm:"column:open_id_2" json:"openIdTwo"`
- Id int `gorm:"column:id" json:"id"`
- Admin int `gorm:"column:admin" json:"admin"` //1-管理员
- AliPay string `gorm:"column:ali_pay" json:"aliPay"` //
- WxPay string `gorm:"column:wx_pay" json:"wxPay"`
- }
- // TableName sets the insert table name for this struct type
- func (u *User) TableName() string {
- return "user"
- }
- func (u *User) GetNum() int {
- var count int
- tableCount := orm.Eloquent.Table(u.TableName()).Where("open_id = ? ", u.OpenID)
- tableCount.Count(&count)
- return count
- }
- func (u *User) GetTotal() (int, error) {
- var count int
- tableCount := orm.Eloquent.Table(u.TableName())
- tableCount.Count(&count)
- return count, nil
- }
- func (u *User) Get() (UserName, error) {
- var doc UserName
- err := orm.Eloquent.Select("id,pid,nick_name,avatar_url,open_id_1,open_id_2,admin,wx_pay,ali_pay").Where("open_id = ? ", u.OpenID).Table(u.TableName()).First(&doc).Error
- if err != nil {
- return doc, err
- }
- return doc, nil
- }
- 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) Update() (update User, err error) {
- if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
- map[string]interface{}{
- "pid": u.Pid,
- "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
- return update, err
- }
- return update, nil
- }
- func (u *User) UpdateName() (update User, err error) {
- if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
- map[string]interface{}{
- "nick_name": u.NickName,
- "avatar_url": u.AvatarUrl,
- "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
- return update, err
- }
- return update, nil
- }
- func (u *User) UpdatePay() (update User, err error) {
- if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
- map[string]interface{}{
- "ali_pay": u.AliPay,
- "wx_pay": u.WxPay,
- "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
- return update, err
- }
- return update, nil
- }
- //list 接口使用
- func (u *User) GetOrderOpenIdList(pageSize int, pageIndex int, status int) ([]UserName, int, error) {
- var doc []UserName
- table := orm.Eloquent.Table(u.TableName())
- if status == 1 {
- table = table.Where("open_id_1 = ? ", u.OpenID)
- } else if status == 2 {
- table = table.Where("open_id_2 = ? ", u.OpenID)
- } else {
- return nil, 0, errors.New("status err")
- }
- //table = table.Where("open_id = ? ", o.OpenId)
- var count int
- if err := table.Select("id,nick_name,avatar_url,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as create_time").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
- return nil, 0, err
- }
- table.Count(&count)
- return doc, count, nil
- }
|