user_repo.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package repositories
  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. } else {
  55. return data, nil
  56. }
  57. }
  58. func (d *UserRepo) GetPage(m map[string]interface{})(*viewmodels.PageResult, error){
  59. ocId := m["ocId"].(int64)
  60. limit := m["limit"].(int)
  61. page := m["page"].(int)
  62. sqlSelect := `SELECT
  63. DISTINCT(A.account_id),
  64. A.gm_id,
  65. A.oc_id,
  66. A.group_id,
  67. B.account_name,
  68. B.account_real_name,
  69. B.account_phone,
  70. B.account_staff_no,
  71. B.account_avatar,
  72. B.status,
  73. B.is_fixed,
  74. C.group_name,
  75. C.node_left,
  76. C.node_right,
  77. D.gmp_id,
  78. D.position_id,
  79. D.position_name,
  80. E.gmpg_id,
  81. E.grant_group_id,
  82. F.group_name as grant_group_name `
  83. sqlFrom := `FROM s_group_member AS A
  84. LEFT JOIN account AS B ON (A.oc_id = B.oc_id AND A.account_id = B.account_id)
  85. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.group_id = C.group_id)
  86. LEFT JOIN s_group_member_position_grant AS E ON (B.oc_id = E.oc_id AND B.account_id = E.account_id)
  87. LEFT JOIN s_group AS F ON (E.oc_id = F.oc_id AND E.grant_group_id = F.group_id)
  88. LEFT JOIN (
  89. SELECT
  90. M.oc_id,
  91. M.account_id,
  92. M.gmp_id,
  93. M.group_id,
  94. M.position_id,
  95. P.position_name,
  96. N.gmpg_id,
  97. N.grant_group_id,
  98. GP.group_name as grant_group_name
  99. FROM s_group_member_position AS M
  100. LEFT JOIN s_group_member_position_grant AS N ON (
  101. M.oc_id = N.oc_id
  102. AND M.account_id = N.account_id
  103. AND M.group_id = N.group_id
  104. AND M.position_id = N.position_id )
  105. LEFT JOIN s_position AS P ON(M.oc_id = P.oc_id AND M.position_id = P.position_id)
  106. LEFT JOIN s_group AS GP ON(M.oc_id = GP.oc_id AND N.grant_group_id = GP.group_id)
  107. ) AS D ON (A.oc_id = D.oc_id AND A.account_id = D.account_id AND A.group_id = D.group_id) `
  108. sqlWhere := ` WHERE B.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  109. if keywords, ok := m["keywords"]; ok {
  110. sqlWhere += " AND ( B.account_real_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  111. sqlWhere += " OR B.account_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  112. sqlWhere += " OR B.account_phone LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  113. sqlWhere += " OR D.position_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' )"
  114. }
  115. nodeLeft, ok1 := m["nodeLeft"]
  116. nodeRight, ok2 := m["nodeRight"]
  117. if ok1 && ok2 {
  118. sqlWhere += ` AND C.node_left >= ` + fmt.Sprintf("%d", nodeLeft)
  119. sqlWhere += ` AND C.node_right <= ` + fmt.Sprintf("%d", nodeRight)
  120. }
  121. sqlCount := `SELECT COUNT(*) `
  122. total, err := d.engine.SQL(sqlCount + sqlFrom + sqlWhere).Count(new(datamodels.GroupMember))
  123. if err != nil {
  124. return nil, err
  125. }
  126. sqlOrderBy := " ORDER BY A.account_id, A.group_id "
  127. pageStart := (page - 1) * limit
  128. sqlLimit := fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, pageStart)
  129. datalist := make([]viewmodels.UserInfo, 0)
  130. err = d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy + sqlLimit).Find(&datalist)
  131. if err != nil {
  132. return nil, err
  133. }
  134. pageResult := &viewmodels.PageResult{}
  135. pageResult.Data = datalist
  136. pageResult.Total = total
  137. pageResult.PageSize = limit
  138. pageResult.Page = page
  139. return pageResult, nil
  140. }
  141. func (d *UserRepo) GetList(m map[string]interface{})([]viewmodels.UserInfo, error){
  142. ocId := m["ocId"].(int64)
  143. sqlSelect := ` SELECT 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. } else {
  207. return datalist, nil
  208. }
  209. }