i2bill_sys_attachment_model.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. i2billSysAttachmentFieldNames = builderx.RawFieldNames(&I2billSysAttachment{})
  14. i2billSysAttachmentRows = strings.Join(i2billSysAttachmentFieldNames, ",")
  15. i2billSysAttachmentRowsExpectAutoSet = strings.Join(stringx.Remove(i2billSysAttachmentFieldNames, "`create_time`", "`update_time`"), ",")
  16. i2billSysAttachmentRowsWithPlaceHolder = strings.Join(stringx.Remove(i2billSysAttachmentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
  17. )
  18. type (
  19. I2billSysAttachmentModel interface {
  20. Insert(data I2billSysAttachment) (sql.Result, error)
  21. FindOne(id string) (*I2billSysAttachment, error)
  22. Update(data I2billSysAttachment) error
  23. Delete(id string) error
  24. }
  25. defaultI2billSysAttachmentModel struct {
  26. conn sqlx.SqlConn
  27. table string
  28. }
  29. I2billSysAttachment struct {
  30. CreateTime time.Time `db:"create_time" json:"create_time"`
  31. DelFlag int64 `db:"del_flag" json:"del_flag"`
  32. Name string `db:"name" json:"name"`
  33. Size int64 `db:"size" json:"size"`
  34. Ext string `db:"ext" json:"ext"`
  35. Hash string `db:"hash" json:"hash"`
  36. Url string `db:"url" json:"url"`
  37. Id string `db:"id" json:"id"`
  38. CreateBy string `db:"create_by" json:"create_by"`
  39. }
  40. )
  41. func NewI2billSysAttachmentModel(conn sqlx.SqlConn) I2billSysAttachmentModel {
  42. return &defaultI2billSysAttachmentModel{
  43. conn: conn,
  44. table: "`i2bill_sys_attachment`",
  45. }
  46. }
  47. func (t *I2billSysAttachment) TableName() string {
  48. return "i2bill_sys_attachment"
  49. }
  50. func (m *defaultI2billSysAttachmentModel) Insert(data I2billSysAttachment) (sql.Result, error) {
  51. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, i2billSysAttachmentRowsExpectAutoSet)
  52. ret, err := m.conn.Exec(query, data.DelFlag, data.Name, data.Size, data.Ext, data.Hash, data.Url, data.Id, data.CreateBy)
  53. return ret, err
  54. }
  55. func (m *defaultI2billSysAttachmentModel) FindOne(id string) (*I2billSysAttachment, error) {
  56. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", i2billSysAttachmentRows, m.table)
  57. var resp I2billSysAttachment
  58. err := m.conn.QueryRow(&resp, query, id)
  59. switch err {
  60. case nil:
  61. return &resp, nil
  62. case sqlc.ErrNotFound:
  63. return nil, ErrNotFound
  64. default:
  65. return nil, err
  66. }
  67. }
  68. func (m *defaultI2billSysAttachmentModel) Update(data I2billSysAttachment) error {
  69. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, i2billSysAttachmentRowsWithPlaceHolder)
  70. _, err := m.conn.Exec(query, data.DelFlag, data.Name, data.Size, data.Ext, data.Hash, data.Url, data.CreateBy, data.Id)
  71. return err
  72. }
  73. func (m *defaultI2billSysAttachmentModel) Delete(id string) error {
  74. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  75. _, err := m.conn.Exec(query, id)
  76. return err
  77. }