112 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
		
		
			
		
	
	
			112 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
|  | // Code generated by goctl. DO NOT EDIT.
 | ||
|  | 
 | ||
|  | package model | ||
|  | 
 | ||
|  | import ( | ||
|  | 	"context" | ||
|  | 	"database/sql" | ||
|  | 	"fmt" | ||
|  | 	"strings" | ||
|  | 
 | ||
|  | 	"github.com/zeromicro/go-zero/core/stores/builder" | ||
|  | 	"github.com/zeromicro/go-zero/core/stores/sqlc" | ||
|  | 	"github.com/zeromicro/go-zero/core/stores/sqlx" | ||
|  | 	"github.com/zeromicro/go-zero/core/stringx" | ||
|  | ) | ||
|  | 
 | ||
|  | var ( | ||
|  | 	userRoleFieldNames          = builder.RawFieldNames(&UserRole{}) | ||
|  | 	userRoleRows                = strings.Join(userRoleFieldNames, ",") | ||
|  | 	userRoleRowsExpectAutoSet   = strings.Join(stringx.Remove(userRoleFieldNames, "`id`"), ",") | ||
|  | 	userRoleRowsWithPlaceHolder = strings.Join(stringx.Remove(userRoleFieldNames, "`id`"), "=?,") + "=?" | ||
|  | ) | ||
|  | 
 | ||
|  | type ( | ||
|  | 	userRoleModel interface { | ||
|  | 		Insert(ctx context.Context, data *UserRole) (sql.Result, error) | ||
|  | 		FindOne(ctx context.Context, id int64) (*UserRole, error) | ||
|  | 		FindOneByUid(ctx context.Context, uid string) (*UserRole, error) | ||
|  | 		Update(ctx context.Context, data *UserRole) error | ||
|  | 		Delete(ctx context.Context, id int64) error | ||
|  | 	} | ||
|  | 
 | ||
|  | 	defaultUserRoleModel struct { | ||
|  | 		conn  sqlx.SqlConn | ||
|  | 		table string | ||
|  | 	} | ||
|  | 
 | ||
|  | 	UserRole struct { | ||
|  | 		Id         int64  `db:"id"` // PK
 | ||
|  | 		Brand      string `db:"brand"` | ||
|  | 		Uid        string `db:"uid"` | ||
|  | 		RoleId     string `db:"role_id"` | ||
|  | 		Status     int64  `db:"status"`      // 狀態 1: 啟用, 2: 禁用
 | ||
|  | 		CreateTime int64  `db:"create_time"` // 創建時間
 | ||
|  | 		UpdateTime int64  `db:"update_time"` // 更新時間
 | ||
|  | 	} | ||
|  | ) | ||
|  | 
 | ||
|  | func newUserRoleModel(conn sqlx.SqlConn) *defaultUserRoleModel { | ||
|  | 	return &defaultUserRoleModel{ | ||
|  | 		conn:  conn, | ||
|  | 		table: "`user_role`", | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | func (m *defaultUserRoleModel) withSession(session sqlx.Session) *defaultUserRoleModel { | ||
|  | 	return &defaultUserRoleModel{ | ||
|  | 		conn:  sqlx.NewSqlConnFromSession(session), | ||
|  | 		table: "`user_role`", | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | func (m *defaultUserRoleModel) Delete(ctx context.Context, id int64) error { | ||
|  | 	query := fmt.Sprintf("delete from %s where `id` = ?", m.table) | ||
|  | 	_, err := m.conn.ExecCtx(ctx, query, id) | ||
|  | 	return err | ||
|  | } | ||
|  | 
 | ||
|  | func (m *defaultUserRoleModel) FindOne(ctx context.Context, id int64) (*UserRole, error) { | ||
|  | 	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userRoleRows, m.table) | ||
|  | 	var resp UserRole | ||
|  | 	err := m.conn.QueryRowCtx(ctx, &resp, query, id) | ||
|  | 	switch err { | ||
|  | 	case nil: | ||
|  | 		return &resp, nil | ||
|  | 	case sqlc.ErrNotFound: | ||
|  | 		return nil, ErrNotFound | ||
|  | 	default: | ||
|  | 		return nil, err | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | func (m *defaultUserRoleModel) FindOneByUid(ctx context.Context, uid string) (*UserRole, error) { | ||
|  | 	var resp UserRole | ||
|  | 	query := fmt.Sprintf("select %s from %s where `uid` = ? limit 1", userRoleRows, m.table) | ||
|  | 	err := m.conn.QueryRowCtx(ctx, &resp, query, uid) | ||
|  | 	switch err { | ||
|  | 	case nil: | ||
|  | 		return &resp, nil | ||
|  | 	case sqlc.ErrNotFound: | ||
|  | 		return nil, ErrNotFound | ||
|  | 	default: | ||
|  | 		return nil, err | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | func (m *defaultUserRoleModel) Insert(ctx context.Context, data *UserRole) (sql.Result, error) { | ||
|  | 	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, userRoleRowsExpectAutoSet) | ||
|  | 	ret, err := m.conn.ExecCtx(ctx, query, data.Brand, data.Uid, data.RoleId, data.Status, data.CreateTime, data.UpdateTime) | ||
|  | 	return ret, err | ||
|  | } | ||
|  | 
 | ||
|  | func (m *defaultUserRoleModel) Update(ctx context.Context, newData *UserRole) error { | ||
|  | 	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, userRoleRowsWithPlaceHolder) | ||
|  | 	_, err := m.conn.ExecCtx(ctx, query, newData.Brand, newData.Uid, newData.RoleId, newData.Status, newData.CreateTime, newData.UpdateTime, newData.Id) | ||
|  | 	return err | ||
|  | } | ||
|  | 
 | ||
|  | func (m *defaultUserRoleModel) tableName() string { | ||
|  | 	return m.table | ||
|  | } |