user_repo.go 10.0 KB

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