project_user.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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:SiteUser" json:"site_user" form:"site_user"`
  14. CreatedAt time.Time `gorm:"column:CreatedAt" json:"created_at" form:"created_at"`
  15. UpdatedAt time.Time `gorm:"column:UpdatedAt" json:"updated_at" form:"updated_at"`
  16. UserType int64 `gorm:"column:UserType" json:"user_type" form:"user_type"`
  17. Enable bool `gorm:"column:Enable"`
  18. ProjectId int64 `gorm:"column:ProjectId"`
  19. }
  20. func (TSiteUser) TableName() string {
  21. return "ProjectUser"
  22. }
  23. func (p *TSiteUser) Insert(db *gorm.DB) error {
  24. return db.Table(p.TableName()).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) List(db *gorm.DB, where map[string]interface{}, page int) (list []TSiteUser, err error) {
  56. if len(where) > 0 {
  57. cond, val, err := whereBuild(where)
  58. if err != nil {
  59. return list, err
  60. }
  61. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  62. return list, result.Error
  63. }
  64. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  65. return list, result.Error
  66. }
  67. func (p *TSiteUser) All(db *gorm.DB) (list []TCompany, err error) {
  68. result := db.Table(p.TableName()).Find(&list)
  69. return list, result.Error
  70. }
  71. func (p *TSiteUser) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  72. if len(where) > 0 {
  73. cond, val, err := whereBuild(where)
  74. if err != nil {
  75. return 0, err
  76. }
  77. ret := int64(0)
  78. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  79. return ret, err
  80. }
  81. ret := int64(0)
  82. err := db.Table(p.TableName()).Count(&ret).Error
  83. return ret, err
  84. }
  85. func (p *TSiteUser) Del(db *gorm.DB, where map[string]interface{}) error {
  86. cond, val, err := whereBuild(where)
  87. if err != nil {
  88. return err
  89. }
  90. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  91. }
  92. type ProjectUserItem struct {
  93. Id int64
  94. Username string
  95. Passwd string
  96. ProjectName string
  97. SafetyRecordNo string
  98. Enable bool
  99. UserType int32
  100. }
  101. func (p *TSiteUser) ListWithProject(db *gorm.DB, filter string, cid int64, page int32) ([]ProjectUserItem, int64, error) {
  102. type ResultCount struct {
  103. Count int64
  104. }
  105. offset := (page - 1) * int32(PageSize)
  106. sql := "select t1.ID as id, t1.UserName as username, t1.Passwd as passwd, t1.UserType as user_type, t1.Enable as enable, t2.Name as project_name, t2.SafetyNo as safety_record_no "+
  107. "from ProjectUser as t1 left join ProjectInfo as t2 on t1.ProjectId = t2.ID"
  108. where := " where t2.Cid = ?"
  109. if filter != "" {
  110. where = fmt.Sprintf(" %s and (t1.UserName like '%%%s%%' or t2.SafetyNo like '%%%s%%' or t2.Name like '%%%s%%')", where, filter, filter, filter)
  111. }
  112. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  113. countSql := fmt.Sprintf("select count(1) as count from ProjectUser as t1 left join ProjectInfo as t2 on t1.ProjectId = t2.ID %s", where)
  114. array := []ResultCount{}
  115. err := db.Raw(countSql, cid).Scan(&array).Error
  116. if err != nil {
  117. return nil, 0, err
  118. }
  119. if len(array) == 0 {
  120. return nil, 0, nil
  121. }
  122. if array[0].Count == 0 {
  123. return nil, 0, nil
  124. }
  125. ret := []ProjectUserItem{}
  126. err = db.Raw(sql, cid).Scan(&ret).Error
  127. if err != nil && err != gorm.ErrRecordNotFound {
  128. return nil, 0, err
  129. }
  130. return ret, array[0].Count, nil
  131. }