project_user.go 4.4 KB

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