group_member_position_repo.go 4.8 KB

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