t_vote.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 TVote struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. Title string `gorm:"column:title" json:"title"`
  15. Pics string `gorm:"column:pics" json:"pics"`
  16. Start int64 `gorm:"column:start" json:"start"`
  17. End int64 `gorm:"column:end" json:"end"`
  18. Topics string `gorm:"column:topics" json:"topics"`
  19. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  20. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  21. table string
  22. }
  23. func (p *TVote) TableName() string {
  24. return p.table
  25. }
  26. func NewVote(database string) *TVote {
  27. return &TVote{table: fmt.Sprintf("%s.%s", database, "t_vote")}
  28. }
  29. func (p *TVote) SetTable(database string) {
  30. p.table = fmt.Sprintf("%s.%s", database, "t_vote")
  31. }
  32. func (p *TVote) CreateTable(db *gorm.DB) error {
  33. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  34. "`id` bigint(11) NOT NULL AUTO_INCREMENT,"+
  35. "`title` varchar(255) COLLATE utf8mb4_bin NOT NULL,"+
  36. "`pics` text COLLATE utf8mb4_bin NOT NULL,"+
  37. "`start` bigint(11) NOT NULL,"+
  38. "`end` bigint(11) NOT NULL,"+
  39. "`topics` text COLLATE utf8mb4_bin NOT NULL COMMENT '题目列表',"+
  40. "`created_at` datetime NOT NULL,"+
  41. "`updated_at` datetime NOT NULL,"+
  42. "PRIMARY KEY (`id`)"+
  43. ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT;"
  44. err := db.Exec(sql).Error
  45. if err != nil {
  46. logger.Error("mysql",
  47. zap.String("sql", "create table "+p.TableName()),
  48. zap.String("fields", ""),
  49. zap.String("error", err.Error()))
  50. }
  51. return err
  52. }
  53. func (p *TVote) Find(db *gorm.DB, where map[string]interface{}) error {
  54. err := db.Table(p.TableName()).Where(where).First(p).Error
  55. if err != nil {
  56. fields, _ := util.MarshalToString(where)
  57. logger.Error("mysql",
  58. zap.String("sql", "select from "+p.TableName()),
  59. zap.String("fields", fields),
  60. zap.String("error", err.Error()))
  61. }
  62. return err
  63. }
  64. func (p *TVote) Last(db *gorm.DB) error {
  65. err := db.Table(p.TableName()).Last(p).Error
  66. if err != nil {
  67. logger.Error("mysql",
  68. zap.String("sql", "select last from "+p.TableName()),
  69. zap.String("fields", ""),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. // Insert 插入一条记录
  75. func (p *TVote) Insert(db *gorm.DB) error {
  76. err := db.Table(p.TableName()).Create(p).Error
  77. if err != nil {
  78. fields, _ := util.MarshalToString(*p)
  79. logger.Error("mysql",
  80. zap.String("sql", "insert into "+p.TableName()),
  81. zap.String("fields", fields),
  82. zap.String("error", err.Error()))
  83. }
  84. return err
  85. }
  86. // Insert 插入多条记录
  87. func (p *TVote) InsertMulti(db *gorm.DB, values interface{}) error {
  88. err := db.Table(p.TableName()).Create(values).Error
  89. if err != nil {
  90. fields, _ := util.MarshalToString(*p)
  91. logger.Error("mysql",
  92. zap.String("sql", "insert into "+p.TableName()),
  93. zap.String("fields", fields),
  94. zap.String("error", err.Error()))
  95. }
  96. return err
  97. }
  98. func (p *TVote) Delete(db *gorm.DB, filter map[string]interface{}) error {
  99. cond, val, err := whereBuild(filter)
  100. if err != nil {
  101. return err
  102. }
  103. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  104. }
  105. func (p *TVote) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  106. cond, val, err := whereBuild(where)
  107. if err != nil {
  108. if err != nil {
  109. fields, _ := util.MarshalToString(values)
  110. logger.Error("mysql",
  111. zap.String("sql", "update "+p.TableName()),
  112. zap.String("fields", fields),
  113. zap.String("error", err.Error()))
  114. }
  115. return err
  116. }
  117. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  118. }
  119. func (p *TVote) UpdateByModel(db *gorm.DB) error {
  120. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  121. if err != nil {
  122. fields, _ := util.MarshalToString(*p)
  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 err
  129. }
  130. func (p *TVote) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  131. cond, val, err := whereBuildAndOr(where, or)
  132. if err != nil {
  133. return 0, err
  134. }
  135. ret := int64(0)
  136. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  137. if err != nil {
  138. fields, _ := util.MarshalToString(where)
  139. logger.Error("mysql",
  140. zap.String("sql", "select count "+p.TableName()),
  141. zap.String("fields", fields),
  142. zap.String("error", err.Error()))
  143. }
  144. return ret, err
  145. }
  146. func (p *TVote) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TVote, err error) {
  147. cond, val, err := whereBuildAndOr(where, or)
  148. if err != nil {
  149. return list, err
  150. }
  151. if pageSize < 0 {
  152. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  153. if result.Error != nil {
  154. wherefields, _ := util.MarshalToString(where)
  155. logger.Error("mysql",
  156. zap.String("sql", "select * from "+p.TableName()),
  157. zap.String("where", wherefields),
  158. zap.String("error", result.Error.Error()))
  159. }
  160. return list, result.Error
  161. }
  162. offset := (page - 1) * pageSize
  163. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  164. if result.Error != nil {
  165. wherefields, _ := util.MarshalToString(where)
  166. logger.Error("mysql",
  167. zap.String("sql", "select * from "+p.TableName()),
  168. zap.String("where", wherefields),
  169. zap.String("error", result.Error.Error()))
  170. }
  171. return list, result.Error
  172. }