department.go 4.8 KB

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