dcAppInfoModel_gen.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Code generated by goctl. DO NOT EDIT.
  2. package model
  3. import (
  4. "context"
  5. "database/sql"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/zeromicro/go-zero/core/stores/builder"
  11. "github.com/zeromicro/go-zero/core/stores/sqlc"
  12. "github.com/zeromicro/go-zero/core/stores/sqlx"
  13. "github.com/zeromicro/go-zero/core/stringx"
  14. )
  15. var (
  16. dcAppInfoFieldNames = builder.RawFieldNames(&DcAppInfo{})
  17. dcAppInfoRows = strings.Join(dcAppInfoFieldNames, ",")
  18. dcAppInfoRowsExpectAutoSet = strings.Join(stringx.Remove(dcAppInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
  19. dcAppInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(dcAppInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
  20. )
  21. type (
  22. dcAppInfoModel interface {
  23. Insert(ctx context.Context, data *DcAppInfo) (sql.Result, error)
  24. Find(ctx context.Context, offset, limit int64, order string) ([]DcAppInfo, int64, error)
  25. FindOne(ctx context.Context, id int64) (*DcAppInfo, error)
  26. FindOneByAppName(ctx context.Context, appName string) (*DcAppInfo, error)
  27. Update(ctx context.Context, data *DcAppInfo) error
  28. Delete(ctx context.Context, id int64) error
  29. }
  30. defaultDcAppInfoModel struct {
  31. conn sqlx.SqlConn
  32. table string
  33. }
  34. ManageProjectIds []int64
  35. DcAppInfo struct {
  36. Id int64 `db:"id"`
  37. AppName string `db:"app_name"` // 应用名称
  38. ProjectId int64 `db:"project_id"` // 限定服务的project_id
  39. ProjectIds ManageProjectIds `db:"project_ids"` // 可以操作的项目id列表
  40. Secret string `db:"secret"` // 数据签名的secret
  41. Status int64 `db:"status"` // 状态 0: 正常 1: 禁用
  42. ExpireAt time.Time `db:"expire_at"` // 失效时间
  43. MTime time.Time `db:"m_time"` // 更新时间
  44. }
  45. )
  46. func newDcAppInfoModel(conn sqlx.SqlConn) *defaultDcAppInfoModel {
  47. return &defaultDcAppInfoModel{
  48. conn: conn,
  49. table: "`dc_app_info`",
  50. }
  51. }
  52. func (m *defaultDcAppInfoModel) withSession(session sqlx.Session) *defaultDcAppInfoModel {
  53. return &defaultDcAppInfoModel{
  54. conn: sqlx.NewSqlConnFromSession(session),
  55. table: "`dc_app_info`",
  56. }
  57. }
  58. func (m *defaultDcAppInfoModel) Find(ctx context.Context, offset, limit int64, order string) ([]DcAppInfo, int64, error) {
  59. var total int64
  60. cq := fmt.Sprintf("select count(*) as total from %s", m.table)
  61. err := m.conn.QueryRowCtx(ctx, &total, cq)
  62. if err != nil || total == 0 {
  63. return nil, total, errors.New("not found appinfo")
  64. }
  65. query := fmt.Sprintf("select %s from %s order by %s limit %d,%d", dcAppInfoRows, m.table, order, offset, limit)
  66. var resp []DcAppInfo
  67. err = m.conn.QueryRowsCtx(ctx, &resp, query)
  68. switch err {
  69. case nil:
  70. return resp, total, nil
  71. case sqlc.ErrNotFound:
  72. return nil, total, ErrNotFound
  73. default:
  74. return nil, total, err
  75. }
  76. }
  77. func (m *defaultDcAppInfoModel) Delete(ctx context.Context, id int64) error {
  78. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  79. _, err := m.conn.ExecCtx(ctx, query, id)
  80. return err
  81. }
  82. func (m *defaultDcAppInfoModel) FindOne(ctx context.Context, id int64) (*DcAppInfo, error) {
  83. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", dcAppInfoRows, m.table)
  84. var resp DcAppInfo
  85. err := m.conn.QueryRowCtx(ctx, &resp, query, id)
  86. switch err {
  87. case nil:
  88. return &resp, nil
  89. case sqlc.ErrNotFound:
  90. return nil, ErrNotFound
  91. default:
  92. return nil, err
  93. }
  94. }
  95. func (m *defaultDcAppInfoModel) FindOneByAppName(ctx context.Context, appName string) (*DcAppInfo, error) {
  96. var resp DcAppInfo
  97. query := fmt.Sprintf("select %s from %s where `app_name` = ? limit 1", dcAppInfoRows, m.table)
  98. err := m.conn.QueryRowCtx(ctx, &resp, query, appName)
  99. switch err {
  100. case nil:
  101. return &resp, nil
  102. case sqlc.ErrNotFound:
  103. return nil, ErrNotFound
  104. default:
  105. return nil, err
  106. }
  107. }
  108. func (m *defaultDcAppInfoModel) Insert(ctx context.Context, data *DcAppInfo) (sql.Result, error) {
  109. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, dcAppInfoRowsExpectAutoSet)
  110. ret, err := m.conn.ExecCtx(ctx, query, data.AppName, data.ProjectId, data.ProjectIds, data.Secret, data.Status, data.ExpireAt, data.MTime)
  111. return ret, err
  112. }
  113. func (m *defaultDcAppInfoModel) Update(ctx context.Context, newData *DcAppInfo) error {
  114. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, dcAppInfoRowsWithPlaceHolder)
  115. _, err := m.conn.ExecCtx(ctx, query, newData.AppName, newData.ProjectId, newData.ProjectIds, newData.Secret, newData.Status, newData.ExpireAt, newData.MTime, newData.Id)
  116. return err
  117. }
  118. func (m *defaultDcAppInfoModel) tableName() string {
  119. return m.table
  120. }