neighbor_class.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 TNeighborClass struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. ClassName string `gorm:"column:class_name" json:"class_name"`
  15. ClassPic string `gorm:"column:class_pic" json:"class_pic"`
  16. Enable int32 `gorm:"column:enable" json:"enable"`
  17. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  18. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  19. ArticleCount int64 `gorm:"column:article_count" json:"article_count"`
  20. table string
  21. }
  22. func (p *TNeighborClass) TableName() string {
  23. return p.table
  24. }
  25. func NewNeighborClass(database string) *TNeighborClass {
  26. return &TNeighborClass{table: fmt.Sprintf("%s.%s", database, "t_neighbor_class")}
  27. }
  28. func (p *TNeighborClass) SetTable(database string) {
  29. p.table = fmt.Sprintf("%s.%s", database, "t_neighbor_class")
  30. }
  31. func (p *TNeighborClass) CreateTable(db *gorm.DB) error {
  32. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  33. "`id` bigint(11) NOT NULL AUTO_INCREMENT,"+
  34. "`class_name` varchar(128) COLLATE utf8mb4_bin NOT NULL,"+
  35. "`class_pic` text COLLATE utf8mb4_bin,"+
  36. "`enable` tinyint(4) NOT NULL COMMENT '1 启用 2 停用',"+
  37. "`created_at` datetime NOT NULL,"+
  38. " `updated_at` datetime NOT NULL,"+
  39. " `article_count` bigint(11) NOT NULL,"+
  40. " PRIMARY KEY (`id`),"+
  41. " UNIQUE KEY `class_name` (`class_name`) USING BTREE"+
  42. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  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 *TNeighborClass) 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 *TNeighborClass) 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 *TNeighborClass) 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 *TNeighborClass) 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 *TNeighborClass) 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 *TNeighborClass) 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 *TNeighborClass) UpdateByModel(db *gorm.DB) error {
  119. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  120. if err != nil {
  121. fields, _ := util.MarshalToString(*p)
  122. logger.Error("mysql",
  123. zap.String("sql", "update "+p.TableName()),
  124. zap.String("fields", fields),
  125. zap.String("error", err.Error()))
  126. }
  127. return err
  128. }
  129. func (p *TNeighborClass) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  130. cond, val, err := whereBuildAndOr(where, or)
  131. if err != nil {
  132. return 0, err
  133. }
  134. ret := int64(0)
  135. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  136. if err != nil {
  137. fields, _ := util.MarshalToString(where)
  138. logger.Error("mysql",
  139. zap.String("sql", "select count "+p.TableName()),
  140. zap.String("fields", fields),
  141. zap.String("error", err.Error()))
  142. }
  143. return ret, err
  144. }
  145. func (p *TNeighborClass) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TNeighborClass, err error) {
  146. cond, val, err := whereBuildAndOr(where, or)
  147. if err != nil {
  148. return list, err
  149. }
  150. if pageSize < 0 {
  151. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  152. if result.Error != nil {
  153. wherefields, _ := util.MarshalToString(where)
  154. logger.Error("mysql",
  155. zap.String("sql", "select * from "+p.TableName()),
  156. zap.String("where", wherefields),
  157. zap.String("error", result.Error.Error()))
  158. }
  159. return list, result.Error
  160. }
  161. offset := (page - 1) * pageSize
  162. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  163. if result.Error != nil {
  164. wherefields, _ := util.MarshalToString(where)
  165. logger.Error("mysql",
  166. zap.String("sql", "select * from "+p.TableName()),
  167. zap.String("where", wherefields),
  168. zap.String("error", result.Error.Error()))
  169. }
  170. return list, result.Error
  171. }