user_repo.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package repositories
  2. import "C"
  3. import (
  4. "fmt"
  5. "xorm.io/xorm"
  6. "xps/datamodel"
  7. "xps/viewmodel"
  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, groupId, userId int64)(*viewmodel.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. sqlFrom += ` LEFT JOIN account AS B ON ( A.oc_id = B.oc_id AND A.account_id = B.account_id ) `
  43. sqlFrom += ` LEFT JOIN s_group AS C ON ( A.oc_id = C.oc_id AND A.group_id = C.group_id ) `
  44. 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)
  45. 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)
  46. sqlFrom += ` LEFT JOIN s_group AS G ON ( G.oc_id = F.oc_id AND G.group_id = F.grant_group_id ) `
  47. sqlFrom += ` 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 `
  49. sqlWhere += ` AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  50. sqlWhere += ` AND A.account_id = ` + fmt.Sprintf("%d", userId)
  51. data := &viewmodel.UserInfo{}
  52. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Limit(1).Get(data)
  53. if !has || err != nil {
  54. return nil, err
  55. } else {
  56. return data, nil
  57. }
  58. }
  59. func (d *UserRepo) GetPage(m map[string]interface{})(*viewmodel.PageResult, error){
  60. ocId := m["ocId"].(int64)
  61. limit := m["limit"].(int)
  62. page := m["page"].(int)
  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, ok := m["keywords"]; ok {
  111. sqlWhere += " AND ( B.account_real_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  112. sqlWhere += " OR B.account_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  113. sqlWhere += " OR B.account_phone LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  114. sqlWhere += " OR D.position_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' )"
  115. }
  116. nodeLeft, ok1 := m["nodeLeft"]
  117. nodeRight, ok2 := m["nodeRight"]
  118. if ok1 && ok2 {
  119. sqlWhere += ` AND C.node_left >= ` + fmt.Sprintf("%d", nodeLeft)
  120. sqlWhere += ` AND C.node_right <= ` + fmt.Sprintf("%d", nodeRight)
  121. }
  122. sqlCount := `SELECT COUNT(*) `
  123. total, err := d.engine.SQL(sqlCount + sqlFrom + sqlWhere).Count(new(datamodel.GroupMember))
  124. if err != nil {
  125. return nil, err
  126. }
  127. sqlOrderBy := " ORDER BY A.account_id, A.group_id "
  128. pageStart := (page - 1) * limit
  129. sqlLimit := fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, pageStart)
  130. datalist := make([]viewmodel.UserInfo, 0)
  131. err = d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy + sqlLimit).Find(&datalist)
  132. if err != nil {
  133. return nil, err
  134. }
  135. pageResult := &viewmodel.PageResult{}
  136. pageResult.Data = datalist
  137. pageResult.Total = total
  138. pageResult.PageSize = limit
  139. pageResult.Page = page
  140. return pageResult, nil
  141. }
  142. func (d *UserRepo) GetList(m map[string]interface{})([]viewmodel.UserInfo, error){
  143. ocId := m["ocId"].(int64)
  144. sqlSelect := ` SELECT DISTINCT(A.account_id),
  145. A.gm_id,
  146. A.oc_id,
  147. A.group_id,
  148. B.account_name,
  149. B.account_real_name,
  150. B.account_phone,
  151. B.account_staff_no,
  152. B.account_avatar,
  153. B.status,
  154. B.is_fixed,
  155. C.group_name,
  156. C.node_left,
  157. C.node_right,
  158. D.gmp_id,
  159. D.position_id,
  160. D.position_name,
  161. E.gmpg_id,
  162. E.grant_group_id,
  163. F.group_name as grant_group_name `
  164. sqlFrom := ` FROM s_group_member AS A
  165. LEFT JOIN account AS B ON (A.oc_id = B.oc_id AND A.account_id = B.account_id)
  166. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.group_id = C.group_id )
  167. LEFT JOIN s_group_member_position_grant AS E ON (B.oc_id = E.oc_id AND B.account_id = E.account_id)
  168. LEFT JOIN s_group AS F ON (E.oc_id = F.oc_id AND E.grant_group_id = F.group_id )
  169. LEFT JOIN (
  170. SELECT
  171. M.oc_id,
  172. M.account_id,
  173. M.gmp_id,
  174. M.group_id,
  175. M.position_id,
  176. P.position_name,
  177. N.gmpg_id,
  178. N.grant_group_id,
  179. GP.group_name as grant_group_name
  180. FROM s_group_member_position AS M
  181. LEFT JOIN s_group_member_position_grant AS N ON (
  182. M.oc_id = N.oc_id
  183. AND M.account_id = N.account_id
  184. AND M.group_id = N.group_id
  185. AND M.position_id = N.position_id )
  186. LEFT JOIN s_position AS P ON(M.oc_id = P.oc_id AND M.position_id = P.position_id)
  187. LEFT JOIN s_group AS GP ON(M.oc_id = GP.oc_id AND N.grant_group_id = GP.group_id)
  188. ) AS D ON (A.oc_id = D.oc_id AND A.account_id = D.account_id AND A.group_id = D.group_id) `
  189. sqlWhere := ` WHERE B.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  190. if keywords, ok := m["keywords"]; ok {
  191. sqlWhere += " AND ( B.account_real_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  192. sqlWhere += " OR B.account_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  193. sqlWhere += " OR B.account_phone LIKE '%" + fmt.Sprintf("%s",keywords) + "%' "
  194. sqlWhere += " OR D.position_name LIKE '%" + fmt.Sprintf("%s",keywords) + "%' )"
  195. }
  196. nodeLeft, ok1 := m["nodeLeft"]
  197. nodeRight, ok2 := m["nodeRight"]
  198. if ok1 && ok2 {
  199. sqlWhere += ` AND C.node_left >= ` + fmt.Sprintf("%d", nodeLeft)
  200. sqlWhere += ` AND C.node_right <= ` + fmt.Sprintf("%d", nodeRight)
  201. }
  202. sqlOrderBy := " ORDER BY A.account_id, A.group_id "
  203. datalist := make([]viewmodel.UserInfo, 0)
  204. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy).Find(&datalist)
  205. if err != nil {
  206. return nil, err
  207. } else {
  208. return datalist, nil
  209. }
  210. }