obj_statistic.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type TStatisticObj struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. ObjType int32 `gorm:"column:obj_type" json:"obj_type"`
  15. Total int64 `gorm:"column:total" json:"total"`
  16. Cid int64 `gorm:"column:cid" json:"cid"`
  17. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  18. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  19. }
  20. func (p *TStatisticObj) TableName() string {
  21. return "t_statistic_obj"
  22. }
  23. func (p *TStatisticObj) Find(db *gorm.DB, where map[string]interface{}) error {
  24. err := db.Table(p.TableName()).Where(where).Find(p).Error
  25. if err != nil {
  26. fields, _ := util.MarshalToString(where)
  27. logger.Error("mysql",
  28. zap.String("sql", "select from "+p.TableName()),
  29. zap.String("fields", fields),
  30. zap.String("error", err.Error()))
  31. }
  32. return err
  33. }
  34. func (p *TStatisticObj) Last(db *gorm.DB) error {
  35. err := db.Table(p.TableName()).Last(p).Error
  36. if err != nil {
  37. logger.Error("mysql",
  38. zap.String("sql", "select last from "+p.TableName()),
  39. zap.String("fields", ""),
  40. zap.String("error", err.Error()))
  41. }
  42. return err
  43. }
  44. // Insert 插入一条记录
  45. func (p *TStatisticObj) Insert(db *gorm.DB) error {
  46. err := db.Create(p).Error
  47. if err != nil {
  48. fields, _ := util.MarshalToString(*p)
  49. logger.Error("mysql",
  50. zap.String("sql", "insert into "+p.TableName()),
  51. zap.String("fields", fields),
  52. zap.String("error", err.Error()))
  53. }
  54. return err
  55. }
  56. func (p *TStatisticObj) Delete(db *gorm.DB, filter map[string]interface{}) error {
  57. err := db.Where(filter).Delete(p).Error
  58. if err != nil {
  59. fields, _ := util.MarshalToString(filter)
  60. logger.Error("mysql",
  61. zap.String("sql", "delete from "+p.TableName()),
  62. zap.String("where", fields),
  63. zap.String("error", err.Error()))
  64. }
  65. return err
  66. }
  67. func (p *TStatisticObj) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  68. cond, val, err := whereBuild(where)
  69. if err != nil {
  70. if err != nil {
  71. fields, _ := util.MarshalToString(values)
  72. logger.Error("mysql",
  73. zap.String("sql", "update "+p.TableName()),
  74. zap.String("fields", fields),
  75. zap.String("error", err.Error()))
  76. }
  77. return err
  78. }
  79. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  80. }
  81. func (p *TStatisticObj) UpdateAffected(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) (int64, error) {
  82. cond, val, err := whereBuild(where)
  83. if err != nil {
  84. if err != nil {
  85. fields, _ := util.MarshalToString(values)
  86. logger.Error("mysql",
  87. zap.String("sql", "update "+p.TableName()),
  88. zap.String("fields", fields),
  89. zap.String("error", err.Error()))
  90. }
  91. return 0, err
  92. }
  93. r := db.Table(p.TableName()).Where(cond, val...).Updates(values)
  94. return r.RowsAffected, r.Error
  95. }
  96. func (p *TStatisticObj) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  97. cond, val, err := whereBuildAndOr(where, or)
  98. if err != nil {
  99. return 0, err
  100. }
  101. ret := int64(0)
  102. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  103. if err != nil {
  104. fields, _ := util.MarshalToString(where)
  105. logger.Error("mysql",
  106. zap.String("sql", "select count "+p.TableName()),
  107. zap.String("fields", fields),
  108. zap.String("error", err.Error()))
  109. }
  110. return ret, err
  111. }
  112. func (p *TStatisticObj) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TStatisticObj, err error) {
  113. cond, val, err := whereBuildAndOr(where, or)
  114. if err != nil {
  115. return list, err
  116. }
  117. if pageSize < 0 {
  118. result := db.Table(p.TableName()).Where(cond, val...).Order("id desc").Find(&list)
  119. if result.Error != nil {
  120. wherefields, _ := util.MarshalToString(where)
  121. logger.Error("mysql",
  122. zap.String("sql", "select * from "+p.TableName()),
  123. zap.String("where", wherefields),
  124. zap.String("error", result.Error.Error()))
  125. }
  126. return list, result.Error
  127. }
  128. offset := (page - 1) * pageSize
  129. result := db.Table(p.TableName()).Where(cond, val...).Order("id desc").Limit(pageSize).Offset(offset).Find(&list)
  130. if result.Error != nil {
  131. wherefields, _ := util.MarshalToString(where)
  132. logger.Error("mysql",
  133. zap.String("sql", "select * from "+p.TableName()),
  134. zap.String("where", wherefields),
  135. zap.String("error", result.Error.Error()))
  136. }
  137. return list, result.Error
  138. }
  139. func (p *TStatisticObj) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TStatisticObj, err error) {
  140. cond, val, err := whereBuildAndOr(where, or)
  141. if err != nil {
  142. return list, err
  143. }
  144. offset := (page - 1) * pageSize
  145. sql := fmt.Sprintf("select * from %s t1 join (select id from %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), pageSize, offset)
  146. if cond != "" {
  147. sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), cond, pageSize, offset)
  148. }
  149. result := db.Raw(sql, val...).Scan(&list)
  150. if result.Error != nil {
  151. wherefields, _ := util.MarshalToString(where)
  152. logger.Error("mysql",
  153. zap.String("sql", "select * from "+p.TableName()),
  154. zap.String("where", wherefields),
  155. zap.String("error", result.Error.Error()))
  156. }
  157. return list, result.Error
  158. }