i2bill_mkt_qr_model.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package model
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "git.i2edu.net/i2/go-zero/core/stores/sqlc"
  8. "git.i2edu.net/i2/go-zero/core/stores/sqlx"
  9. "git.i2edu.net/i2/go-zero/core/stringx"
  10. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/builderx"
  11. )
  12. var (
  13. i2billMktQrFieldNames = builderx.RawFieldNames(&I2billMktQr{})
  14. i2billMktQrRows = strings.Join(i2billMktQrFieldNames, ",")
  15. i2billMktQrRowsExpectAutoSet = strings.Join(stringx.Remove(i2billMktQrFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
  16. i2billMktQrRowsWithPlaceHolder = strings.Join(stringx.Remove(i2billMktQrFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
  17. )
  18. type (
  19. I2billMktQrModel interface {
  20. Insert(data I2billMktQr) (sql.Result, error)
  21. FindOne(id int64) (*I2billMktQr, error)
  22. Update(data I2billMktQr) error
  23. Delete(id int64) error
  24. }
  25. defaultI2billMktQrModel struct {
  26. conn sqlx.SqlConn
  27. table string
  28. }
  29. I2billMktQr struct {
  30. MkId string `db:"mk_id" json:"mk_id"`
  31. DelFlag int64 `db:"del_flag" json:"del_flag"`
  32. CreateTime time.Time `db:"create_time" json:"create_time"`
  33. Id int64 `db:"id" json:"id"` // 主键
  34. Qr string `db:"qr" json:"qr"` // 二维码url
  35. }
  36. )
  37. func NewI2billMktQrModel(conn sqlx.SqlConn) I2billMktQrModel {
  38. return &defaultI2billMktQrModel{
  39. conn: conn,
  40. table: "`i2bill_mkt_qr`",
  41. }
  42. }
  43. func (t *I2billMktQr) TableName() string {
  44. return "i2bill_mkt_qr"
  45. }
  46. func (m *defaultI2billMktQrModel) Insert(data I2billMktQr) (sql.Result, error) {
  47. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, i2billMktQrRowsExpectAutoSet)
  48. ret, err := m.conn.Exec(query, data.MkId, data.DelFlag, data.Qr)
  49. return ret, err
  50. }
  51. func (m *defaultI2billMktQrModel) FindOne(id int64) (*I2billMktQr, error) {
  52. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", i2billMktQrRows, m.table)
  53. var resp I2billMktQr
  54. err := m.conn.QueryRow(&resp, query, id)
  55. switch err {
  56. case nil:
  57. return &resp, nil
  58. case sqlc.ErrNotFound:
  59. return nil, ErrNotFound
  60. default:
  61. return nil, err
  62. }
  63. }
  64. func (m *defaultI2billMktQrModel) Update(data I2billMktQr) error {
  65. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, i2billMktQrRowsWithPlaceHolder)
  66. _, err := m.conn.Exec(query, data.MkId, data.DelFlag, data.Qr, data.Id)
  67. return err
  68. }
  69. func (m *defaultI2billMktQrModel) Delete(id int64) error {
  70. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  71. _, err := m.conn.Exec(query, id)
  72. return err
  73. }