group_repo.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package repositories
  2. import (
  3. "fmt"
  4. "time"
  5. "xorm.io/xorm"
  6. "xps/datamodel"
  7. "xps/viewmodel"
  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)(*viewmodel.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 := &viewmodel.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{})([]viewmodel.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([]viewmodel.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) GetAll(ocId int64)([]viewmodel.GroupInfo, error) {
  82. sqlSelect := ` SELECT A.oc_id,
  83. A.group_id ,
  84. A.group_name,
  85. A.group_cat_id,
  86. B.group_cat_title,
  87. A.group_code,
  88. A.group_select,
  89. A.group_path,
  90. A.group_desc,
  91. A.root_id,
  92. A.parent_id,
  93. C.group_name as parent_name,
  94. A.node_left,
  95. A.node_right,
  96. A.node_level,
  97. A.is_leaf,
  98. A.is_fixed `
  99. sqlFrom := ` FROM s_group AS A
  100. LEFT JOIN s_group_cat AS B ON (A.oc_id = B.oc_id AND A.group_cat_id = B.group_cat_id)
  101. LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.parent_id = C.group_id )`
  102. sqlWhere := ` WHERE A.deleted_flag = 0 and A.oc_id = ` + fmt.Sprintf("%d" ,ocId)
  103. datalist := make([]viewmodel.GroupInfo, 0)
  104. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id ASC").Find(&datalist)
  105. if err != nil{
  106. return nil, err
  107. }
  108. return datalist, nil
  109. }
  110. func (d *GroupRepo) Delete(m map[string]interface{}) error {
  111. ocId := m["ocId"].(int64)
  112. groupId := m["groupId"].(int64)
  113. deletedBy := m["deletedBy"].(int64)
  114. group := &datamodel.Group{}
  115. group.OcId = ocId
  116. group.GroupId = groupId
  117. group.DeletedFlag = 1
  118. group.DeletedBy = deletedBy
  119. group.DeletedAt = time.Now()
  120. _, err := d.engine.Where("oc_id = ?", ocId).Where("group_id =?", groupId).Update(group)
  121. return err
  122. }
  123. func (d *GroupRepo) Update(data *datamodel.Group) error{
  124. ocId := data.OcId
  125. groupId := data.GroupId
  126. _, err := d.engine.Where("oc_id = ?", ocId).Where("group_id =?", groupId).Update(data)
  127. return err
  128. }
  129. func (d *GroupRepo) RefreshLeafStatus(ocId int64 ,groupId int64 ,isLeaf bool) error {
  130. var sql string
  131. if isLeaf {
  132. sql = fmt.Sprintf("UPDATE s_group SET is_leaf = 1 WHERE oc_id = %d AND group_id = %d" ,ocId ,groupId)
  133. }else {
  134. sql = fmt.Sprintf("UPDATE s_group SET is_leaf = 0 WHERE oc_id = %d AND group_id = %d" ,ocId ,groupId)
  135. }
  136. _ ,err := d.engine.Exec(sql)
  137. return err
  138. }
  139. func (d *GroupRepo) Create(data *datamodel.Group) error {
  140. _, err := d.engine.Insert(data)
  141. return err
  142. }
  143. func (d *GroupRepo) IncrementGroupLeft(ocId int64 ,steps int64 ,nodeLeft int64) error {
  144. 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)
  145. _, err :=d.engine.Exec(sql)
  146. return err
  147. }
  148. func (d *GroupRepo) IncrementGroupRight(ocId int64 ,steps int64 ,nodeRight int64) error {
  149. 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)
  150. _, err :=d.engine.Exec(sql)
  151. return err
  152. }
  153. func (d *GroupRepo) DecrementGroupLeft(ocId int64 ,steps int64 ,nodeLeft int64) error {
  154. 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)
  155. _, err :=d.engine.Exec(sql)
  156. return err
  157. }
  158. func (d *GroupRepo) DecrementGroupRight(ocId int64 ,steps int64 ,nodeRight int64) error {
  159. 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)
  160. _, err :=d.engine.Exec(sql)
  161. return err
  162. }
  163. func (d *GroupRepo) BatchDelete(ocId ,nodeLeft ,nodeRight int64) error {
  164. sql := fmt.Sprintf("DELETE FROM s_group WHERE oc_id = %d AND node_left >= %d AND node_right <= %d " ,ocId ,nodeLeft ,nodeRight)
  165. _ ,err := d.engine.Exec(sql)
  166. return err
  167. }
  168. func (d *GroupRepo) CountOfChildren(ocId, groupId int64)(int64, error){
  169. count, err := d.engine.Where("oc_id = ?" ,ocId).
  170. Where("group_parent_id = ?" ,groupId).
  171. Where("deleted_flag = 0 ").
  172. Count(new(datamodel.Group))
  173. if err != nil {
  174. return 0, err
  175. }
  176. return count, nil
  177. }