statistic_repair.go 6.3 KB

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