140 lines
3.8 KiB
Go
140 lines
3.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"yintai-api/app/common/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type User struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewUser(db ...*gorm.DB) *User {
|
|
return &User{db: getDB(db...)}
|
|
}
|
|
|
|
func (r *User) TableName() string {
|
|
return "yt_user"
|
|
}
|
|
|
|
func (r *User) Create(m *model.User) error {
|
|
return r.db.Table(r.TableName()).Create(m).Error
|
|
}
|
|
|
|
func (r *User) Update(m *model.User) error {
|
|
return r.db.Table(r.TableName()).Save(m).Error
|
|
}
|
|
|
|
func (r *User) Delete(id int64) error {
|
|
return r.db.Table(r.TableName()).Where("id = ?", id).Delete(&User{}).Error
|
|
}
|
|
|
|
func (r *User) BatchDelete(ids []int64) error {
|
|
return r.db.Table(r.TableName()).Where("id in (?)", ids).Delete(&User{}).Error
|
|
}
|
|
|
|
func (r *User) Get(id int64) (*model.User, error) {
|
|
var res model.User
|
|
if err := r.db.Table(r.TableName()).Where("id = ?", id).First(&res).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *User) AccountGet(account string) (*model.User, error) {
|
|
var res model.User
|
|
if err := r.db.Table(r.TableName()).Where("account = ?", account).First(&res).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (r *User) GetWithRole(account string) (*model.UserWithRole, error) {
|
|
var res *model.UserWithRole
|
|
tx := r.db.Table(r.TableName()+" as u ").Where("u.account = ?", account).Where("u.status = ?", model.UserStatusNormal)
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s AS ur ON ur.user_id = u.id", NewUserRole().TableName()))
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s AS r ON r.id = ur.role_id", NewRole().TableName()))
|
|
|
|
if err := tx.Select("u.*,r.id as role_id,r.name as role_name").First(&res).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (r *User) GetUserId(userId int64) (*model.UserWithRole, error) {
|
|
var res *model.UserWithRole
|
|
tx := r.db.Table(r.TableName()+" as u ").Where("u.id = ?", userId).Where("u.status = ?", model.UserStatusNormal)
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s AS ur ON ur.user_id = u.id", NewUserRole().TableName()))
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s AS r ON r.id = ur.role_id", NewRole().TableName()))
|
|
|
|
if err := tx.Select("u.*,r.id as role_id,r.name as role_name").First(&res).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (r *User) Exists(id int64) (bool, error) {
|
|
var count int64
|
|
if err := r.db.Table(r.TableName()).Where("id = ?", id).Count(&count).Error; err != nil {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
func (r *User) AccountExists(account string) (bool, error) {
|
|
var count int64
|
|
if err := r.db.Table(r.TableName()).Where("account = ?", account).Count(&count).Error; err != nil {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
func (r *User) List(username, account string, role int64, page, size int, sort string) ([]*model.UserWithRole, int64, error) {
|
|
var res []*model.UserWithRole
|
|
var count int64
|
|
tx := r.db.Table(r.TableName() + " as u")
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s AS ur ON ur.user_id = u.id", NewUserRole().TableName()))
|
|
tx = tx.Joins(fmt.Sprintf(" LEFT JOIN %s AS r ON r.id = ur.role_id", NewRole().TableName()))
|
|
|
|
if username != "" {
|
|
tx = tx.Where("u.username LIKE ?", fmt.Sprintf("%%%s%%", username))
|
|
}
|
|
|
|
if account != "" {
|
|
tx = tx.Where("u.account LIKE ?", fmt.Sprintf("%%%s%%", account))
|
|
}
|
|
|
|
if role != 0 {
|
|
tx = tx.Where("r.role = ?", role)
|
|
}
|
|
|
|
if err := tx.Count(&count).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
tx = tx.Order("id DESC")
|
|
if sort != "" {
|
|
tx = tx.Order(sort)
|
|
}
|
|
|
|
if err := tx.Limit(size).Offset((page - 1) * size).Select("u.*,r.id as role_id,r.name as role_name").Find(&res).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return res, count, nil
|
|
}
|