event_sign.go 6.0 KB

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