cbcustomermodel_gen.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. FindOneByOpenKfidExternalUserid(ctx context.Context, openKfid string, externalUserid string) (*CbCustomer, error)
  25. Update(ctx context.Context, data *CbCustomer) error
  26. Delete(ctx context.Context, id int64) error
  27. }
  28. defaultCbCustomerModel struct {
  29. conn sqlx.SqlConn
  30. table string
  31. }
  32. CbCustomer struct {
  33. Id int64 `db:"id"`
  34. OpenKfid string `db:"open_kfid"` // 客服ID
  35. ExternalUserid string `db:"external_userid"` // 微信客户的external_userid
  36. Nickname string `db:"nickname"` // 昵称
  37. Avatar string `db:"avatar"` // 图片
  38. Gender int64 `db:"gender"` // 性别
  39. ServiceState int64 `db:"service_state"` // 会话状态:0未处理,1由智能助手接待,2待接入池排队中,3由人工接待,4已结束/未开始
  40. CreatedAt time.Time `db:"created_at"`
  41. UpdatedAt time.Time `db:"updated_at"`
  42. LastMsgTime int64 `db:"last_msg_time"` // 最后消息时间
  43. }
  44. )
  45. func newCbCustomerModel(conn sqlx.SqlConn) *defaultCbCustomerModel {
  46. return &defaultCbCustomerModel{
  47. conn: conn,
  48. table: "`cb_customer`",
  49. }
  50. }
  51. func (m *defaultCbCustomerModel) withSession(session sqlx.Session) *defaultCbCustomerModel {
  52. return &defaultCbCustomerModel{
  53. conn: sqlx.NewSqlConnFromSession(session),
  54. table: "`cb_customer`",
  55. }
  56. }
  57. func (m *defaultCbCustomerModel) Delete(ctx context.Context, id int64) error {
  58. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  59. _, err := m.conn.ExecCtx(ctx, query, id)
  60. return err
  61. }
  62. func (m *defaultCbCustomerModel) FindOne(ctx context.Context, id int64) (*CbCustomer, error) {
  63. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", cbCustomerRows, m.table)
  64. var resp CbCustomer
  65. err := m.conn.QueryRowCtx(ctx, &resp, query, id)
  66. switch err {
  67. case nil:
  68. return &resp, nil
  69. case sqlc.ErrNotFound:
  70. return nil, ErrNotFound
  71. default:
  72. return nil, err
  73. }
  74. }
  75. func (m *defaultCbCustomerModel) FindOneByOpenKfidExternalUserid(ctx context.Context, openKfid string, externalUserid string) (*CbCustomer, error) {
  76. var resp CbCustomer
  77. query := fmt.Sprintf("select %s from %s where `open_kfid` = ? and `external_userid` = ? limit 1", cbCustomerRows, m.table)
  78. err := m.conn.QueryRowCtx(ctx, &resp, query, openKfid, externalUserid)
  79. switch err {
  80. case nil:
  81. return &resp, nil
  82. case sqlc.ErrNotFound:
  83. return nil, ErrNotFound
  84. default:
  85. return nil, err
  86. }
  87. }
  88. func (m *defaultCbCustomerModel) Insert(ctx context.Context, data *CbCustomer) (sql.Result, error) {
  89. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, cbCustomerRowsExpectAutoSet)
  90. ret, err := m.conn.ExecCtx(ctx, query, data.OpenKfid, data.ExternalUserid, data.Nickname, data.Avatar, data.Gender, data.ServiceState, data.LastMsgTime)
  91. return ret, err
  92. }
  93. func (m *defaultCbCustomerModel) Update(ctx context.Context, newData *CbCustomer) error {
  94. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, cbCustomerRowsWithPlaceHolder)
  95. _, err := m.conn.ExecCtx(ctx, query, newData.OpenKfid, newData.ExternalUserid, newData.Nickname, newData.Avatar, newData.Gender, newData.ServiceState, newData.LastMsgTime, newData.Id)
  96. return err
  97. }
  98. func (m *defaultCbCustomerModel) tableName() string {
  99. return m.table
  100. }