event.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 TEvent struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. Pic string `gorm:"column:pic" json:"pic"`
  15. Content string `gorm:"column:content" json:"content"`
  16. Title string `gorm:"column:title" json:"title"`
  17. Start int64 `gorm:"column:start" json:"start"`
  18. End int64 `gorm:"column:end" json:"end"`
  19. SignUpEnd int64 `gorm:"column:sign_up_end" json:"sign_up_end"`
  20. PeopleLimit int64 `gorm:"column:people_limit" json:"people_limit"`
  21. Fee int64 `gorm:"column:fee" json:"fee"`
  22. Addr string `gorm:"column:addr" json:"addr"`
  23. PeopleCount int64 `gorm:"column:people_count" json:"people_count"`
  24. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  25. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  26. table string
  27. }
  28. func (p *TEvent) TableName() string {
  29. return p.table
  30. }
  31. func NewEvent(database string) *TEvent {
  32. return &TEvent{table: fmt.Sprintf("%s.%s", database, "t_event")}
  33. }
  34. func (p *TEvent) SetTable(database string) {
  35. p.table = fmt.Sprintf("%s.%s", database, "t_event")
  36. }
  37. func (p *TEvent) CreateTable(db *gorm.DB) error {
  38. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  39. " `id` int(11) NOT NULL AUTO_INCREMENT,"+
  40. " `pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '图片',"+
  41. "`content` text COLLATE utf8mb4_bin NOT NULL COMMENT '活动内容',"+
  42. "`title` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '标题',"+
  43. "`start` bigint(11) NOT NULL COMMENT '活动开始时间',"+
  44. "`end` bigint(11) NOT NULL COMMENT '活动结束时间',"+
  45. "`sign_up_end` bigint(11) NOT NULL COMMENT '报名截止时间',"+
  46. "`people_limit` int(10) NOT NULL COMMENT '报名人数限制',"+
  47. "`fee` int(10) NOT NULL COMMENT '报名费用',"+
  48. "`addr` varchar(512) COLLATE utf8mb4_bin NOT NULL COMMENT '活动地址',"+
  49. "`people_count` int(10) DEFAULT NULL COMMENT '已报名人数',"+
  50. "`created_at` datetime NOT NULL,"+
  51. " `updated_at` datetime NOT NULL,"+
  52. " PRIMARY KEY (`id`)"+
  53. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='社区活动';"
  54. err := db.Exec(sql).Error
  55. if err != nil {
  56. logger.Error("mysql",
  57. zap.String("sql", "create table "+p.TableName()),
  58. zap.String("fields", ""),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *TEvent) Find(db *gorm.DB, where map[string]interface{}) error {
  64. err := db.Table(p.TableName()).Where(where).First(p).Error
  65. if err != nil {
  66. fields, _ := util.MarshalToString(where)
  67. logger.Error("mysql",
  68. zap.String("sql", "select from "+p.TableName()),
  69. zap.String("fields", fields),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *TEvent) FindForUpdate(db *gorm.DB, where map[string]interface{}) error {
  75. err := db.Table(p.TableName()).Set("gorm:query_option", "FOR UPDATE").Where(where).First(p).Error
  76. if err != nil {
  77. fields, _ := util.MarshalToString(where)
  78. logger.Error("mysql",
  79. zap.String("sql", "select from "+p.TableName()),
  80. zap.String("fields", fields),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. func (p *TEvent) Last(db *gorm.DB) error {
  86. err := db.Table(p.TableName()).Last(p).Error
  87. if err != nil {
  88. logger.Error("mysql",
  89. zap.String("sql", "select last from "+p.TableName()),
  90. zap.String("fields", ""),
  91. zap.String("error", err.Error()))
  92. }
  93. return err
  94. }
  95. // Insert 插入一条记录
  96. func (p *TEvent) Insert(db *gorm.DB) error {
  97. err := db.Table(p.TableName()).Create(p).Error
  98. if err != nil {
  99. fields, _ := util.MarshalToString(*p)
  100. logger.Error("mysql",
  101. zap.String("sql", "insert into "+p.TableName()),
  102. zap.String("fields", fields),
  103. zap.String("error", err.Error()))
  104. }
  105. return err
  106. }
  107. // Insert 插入多条记录
  108. func (p *TEvent) InsertMulti(db *gorm.DB, values interface{}) error {
  109. err := db.Table(p.TableName()).Create(values).Error
  110. if err != nil {
  111. fields, _ := util.MarshalToString(*p)
  112. logger.Error("mysql",
  113. zap.String("sql", "insert into "+p.TableName()),
  114. zap.String("fields", fields),
  115. zap.String("error", err.Error()))
  116. }
  117. return err
  118. }
  119. func (p *TEvent) Delete(db *gorm.DB, filter map[string]interface{}) error {
  120. cond, val, err := whereBuild(filter)
  121. if err != nil {
  122. return err
  123. }
  124. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  125. }
  126. func (p *TEvent) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  127. cond, val, err := whereBuild(where)
  128. if err != nil {
  129. if err != nil {
  130. fields, _ := util.MarshalToString(values)
  131. logger.Error("mysql",
  132. zap.String("sql", "update "+p.TableName()),
  133. zap.String("fields", fields),
  134. zap.String("error", err.Error()))
  135. }
  136. return err
  137. }
  138. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  139. }
  140. func (p *TEvent) UpdateByModel(db *gorm.DB) error {
  141. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  142. if err != nil {
  143. fields, _ := util.MarshalToString(*p)
  144. logger.Error("mysql",
  145. zap.String("sql", "update "+p.TableName()),
  146. zap.String("fields", fields),
  147. zap.String("error", err.Error()))
  148. }
  149. return err
  150. }
  151. func (p *TEvent) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  152. cond, val, err := whereBuildAndOr(where, or)
  153. if err != nil {
  154. return 0, err
  155. }
  156. ret := int64(0)
  157. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  158. if err != nil {
  159. fields, _ := util.MarshalToString(where)
  160. logger.Error("mysql",
  161. zap.String("sql", "select count "+p.TableName()),
  162. zap.String("fields", fields),
  163. zap.String("error", err.Error()))
  164. }
  165. return ret, err
  166. }
  167. func (p *TEvent) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TEvent, err error) {
  168. cond, val, err := whereBuildAndOr(where, or)
  169. if err != nil {
  170. return list, err
  171. }
  172. if pageSize < 0 {
  173. result := db.Table(p.TableName()).Where(cond, val...).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. }
  183. offset := (page - 1) * pageSize
  184. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).Find(&list)
  185. if result.Error != nil {
  186. wherefields, _ := util.MarshalToString(where)
  187. logger.Error("mysql",
  188. zap.String("sql", "select * from "+p.TableName()),
  189. zap.String("where", wherefields),
  190. zap.String("error", result.Error.Error()))
  191. }
  192. return list, result.Error
  193. }