group_member_position_repo.go 4.8 KB

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