home.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package models
  2. import (
  3. "fmt"
  4. "hongze/hongze_api/utils"
  5. "rdluck_tools/orm"
  6. "rdluck_tools/paging"
  7. )
  8. type HomeList struct {
  9. ClassifyId int `orm:"column(id)"`
  10. ClassifyName string `description:"分类名称"`
  11. Child []*HomeClassifyItem
  12. TitleType string `description:"标题类型,FICC或者权益"`
  13. }
  14. type HomeClassifyItem struct {
  15. ClassifyId int `orm:"column(id)"`
  16. ClassifyName string `description:"分类名称"`
  17. Child *Report
  18. }
  19. func ListHome(userId, maxPermission, userPermission int, permissionStr string) (items []*HomeList, err error) {
  20. o := orm.NewOrm()
  21. o.Using("rddp")
  22. sql := `SELECT id,classify_name FROM classify WHERE parent_id=0 ORDER BY create_time ASC `
  23. _, err = o.Raw(sql).QueryRows(&items)
  24. if err != nil {
  25. fmt.Println("Get classify Err:" + err.Error())
  26. return
  27. }
  28. lenItem := len(items)
  29. fmt.Println("lenItem:", lenItem)
  30. for i := 0; i < lenItem; i++ {
  31. item := items[i]
  32. subItems := make([]*HomeClassifyItem, 0)
  33. subWhere := ``
  34. if maxPermission < 15 && userPermission == 0 {
  35. if permissionStr != "" {
  36. subWhere = ` WHERE parent_id=? AND classify_name IN(` + permissionStr + `) `
  37. } else {
  38. subWhere = ` WHERE parent_id=? `
  39. }
  40. } else {
  41. subWhere = ` WHERE parent_id=? `
  42. }
  43. subSql := `SELECT id ,classify_name FROM classify `
  44. subSql += subWhere
  45. subSql += ` ORDER BY create_time ASC `
  46. _, err = o.Raw(subSql, item.ClassifyId).QueryRows(&subItems)
  47. if err != nil {
  48. fmt.Println("Sub Err:" + err.Error())
  49. return
  50. }
  51. lenSub := len(subItems)
  52. for k := 0; k < lenSub; k++ {
  53. subItem := subItems[k]
  54. reportSql := ` SELECT a.id,a.classify_name,b.*,
  55. (SELECT COUNT(1) FROM report_view_log AS c WHERE c.user_id=? AND c.report_id=b.id ) AS is_show_new_label,
  56. CASE WHEN DATE(b.modify_time)=DATE(NOW()) THEN 1 ELSE 0 END AS is_current_date
  57. FROM classify AS a
  58. INNER JOIN report AS b ON a.id=b.classify_id_second
  59. WHERE b.state=2 AND a.id=?
  60. ORDER BY b.publish_time DESC LIMIT 1 `
  61. report := new(Report)
  62. err = o.Raw(reportSql, userId, subItem.ClassifyId).QueryRow(&report)
  63. if err != nil {
  64. if err.Error() != utils.ErrNoRow() {
  65. return
  66. } else {
  67. err = nil
  68. }
  69. }
  70. subItems[k].Child = report
  71. }
  72. items[i].Child = subItems
  73. }
  74. return
  75. }
  76. type PcHomeClassifyItem struct {
  77. ClassifyId int `orm:"column(id)"`
  78. ClassifyName string `description:"分类名称"`
  79. ReportInfo *PcReport
  80. }
  81. func PcListHomeCount(classifyId int) (count int, err error) {
  82. o := orm.NewOrm()
  83. o.Using("rddp")
  84. sql := `SELECT COUNT(1) AS count FROM classify WHERE parent_id=? `
  85. err = o.Raw(sql, classifyId).QueryRow(&count)
  86. return
  87. }
  88. func PcListHome(classifyId, startSize, pageSize int) (items []*PcHomeClassifyItem, err error) {
  89. o := orm.NewOrm()
  90. o.Using("rddp")
  91. subSql := ` SELECT id ,classify_name FROM classify WHERE parent_id=? `
  92. subSql += ` ORDER BY create_time ASC LIMIT ?,? `
  93. _, err = o.Raw(subSql, classifyId, startSize, pageSize).QueryRows(&items)
  94. if err != nil {
  95. fmt.Println("Sub Err:" + err.Error())
  96. return
  97. }
  98. lenSub := len(items)
  99. for k := 0; k < lenSub; k++ {
  100. subItem := items[k]
  101. reportSql := ` SELECT a.id,a.classify_name,b.*,c.*,
  102. CASE WHEN DATE(b.modify_time)=DATE(NOW()) THEN 1 ELSE 0 END AS is_current_date
  103. FROM classify AS a
  104. INNER JOIN report AS b ON a.id=b.classify_id_second
  105. INNER JOIN report_author AS c ON b.author=c.report_author
  106. WHERE b.state=2 AND a.id=?
  107. ORDER BY b.publish_time DESC LIMIT 1 `
  108. report := new(PcReport)
  109. err = o.Raw(reportSql, subItem.ClassifyId).QueryRow(&report)
  110. if err != nil {
  111. if err.Error() != utils.ErrNoRow() {
  112. return
  113. } else {
  114. err = nil
  115. }
  116. }
  117. items[k].ReportInfo = report
  118. }
  119. return
  120. }
  121. type PcListHomeResp struct {
  122. Paging *paging.PagingItem
  123. List []*PcHomeClassifyItem
  124. }