staff_type.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "cp-organization-management/errors"
  6. "fmt"
  7. "time"
  8. "github.com/jaryhe/gopkgs/logger"
  9. "github.com/jaryhe/gopkgs/util"
  10. "github.com/jinzhu/gorm"
  11. "go.uber.org/zap"
  12. )
  13. type StaffType struct {
  14. Id int64 `json:"id" gorm:"primary_key"`
  15. StaffType string `json:"staff_type" gorm:"staff_type;commnet:'员工类型';not null;default:''"`
  16. CreatedAt time.Time `json:"created_at" gorm:"created_at"`
  17. UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
  18. table string `json:"table"`
  19. }
  20. func (p *StaffType) TableName() string {
  21. return p.table
  22. }
  23. func NewStaffType(database string) *StaffType {
  24. return &StaffType{table: fmt.Sprintf("%s.%s", database, "t_staff_type")}
  25. }
  26. func (p *StaffType) CreateTable(db *gorm.DB) error {
  27. err := db.CreateTable(p).Error
  28. if err != nil {
  29. logger.Error("mysql",
  30. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  31. zap.String("error", err.Error()))
  32. }
  33. return nil
  34. }
  35. func (p *StaffType) Find(db *gorm.DB, where map[string]interface{}) error {
  36. err := db.Table(p.TableName()).Where(where).Find(p).Error
  37. if err != nil {
  38. logger.Error("mysql",
  39. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  40. zap.String("error", err.Error()))
  41. }
  42. return err
  43. }
  44. func (p *StaffType) Last(db *gorm.DB) error {
  45. err := db.Table(p.TableName()).Last(p).Error
  46. if err != nil {
  47. logger.Error("mysql",
  48. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  49. zap.String("error", err.Error()))
  50. }
  51. return err
  52. }
  53. // Insert 插入一条记录
  54. func (p *StaffType) Insert(db *gorm.DB) error {
  55. timeNow := time.Now()
  56. p.CreatedAt = timeNow
  57. p.UpdatedAt = timeNow
  58. err := db.Create(p).Error
  59. if err != nil {
  60. fields, _ := util.MarshalToString(*p)
  61. logger.Error("mysql",
  62. zap.String("sql", "insert into "+p.TableName()),
  63. zap.String("fields", fields),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. func (p *StaffType) Delete(db *gorm.DB, filter map[string]interface{}) error {
  69. err := db.Where(filter).Delete(p).Error
  70. if err != nil {
  71. logger.Error("mysql",
  72. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. func (p *StaffType) Save(db *gorm.DB) error {
  78. p.UpdatedAt = time.Now()
  79. err := db.Save(p).Error
  80. if err != nil {
  81. logger.Error("mysql",
  82. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  83. zap.String("error", err.Error()))
  84. }
  85. return err
  86. }
  87. func (p *StaffType) Update(db *gorm.DB) error {
  88. p.UpdatedAt = time.Now()
  89. err := db.Model(p).Updates(p).Error
  90. if err != nil {
  91. logger.Error("mysql",
  92. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  93. zap.String("error", err.Error()))
  94. }
  95. return err
  96. }
  97. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  98. func (p *StaffType) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  99. if filed == nil {
  100. return errors.ParamsError
  101. }
  102. filed["updated_at"] = time.Now()
  103. err := db.Model(p).Updates(filed).Error
  104. if err != nil {
  105. logger.Error("mysql",
  106. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  107. zap.String("error", err.Error()))
  108. }
  109. return err
  110. }
  111. func (p *StaffType) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  112. err := db.Table(p.TableName()).Where(filter).Find(out).Error
  113. if err != nil {
  114. logger.Error("mysql",
  115. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  116. zap.String("error", err.Error()))
  117. }
  118. return err
  119. }
  120. func (p *StaffType) List(db *gorm.DB, limit, offset int, out interface{}) error {
  121. err := db.Table(p.TableName()).Limit(limit).Offset(offset).Find(out).Error
  122. if err != nil {
  123. logger.Error("mysql",
  124. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  125. zap.String("error", err.Error()))
  126. }
  127. return err
  128. }
  129. func (p *StaffType) Count(db *gorm.DB) (int64, error) {
  130. var count int64
  131. err := db.Table(p.TableName()).Count(&count).Error
  132. if err != nil {
  133. logger.Error("mysql",
  134. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  135. zap.String("error", err.Error()))
  136. }
  137. return count, err
  138. }