user.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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-system-management/errors"
  6. "time"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "github.com/jinzhu/gorm"
  9. jsoniter "github.com/json-iterator/go"
  10. "go.uber.org/zap"
  11. )
  12. // 替换encoding/json包
  13. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  14. type SystemUser struct {
  15. Id int64 `json:"id" gorm:"primary_key"`
  16. Username string `json:"username"`
  17. Password string `json:"password"`
  18. CreatedAt time.Time `json:"created_at" gorm:"created_at"`
  19. UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
  20. }
  21. func (p *SystemUser) TableName() string {
  22. return "t_system_user"
  23. }
  24. func (p *SystemUser) Find(db *gorm.DB, where map[string]interface{}) error {
  25. err := db.Where(where).Find(p).Error
  26. if err != nil {
  27. fields, _ := json.MarshalToString(where)
  28. logger.Error("mysql",
  29. zap.String("sql", "select from "+p.TableName()),
  30. zap.String("fields", fields),
  31. zap.String("error", err.Error()))
  32. }
  33. return err
  34. }
  35. // Insert 插入一条记录
  36. func (p *SystemUser) Insert(db *gorm.DB) error {
  37. timeNow := time.Now()
  38. p.CreatedAt = timeNow
  39. p.UpdatedAt = timeNow
  40. err := db.Create(p).Error
  41. if err != nil {
  42. fields, _ := json.MarshalToString(*p)
  43. logger.Error("mysql",
  44. zap.String("sql", "insert into "+p.TableName()),
  45. zap.String("fields", fields),
  46. zap.String("error", err.Error()))
  47. }
  48. return err
  49. }
  50. func (p *SystemUser) Delete(db *gorm.DB, filter map[string]interface{}) error {
  51. err := db.Where(filter).Delete(p).Error
  52. if err != nil {
  53. fields, _ := json.MarshalToString(filter)
  54. logger.Error("mysql",
  55. zap.String("sql", "delete from "+p.TableName()),
  56. zap.String("fields", fields),
  57. zap.String("error", err.Error()))
  58. }
  59. return err
  60. }
  61. func (p *SystemUser) Save(db *gorm.DB) error {
  62. p.UpdatedAt = time.Now()
  63. err := db.Save(p).Error
  64. if err != nil {
  65. fields, _ := json.MarshalToString(*p)
  66. logger.Error("mysql",
  67. zap.String("sql", "save "+p.TableName()),
  68. zap.String("fields", fields),
  69. zap.String("error", err.Error()))
  70. }
  71. return err
  72. }
  73. func (p *SystemUser) Update(db *gorm.DB) error {
  74. p.UpdatedAt = time.Now()
  75. err := db.Model(p).Updates(p).Error
  76. if err != nil {
  77. fields, _ := json.MarshalToString(*p)
  78. logger.Error("mysql",
  79. zap.String("sql", "update "+p.TableName()),
  80. zap.String("fields", fields),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  86. func (p *SystemUser) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  87. if filed == nil {
  88. return errors.ParamsError
  89. }
  90. filed["updated_at"] = time.Now()
  91. err := db.Model(p).Updates(filed).Error
  92. if err != nil {
  93. fields, _ := json.MarshalToString(filed)
  94. logger.Error("mysql",
  95. zap.String("sql", "update "+p.TableName()),
  96. zap.String("fields", fields),
  97. zap.String("error", err.Error()))
  98. }
  99. return err
  100. }
  101. func (p *SystemUser) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  102. err := db.Where(filter).Find(out).Error
  103. if err != nil {
  104. fields, _ := json.MarshalToString(filter)
  105. logger.Error("mysql",
  106. zap.String("sql", "select from "+p.TableName()),
  107. zap.String("fields", fields),
  108. zap.String("error", err.Error()))
  109. }
  110. return err
  111. }
  112. func (p *SystemUser) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  113. cond, val, err := whereBuildAndOr(where, or)
  114. if err != nil {
  115. return 0, err
  116. }
  117. ret := int64(0)
  118. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  119. if err != nil {
  120. fields, _ := json.MarshalToString(where)
  121. logger.Error("mysql",
  122. zap.String("sql", "select count(1) from "+p.TableName()),
  123. zap.String("fields", fields),
  124. zap.String("error", err.Error()))
  125. }
  126. return ret, err
  127. }