group_repo.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package system
  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. ocId := m["ocId"].(int64)
  50. keywords := m["keywords"].(string)
  51. sqlSelect := ` SELECT A.oc_id,
  52. A.group_id ,
  53. A.group_name,
  54. A.group_cat_id,
  55. B.group_cat_title,
  56. A.group_code,
  57. A.group_select,
  58. A.group_path,
  59. A.group_desc,
  60. A.root_id,
  61. A.parent_id,
  62. C.group_name as parent_name,
  63. A.node_left,
  64. A.node_right,
  65. A.node_level,
  66. A.is_leaf,
  67. A.is_fixed `
  68. sqlFrom := ` FROM s_group AS A
  69. LEFT JOIN s_group_cat AS B ON (A.oc_id = B.oc_id AND A.group_cat_id = B.group_cat_id)
  70. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.parent_id = C.group_id )`
  71. sqlWhere := ` WHERE A.deleted_flag = 0 and A.oc_id = ` + fmt.Sprintf("%d" ,ocId)
  72. if keywords != "" {
  73. sqlWhere+= ` AND A.group_name like `+"'%"+keywords+"%'"
  74. }
  75. datalist := make([]viewmodels.GroupInfo, 0)
  76. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id ASC").Find(&datalist)
  77. if err != nil{
  78. return nil, err
  79. }
  80. return datalist, nil
  81. }
  82. // Delete
  83. func (d *GroupRepo) Delete(m map[string]interface{}) error {
  84. ocId := m["ocId"].(int64)
  85. groupId := m["groupId"].(int64)
  86. deletedBy := m["deletedBy"].(int64)
  87. group := &datamodels.Group{}
  88. group.OcId = ocId
  89. group.GroupId = groupId
  90. group.DeletedFlag = 1
  91. group.DeletedBy = deletedBy
  92. group.DeletedAt = time.Now()
  93. _, err := d.engine.Where("oc_id = ?", ocId).Where("group_id =?", groupId).Update(group)
  94. return err
  95. }
  96. func (d *GroupRepo) Update(data *datamodels.Group) error{
  97. ocId := data.OcId
  98. groupId := data.GroupId
  99. _, err := d.engine.Where("oc_id = ?", ocId).Where("group_id =?", groupId).Update(data)
  100. return err
  101. }
  102. func (d *GroupRepo) RefreshLeafStatus(ocId int64 ,groupId int64 ,isLeaf bool) error {
  103. var sql string
  104. if isLeaf {
  105. sql = fmt.Sprintf("UPDATE s_group SET is_leaf = 1 WHERE oc_id = %d AND group_id = %d" ,ocId ,groupId)
  106. }else {
  107. sql = fmt.Sprintf("UPDATE s_group SET is_leaf = 0 WHERE oc_id = %d AND group_id = %d" ,ocId ,groupId)
  108. }
  109. _ ,err := d.engine.Exec(sql)
  110. return err
  111. }
  112. func (d *GroupRepo) Create(data *datamodels.Group) error {
  113. _, err := d.engine.Insert(data)
  114. return err
  115. }
  116. func (d *GroupRepo) IncrementGroupLeft(ocId int64 ,steps int64 ,nodeLeft int64) error {
  117. 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)
  118. _, err :=d.engine.Exec(sql)
  119. return err
  120. }
  121. func (d *GroupRepo) IncrementGroupRight(ocId int64 ,steps int64 ,nodeRight int64) error {
  122. 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)
  123. _, err :=d.engine.Exec(sql)
  124. return err
  125. }
  126. func (d *GroupRepo) DecrementGroupLeft(ocId int64 ,steps int64 ,nodeLeft int64) error {
  127. 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)
  128. _, err :=d.engine.Exec(sql)
  129. return err
  130. }
  131. func (d *GroupRepo) DecrementGroupRight(ocId int64 ,steps int64 ,nodeRight int64) error {
  132. 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)
  133. _, err :=d.engine.Exec(sql)
  134. return err
  135. }
  136. func (d *GroupRepo) BatchDelete(ocId ,nodeLeft ,nodeRight int64) error {
  137. sql := fmt.Sprintf("DELETE FROM s_group WHERE oc_id = %d AND node_left >= %d AND node_right <= %d " ,ocId ,nodeLeft ,nodeRight)
  138. _ ,err := d.engine.Exec(sql)
  139. return err
  140. }
  141. func (d *GroupRepo) CountOfChildren(ocId, groupId int64)(int64, error){
  142. count, err := d.engine.Where("oc_id = ?" ,ocId).
  143. Where("group_parent_id = ?" ,groupId).
  144. Where("deleted_flag = 0 ").
  145. Count(new(datamodels.Group))
  146. if err != nil {
  147. return 0, err
  148. }
  149. return count, nil
  150. }