local_objs.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 TLocalObjs struct {
  11. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  12. Name string `gorm:"column:name" json:"name"`
  13. Timestamp int64 `gorm:"column:timestamp" json:"timestamp"`
  14. Used int32 `gorm:"column:used" json:"used"`
  15. }
  16. func (p *TLocalObjs) TableName() string {
  17. return "t_local_objs"
  18. }
  19. func (p *TLocalObjs) Find(db *gorm.DB, where map[string]interface{}) error {
  20. err := db.Table(p.TableName()).Where(where).First(p).Error
  21. if err != nil {
  22. fields, _ := util.MarshalToString(where)
  23. logger.Error("mysql",
  24. zap.String("sql", "select from "+p.TableName()),
  25. zap.String("fields", fields),
  26. zap.String("error", err.Error()))
  27. }
  28. return err
  29. }
  30. func (p *TLocalObjs) Last(db *gorm.DB) error {
  31. err := db.Table(p.TableName()).Last(p).Error
  32. if err != nil {
  33. logger.Error("mysql",
  34. zap.String("sql", "select last from "+p.TableName()),
  35. zap.String("fields", ""),
  36. zap.String("error", err.Error()))
  37. }
  38. return err
  39. }
  40. // Insert 插入一条记录
  41. func (p *TLocalObjs) Insert(db *gorm.DB) error {
  42. err := db.Table(p.TableName()).Create(p).Error
  43. if err != nil {
  44. fields, _ := util.MarshalToString(*p)
  45. logger.Error("mysql",
  46. zap.String("sql", "insert into "+p.TableName()),
  47. zap.String("fields", fields),
  48. zap.String("error", err.Error()))
  49. }
  50. return err
  51. }
  52. func (p *TLocalObjs) 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 *TLocalObjs) Delete(db *gorm.DB, filter map[string]interface{}) error {
  64. cond, val, err := whereBuild(filter)
  65. if err != nil {
  66. return err
  67. }
  68. err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  69. if err != nil {
  70. fields, _ := util.MarshalToString(filter)
  71. logger.Error("mysql",
  72. zap.String("sql", "delete from "+p.TableName()),
  73. zap.String("where", fields),
  74. zap.String("error", err.Error()))
  75. }
  76. return err
  77. }
  78. func (p *TLocalObjs) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  79. cond, val, err := whereBuild(where)
  80. if err != nil {
  81. if err != nil {
  82. fields, _ := util.MarshalToString(values)
  83. logger.Error("mysql",
  84. zap.String("sql", "update "+p.TableName()),
  85. zap.String("fields", fields),
  86. zap.String("error", err.Error()))
  87. }
  88. return err
  89. }
  90. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  91. }
  92. func (p *TLocalObjs) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  93. cond, val, err := whereBuildAndOr(where, or)
  94. if err != nil {
  95. return 0, err
  96. }
  97. ret := int64(0)
  98. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  99. if err != nil {
  100. fields, _ := util.MarshalToString(where)
  101. logger.Error("mysql",
  102. zap.String("sql", "select count "+p.TableName()),
  103. zap.String("fields", fields),
  104. zap.String("error", err.Error()))
  105. }
  106. return ret, err
  107. }
  108. func (p *TLocalObjs) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TLocalObjs, err error) {
  109. cond, val, err := whereBuildAndOr(where, or)
  110. if err != nil {
  111. return list, err
  112. }
  113. if pageSize < 0 {
  114. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  115. if result.Error != nil {
  116. wherefields, _ := util.MarshalToString(where)
  117. logger.Error("mysql",
  118. zap.String("sql", "select * from "+p.TableName()),
  119. zap.String("where", wherefields),
  120. zap.String("error", result.Error.Error()))
  121. }
  122. return list, result.Error
  123. }
  124. offset := (page - 1) * pageSize
  125. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  126. if result.Error != nil {
  127. wherefields, _ := util.MarshalToString(where)
  128. logger.Error("mysql",
  129. zap.String("sql", "select * from "+p.TableName()),
  130. zap.String("where", wherefields),
  131. zap.String("error", result.Error.Error()))
  132. }
  133. return list, result.Error
  134. }