user_repo.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package system
  2. import (
  3. "fmt"
  4. "xorm.io/xorm"
  5. "xps/datamodels"
  6. "xps/viewmodels"
  7. )
  8. type UserRepo struct {
  9. engine *xorm.Engine
  10. }
  11. func NewUserRepo(engine *xorm.Engine) *UserRepo {
  12. return &UserRepo{
  13. engine: engine,
  14. }
  15. }
  16. func (d *UserRepo) GetById(ocId int64, userId int64)(*viewmodels.UserInfo, error){
  17. sqlSelect := ` SELECT A.gm_id,
  18. A.oc_id,
  19. A.group_id,
  20. A.account_id,
  21. B.account_name,
  22. B.account_real_name,
  23. B.account_staff_no,
  24. B.account_phone,
  25. B.account_type,
  26. B.account_avatar,
  27. B.account_photo,
  28. B.account_loc,
  29. B.status,
  30. B.is_fixed,
  31. C.group_name,
  32. C.node_left,
  33. C.node_right,
  34. E.gmp_id,
  35. E.position_id,
  36. H.position_name,
  37. F.gmpg_id,
  38. F.grant_group_id,
  39. G.group_name AS grant_group_name`
  40. sqlFrom := ` FROM s_group_member AS A
  41. LEFT JOIN account AS B ON ( A.oc_id = B.oc_id AND A.account_id = B.account_id )
  42. LEFT JOIN s_group AS C ON ( A.oc_id = C.oc_id AND A.group_id = C.group_id )
  43. LEFT JOIN s_group_member_position AS E ON ( A.oc_id = E.oc_id AND A.account_id = E.account_id AND E.group_id= #{groupId ,jdbcType=BIGINT} )
  44. LEFT JOIN s_group_member_position_grant AS F ON ( E.oc_id = F.oc_id AND E.account_id = F.account_id AND E.position_id = F.position_id AND F.group_id = #{groupId ,jdbcType=BIGINT} )
  45. LEFT JOIN s_group AS G ON ( G.oc_id = F.oc_id AND G.group_id = F.grant_group_id )
  46. LEFT JOIN s_position AS H ON ( A.oc_id = E.oc_id AND H.position_id = E.position_id ) `
  47. sqlWhere := ` WHERE B.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  48. sqlWhere += ` AND A.account_id = ` + fmt.Sprintf("%d", userId)
  49. data := &viewmodels.UserInfo{}
  50. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Limit(1).Get(data)
  51. if !has || err != nil {
  52. return nil, err
  53. }
  54. return data, nil
  55. }
  56. func (d *UserRepo) GetByPage(m map[string]interface{})(*viewmodels.PageResult, error){
  57. ocId := m["ocId"].(int64)
  58. keywords := m["keywords"].(string)
  59. limit := m["limit"].(int)
  60. page := m["page"].(int)
  61. nodeLeft := m["nodeLeft"].(int64)
  62. nodeRight := m["nodeRight"].(int64)
  63. sqlSelect := `SELECT
  64. DISTINCT(A.account_id),
  65. A.gm_id,
  66. A.oc_id,
  67. A.group_id,
  68. B.account_name,
  69. B.account_real_name,
  70. B.account_phone,
  71. B.account_staff_no,
  72. B.account_avatar,
  73. B.status,
  74. B.is_fixed,
  75. C.group_name,
  76. C.node_left,
  77. C.node_right,
  78. D.gmp_id,
  79. D.position_id,
  80. D.position_name,
  81. E.gmpg_id,
  82. E.grant_group_id,
  83. F.group_name as grant_group_name `
  84. sqlFrom := `FROM s_group_member AS A
  85. LEFT JOIN account AS B ON (A.oc_id = B.oc_id AND A.account_id = B.account_id)
  86. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.group_id = C.group_id)
  87. LEFT JOIN s_group_member_position_grant AS E ON (B.oc_id = E.oc_id AND B.account_id = E.account_id)
  88. LEFT JOIN s_group AS F ON (E.oc_id = F.oc_id AND E.grant_group_id = F.group_id)
  89. LEFT JOIN (
  90. SELECT
  91. M.oc_id,
  92. M.account_id,
  93. M.gmp_id,
  94. M.group_id,
  95. M.position_id,
  96. P.position_name,
  97. N.gmpg_id,
  98. N.grant_group_id,
  99. GP.group_name as grant_group_name
  100. FROM s_group_member_position AS M
  101. LEFT JOIN s_group_member_position_grant AS N ON (
  102. M.oc_id = N.oc_id
  103. AND M.account_id = N.account_id
  104. AND M.group_id = N.group_id
  105. AND M.position_id = N.position_id )
  106. LEFT JOIN s_position AS P ON(M.oc_id = P.oc_id AND M.position_id = P.position_id)
  107. LEFT JOIN s_group AS GP ON(M.oc_id = GP.oc_id AND N.grant_group_id = GP.group_id)
  108. ) AS D ON (A.oc_id = D.oc_id AND A.account_id = D.account_id AND A.group_id = D.group_id) `
  109. sqlWhere := ` WHERE B.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  110. if keywords != "" {
  111. sqlWhere+= ` AND ( B.account_real_name LIKE ` + "'%"+keywords+"%'"
  112. sqlWhere+= ` OR B.account_name LIKE ` + "'%"+keywords+"%'"
  113. sqlWhere+= ` OR B.account_phone LIKE ` + "'%"+keywords+"%'"
  114. sqlWhere+= ` OR D.position_name LIKE ` + "'%"+keywords+"%'"
  115. }
  116. if nodeLeft > 0 && nodeRight > 0 {
  117. sqlWhere+= ` AND C.node_left >= ` + fmt.Sprintf("%d", nodeLeft)
  118. sqlWhere+= ` AND C.node_right <= ` + fmt.Sprintf("%d", nodeRight)
  119. }
  120. sqlCount := `SELECT COUNT(*) `
  121. total, err := d.engine.SQL(sqlCount + sqlFrom + sqlWhere).Count(new(datamodels.GroupMember))
  122. if err != nil {
  123. return nil, err
  124. }
  125. sqlOrderBy := " ORDER BY A.account_id, A.group_id "
  126. pageStart := (page - 1) * limit
  127. sqlLimit := fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, pageStart)
  128. datalist := make([]viewmodels.UserInfo, 0)
  129. err = d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy + sqlLimit).Find(&datalist)
  130. if err != nil {
  131. return nil, err
  132. }
  133. pageResult := &viewmodels.PageResult{}
  134. pageResult.Data = datalist
  135. pageResult.Total = total
  136. pageResult.PageSize = limit
  137. pageResult.Page = page
  138. return pageResult, nil
  139. }