query.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package classify
  2. import (
  3. "errors"
  4. "hongze/hongze_yb/global"
  5. "hongze/hongze_yb/utils"
  6. )
  7. func GetSimpleAll() (list []*Classify, err error) { // 过滤权益研报的分类
  8. err = global.MYSQL["rddp"].Select("id, classify_name, yb_icon_url, yb_bg_url, parent_id").Model(Classify{}).Where("is_show = 1 AND parent_id != 56 AND id != 56").Scan(&list).Error
  9. return
  10. }
  11. // GetByClassifyName 根据分类名称查找专栏详情
  12. func GetByClassifyName(classifyName string) (item *Classify, err error) {
  13. err = global.MYSQL["rddp"].Model(Classify{}).Where("classify_name = ?", classifyName).First(&item).Error
  14. if err == utils.ErrNoRow {
  15. err = nil
  16. }
  17. return
  18. }
  19. // GetByClassifyNameFirst 根据一级分类名称查找一级分类
  20. func GetByClassifyNameFirst(classifyName string) (item *Classify, err error) {
  21. err = global.MYSQL["rddp"].Model(Classify{}).Where("classify_name = ? and parent_id = 0", classifyName).First(&item).Error
  22. if err == utils.ErrNoRow {
  23. err = nil
  24. }
  25. return
  26. }
  27. // GetSecondIdsByClassifyNames 根据权限相关的二级分类名称获取名称ID
  28. func GetSecondIdsByClassifyNames(names []string) (ids []int, err error) {
  29. var list []*Classify
  30. err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) and parent_id >0 AND is_show = 1 AND enabled = 1 AND parent_id != 56", names).Scan(&list).Error
  31. if err != nil {
  32. if err == utils.ErrNoRow {
  33. err = nil
  34. }
  35. return
  36. }
  37. for _, v := range list {
  38. ids = append(ids, v.Id)
  39. }
  40. return
  41. }
  42. // GetParentList 查询所有一级分类
  43. func GetParentList() (list []*Classify, err error) {
  44. err = global.MYSQL["rddp"].Model(Classify{}).
  45. Select("id, classify_name, show_type, yb_ficc_icon, yb_ficc_pc_icon, yb_ficc_sort").
  46. Where("parent_id = 0 and classify_name != '权益研报' AND is_show = 1 AND enabled = 1").
  47. Order("sort asc, id asc").Scan(&list).Error
  48. if err == utils.ErrNoRow {
  49. err = nil
  50. }
  51. return
  52. }
  53. // GetByClassifyId 根据分类ID查询分类详情
  54. func GetByClassifyId(id int) (item *Classify, err error) {
  55. err = global.MYSQL["rddp"].Model(Classify{}).Where("id = ? AND is_show = 1 AND enabled = 1", id).First(&item).Error
  56. if err == utils.ErrNoRow {
  57. err = nil
  58. }
  59. return
  60. }
  61. // GetListByPid 根据分类名称查找专栏列表
  62. func GetListByPid(pid int) (list []*Classify, err error) {
  63. err = global.MYSQL["rddp"].Model(Classify{}).
  64. Select("id, classify_name, parent_id, abstract, report_author, author_descript, home_img_url, vip_title, avatar_img_url").
  65. Where("parent_id = ? AND is_show = 1 AND enabled = 1", pid).Order("sort asc, id asc").Scan(&list).Error
  66. if err == utils.ErrNoRow {
  67. err = nil
  68. }
  69. return
  70. }
  71. // GetIdsByClassifyNameAndParentId 查询
  72. func GetIdsByClassifyNameAndParentId(names []string, parentId int) (ids []int, err error) {
  73. var list []*Classify
  74. err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) and parent_id = ? AND is_show = 1 AND enabled = 1", names, parentId).Scan(&list).Error
  75. if err != nil {
  76. return
  77. }
  78. for _, v := range list {
  79. ids = append(ids, v.Id)
  80. }
  81. return
  82. }
  83. // GetOtherIdsByClassifyNames 查询
  84. func GetOtherIdsByClassifyNames(names []string) (ids []int, err error) {
  85. var list []*Classify
  86. err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) and parent_id > 0 AND is_show = 1 AND enabled = 1", names).Scan(&list).Error
  87. if err != nil {
  88. return
  89. }
  90. for _, v := range list {
  91. ids = append(ids, v.Id)
  92. }
  93. return
  94. }
  95. // GetOtherIdsByClassifyNames 查询
  96. func GetOtherSecondIds(names []string) (ids []int, err error) {
  97. var list []*Classify
  98. err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("parent_id IN (SELECT id FROM classify WHERE classify_name IN (?) AND parent_id = 0 AND is_show = 1 AND enabled = 1)", names).Scan(&list).Error
  99. if err != nil {
  100. return
  101. }
  102. for _, v := range list {
  103. ids = append(ids, v.Id)
  104. }
  105. return
  106. }
  107. // GetChildByPid 根据一级分类查找二级分类
  108. func GetChildByPid(pid int) (list []*Classify, err error) {
  109. err = global.MYSQL["rddp"].Model(Classify{}).Where("parent_id = ? AND is_show = 1 AND enabled = 1", pid).Scan(&list).Error
  110. if err == utils.ErrNoRow {
  111. err = nil
  112. }
  113. return
  114. }
  115. // GetClassifyList 获取全部分类
  116. func GetClassifyList() (list []*Classify, err error) {
  117. err = global.MYSQL["rddp"].Model(Classify{}).Scan(&list).Error
  118. return
  119. }
  120. // GetByClassifyIdFirst
  121. // @Description: 根据一级分类id查找一级分类
  122. // @author: Roc
  123. // @datetime 2024-06-24 13:57:59
  124. // @param classifyId int
  125. // @return item *Classify
  126. // @return err error
  127. func GetByClassifyIdFirst(classifyId int) (item *Classify, err error) {
  128. err = global.MYSQL["rddp"].Model(Classify{}).Where("id = ? and parent_id = 0", classifyId).First(&item).Error
  129. if errors.Is(err, utils.ErrNoRow) {
  130. err = nil
  131. }
  132. return
  133. }
  134. // GetListByClassifyIdList
  135. // @Description: 根据ID列表获取所有的分类
  136. // @param idList
  137. // @return list
  138. // @return err
  139. func GetListByClassifyIdList(idList []int) (list []*Classify, err error) {
  140. err = global.MYSQL["rddp"].Model(Classify{}).Where("id in (?) AND is_show = 1 AND enabled = 1", idList).Find(&list).Error
  141. return
  142. }