cbcustomermodel_gen.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Code generated by goctl. DO NOT EDIT.
  2. package model
  3. import (
  4. "context"
  5. "database/sql"
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/zeromicro/go-zero/core/stores/builder"
  10. "github.com/zeromicro/go-zero/core/stores/sqlc"
  11. "github.com/zeromicro/go-zero/core/stores/sqlx"
  12. "github.com/zeromicro/go-zero/core/stringx"
  13. )
  14. var (
  15. cbCustomerFieldNames = builder.RawFieldNames(&CbCustomer{})
  16. cbCustomerRows = strings.Join(cbCustomerFieldNames, ",")
  17. cbCustomerRowsExpectAutoSet = strings.Join(stringx.Remove(cbCustomerFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
  18. cbCustomerRowsWithPlaceHolder = strings.Join(stringx.Remove(cbCustomerFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
  19. )
  20. type (
  21. cbCustomerModel interface {
  22. Insert(ctx context.Context, data *CbCustomer) (sql.Result, error)
  23. FindOne(ctx context.Context, id int64) (*CbCustomer, error)
  24. Update(ctx context.Context, data *CbCustomer) error
  25. Delete(ctx context.Context, id int64) error
  26. }
  27. defaultCbCustomerModel struct {
  28. conn sqlx.SqlConn
  29. table string
  30. }
  31. CbCustomer struct {
  32. Id int64 `db:"id"`
  33. ExternalUserid string `db:"external_userid"` // 微信客户的external_userid
  34. Nickname string `db:"nickname"` // 昵称
  35. Avatar string `db:"avatar"` // 图片
  36. Gender int64 `db:"gender"` // 性别
  37. CreatedAt time.Time `db:"created_at"`
  38. UpdatedAt time.Time `db:"updated_at"`
  39. }
  40. )
  41. func newCbCustomerModel(conn sqlx.SqlConn) *defaultCbCustomerModel {
  42. return &defaultCbCustomerModel{
  43. conn: conn,
  44. table: "`cb_customer`",
  45. }
  46. }
  47. func (m *defaultCbCustomerModel) withSession(session sqlx.Session) *defaultCbCustomerModel {
  48. return &defaultCbCustomerModel{
  49. conn: sqlx.NewSqlConnFromSession(session),
  50. table: "`cb_customer`",
  51. }
  52. }
  53. func (m *defaultCbCustomerModel) Delete(ctx context.Context, id int64) error {
  54. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  55. _, err := m.conn.ExecCtx(ctx, query, id)
  56. return err
  57. }
  58. func (m *defaultCbCustomerModel) FindOne(ctx context.Context, id int64) (*CbCustomer, error) {
  59. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", cbCustomerRows, m.table)
  60. var resp CbCustomer
  61. err := m.conn.QueryRowCtx(ctx, &resp, query, id)
  62. switch err {
  63. case nil:
  64. return &resp, nil
  65. case sqlc.ErrNotFound:
  66. return nil, ErrNotFound
  67. default:
  68. return nil, err
  69. }
  70. }
  71. func (m *defaultCbCustomerModel) Insert(ctx context.Context, data *CbCustomer) (sql.Result, error) {
  72. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, cbCustomerRowsExpectAutoSet)
  73. ret, err := m.conn.ExecCtx(ctx, query, data.ExternalUserid, data.Nickname, data.Avatar, data.Gender)
  74. return ret, err
  75. }
  76. func (m *defaultCbCustomerModel) Update(ctx context.Context, data *CbCustomer) error {
  77. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, cbCustomerRowsWithPlaceHolder)
  78. _, err := m.conn.ExecCtx(ctx, query, data.ExternalUserid, data.Nickname, data.Avatar, data.Gender, data.Id)
  79. return err
  80. }
  81. func (m *defaultCbCustomerModel) tableName() string {
  82. return m.table
  83. }