statistic_obj.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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" json:"id"`
  14. ObjType int32 `gorm:"column:obj_type" json:"obj_type"`
  15. Total int64 `gorm:"column:total" json:"total"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. table string
  19. }
  20. func (p *TStatisticObj) TableName() string {
  21. return p.table
  22. }
  23. func NewStatisticObj(database string) *TStatisticObj {
  24. return &TStatisticObj{table: fmt.Sprintf("%s.%s", database, "t_statistic_obj")}
  25. }
  26. func (p *TStatisticObj) SetTable(database string) {
  27. p.table = fmt.Sprintf("%s.%s", database, "t_statistic_obj")
  28. }
  29. func (p *TStatisticObj) CreateTable(db *gorm.DB) error {
  30. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  31. "`id` int(11) NOT NULL AUTO_INCREMENT," +
  32. "`obj_type` tinyint(1) NOT NULL COMMENT '1房屋 2 车位 3 车辆 4 住户'," +
  33. "`total` int(10) NOT NULL," +
  34. "`created_at` datetime NOT NULL," +
  35. " `updated_at` datetime DEFAULT NULL," +
  36. " PRIMARY KEY (`id`)," +
  37. " UNIQUE KEY `obj_type` (`obj_type`) USING BTREE" +
  38. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='统计用户,房屋,车位,车辆';"
  39. err := db.Exec(sql).Error
  40. if err != nil {
  41. logger.Error("mysql",
  42. zap.String("sql", "create table "+p.TableName()),
  43. zap.String("fields", ""),
  44. zap.String("error", err.Error()))
  45. }
  46. return err
  47. }
  48. func (p *TStatisticObj) Find(db *gorm.DB, where map[string]interface{}) error {
  49. err := db.Table(p.TableName()).Where(where).First(p).Error
  50. if err != nil {
  51. fields, _ := util.MarshalToString(where)
  52. logger.Error("mysql",
  53. zap.String("sql", "select from "+p.TableName()),
  54. zap.String("fields", fields),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. func (p *TStatisticObj) Last(db *gorm.DB) error {
  60. err := db.Table(p.TableName()).Last(p).Error
  61. if err != nil {
  62. logger.Error("mysql",
  63. zap.String("sql", "select last from "+p.TableName()),
  64. zap.String("fields", ""),
  65. zap.String("error", err.Error()))
  66. }
  67. return err
  68. }
  69. // Insert 插入一条记录
  70. func (p *TStatisticObj) Insert(db *gorm.DB) error {
  71. err := db.Table(p.TableName()).Create(p).Error
  72. if err != nil {
  73. fields, _ := util.MarshalToString(*p)
  74. logger.Error("mysql",
  75. zap.String("sql", "insert into "+p.TableName()),
  76. zap.String("fields", fields),
  77. zap.String("error", err.Error()))
  78. }
  79. return err
  80. }
  81. // Insert 插入多条记录
  82. func (p *TStatisticObj) InsertMulti(db *gorm.DB, values interface{}) error {
  83. err := db.Table(p.TableName()).Create(values).Error
  84. if err != nil {
  85. fields, _ := util.MarshalToString(*p)
  86. logger.Error("mysql",
  87. zap.String("sql", "insert into "+p.TableName()),
  88. zap.String("fields", fields),
  89. zap.String("error", err.Error()))
  90. }
  91. return err
  92. }
  93. func (p *TStatisticObj) Delete(db *gorm.DB, filter map[string]interface{}) error {
  94. cond, val, err := whereBuild(filter)
  95. if err != nil {
  96. return err
  97. }
  98. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  99. }
  100. func (p *TStatisticObj) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  101. cond, val, err := whereBuild(where)
  102. if err != nil {
  103. if err != nil {
  104. fields, _ := util.MarshalToString(values)
  105. logger.Error("mysql",
  106. zap.String("sql", "update "+p.TableName()),
  107. zap.String("fields", fields),
  108. zap.String("error", err.Error()))
  109. }
  110. return err
  111. }
  112. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  113. }
  114. func (p *TStatisticObj) UpdateAffected(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) (int64, error) {
  115. cond, val, err := whereBuild(where)
  116. if err != nil {
  117. if err != nil {
  118. fields, _ := util.MarshalToString(values)
  119. logger.Error("mysql",
  120. zap.String("sql", "update "+p.TableName()),
  121. zap.String("fields", fields),
  122. zap.String("error", err.Error()))
  123. }
  124. return 0, err
  125. }
  126. r := db.Table(p.TableName()).Where(cond, val...).Updates(values)
  127. return r.RowsAffected, r.Error
  128. }
  129. func (p *TStatisticObj) UpdateByModel(db *gorm.DB) error {
  130. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  131. if err != nil {
  132. fields, _ := util.MarshalToString(*p)
  133. logger.Error("mysql",
  134. zap.String("sql", "update "+p.TableName()),
  135. zap.String("fields", fields),
  136. zap.String("error", err.Error()))
  137. }
  138. return err
  139. }
  140. func (p *TStatisticObj) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  141. cond, val, err := whereBuildAndOr(where, or)
  142. if err != nil {
  143. return 0, err
  144. }
  145. ret := int64(0)
  146. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  147. if err != nil {
  148. fields, _ := util.MarshalToString(where)
  149. logger.Error("mysql",
  150. zap.String("sql", "select count "+p.TableName()),
  151. zap.String("fields", fields),
  152. zap.String("error", err.Error()))
  153. }
  154. return ret, err
  155. }
  156. func (p *TStatisticObj) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TStatisticObj, err error) {
  157. cond, val, err := whereBuildAndOr(where, or)
  158. if err != nil {
  159. return list, err
  160. }
  161. if pageSize < 0 {
  162. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  163. if result.Error != nil {
  164. wherefields, _ := util.MarshalToString(where)
  165. logger.Error("mysql",
  166. zap.String("sql", "select * from "+p.TableName()),
  167. zap.String("where", wherefields),
  168. zap.String("error", result.Error.Error()))
  169. }
  170. return list, result.Error
  171. }
  172. offset := (page - 1) * pageSize
  173. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  174. if result.Error != nil {
  175. wherefields, _ := util.MarshalToString(where)
  176. logger.Error("mysql",
  177. zap.String("sql", "select * from "+p.TableName()),
  178. zap.String("where", wherefields),
  179. zap.String("error", result.Error.Error()))
  180. }
  181. return list, result.Error
  182. }