2024-08-19 01:39:05 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-19 16:59:38 +00:00
|
|
|
"errors"
|
2024-08-19 01:39:05 +00:00
|
|
|
"fmt"
|
2024-08-19 16:59:38 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
2024-08-19 01:39:05 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
2024-08-19 16:59:38 +00:00
|
|
|
"strings"
|
2024-08-19 01:39:05 +00:00
|
|
|
"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
|
2024-08-19 16:59:38 +00:00
|
|
|
Find(ctx context.Context) ([]*Role, error)
|
|
|
|
Count(ctx context.Context, role *Role) (int64, error)
|
|
|
|
SearchRoles(ctx context.Context, role *Role, pageIndex, PageSize int64) (result []Role, err error)
|
2024-08-19 01:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-08-19 16:59:38 +00:00
|
|
|
|
|
|
|
func (m *defaultRoleModel) Find(ctx context.Context) ([]*Role, error) {
|
|
|
|
query := fmt.Sprintf("select %s from %s", roleRows, m.table)
|
|
|
|
var resp []*Role
|
|
|
|
err := m.conn.QueryRowsCtx(ctx, &resp, query)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return resp, nil
|
|
|
|
case errors.Is(err, sqlc.ErrNotFound):
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *defaultRoleModel) Count(ctx context.Context, role *Role) (int64, error) {
|
|
|
|
var conditions []string
|
|
|
|
var args []any
|
|
|
|
|
|
|
|
// 構建條件
|
|
|
|
if role.RoleId != "" {
|
|
|
|
conditions = append(conditions, "`role_id` = ?")
|
|
|
|
args = append(args, role.RoleId)
|
|
|
|
}
|
|
|
|
if role.DisplayName != "" {
|
|
|
|
conditions = append(conditions, "`display_name` = ?")
|
|
|
|
args = append(args, role.DisplayName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if role.Status != 0 {
|
|
|
|
conditions = append(conditions, "`status` = ?")
|
|
|
|
args = append(args, role.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 構建基礎查詢語句
|
|
|
|
query := fmt.Sprintf("select COUNT(*) from %s", m.table)
|
|
|
|
|
|
|
|
// 如果有條件,添加 WHERE 子句
|
|
|
|
if len(conditions) > 0 {
|
|
|
|
query += " where " + strings.Join(conditions, " AND ")
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp int64
|
|
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, args...)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return resp, nil
|
|
|
|
case errors.Is(err, sqlc.ErrNotFound):
|
|
|
|
return 0, ErrNotFound
|
|
|
|
default:
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *customRoleModel) SearchRoles(ctx context.Context, role *Role, pageIndex, PageSize int64) (result []Role, err error) {
|
|
|
|
var conditions []string
|
|
|
|
var args []any
|
|
|
|
|
|
|
|
// 構建條件
|
|
|
|
if role.RoleId != "" {
|
|
|
|
conditions = append(conditions, "`role_id` = ?")
|
|
|
|
args = append(args, role.RoleId)
|
|
|
|
}
|
|
|
|
if role.DisplayName != "" {
|
|
|
|
conditions = append(conditions, "`display_name` = ?")
|
|
|
|
args = append(args, role.DisplayName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if role.Status != 0 {
|
|
|
|
conditions = append(conditions, "`status` = ?")
|
|
|
|
args = append(args, role.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 構建基礎查詢語句
|
|
|
|
query := fmt.Sprintf("select * from %s", m.table)
|
|
|
|
|
|
|
|
// 如果有條件,添加 WHERE 子句
|
|
|
|
if len(conditions) > 0 {
|
|
|
|
query += " where " + strings.Join(conditions, " AND ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 添加排序和分頁
|
|
|
|
query += " order by `id` desc limit ? offset ?"
|
|
|
|
args = append(args, PageSize, (pageIndex-1)*PageSize)
|
|
|
|
|
|
|
|
// 執行查詢
|
|
|
|
err = m.conn.QueryRowCtx(ctx, &result, query, args...)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return result, nil
|
|
|
|
case errors.Is(err, sqlc.ErrNotFound):
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|