excel_export_log_model.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package model
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strings"
  6. "git.i2edu.net/i2/go-zero/core/stores/cache"
  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. excelExportLogFieldNames = builderx.RawFieldNames(&ExcelExportLog{})
  14. excelExportLogRows = strings.Join(excelExportLogFieldNames, ",")
  15. excelExportLogRowsExpectAutoSet = strings.Join(stringx.Remove(excelExportLogFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
  16. excelExportLogRowsWithPlaceHolder = strings.Join(stringx.Remove(excelExportLogFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
  17. cacheExcelExportLogIdPrefix = "cache:excelExportLog:id:"
  18. )
  19. type (
  20. ExcelExportLogModel interface {
  21. Insert(data ExcelExportLog) (sql.Result, error)
  22. FindOne(id int64) (*ExcelExportLog, error)
  23. Update(data ExcelExportLog) error
  24. Delete(id int64) error
  25. }
  26. defaultExcelExportLogModel struct {
  27. sqlc.CachedConn
  28. table string
  29. }
  30. ExcelExportLog struct {
  31. Cond string `db:"cond"`
  32. CreateTime sql.NullTime `db:"create_time"`
  33. LastUpdateTime sql.NullTime `db:"last_update_time"`
  34. CreateBy sql.NullString `db:"create_by"`
  35. LastUpdateBy sql.NullString `db:"last_update_by"`
  36. DelFlag int64 `db:"del_flag"`
  37. Id int64 `db:"id"`
  38. Module sql.NullString `db:"module"`
  39. }
  40. )
  41. func NewExcelExportLogModel(conn sqlx.SqlConn, c cache.CacheConf) ExcelExportLogModel {
  42. return &defaultExcelExportLogModel{
  43. CachedConn: sqlc.NewConn(conn, c),
  44. table: "`excel_export_log`",
  45. }
  46. }
  47. func (m *defaultExcelExportLogModel) Insert(data ExcelExportLog) (sql.Result, error) {
  48. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, excelExportLogRowsExpectAutoSet)
  49. ret, err := m.ExecNoCache(query, data.Cond, data.LastUpdateTime, data.CreateBy, data.LastUpdateBy, data.DelFlag, data.Module)
  50. return ret, err
  51. }
  52. func (m *defaultExcelExportLogModel) FindOne(id int64) (*ExcelExportLog, error) {
  53. excelExportLogIdKey := fmt.Sprintf("%s%v", cacheExcelExportLogIdPrefix, id)
  54. var resp ExcelExportLog
  55. err := m.QueryRow(&resp, excelExportLogIdKey, func(conn sqlx.SqlConn, v interface{}) error {
  56. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", excelExportLogRows, m.table)
  57. return conn.QueryRow(v, query, id)
  58. })
  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 *defaultExcelExportLogModel) Update(data ExcelExportLog) error {
  69. excelExportLogIdKey := fmt.Sprintf("%s%v", cacheExcelExportLogIdPrefix, data.Id)
  70. _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  71. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, excelExportLogRowsWithPlaceHolder)
  72. return conn.Exec(query, data.Cond, data.LastUpdateTime, data.CreateBy, data.LastUpdateBy, data.DelFlag, data.Module, data.Id)
  73. }, excelExportLogIdKey)
  74. return err
  75. }
  76. func (m *defaultExcelExportLogModel) Delete(id int64) error {
  77. excelExportLogIdKey := fmt.Sprintf("%s%v", cacheExcelExportLogIdPrefix, id)
  78. _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  79. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  80. return conn.Exec(query, id)
  81. }, excelExportLogIdKey)
  82. return err
  83. }
  84. func (m *defaultExcelExportLogModel) formatPrimary(primary interface{}) string {
  85. return fmt.Sprintf("%s%v", cacheExcelExportLogIdPrefix, primary)
  86. }
  87. func (m *defaultExcelExportLogModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
  88. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", excelExportLogRows, m.table)
  89. return conn.QueryRow(v, query, primary)
  90. }