user_repo.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package repository
  2. import "C"
  3. import (
  4. "fmt"
  5. "xorm.io/xorm"
  6. "xps/pkg/common"
  7. "xps/pkg/system/datamodel"
  8. "xps/pkg/system/viewmodel"
  9. )
  10. type UserRepo struct {
  11. engine *xorm.Engine
  12. }
  13. func NewUserRepo(engine *xorm.Engine) *UserRepo {
  14. return &UserRepo{
  15. engine: engine,
  16. }
  17. }
  18. func (d *UserRepo) GetById(ocId, groupId, userId int64) (*viewmodel.UserInfo, error) {
  19. sqlSelect := ` SELECT A.gm_id,
  20. A.oc_id,
  21. A.group_id,
  22. A.account_id,
  23. B.account_name,
  24. B.account_real_name,
  25. B.account_staff_no,
  26. B.account_phone,
  27. B.account_type,
  28. B.account_avatar,
  29. B.account_photo,
  30. B.account_loc,
  31. B.status,
  32. B.is_fixed,
  33. C.group_name,
  34. C.node_left,
  35. C.node_right,
  36. E.gmp_id,
  37. E.position_id,
  38. H.position_name,
  39. F.gmpg_id,
  40. F.grant_group_id,
  41. G.group_name AS grant_group_name `
  42. sqlFrom := ` FROM s_group_member AS A `
  43. sqlFrom += ` LEFT JOIN account AS B ON ( A.oc_id = B.oc_id AND A.account_id = B.account_id ) `
  44. sqlFrom += ` LEFT JOIN s_group AS C ON ( A.oc_id = C.oc_id AND A.group_id = C.group_id ) `
  45. sqlFrom += fmt.Sprintf(` 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= %d )`, groupId)
  46. sqlFrom += fmt.Sprintf(` 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 = %d )`, groupId)
  47. sqlFrom += ` LEFT JOIN s_group AS G ON ( G.oc_id = F.oc_id AND G.group_id = F.grant_group_id ) `
  48. sqlFrom += ` LEFT JOIN s_position AS H ON ( A.oc_id = E.oc_id AND H.position_id = E.position_id ) `
  49. sqlWhere := ` WHERE B.deleted_flag = 0 `
  50. sqlWhere += ` AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  51. sqlWhere += ` AND A.account_id = ` + fmt.Sprintf("%d", userId)
  52. data := &viewmodel.UserInfo{}
  53. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Limit(1).Get(data)
  54. if !has || err != nil {
  55. return nil, err
  56. } else {
  57. return data, nil
  58. }
  59. }
  60. func (d *UserRepo) GetPage(m map[string]interface{}) (*common.PageResult, error) {
  61. ocId := m["ocId"].(int64)
  62. limit := m["limit"].(int)
  63. page := m["page"].(int)
  64. sqlSelect := ` SELECT
  65. DISTINCT(A.account_id),
  66. A.gm_id,
  67. A.oc_id,
  68. A.group_id,
  69. B.account_name,
  70. B.account_real_name,
  71. B.account_phone,
  72. B.account_staff_no,
  73. B.account_avatar,
  74. B.status,
  75. B.is_fixed,
  76. C.group_name,
  77. C.node_left,
  78. C.node_right,
  79. D.gmp_id,
  80. D.position_id,
  81. D.position_name,
  82. E.gmpg_id,
  83. E.grant_group_id,
  84. F.group_name as grant_group_name `
  85. sqlFrom := ` FROM s_group_member AS A
  86. LEFT JOIN account AS B ON (A.oc_id = B.oc_id AND A.account_id = B.account_id )
  87. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.group_id = C.group_id )
  88. LEFT JOIN s_group_member_position_grant AS E ON (B.oc_id = E.oc_id AND B.account_id = E.account_id )
  89. LEFT JOIN s_group AS F ON (E.oc_id = F.oc_id AND E.grant_group_id = F.group_id )
  90. LEFT JOIN (
  91. SELECT
  92. M.oc_id,
  93. M.account_id,
  94. M.gmp_id,
  95. M.group_id,
  96. M.position_id,
  97. P.position_name,
  98. N.gmpg_id,
  99. N.grant_group_id,
  100. GP.group_name as grant_group_name
  101. FROM s_group_member_position AS M
  102. LEFT JOIN s_group_member_position_grant AS N ON (
  103. M.oc_id = N.oc_id
  104. AND M.account_id = N.account_id
  105. AND M.group_id = N.group_id
  106. AND M.position_id = N.position_id )
  107. LEFT JOIN s_position AS P ON(M.oc_id = P.oc_id AND M.position_id = P.position_id)
  108. LEFT JOIN s_group AS GP ON(M.oc_id = GP.oc_id AND N.grant_group_id = GP.group_id)
  109. ) AS D ON (A.oc_id = D.oc_id AND A.account_id = D.account_id AND A.group_id = D.group_id) `
  110. sqlWhere := ` WHERE B.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  111. if keywords, ok := m["keywords"]; ok {
  112. sqlWhere += " AND ( B.account_real_name LIKE '%" + fmt.Sprintf("%s", keywords) + "%' "
  113. sqlWhere += " OR B.account_name LIKE '%" + fmt.Sprintf("%s", keywords) + "%' "
  114. sqlWhere += " OR B.account_phone LIKE '%" + fmt.Sprintf("%s", keywords) + "%' "
  115. sqlWhere += " OR D.position_name LIKE '%" + fmt.Sprintf("%s", keywords) + "%' )"
  116. }
  117. nodeLeft, ok1 := m["nodeLeft"]
  118. nodeRight, ok2 := m["nodeRight"]
  119. if ok1 && ok2 {
  120. sqlWhere += ` AND C.node_left >= ` + fmt.Sprintf("%d", nodeLeft)
  121. sqlWhere += ` AND C.node_right <= ` + fmt.Sprintf("%d", nodeRight)
  122. }
  123. sqlCount := `SELECT COUNT(*) `
  124. total, err := d.engine.SQL(sqlCount + sqlFrom + sqlWhere).Count(new(datamodel.GroupMember))
  125. if err != nil {
  126. return nil, err
  127. }
  128. sqlOrderBy := " ORDER BY A.account_id, A.group_id "
  129. pageStart := (page - 1) * limit
  130. sqlLimit := fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, pageStart)
  131. datalist := make([]viewmodel.UserInfo, 0)
  132. err = d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy + sqlLimit).Find(&datalist)
  133. if err != nil {
  134. return nil, err
  135. }
  136. pageResult := &common.PageResult{}
  137. pageResult.Data = datalist
  138. pageResult.Total = total
  139. pageResult.PageSize = limit
  140. pageResult.Page = page
  141. return pageResult, nil
  142. }
  143. func (d *UserRepo) GetList(m map[string]interface{}) ([]viewmodel.UserInfo, error) {
  144. ocId := m["ocId"].(int64)
  145. sqlSelect := ` SELECT DISTINCT(A.account_id),
  146. A.gm_id,
  147. A.oc_id,
  148. A.group_id,
  149. B.account_name,
  150. B.account_real_name,
  151. B.account_phone,
  152. B.account_staff_no,
  153. B.account_avatar,
  154. B.status,
  155. B.is_fixed,
  156. C.group_name,
  157. C.node_left,
  158. C.node_right,
  159. D.gmp_id,
  160. D.position_id,
  161. D.position_name,
  162. E.gmpg_id,
  163. E.grant_group_id,
  164. F.group_name as grant_group_name `
  165. sqlFrom := ` FROM s_group_member AS A
  166. LEFT JOIN account AS B ON (A.oc_id = B.oc_id AND A.account_id = B.account_id)
  167. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.group_id = C.group_id )
  168. LEFT JOIN s_group_member_position_grant AS E ON (B.oc_id = E.oc_id AND B.account_id = E.account_id)
  169. LEFT JOIN s_group AS F ON (E.oc_id = F.oc_id AND E.grant_group_id = F.group_id )
  170. LEFT JOIN (
  171. SELECT
  172. M.oc_id,
  173. M.account_id,
  174. M.gmp_id,
  175. M.group_id,
  176. M.position_id,
  177. P.position_name,
  178. N.gmpg_id,
  179. N.grant_group_id,
  180. GP.group_name as grant_group_name
  181. FROM s_group_member_position AS M
  182. LEFT JOIN s_group_member_position_grant AS N ON (
  183. M.oc_id = N.oc_id
  184. AND M.account_id = N.account_id
  185. AND M.group_id = N.group_id
  186. AND M.position_id = N.position_id )
  187. LEFT JOIN s_position AS P ON(M.oc_id = P.oc_id AND M.position_id = P.position_id)
  188. LEFT JOIN s_group AS GP ON(M.oc_id = GP.oc_id AND N.grant_group_id = GP.group_id)
  189. ) AS D ON (A.oc_id = D.oc_id AND A.account_id = D.account_id AND A.group_id = D.group_id) `
  190. sqlWhere := ` WHERE B.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  191. if keywords, ok := m["keywords"]; ok {
  192. sqlWhere += " AND ( B.account_real_name LIKE '%" + fmt.Sprintf("%s", keywords) + "%' "
  193. sqlWhere += " OR B.account_name LIKE '%" + fmt.Sprintf("%s", keywords) + "%' "
  194. sqlWhere += " OR B.account_phone LIKE '%" + fmt.Sprintf("%s", keywords) + "%' "
  195. sqlWhere += " OR D.position_name LIKE '%" + fmt.Sprintf("%s", keywords) + "%' )"
  196. }
  197. nodeLeft, ok1 := m["nodeLeft"]
  198. nodeRight, ok2 := m["nodeRight"]
  199. if ok1 && ok2 {
  200. sqlWhere += ` AND C.node_left >= ` + fmt.Sprintf("%d", nodeLeft)
  201. sqlWhere += ` AND C.node_right <= ` + fmt.Sprintf("%d", nodeRight)
  202. }
  203. sqlOrderBy := " ORDER BY A.account_id, A.group_id "
  204. datalist := make([]viewmodel.UserInfo, 0)
  205. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy).Find(&datalist)
  206. if err != nil {
  207. return nil, err
  208. } else {
  209. return datalist, nil
  210. }
  211. }