| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package model
- import (
- "context"
- "fmt"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- )
- var _ TServiceModel = (*customTServiceModel)(nil)
- type (
- // TServiceModel is an interface to be customized, add more methods here,
- // and implement the added methods in customTServiceModel.
- TServiceModel interface {
- tServiceModel
- FindPage(ctx context.Context, page, pageSize int64) ([]TService, int64, error)
- UpdateMountStatus(ctx context.Context, id int64, status string) error
- UpdateStatus(ctx context.Context, id int64, status string) error
- WithSession(session sqlx.Session) TServiceModel
- }
- customTServiceModel struct {
- *defaultTServiceModel
- }
- )
- // NewTServiceModel returns a model for the database table.
- func NewTServiceModel(conn sqlx.SqlConn) TServiceModel {
- return &customTServiceModel{
- defaultTServiceModel: newTServiceModel(conn),
- }
- }
- func (m *customTServiceModel) FindPage(ctx context.Context, page, pageSize int64) ([]TService, int64, error) {
- var total int64
- if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s", m.table)); err != nil {
- return nil, 0, err
- }
- if total == 0 {
- return nil, 0, nil
- }
- offset := (page - 1) * pageSize
- query := fmt.Sprintf("select %s from %s order by `id` limit ? offset ?", tServiceRows, m.table)
- var resp []TService
- if err := m.conn.QueryRowsCtx(ctx, &resp, query, pageSize, offset); err != nil {
- return nil, 0, err
- }
- return resp, total, nil
- }
- // UpdateMountStatus 只更新挂载状态(挂载链路专用,不做全字段 Update,避免并发覆盖其他字段)。
- // status 取值: mounted=已挂载 unmounted=未挂载 failed=挂载失败
- func (m *customTServiceModel) UpdateMountStatus(ctx context.Context, id int64, status string) error {
- query := fmt.Sprintf("update %s set `mount_status` = ? where `id` = ?", m.table)
- _, err := m.conn.ExecCtx(ctx, query, status, id)
- return err
- }
- // UpdateStatus 只更新服务启用状态(启用/停用)。挂载/卸载联动接口专用:
- // 不走全字段 Update,避免并发时把 brush(一键刷库)等配置覆盖成零值。
- func (m *customTServiceModel) UpdateStatus(ctx context.Context, id int64, status string) error {
- query := fmt.Sprintf("update %s set `status` = ? where `id` = ?", m.table)
- _, err := m.conn.ExecCtx(ctx, query, status, id)
- return err
- }
- func (m *customTServiceModel) WithSession(session sqlx.Session) TServiceModel {
- return NewTServiceModel(sqlx.NewSqlConnFromSession(session))
- }
|