origanization.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "go.uber.org/zap"
  10. )
  11. type Organization struct {
  12. Id int64 `json:"id" gorm:"primary_key"`
  13. OrganizationName string `json:"organization_name"`
  14. OrganizationCode string `json:"organization_code"`
  15. ParentOrganizationCode string `json:"parent_organization_code"`
  16. EndTime int64 `json:"end_time"`
  17. CreatedAt time.Time `json:"created_at" gorm:"created_at"`
  18. UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
  19. IsExpire bool `json:"is_expire" gorm:"column:is_expire"`
  20. IsDisable bool `json:"is_disable" gorm:"column:is_disable"`
  21. StartTime int64 `json:"start_time" gorm:"start_time"`
  22. Key string `json:"key" gorm:"key"`
  23. }
  24. func (p *Organization) TableName() string {
  25. return "t_organization"
  26. }
  27. func (p *Organization) Find(db *gorm.DB, where map[string]interface{}) error {
  28. err := db.Table(p.TableName()).Where(where).Find(p).Error
  29. if err != nil {
  30. fields, _ := json.MarshalToString(where)
  31. logger.Error("mysql",
  32. zap.String("sql", "select from "+p.TableName()),
  33. zap.String("fields", fields),
  34. zap.String("error", err.Error()))
  35. }
  36. return err
  37. }
  38. func (p *Organization) Last(db *gorm.DB) error {
  39. err := db.Table(p.TableName()).Last(p).Error
  40. if err != nil {
  41. logger.Error("mysql",
  42. zap.String("sql", "select last from "+p.TableName()),
  43. zap.String("fields", ""),
  44. zap.String("error", err.Error()))
  45. }
  46. return err
  47. }
  48. // Insert 插入一条记录
  49. func (p *Organization) Insert(db *gorm.DB) error {
  50. timeNow := time.Now()
  51. p.CreatedAt = timeNow
  52. p.UpdatedAt = timeNow
  53. err := db.Create(p).Error
  54. if err != nil {
  55. fields, _ := json.MarshalToString(*p)
  56. logger.Error("mysql",
  57. zap.String("sql", "insert into "+p.TableName()),
  58. zap.String("fields", fields),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *Organization) Delete(db *gorm.DB, filter map[string]interface{}) error {
  64. err := db.Where(filter).Delete(p).Error
  65. if err != nil {
  66. fields, _ := json.MarshalToString(filter)
  67. logger.Error("mysql",
  68. zap.String("sql", "delete from "+p.TableName()),
  69. zap.String("fields", fields),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *Organization) Save(db *gorm.DB) error {
  75. p.UpdatedAt = time.Now()
  76. err := db.Save(p).Error
  77. if err != nil {
  78. fields, _ := json.MarshalToString(*p)
  79. logger.Error("mysql",
  80. zap.String("sql", "save "+p.TableName()),
  81. zap.String("fields", fields),
  82. zap.String("error", err.Error()))
  83. }
  84. return err
  85. }
  86. func (p *Organization) Update(db *gorm.DB) error {
  87. p.UpdatedAt = time.Now()
  88. err := db.Model(p).Updates(p).Error
  89. if err != nil {
  90. fields, _ := json.MarshalToString(*p)
  91. logger.Error("mysql",
  92. zap.String("sql", "update "+p.TableName()),
  93. zap.String("fields", fields),
  94. zap.String("error", err.Error()))
  95. }
  96. return err
  97. }
  98. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  99. func (p *Organization) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  100. if filed == nil {
  101. return errors.ParamsError
  102. }
  103. filed["updated_at"] = time.Now()
  104. err := db.Model(p).Updates(filed).Error
  105. if err != nil {
  106. fields, _ := json.MarshalToString(filed)
  107. logger.Error("mysql",
  108. zap.String("sql", "update "+p.TableName()),
  109. zap.String("fields", fields),
  110. zap.String("error", err.Error()))
  111. }
  112. return err
  113. }
  114. func (p *Organization) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  115. err := db.Table(p.TableName()).Where(filter).Find(out).Error
  116. if err != nil {
  117. fields, _ := json.MarshalToString(filter)
  118. logger.Error("mysql",
  119. zap.String("sql", "select from "+p.TableName()),
  120. zap.String("fields", fields),
  121. zap.String("error", err.Error()))
  122. }
  123. return err
  124. }
  125. func (p *Organization) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, limit, offset int64) (list []Organization, err error) {
  126. cond, val, err := whereBuildAndOr(where, or)
  127. if err != nil {
  128. return list, err
  129. }
  130. err = db.Table(p.TableName()).Where(cond, val...).Limit(limit).Offset(offset).Find(&list).Error
  131. if err != nil {
  132. logger.Error("mysql",
  133. zap.String("sql", "select * from "+p.TableName()),
  134. zap.String("error", err.Error()))
  135. return list, errors.DataBaseError
  136. }
  137. return list, err
  138. }
  139. func (p *Organization) Count(db *gorm.DB) (int64, error) {
  140. var count int64
  141. err := db.Table(p.TableName()).Count(&count).Error
  142. if err != nil {
  143. logger.Error("mysql",
  144. zap.String("sql", "select count from "+p.TableName()),
  145. zap.String("fields", ""),
  146. zap.String("error", err.Error()))
  147. }
  148. return count, err
  149. }