group_repo.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package repositories
  2. import (
  3. "fmt"
  4. "time"
  5. "xorm.io/xorm"
  6. "xps/datamodels"
  7. "xps/viewmodels"
  8. )
  9. type GroupRepo struct {
  10. engine *xorm.Engine
  11. }
  12. func NewGroupRepo(engine *xorm.Engine) *GroupRepo {
  13. return &GroupRepo{
  14. engine:engine,
  15. }
  16. }
  17. func (d *GroupRepo) GetById(ocId int64 ,groupId int64)(*viewmodels.GroupInfo, error) {
  18. sqlSelect := ` SELECT A.oc_id,
  19. A.group_id ,
  20. A.group_name,
  21. A.group_cat_id,
  22. B.group_cat_title,
  23. A.group_code,
  24. A.group_select,
  25. A.group_path,
  26. A.group_desc,
  27. A.root_id,
  28. A.parent_id,
  29. C.group_name as parent_name,
  30. A.node_left,
  31. A.node_right,
  32. A.node_level,
  33. A.is_leaf,
  34. A.is_fixed `
  35. sqlFrom := ` FROM s_group AS A
  36. LEFT JOIN s_group_cat AS B ON (A.oc_id = B.oc_id AND A.group_cat_id = B.group_cat_id)
  37. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.parent_id = C.group_id )`
  38. sqlWhere := ` WHERE A.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d" ,ocId)
  39. sqlWhere += ` AND A.group_id = ` + fmt.Sprintf("%d" ,groupId)
  40. data := &viewmodels.GroupInfo{GroupId: groupId}
  41. ok, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id ASC").Get(data)
  42. if !ok && err != nil {
  43. return nil, err
  44. } else {
  45. return data, nil
  46. }
  47. }
  48. func (d *GroupRepo) GetList(m map[string]interface{})([]viewmodels.GroupInfo, error) {
  49. sqlSelect := ` SELECT A.oc_id,
  50. A.group_id ,
  51. A.group_name,
  52. A.group_cat_id,
  53. B.group_cat_title,
  54. A.group_code,
  55. A.group_select,
  56. A.group_path,
  57. A.group_desc,
  58. A.root_id,
  59. A.parent_id,
  60. C.group_name as parent_name,
  61. A.node_left,
  62. A.node_right,
  63. A.node_level,
  64. A.is_leaf,
  65. A.is_fixed `
  66. sqlFrom := ` FROM s_group AS A
  67. LEFT JOIN s_group_cat AS B ON (A.oc_id = B.oc_id AND A.group_cat_id = B.group_cat_id)
  68. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.parent_id = C.group_id )`
  69. ocId := m["ocId"].(int64)
  70. sqlWhere := ` WHERE A.deleted_flag = 0 and A.oc_id = ` + fmt.Sprintf("%d" ,ocId)
  71. if keywords, ok := m["keywords"]; ok {
  72. sqlWhere+= ` AND A.group_name like `+"'%"+ fmt.Sprintf("%s", keywords) +"%'"
  73. }
  74. datalist := make([]viewmodels.GroupInfo, 0)
  75. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id ASC").Find(&datalist)
  76. if err != nil{
  77. return nil, err
  78. }
  79. return datalist, nil
  80. }
  81. func (d *GroupRepo) Delete(m map[string]interface{}) error {
  82. ocId := m["ocId"].(int64)
  83. groupId := m["groupId"].(int64)
  84. deletedBy := m["deletedBy"].(int64)
  85. group := &datamodels.Group{}
  86. group.OcId = ocId
  87. group.GroupId = groupId
  88. group.DeletedFlag = 1
  89. group.DeletedBy = deletedBy
  90. group.DeletedAt = time.Now()
  91. _, err := d.engine.Where("oc_id = ?", ocId).Where("group_id =?", groupId).Update(group)
  92. return err
  93. }
  94. func (d *GroupRepo) Update(data *datamodels.Group) error{
  95. ocId := data.OcId
  96. groupId := data.GroupId
  97. _, err := d.engine.Where("oc_id = ?", ocId).Where("group_id =?", groupId).Update(data)
  98. return err
  99. }
  100. func (d *GroupRepo) RefreshLeafStatus(ocId int64 ,groupId int64 ,isLeaf bool) error {
  101. var sql string
  102. if isLeaf {
  103. sql = fmt.Sprintf("UPDATE s_group SET is_leaf = 1 WHERE oc_id = %d AND group_id = %d" ,ocId ,groupId)
  104. }else {
  105. sql = fmt.Sprintf("UPDATE s_group SET is_leaf = 0 WHERE oc_id = %d AND group_id = %d" ,ocId ,groupId)
  106. }
  107. _ ,err := d.engine.Exec(sql)
  108. return err
  109. }
  110. func (d *GroupRepo) Create(data *datamodels.Group) error {
  111. _, err := d.engine.Insert(data)
  112. return err
  113. }
  114. func (d *GroupRepo) IncrementGroupLeft(ocId int64 ,steps int64 ,nodeLeft int64) error {
  115. sql := fmt.Sprintf("UPDATE s_group SET node_left = node_left + %d WHERE oc_id =%d AND node_left >= %d AND deleted_flag = 0" ,steps ,ocId ,nodeLeft)
  116. _, err :=d.engine.Exec(sql)
  117. return err
  118. }
  119. func (d *GroupRepo) IncrementGroupRight(ocId int64 ,steps int64 ,nodeRight int64) error {
  120. sql := fmt.Sprintf("UPDATE s_group SET node_right = node_right + %d WHERE oc_id =%d AND node_right >= %d AND deleted_flag = 0" ,steps ,ocId ,nodeRight)
  121. _, err :=d.engine.Exec(sql)
  122. return err
  123. }
  124. func (d *GroupRepo) DecrementGroupLeft(ocId int64 ,steps int64 ,nodeLeft int64) error {
  125. sql := fmt.Sprintf("UPDATE s_group SET node_left = node_left - %d WHERE oc_id =%d AND node_left > %d AND deleted_flag = 0" ,steps ,ocId ,nodeLeft)
  126. _, err :=d.engine.Exec(sql)
  127. return err
  128. }
  129. func (d *GroupRepo) DecrementGroupRight(ocId int64 ,steps int64 ,nodeRight int64) error {
  130. sql := fmt.Sprintf("UPDATE s_group SET node_right = node_right - %d WHERE oc_id =%d AND node_right > %d AND deleted_flag = 0" ,steps ,ocId ,nodeRight)
  131. _, err :=d.engine.Exec(sql)
  132. return err
  133. }
  134. func (d *GroupRepo) BatchDelete(ocId ,nodeLeft ,nodeRight int64) error {
  135. sql := fmt.Sprintf("DELETE FROM s_group WHERE oc_id = %d AND node_left >= %d AND node_right <= %d " ,ocId ,nodeLeft ,nodeRight)
  136. _ ,err := d.engine.Exec(sql)
  137. return err
  138. }
  139. func (d *GroupRepo) CountOfChildren(ocId, groupId int64)(int64, error){
  140. count, err := d.engine.Where("oc_id = ?" ,ocId).
  141. Where("group_parent_id = ?" ,groupId).
  142. Where("deleted_flag = 0 ").
  143. Count(new(datamodels.Group))
  144. if err != nil {
  145. return 0, err
  146. }
  147. return count, nil
  148. }