project_user.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "go.uber.org/zap"
  8. "smart-site-management/errors"
  9. "time"
  10. "github.com/jaryhe/gopkgs/logger"
  11. "encoding/json"
  12. )
  13. type TSiteUser struct {
  14. Id int64 `gorm:"column:id" json:"id" form:"id"`
  15. Username string `gorm:"column:username" json:"username" form:"username"`
  16. Passwd string `gorm:"column:passwd" json:"passwd" form:"passwd"`
  17. SiteUser int64 `gorm:"column:site_user" json:"site_user" form:"site_user"`
  18. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  19. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  20. UserType int64 `gorm:"column:user_type" json:"user_type" form:"user_type"`
  21. Enable bool `json:"enable"`
  22. ProjectId int64 `json:"project_id"`
  23. Phone string
  24. Email string
  25. }
  26. func (TSiteUser) TableName() string {
  27. return "t_site_user"
  28. }
  29. func (p *TSiteUser) Insert(db *gorm.DB) error {
  30. err := db.Create(p).Error
  31. if err != nil {
  32. fields, _ := json.Marshal(p)
  33. logger.Error("mysql",
  34. zap.String("sql", "insert into t_site_user"),
  35. zap.String("fields", string(fields)),
  36. zap.String("error", err.Error()))
  37. return errors.DataBaseError
  38. }
  39. return err
  40. }
  41. func (p *TSiteUser) Find(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error {
  42. cond, val, err := whereBuildAndOr(where, or)
  43. if err != nil {
  44. return err
  45. }
  46. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  47. if err != nil {
  48. fields, _ := json.Marshal(val)
  49. logger.Error("mysql",
  50. zap.String("sql", "select from t_site_user"),
  51. zap.String("fields", cond +","+string(fields)),
  52. zap.String("error", err.Error()))
  53. if err == gorm.ErrRecordNotFound {
  54. return nil
  55. }
  56. return errors.DataBaseError
  57. }
  58. return err
  59. }
  60. func (p *TSiteUser) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  61. cond, val, err := whereBuild(where)
  62. if err != nil {
  63. return err
  64. }
  65. err = db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  66. if err != nil {
  67. fields, _ := json.Marshal(where)
  68. updateValues, _ := json.Marshal(values)
  69. logger.Error("mysql",
  70. zap.String("sql", "update t_site_user"),
  71. zap.String("fields",string(fields)),
  72. zap.String("values",string(updateValues)),
  73. zap.String("error", err.Error()))
  74. return errors.DataBaseError
  75. }
  76. return err
  77. }
  78. type ProjectUserItem struct {
  79. Id int64
  80. Username string
  81. Passwd string
  82. ProjectName string
  83. SafetyRecordNo string
  84. Enable bool
  85. }
  86. func (p *TSiteUser) ListWithProject(db *gorm.DB, filter string, cid int64, page int32) ([]ProjectUserItem, int64, error) {
  87. type ResultCount struct {
  88. Count int64
  89. }
  90. offset := (page - 1) * int32(PageSize)
  91. sql := "select t1.id, t1.username, t1.passwd, t1.enable, t2.name as project_name, t2.safety_record_no "+
  92. "from t_site_user as t1 left join t_project as t2 on t1.project_id = t2.id"
  93. where := " where t2.cid = ?"
  94. if filter != "" {
  95. where = fmt.Sprintf(" and (t1.username like '%%%s%%' or t2.safety_record_no like '%%%s%%' or t2.name like '%%%s%%')", filter, filter, filter)
  96. sql = sql + where
  97. }
  98. sql = fmt.Sprintf("%s limit %d offset %d", sql, PageSize, offset)
  99. countSql := fmt.Sprintf("select count(1) as count from t_site_user as t1 left join t_project as t2 on t1.project_id = t2.id %s", where)
  100. array := []ResultCount{}
  101. err := db.Raw(countSql, cid).Scan(&array).Error
  102. if err != nil {
  103. logger.Error("mysql",
  104. zap.String("sql", countSql),
  105. zap.String("fields",fmt.Sprintf("%d", cid)),
  106. zap.String("error", err.Error()))
  107. return nil, 0, err
  108. }
  109. if len(array) == 0 {
  110. return nil, 0, nil
  111. }
  112. if array[0].Count == 0 {
  113. return nil, 0, nil
  114. }
  115. ret := []ProjectUserItem{}
  116. err = db.Raw(sql, cid).Scan(&ret).Error
  117. if err != nil && err != gorm.ErrRecordNotFound {
  118. logger.Error("mysql",
  119. zap.String("sql", sql),
  120. zap.String("fields",fmt.Sprintf("%d", cid)),
  121. zap.String("error", err.Error()))
  122. }
  123. return ret, array[0].Count, nil
  124. }