// Code generated by goctl. DO NOT EDIT. package model import ( "context" "database/sql" "errors" "fmt" "strings" "time" "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 ( dcAppInfoFieldNames = builder.RawFieldNames(&DcAppInfo{}) dcAppInfoRows = strings.Join(dcAppInfoFieldNames, ",") dcAppInfoRowsExpectAutoSet = strings.Join(stringx.Remove(dcAppInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") dcAppInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(dcAppInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" ) type ( dcAppInfoModel interface { Insert(ctx context.Context, data *DcAppInfo) (sql.Result, error) Find(ctx context.Context, offset, limit int64, order string) ([]DcAppInfo, int64, error) FindOne(ctx context.Context, id int64) (*DcAppInfo, error) FindOneByAppName(ctx context.Context, appName string) (*DcAppInfo, error) Update(ctx context.Context, data *DcAppInfo) error Delete(ctx context.Context, id int64) error } defaultDcAppInfoModel struct { conn sqlx.SqlConn table string } ManageProjectIds []int64 DcAppInfo struct { Id int64 `db:"id"` AppName string `db:"app_name"` // 应用名称 ProjectId int64 `db:"project_id"` // 限定服务的project_id ProjectIds ManageProjectIds `db:"project_ids"` // 可以操作的项目id列表 Secret string `db:"secret"` // 数据签名的secret Status int64 `db:"status"` // 状态 0: 正常 1: 禁用 ExpireAt time.Time `db:"expire_at"` // 失效时间 MTime time.Time `db:"m_time"` // 更新时间 } ) func newDcAppInfoModel(conn sqlx.SqlConn) *defaultDcAppInfoModel { return &defaultDcAppInfoModel{ conn: conn, table: "`dc_app_info`", } } func (m *defaultDcAppInfoModel) withSession(session sqlx.Session) *defaultDcAppInfoModel { return &defaultDcAppInfoModel{ conn: sqlx.NewSqlConnFromSession(session), table: "`dc_app_info`", } } func (m *defaultDcAppInfoModel) Find(ctx context.Context, offset, limit int64, order string) ([]DcAppInfo, int64, error) { var total int64 cq := fmt.Sprintf("select count(*) as total from %s", m.table) err := m.conn.QueryRowCtx(ctx, &total, cq) if err != nil || total == 0 { return nil, total, errors.New("not found appinfo") } query := fmt.Sprintf("select %s from %s order by %s limit %d,%d", dcAppInfoRows, m.table, order, offset, limit) var resp []DcAppInfo err = m.conn.QueryRowsCtx(ctx, &resp, query) switch err { case nil: return resp, total, nil case sqlc.ErrNotFound: return nil, total, ErrNotFound default: return nil, total, err } } func (m *defaultDcAppInfoModel) 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 *defaultDcAppInfoModel) FindOne(ctx context.Context, id int64) (*DcAppInfo, error) { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", dcAppInfoRows, m.table) var resp DcAppInfo 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 *defaultDcAppInfoModel) FindOneByAppName(ctx context.Context, appName string) (*DcAppInfo, error) { var resp DcAppInfo query := fmt.Sprintf("select %s from %s where `app_name` = ? limit 1", dcAppInfoRows, m.table) err := m.conn.QueryRowCtx(ctx, &resp, query, appName) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultDcAppInfoModel) Insert(ctx context.Context, data *DcAppInfo) (sql.Result, error) { query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, dcAppInfoRowsExpectAutoSet) ret, err := m.conn.ExecCtx(ctx, query, data.AppName, data.ProjectId, data.ProjectIds, data.Secret, data.Status, data.ExpireAt, data.MTime) return ret, err } func (m *defaultDcAppInfoModel) Update(ctx context.Context, newData *DcAppInfo) error { query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, dcAppInfoRowsWithPlaceHolder) _, err := m.conn.ExecCtx(ctx, query, newData.AppName, newData.ProjectId, newData.ProjectIds, newData.Secret, newData.Status, newData.ExpireAt, newData.MTime, newData.Id) return err } func (m *defaultDcAppInfoModel) tableName() string { return m.table }