group_member_position_repo.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. `
  42. sqlWhere := ` WHERE A.oc_id = ` + fmt.Sprintf("%d", ocId)
  43. sqlWhere += ` AND A.group_id = ` + fmt.Sprintf("%d", groupId)
  44. sqlWhere += ` AND A.account_id = ` + fmt.Sprintf("%d", accountId)
  45. roleMemberPositionInfo := &viewmodels.GroupMemberPositionInfo{}
  46. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Limit(1).Get(roleMemberPositionInfo)
  47. if !has || err != nil {
  48. return nil, err
  49. }
  50. return roleMemberPositionInfo, nil
  51. }
  52. func (d *GroupMemberPositionRepo) GetByAccountId(ocId int64, accountId int64)(*datamodels.GroupMemberPosition, error){
  53. data := &datamodels.GroupMemberPosition{OcId: ocId, AccountId: accountId}
  54. ok, err := d.engine.Get(data)
  55. if !ok && err != nil {
  56. return nil, err
  57. } else {
  58. return data, nil
  59. }
  60. }
  61. func (d *GroupMemberPositionRepo) GetByList(m map[string]interface{})([]viewmodels.GroupMemberPositionInfo, error){
  62. ocId := m["ocId"].(int64)
  63. keywords := m["keywords"].(string)
  64. sqlSelect := `SELECT A.gm_id,
  65. A.group_id,
  66. A.group_name,
  67. A.account_id,
  68. A.group_member_remark `
  69. sqlFrom := ` FROM s_group_member AS A
  70. LEFT JOIN s_group AS B ON (A.group_id = B.group_id) `
  71. sqlWhere := `WHERE A.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  72. if keywords != "" {
  73. sqlWhere += ` AND B.group_name like ` + "'%" + keywords + "%'"
  74. }
  75. datalist := make([]viewmodels.GroupMemberPositionInfo, 0)
  76. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id DESC").Find(&datalist)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return datalist, nil
  81. }
  82. func (d *GroupMemberPositionRepo) Create(data *datamodels.GroupMemberPosition) error {
  83. _, err := d.engine.Insert(data)
  84. return err
  85. }
  86. func (d *GroupMemberPositionRepo) Update(ocId int64, gmpId int64, data *datamodels.GroupMemberPosition) error {
  87. _, err := d.engine.ID(core.PK{ocId, gmpId}).Update(data)
  88. return err
  89. }
  90. func (d *GroupMemberPositionRepo) Delete(ocId int64, gmpId int64) error {
  91. _, err := d.engine.ID(core.PK{ocId, gmpId}).Delete(new(datamodels.GroupMemberPosition))
  92. return err
  93. }
  94. func (d *GroupMemberPositionRepo) DeleteByGroupId(ocId int64, groupId int64) error {
  95. _, err := d.engine.Where("oc_id = ?", ocId).
  96. Where("group_id = ?", groupId).
  97. Delete(new(datamodels.GroupMemberPosition))
  98. return err
  99. }
  100. func (d *GroupMemberPositionRepo) DeleteByAccountId(ocId int64, accountId int64) error {
  101. _, err := d.engine.Where("oc_id = ?", ocId).
  102. Where("account_id = ?", accountId).
  103. Delete(new(datamodels.GroupMemberPosition))
  104. return err
  105. }