local_objs.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.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. err := db.Where(filter).Delete(p).Error
  65. if err != nil {
  66. fields, _ := util.MarshalToString(filter)
  67. logger.Error("mysql",
  68. zap.String("sql", "delete from "+p.TableName()),
  69. zap.String("where", fields),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *TLocalObjs) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  75. cond, val, err := whereBuild(where)
  76. if err != nil {
  77. if err != nil {
  78. fields, _ := util.MarshalToString(values)
  79. logger.Error("mysql",
  80. zap.String("sql", "update "+p.TableName()),
  81. zap.String("fields", fields),
  82. zap.String("error", err.Error()))
  83. }
  84. return err
  85. }
  86. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  87. }
  88. func (p *TLocalObjs) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  89. cond, val, err := whereBuildAndOr(where, or)
  90. if err != nil {
  91. return 0, err
  92. }
  93. ret := int64(0)
  94. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  95. if err != nil {
  96. fields, _ := util.MarshalToString(where)
  97. logger.Error("mysql",
  98. zap.String("sql", "select count "+p.TableName()),
  99. zap.String("fields", fields),
  100. zap.String("error", err.Error()))
  101. }
  102. return ret, err
  103. }
  104. func (p *TLocalObjs) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TLocalObjs, err error) {
  105. cond, val, err := whereBuildAndOr(where, or)
  106. if err != nil {
  107. return list, err
  108. }
  109. if pageSize < 0 {
  110. result := db.Table(p.TableName()).Where(cond, val...).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. offset := (page - 1) * pageSize
  121. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  122. if result.Error != nil {
  123. wherefields, _ := util.MarshalToString(where)
  124. logger.Error("mysql",
  125. zap.String("sql", "select * from "+p.TableName()),
  126. zap.String("where", wherefields),
  127. zap.String("error", result.Error.Error()))
  128. }
  129. return list, result.Error
  130. }