group_member_position_repo.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package repository
  2. import (
  3. "fmt"
  4. "xorm.io/core"
  5. "xorm.io/xorm"
  6. "xps/pkg/system/datamodel"
  7. "xps/pkg/system/viewmodel"
  8. )
  9. type GroupMemberPositionRepo struct {
  10. engine *xorm.Engine
  11. }
  12. func NewGroupMemberPositionRepo(engine *xorm.Engine) *GroupMemberPositionRepo {
  13. return &GroupMemberPositionRepo{
  14. engine: engine,
  15. }
  16. }
  17. func (d *GroupMemberPositionRepo) GetFirstPositionByGroupIdAndAccountId(ocId, groupId, accountId int64) (*viewmodel.GroupMemberPositionInfo, error) {
  18. sqlSelect := ` SELECT A.oc_id,
  19. A.gmp_id,
  20. A.group_id,
  21. B.group_name,
  22. A.account_id,
  23. C.account_name,
  24. A.position_id,
  25. D.position_name `
  26. sqlFrom := ` FROM s_group_member_position AS A
  27. LEFT JOIN s_group AS B ON ( A.oc_id = B.oc_id AND A.group_id = B.group_id )
  28. LEFT JOIN account AS C ON ( A.oc_id = C.oc_id AND A.account_id = C.account_id )
  29. LEFT JOIN s_position AS D ON ( A.oc_id = D.oc_id AND A.position_id = D.position_id) `
  30. sqlWhere := ` WHERE A.oc_id = ` + fmt.Sprintf("%d", ocId)
  31. sqlWhere += ` AND A.group_id = ` + fmt.Sprintf("%d", groupId)
  32. sqlWhere += ` AND A.account_id = ` + fmt.Sprintf("%d", accountId)
  33. roleMemberPositionInfo := &viewmodel.GroupMemberPositionInfo{}
  34. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Limit(1).Get(roleMemberPositionInfo)
  35. if !has || err != nil {
  36. return nil, err
  37. }
  38. return roleMemberPositionInfo, nil
  39. }
  40. func (d *GroupMemberPositionRepo) GetByAccountId(ocId int64, accountId int64) (*datamodel.GroupMemberPosition, error) {
  41. data := &datamodel.GroupMemberPosition{OcId: ocId, AccountId: accountId}
  42. ok, err := d.engine.Get(data)
  43. if !ok && err != nil {
  44. return nil, err
  45. } else {
  46. return data, nil
  47. }
  48. }
  49. func (d *GroupMemberPositionRepo) GetList(m map[string]interface{}) ([]viewmodel.GroupMemberPositionInfo, error) {
  50. sqlSelect := ` SELECT A.gmp_id,
  51. A.oc_id,
  52. A.group_id,
  53. B.group_name,
  54. A.account_id,
  55. D.account_name,
  56. D.account_real_name,
  57. A.position_id,
  58. C.position_name `
  59. sqlFrom := ` FROM s_group_member_position A
  60. LEFT JOIN s_group B ON (A.oc_id = B.oc_id AND A.group_id = B.group_id)
  61. LEFT JOIN s_position C ON (A.oc_id = C.oc_id AND A.position_id = C.position_id)
  62. LEFT JOIN account D ON (A.oc_id = D.oc_id AND A.account_id = D.account_id) `
  63. sqlWhere := ` WHERE A.deleted_flag = 0 `
  64. if ocId, ok := m["ocId"]; ok {
  65. sqlWhere += ` AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  66. }
  67. if groupId, ok := m["groupId"]; ok {
  68. sqlWhere += ` AND A.group_id = ` + fmt.Sprintf("%d", groupId)
  69. }
  70. if accountId, ok := m["accountId"]; ok {
  71. sqlWhere += ` AND A.account_id = ` + fmt.Sprintf("%d", accountId)
  72. }
  73. if positionId, ok := m["positionId"]; ok {
  74. sqlWhere += ` AND A.position_id = ` + fmt.Sprintf("%d", positionId)
  75. }
  76. if keywords, ok := m["keywords"]; ok {
  77. sqlWhere += ` AND A.group_cat_title like '%` + fmt.Sprintf("%s", keywords) + `%'`
  78. }
  79. nodeLeft, ok1 := m["nodeLeft"]
  80. nodeRight, ok2 := m["nodeRight"]
  81. if ok1 && ok2 {
  82. sqlWhere += ` AND B.node_left >= ` + fmt.Sprintf("%d", nodeLeft)
  83. sqlWhere += ` AND B.node_right <= ` + fmt.Sprintf("%d", nodeRight)
  84. }
  85. datalist := make([]viewmodel.GroupMemberPositionInfo, 0)
  86. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id DESC").Find(&datalist)
  87. if err != nil {
  88. return nil, err
  89. } else {
  90. return datalist, nil
  91. }
  92. }
  93. func (d *GroupMemberPositionRepo) GetById(ocId int64, gmpid int64) (*viewmodel.GroupMemberPositionInfo, error) {
  94. sqlSelect := ` SELECT A.gmp_id,
  95. A.oc_id,
  96. A.group_id,
  97. B.group_name,
  98. A.account_id,
  99. D.account_name,
  100. D.account_real_name,
  101. A.position_id,
  102. C.position_name `
  103. sqlFrom := ` FROM s_group_member_position A
  104. LEFT JOIN s_group B ON (A.oc_id = B.oc_id AND A.group_id = B.group_id)
  105. LEFT JOIN s_position C ON (A.oc_id = C.oc_id AND A.position_id = C.position_id)
  106. LEFT JOIN account D ON (A.oc_id = D.oc_id AND A.account_id = D.account_id) `
  107. sqlWhere := ` WHERE A.deleted_flag = 0 `
  108. data := &viewmodel.GroupMemberPositionInfo{}
  109. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data)
  110. if !has || err != nil {
  111. return nil, err
  112. } else {
  113. return data, nil
  114. }
  115. }
  116. func (d *GroupMemberPositionRepo) Create(data *datamodel.GroupMemberPosition) error {
  117. _, err := d.engine.Insert(data)
  118. return err
  119. }
  120. func (d *GroupMemberPositionRepo) Update(ocId int64, gmpId int64, data *datamodel.GroupMemberPosition) error {
  121. _, err := d.engine.ID(core.PK{ocId, gmpId}).Update(data)
  122. return err
  123. }
  124. func (d *GroupMemberPositionRepo) Delete(ocId int64, gmpId int64) error {
  125. _, err := d.engine.ID(core.PK{ocId, gmpId}).
  126. Delete(new(datamodel.GroupMemberPosition))
  127. return err
  128. }
  129. func (d *GroupMemberPositionRepo) DeleteByGroupId(ocId int64, groupId int64) error {
  130. _, err := d.engine.Where("oc_id = ?", ocId).
  131. Where("group_id = ?", groupId).
  132. Delete(new(datamodel.GroupMemberPosition))
  133. return err
  134. }
  135. func (d *GroupMemberPositionRepo) DeleteByAccountId(ocId int64, accountId int64) error {
  136. _, err := d.engine.Where("oc_id = ?", ocId).
  137. Where("account_id = ?", accountId).
  138. Delete(new(datamodel.GroupMemberPosition))
  139. return err
  140. }