team.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "time"
  6. "github.com/jaryhe/gopkgs/logger"
  7. "github.com/jinzhu/gorm"
  8. "go.uber.org/zap"
  9. )
  10. type TeamMasterInfo struct {
  11. ID int64 `gorm:"column:ID;primary_key"`
  12. Code string `gorm:"column:Code" json:"code"`
  13. TeamSysNo int `gorm:"column:TeamSysNo" json:"team_sys_no"`
  14. CorpCode string `gorm:"column:CorpCode" json:"corp_code"`
  15. CorpName string `gorm:"column:CorpName" json:"corp_name"`
  16. TeamName string `gorm:"column:TeamName" json:"team_name"`
  17. TeamLeaderName string `gorm:"column:TeamLeaderName" json:"team_leader_name"`
  18. TeamLeaderPhone string `gorm:"column:TeamLeaderPhone" json:"team_leader_phone"`
  19. Remark string `gorm:"column:Remark" json:"remark"`
  20. ProjectId int64 `gorm:"column:ProjectId" json:"project_id"`
  21. CreatedAt time.Time `gorm:"column:CreatedAt" json:"created_at"`
  22. UpdatedAt time.Time `gorm:"column:UpdatedAt" json:"updated_at"`
  23. Sn string `gorm:"column:Sn" json:"sn"`
  24. ProvincialVerifyStatus int32 `gorm:"column:ProvincialVerifyStatus" json:"provincial_verify_status"`
  25. ProvincialVerifyCode int32 `gorm:"column:ProvincialVerifyCode" json:"provincial_verify_code"`
  26. ProvincialVerifyInfo string `gorm:"column:ProvincialVerifyInfo" json:"provincial_verify_info"`
  27. ProvincialVerifyID string `gorm:"column:ProvincialVerifyID" json:"provincial_verify_id"`
  28. }
  29. func (TeamMasterInfo) TableName() string {
  30. return "db_smart_attendance.TeamMasterInfo"
  31. }
  32. // Insert 插入一条记录
  33. func (p *TeamMasterInfo) Insert(db *gorm.DB) error {
  34. err := db.Table(p.TableName()).Create(p).Error
  35. if err != nil {
  36. fields, _ := json.MarshalToString(*p)
  37. logger.Error("mysql",
  38. zap.String("sql", "insert into TeamMasterInfo"),
  39. zap.String("fields", fields),
  40. zap.String("error", err.Error()))
  41. }
  42. return err
  43. }
  44. func (p *TeamMasterInfo) Delete(db *gorm.DB, filter map[string]interface{}) error {
  45. cond, val, err := whereBuild(filter)
  46. if err != nil {
  47. return err
  48. }
  49. err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  50. if err != nil {
  51. fields, _ := json.MarshalToString(filter)
  52. logger.Error("mysql",
  53. zap.String("sql", "delete from "+p.TableName()),
  54. zap.String("fields", fields),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. func (p *TeamMasterInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  60. cond, val, err := whereBuild(where)
  61. if err != nil {
  62. whereStr, _ := json.MarshalToString(where)
  63. valuesStr, _ := json.MarshalToString(values)
  64. logger.Error("mysql",
  65. zap.String("sql", "update TeamMasterInfo"),
  66. zap.String("where", whereStr),
  67. zap.String("values", valuesStr),
  68. zap.String("error", err.Error()))
  69. return err
  70. }
  71. err = db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  72. if err != nil {
  73. whereStr, _ := json.MarshalToString(where)
  74. valuesStr, _ := json.MarshalToString(values)
  75. logger.Error("mysql",
  76. zap.String("sql", "update TeamMasterInfo"),
  77. zap.String("where", whereStr),
  78. zap.String("values", valuesStr),
  79. zap.String("error", err.Error()))
  80. }
  81. return err
  82. }
  83. func (p *TeamMasterInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  84. cond, val, err := whereBuild(where)
  85. if err != nil {
  86. whereStr, _ := json.MarshalToString(where)
  87. logger.Error("mysql",
  88. zap.String("sql", "select from TeamMasterInfo"),
  89. zap.String("where", whereStr),
  90. zap.String("error", err.Error()))
  91. return err
  92. }
  93. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  94. if err != nil {
  95. whereStr, _ := json.MarshalToString(where)
  96. logger.Error("mysql",
  97. zap.String("sql", "select from TeamMasterInfo"),
  98. zap.String("where", whereStr),
  99. zap.String("error", err.Error()))
  100. return err
  101. }
  102. return err
  103. }
  104. func (p *TeamMasterInfo) FindOr(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error {
  105. cond, val, err := whereBuildAndOr(where, or)
  106. if err != nil {
  107. whereStr, _ := json.MarshalToString(where)
  108. logger.Error("mysql",
  109. zap.String("sql", "select from TeamMasterInfo"),
  110. zap.String("where", whereStr),
  111. zap.String("error", err.Error()))
  112. return err
  113. }
  114. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  115. if err != nil {
  116. whereStr, _ := json.MarshalToString(where)
  117. logger.Error("mysql",
  118. zap.String("sql", "select from TeamMasterInfo"),
  119. zap.String("where", whereStr),
  120. zap.String("error", err.Error()))
  121. return err
  122. }
  123. return err
  124. }
  125. func (p *TeamMasterInfo) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int32, pageSize int32) (list []TeamMasterInfo, err error) {
  126. if len(where) > 0 || len(or) > 0 {
  127. cond, val, err := whereBuildAndOr(where, or)
  128. if err != nil {
  129. return list, err
  130. }
  131. if pageSize < 0 {
  132. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  133. return list, result.Error
  134. }
  135. offset := (page - 1) * pageSize
  136. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  137. return list, result.Error
  138. }
  139. if pageSize < 0 {
  140. result := db.Table(p.TableName()).Find(&list)
  141. return list, result.Error
  142. }
  143. offset := (page - 1) * pageSize
  144. result := db.Table(p.TableName()).Limit(pageSize).Offset(offset).Find(&list)
  145. return list, result.Error
  146. }