oss_obj.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. )
  10. type TOssObj struct {
  11. ID int64 `gorm:"column:id" json:"id"`
  12. Bucket string `gorm:"column:bucket" json:"bucket"`
  13. Obj string `gorm:"column:obj" json:"obj"`
  14. }
  15. func (p *TOssObj) TableName() string {
  16. return "t_oss_obj"
  17. }
  18. func (p *TOssObj) Find(db *gorm.DB, where map[string]interface{}) error {
  19. err := db.Table(p.TableName()).Where(where).Find(p).Error
  20. if err != nil {
  21. fields, _ := util.MarshalToString(where)
  22. logger.Error("mysql",
  23. zap.String("sql", "select from "+p.TableName()),
  24. zap.String("fields", fields),
  25. zap.String("error", err.Error()))
  26. }
  27. return err
  28. }
  29. func (p *TOssObj) Last(db *gorm.DB) error {
  30. err := db.Table(p.TableName()).Last(p).Error
  31. if err != nil {
  32. logger.Error("mysql",
  33. zap.String("sql", "select last from "+p.TableName()),
  34. zap.String("fields", ""),
  35. zap.String("error", err.Error()))
  36. }
  37. return err
  38. }
  39. // Insert 插入一条记录
  40. func (p *TOssObj) Insert(db *gorm.DB) error {
  41. err := db.Create(p).Error
  42. if err != nil {
  43. fields, _ := util.MarshalToString(*p)
  44. logger.Error("mysql",
  45. zap.String("sql", "insert into "+p.TableName()),
  46. zap.String("fields", fields),
  47. zap.String("error", err.Error()))
  48. }
  49. return err
  50. }
  51. // Insert 插入多条记录
  52. func (p *TOssObj) InsertMulti(db *gorm.DB, values interface{}) error {
  53. err := db.Table(p.TableName()).Create(values).Error
  54. if err != nil {
  55. fields, _ := util.MarshalToString(*p)
  56. logger.Error("mysql",
  57. zap.String("sql", "insert into "+p.TableName()),
  58. zap.String("fields", fields),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *TOssObj) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  64. cond, val, err := whereBuild(where)
  65. if err != nil {
  66. if err != nil {
  67. fields, _ := util.MarshalToString(values)
  68. logger.Error("mysql",
  69. zap.String("sql", "update "+p.TableName()),
  70. zap.String("fields", fields),
  71. zap.String("error", err.Error()))
  72. }
  73. return err
  74. }
  75. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  76. }
  77. func (p *TOssObj) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  78. cond, val, err := whereBuildAndOr(where, or)
  79. if err != nil {
  80. return 0, err
  81. }
  82. ret := int64(0)
  83. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  84. if err != nil {
  85. fields, _ := util.MarshalToString(where)
  86. logger.Error("mysql",
  87. zap.String("sql", "select count "+p.TableName()),
  88. zap.String("fields", fields),
  89. zap.String("error", err.Error()))
  90. }
  91. return ret, err
  92. }
  93. func (p *TOssObj) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TOssObj, err error) {
  94. cond, val, err := whereBuildAndOr(where, or)
  95. if err != nil {
  96. return list, err
  97. }
  98. if pageSize < 0 {
  99. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  100. if result.Error != nil {
  101. wherefields, _ := util.MarshalToString(where)
  102. logger.Error("mysql",
  103. zap.String("sql", "select * from "+p.TableName()),
  104. zap.String("where", wherefields),
  105. zap.String("error", result.Error.Error()))
  106. }
  107. return list, result.Error
  108. }
  109. offset := (page - 1) * pageSize
  110. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  111. if result.Error != nil {
  112. wherefields, _ := util.MarshalToString(where)
  113. logger.Error("mysql",
  114. zap.String("sql", "select * from "+p.TableName()),
  115. zap.String("where", wherefields),
  116. zap.String("error", result.Error.Error()))
  117. }
  118. return list, result.Error
  119. }
  120. func (p *TOssObj) Delete(db *gorm.DB, filter map[string]interface{}) error {
  121. cond, val, err := whereBuild(filter)
  122. if err != nil {
  123. return err
  124. }
  125. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  126. }