group.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TGroup struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. PermissionCodes string `gorm:"column:permission_codes" json:"permission_codes"`
  14. GroupName string `gorm:"column:group_name" json:"group_name"`
  15. GroupDesc string `gorm:"column:group_desc" json:"group_desc"`
  16. Cid int64 `gorm:"column:cid" json:"cid"`
  17. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  18. IsSuper int64 `gorm:"column:is_super" json:"is_super"`
  19. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  20. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  21. }
  22. func (p *TGroup) TableName() string {
  23. return "t_group"
  24. }
  25. func (p *TGroup) Find(db *gorm.DB, where map[string]interface{}) error {
  26. err := db.Table(p.TableName()).Where(where).Find(p).Error
  27. if err != nil {
  28. fields, _ := util.MarshalToString(where)
  29. logger.Error("mysql",
  30. zap.String("sql", "select from "+p.TableName()),
  31. zap.String("fields", fields),
  32. zap.String("error", err.Error()))
  33. }
  34. return err
  35. }
  36. func (p *TGroup) Last(db *gorm.DB) error {
  37. err := db.Table(p.TableName()).Last(p).Error
  38. if err != nil {
  39. logger.Error("mysql",
  40. zap.String("sql", "select last from "+p.TableName()),
  41. zap.String("fields", ""),
  42. zap.String("error", err.Error()))
  43. }
  44. return err
  45. }
  46. // Insert 插入一条记录
  47. func (p *TGroup) Insert(db *gorm.DB) error {
  48. err := db.Create(p).Error
  49. if err != nil {
  50. fields, _ := util.MarshalToString(*p)
  51. logger.Error("mysql",
  52. zap.String("sql", "insert into "+p.TableName()),
  53. zap.String("fields", fields),
  54. zap.String("error", err.Error()))
  55. }
  56. return err
  57. }
  58. func (p *TGroup) Delete(db *gorm.DB, filter map[string]interface{}) error {
  59. cond, val, err := whereBuild(filter)
  60. if err != nil {
  61. return err
  62. }
  63. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  64. }
  65. func (p *TGroup) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  66. cond, val, err := whereBuild(where)
  67. if err != nil {
  68. if err != nil {
  69. fields, _ := util.MarshalToString(values)
  70. logger.Error("mysql",
  71. zap.String("sql", "update "+p.TableName()),
  72. zap.String("fields", fields),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  78. }
  79. func (p *TGroup) UpdateByModel(db *gorm.DB) error {
  80. err := db.Model(p).Updates(p).Error
  81. if err != nil {
  82. fields, _ := util.MarshalToString(*p)
  83. logger.Error("mysql",
  84. zap.String("sql", "update "+p.TableName()),
  85. zap.String("fields", fields),
  86. zap.String("error", err.Error()))
  87. }
  88. return err
  89. }
  90. func (p *TGroup) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  91. cond, val, err := whereBuildAndOr(where, or)
  92. if err != nil {
  93. return 0, err
  94. }
  95. ret := int64(0)
  96. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  97. if err != nil {
  98. fields, _ := util.MarshalToString(where)
  99. logger.Error("mysql",
  100. zap.String("sql", "select count "+p.TableName()),
  101. zap.String("fields", fields),
  102. zap.String("error", err.Error()))
  103. }
  104. return ret, err
  105. }
  106. func (p *TGroup) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TGroup, err error) {
  107. cond, val, err := whereBuildAndOr(where, or)
  108. if err != nil {
  109. return list, err
  110. }
  111. if pageSize < 0 {
  112. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  113. if result.Error != nil {
  114. wherefields, _ := util.MarshalToString(where)
  115. logger.Error("mysql",
  116. zap.String("sql", "select * from "+p.TableName()),
  117. zap.String("where", wherefields),
  118. zap.String("error", result.Error.Error()))
  119. }
  120. return list, result.Error
  121. }
  122. offset := (page - 1) * pageSize
  123. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  124. if result.Error != nil {
  125. wherefields, _ := util.MarshalToString(where)
  126. logger.Error("mysql",
  127. zap.String("sql", "select * from "+p.TableName()),
  128. zap.String("where", wherefields),
  129. zap.String("error", result.Error.Error()))
  130. }
  131. return list, result.Error
  132. }
  133. func (p *TGroup) ListColums(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, columns []string) (list []TGroup, err error) {
  134. cond, val, err := whereBuildAndOr(where, or)
  135. if err != nil {
  136. return list, err
  137. }
  138. result := db.Table(p.TableName()).Where(cond, val...).Select(columns).Find(&list)
  139. if result.Error != nil {
  140. wherefields, _ := util.MarshalToString(where)
  141. logger.Error("mysql",
  142. zap.String("sql", "select * from "+p.TableName()),
  143. zap.String("where", wherefields),
  144. zap.String("error", result.Error.Error()))
  145. }
  146. return list, result.Error
  147. }