doc_dir_repo.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package repository
  2. import (
  3. "fmt"
  4. "time"
  5. "xorm.io/xorm"
  6. "xps/pkg/base"
  7. "xps/pkg/system/datamodel"
  8. "xps/pkg/system/viewmodel"
  9. )
  10. type DocDirRepo struct {
  11. engine *xorm.Engine
  12. }
  13. func NewDocDirRepo(engine *xorm.Engine) *DocDirRepo {
  14. return &DocDirRepo{
  15. engine: engine,
  16. }
  17. }
  18. func (d *DocDirRepo) GetPage(m map[string]interface{}) (*base.PageResult, error) {
  19. limit := m["limit"].(int)
  20. page := m["page"].(int)
  21. sqlSelect := ` SELECT A.oc_id,
  22. A.dir_id,
  23. A.dir_title,
  24. A.dir_desc,
  25. A.parent_id,
  26. A.is_fixed,
  27. A.node_left,
  28. A.node_right,
  29. A.node_level,
  30. A.is_leaf `
  31. sqlFrom := ` FROM doc_dir AS A `
  32. sqlWhere := ` WHERE A.deleted_flag = 0 `
  33. if ocId, ok := m["ocId"]; ok {
  34. sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
  35. }
  36. if dirId, ok := m["dirId"]; ok {
  37. sqlWhere += fmt.Sprintf(" AND A.dir_id = %d ", dirId)
  38. }
  39. if keywords, ok := m["keywords"]; ok {
  40. sqlWhere += " AND A.dir_title like '%" + fmt.Sprintf("%s", keywords) + "%' "
  41. }
  42. sqlCount := ` SELECT COUNT(*) `
  43. total, err1 := d.engine.SQL(sqlCount + sqlFrom + sqlWhere).Count(new(datamodel.DocDir))
  44. if err1 != nil {
  45. return nil, err1
  46. }
  47. sqlOrderBy := " ORDER BY A.oc_id, A.dir_id "
  48. pageStart := (page - 1) * limit
  49. sqlLimit := fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, pageStart)
  50. datalist := make([]datamodel.DocDir, 0)
  51. err2 := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy + sqlLimit).Find(&datalist)
  52. if err2 != nil {
  53. return nil, err2
  54. }
  55. pageResult := &base.PageResult{}
  56. pageResult.Data = datalist
  57. pageResult.Total = total
  58. pageResult.PageSize = limit
  59. pageResult.Page = page
  60. return pageResult, nil
  61. }
  62. func (d *DocDirRepo) GetList(m map[string]interface{}) ([]viewmodel.DocDirInfo, error) {
  63. sqlSelect := ` SELECT A.oc_id,
  64. A.dir_id,
  65. A.dir_title,
  66. A.dir_desc,
  67. A.parent_id,
  68. A.is_fixed,
  69. A.node_left,
  70. A.node_right,
  71. A.node_level,
  72. A.is_leaf `
  73. sqlFrom := ` FROM doc_dir AS A `
  74. sqlWhere := ` WHERE A.deleted_flag = 0 `
  75. if ocId, ok := m["ocId"]; ok {
  76. sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
  77. }
  78. if dirId, ok := m["dirId"]; ok {
  79. sqlWhere += fmt.Sprintf(" AND A.dir_id = %d ", dirId)
  80. }
  81. nodeLeft, ok1 := m["nodeLeft"]
  82. nodeRight, ok2 := m["nodeRight"]
  83. if ok1 && ok2 {
  84. sqlWhere += fmt.Sprintf(" AND A.node_left > %d", nodeLeft)
  85. sqlWhere += fmt.Sprintf(" AND A.node_right < %d", nodeRight)
  86. }
  87. if keywords, ok := m["keywords"]; ok {
  88. sqlWhere += " AND A.dir_title like '%" + fmt.Sprintf("%s", keywords) + "%' "
  89. }
  90. sqlOrderBy := " ORDER BY A.oc_id, A.dir_id "
  91. datalist := make([]viewmodel.DocDirInfo, 0)
  92. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy).Find(&datalist)
  93. if err != nil {
  94. return nil, err
  95. } else {
  96. return datalist, nil
  97. }
  98. }
  99. func (d *DocDirRepo) GetById(ocId, artCatId int64) (*viewmodel.DocDirInfo, error) {
  100. sqlSelect := ` SELECT A.oc_id,
  101. A.dir_id,
  102. A.dir_title,
  103. A.dir_desc,
  104. A.parent_id,
  105. A.is_fixed,
  106. A.node_left,
  107. A.node_right,
  108. A.node_level,
  109. A.is_leaf `
  110. sqlFrom := ` FROM doc_dir AS A `
  111. sqlWhere := ` WHERE A.deleted_flag = 0 `
  112. sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
  113. sqlWhere += fmt.Sprintf(" AND A.dir_id = %d ", artCatId)
  114. data := &viewmodel.DocDirInfo{}
  115. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data)
  116. if !has || err != nil {
  117. return nil, err
  118. } else {
  119. return data, nil
  120. }
  121. }
  122. func (d *DocDirRepo) Create(data *datamodel.DocDir) error {
  123. data.CreatedAt = time.Now()
  124. _, err := d.engine.Insert(data)
  125. return err
  126. }
  127. func (d *DocDirRepo) Update(data *datamodel.DocDir) error {
  128. ocId := data.OcId
  129. dirId := data.DirId
  130. data.UpdatedAt = time.Now()
  131. _, err := d.engine.
  132. Where("oc_id = ?", ocId).
  133. Where("dir_id = ?", dirId).
  134. Update(data)
  135. return err
  136. }
  137. func (d *DocDirRepo) Delete(m map[string]interface{}) error {
  138. ocId := m["ocId"].(int64)
  139. artCatId := m["artCatId"].(int64)
  140. deletedBy := m["deletedBy"].(int64)
  141. data := &datamodel.DocDir{}
  142. data.DeletedFlag = 1
  143. data.DeletedBy = deletedBy
  144. data.DeletedAt = time.Now()
  145. _, err := d.engine.
  146. Where("oc_id = ?", ocId).
  147. Where("dir_id = ?", artCatId).
  148. Update(data)
  149. return err
  150. }
  151. func (d *DocDirRepo) CountByParent(ocId, parentId int64) (int64, error) {
  152. docDir := &datamodel.DocDir{}
  153. return d.engine.Where("oc_id = %d", ocId).
  154. Where("parent_id = %d", parentId).
  155. Where("deleted_flag = %d", 0).
  156. Count(docDir)
  157. }
  158. func (d *DocDirRepo) IncrementLeft(ocId int64, steps int64, nodeLeft int64) error {
  159. sql := fmt.Sprintf("UPDATE doc_dir SET node_left = node_left + %d WHERE oc_id =%d AND node_left >= %d AND deleted_flag = 0", steps, ocId, nodeLeft)
  160. _, err := d.engine.Exec(sql)
  161. return err
  162. }
  163. func (d *DocDirRepo) IncrementRight(ocId int64, steps int64, nodeRight int64) error {
  164. sql := fmt.Sprintf("UPDATE doc_dir SET node_right = node_right + %d WHERE oc_id =%d AND node_right >= %d AND deleted_flag = 0", steps, ocId, nodeRight)
  165. _, err := d.engine.Exec(sql)
  166. return err
  167. }
  168. func (d *DocDirRepo) DecrementLeft(ocId int64, steps int64, nodeLeft int64) error {
  169. sql := fmt.Sprintf("UPDATE doc_dir SET node_left = node_left - %d WHERE oc_id =%d AND node_left > %d AND deleted_flag = 0", steps, ocId, nodeLeft)
  170. _, err := d.engine.Exec(sql)
  171. return err
  172. }
  173. func (d *DocDirRepo) DecrementRight(ocId int64, steps int64, nodeRight int64) error {
  174. sql := fmt.Sprintf("UPDATE doc_dir SET node_right = node_right - %d WHERE oc_id =%d AND node_right > %d AND deleted_flag = 0", steps, ocId, nodeRight)
  175. _, err := d.engine.Exec(sql)
  176. return err
  177. }
  178. func (d *DocDirRepo) BatchDelete(ocId, nodeLeft, nodeRight int64) error {
  179. sql := fmt.Sprintf("DELETE FROM doc_dir WHERE oc_id = %d AND node_left >= %d AND node_right <= %d ", ocId, nodeLeft, nodeRight)
  180. _, err := d.engine.Exec(sql)
  181. return err
  182. }