第一版完成
This commit is contained in:
95
internal/dao/admin.go
Normal file
95
internal/dao/admin.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model/entity"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
// AdminDao 管理员数据访问对象
|
||||
type AdminDao struct {
|
||||
table string
|
||||
group string
|
||||
columns AdminColumns
|
||||
}
|
||||
|
||||
// AdminColumns 管理员表字段
|
||||
type AdminColumns struct {
|
||||
Id string
|
||||
Username string
|
||||
Password string
|
||||
RealName string
|
||||
Email string
|
||||
Phone string
|
||||
Avatar string
|
||||
RoleId string
|
||||
Status string
|
||||
LastLoginAt string
|
||||
LastLoginIp string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
// NewAdminDao 创建管理员DAO
|
||||
func NewAdminDao() *AdminDao {
|
||||
return &AdminDao{
|
||||
group: "default",
|
||||
table: "cms_admin",
|
||||
columns: AdminColumns{
|
||||
Id: "id",
|
||||
Username: "username",
|
||||
Password: "password",
|
||||
RealName: "real_name",
|
||||
Email: "email",
|
||||
Phone: "phone",
|
||||
Avatar: "avatar",
|
||||
RoleId: "role_id",
|
||||
Status: "status",
|
||||
LastLoginAt: "last_login_at",
|
||||
LastLoginIp: "last_login_ip",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Admin 管理员DAO实例
|
||||
var Admin = NewAdminDao()
|
||||
|
||||
// DB 获取数据库连接
|
||||
func (dao *AdminDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
// Ctx 创建上下文查询
|
||||
func (dao *AdminDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// GetByUsername 根据用户名获取管理员
|
||||
func (dao *AdminDao) GetByUsername(ctx context.Context, username string) (*entity.Admin, error) {
|
||||
var admin *entity.Admin
|
||||
err := dao.Ctx(ctx).Where(dao.columns.Username, username).Where(dao.columns.DeletedAt, 0).Scan(&admin)
|
||||
return admin, err
|
||||
}
|
||||
|
||||
// GetById 根据ID获取管理员
|
||||
func (dao *AdminDao) GetById(ctx context.Context, id uint64) (*entity.Admin, error) {
|
||||
var admin *entity.Admin
|
||||
err := dao.Ctx(ctx).Where(dao.columns.Id, id).Where(dao.columns.DeletedAt, 0).Scan(&admin)
|
||||
return admin, err
|
||||
}
|
||||
|
||||
// UpdateLastLogin 更新最后登录信息
|
||||
func (dao *AdminDao) UpdateLastLogin(ctx context.Context, id uint64, ip string) error {
|
||||
_, err := dao.Ctx(ctx).Data(g.Map{
|
||||
dao.columns.LastLoginAt: gtime.Now(),
|
||||
dao.columns.LastLoginIp: ip,
|
||||
dao.columns.UpdatedAt: gtime.Now(),
|
||||
}).Where(dao.columns.Id, id).Where(dao.columns.DeletedAt, 0).Update()
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user