69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"twin-api/app/common/model"
|
|
"twin-api/base/global"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UserPkg struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewUserPkg() *UserPkg {
|
|
return &UserPkg{
|
|
db: global.GetDB(),
|
|
}
|
|
}
|
|
|
|
func (p *UserPkg) TableName() string {
|
|
return "tw_user_package"
|
|
}
|
|
|
|
func (p *UserPkg) Create(m *model.UserPkg) error {
|
|
return p.db.Table(p.TableName()).Create(m).Error
|
|
}
|
|
|
|
func (p *UserPkg) Update(m *model.UserPkg) error {
|
|
return p.db.Table(p.TableName()).Save(m).Error
|
|
}
|
|
|
|
func (p *UserPkg) Delete(m *model.UserPkg) error {
|
|
return nil
|
|
}
|
|
|
|
func (p *UserPkg) Get(id, userId int64) (*model.UserPkg, error) {
|
|
var res model.UserPkg
|
|
if err := p.db.Table(p.TableName()).Where("id = ? AND user_id = ?", id, userId).First(&res).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &res, nil
|
|
}
|
|
|
|
func (p *UserPkg) GetByUserId(userId int64) (*model.UserPkgInfo, error) {
|
|
var res model.UserPkgInfo
|
|
tx := p.db.Table(p.TableName() + " u ")
|
|
tx = tx.Joins(fmt.Sprintf(" JOIN %s p ON u.id = p.user_id", NewPackage().TableName()))
|
|
tx = tx.Where("u.user_id = ?", userId)
|
|
tx = tx.Order("u.id desc")
|
|
if err := tx.Select("u.*,p.*").First(&res).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (p *UserPkg) Query(...any) (any, error) {
|
|
panic("implement me")
|
|
}
|