t_msg.go 5.4 KB

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