guard/internal/model/role_model.go

85 lines
2.0 KiB
Go
Raw Normal View History

2024-08-19 01:39:05 +00:00
package model
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"time"
)
var _ RoleModel = (*customRoleModel)(nil)
type (
// RoleModel is an interface to be customized, add more methods here,
// and implement the added methods in customRoleModel.
RoleModel interface {
roleModel
IncrementID(ctx context.Context) (int64, error)
TransUpdateStatusByRoleID(
ctx context.Context,
session sqlx.Session,
status int64,
roleId string) error
TransUpdateDisplayNameByRoleID(
ctx context.Context,
session sqlx.Session,
roleId string,
displayName string) error
}
customRoleModel struct {
*defaultRoleModel
}
RoleFilter struct {
PageIndex *int64
PageSize *int64
RoleId *string
DisplayName *string
Status *int64
}
)
// NewRoleModel returns a model for the database table.
func NewRoleModel(conn sqlx.SqlConn) RoleModel {
return &customRoleModel{
defaultRoleModel: newRoleModel(conn),
}
}
func (m *customRoleModel) IncrementID(ctx context.Context) (int64, error) {
var maxID int64
query := fmt.Sprintf("select IFNULL(MAX(`id`), 0) from %s", m.table)
err := m.conn.QueryRowCtx(ctx, &maxID, query)
if err != nil {
return 0, err
}
// 返回最大 ID 加 1
return maxID + 1, nil
}
func (m *customRoleModel) TransUpdateDisplayNameByRoleID(
ctx context.Context,
session sqlx.Session,
displayName string,
roleId string) error {
query := fmt.Sprintf("update %s set `display_name` = ?, `update_time` = ? where `role_id` = ?", m.table)
// 執行更新操作
updateTime := time.Now().UTC().Unix()
_, err := session.ExecCtx(ctx, query, displayName, updateTime, roleId)
return err
}
func (m *customRoleModel) TransUpdateStatusByRoleID(
ctx context.Context,
session sqlx.Session,
status int64,
roleId string) error {
query := fmt.Sprintf("update %s set `status` = ?, `update_time` = ? where `role_id` = ?", m.table)
// 執行更新操作
updateTime := time.Now().UTC().Unix()
_, err := session.ExecCtx(ctx, query, status, updateTime, roleId)
return err
}