home.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package models
  2. import (
  3. "fmt"
  4. "hongze/hongze_api/utils"
  5. "rdluck_tools/orm"
  6. )
  7. type HomeList struct {
  8. ClassifyId int `orm:"column(id)"`
  9. ClassifyName string `description:"分类名称"`
  10. Child []*HomeClassifyItem
  11. }
  12. type HomeClassifyItem struct {
  13. ClassifyId int `orm:"column(id)"`
  14. ClassifyName string `description:"分类名称"`
  15. Child *Report
  16. }
  17. func ListHome(userId, maxPermission, userPermission int, permissionStr string) (items []*HomeList, err error) {
  18. o := orm.NewOrm()
  19. o.Using("rddp")
  20. sql := `SELECT id,classify_name FROM classify WHERE parent_id=0 ORDER BY create_time ASC `
  21. _, err = o.Raw(sql).QueryRows(&items)
  22. if err != nil {
  23. fmt.Println("Get classify Err:" + err.Error())
  24. return
  25. }
  26. lenItem := len(items)
  27. fmt.Println("lenItem:", lenItem)
  28. for i := 0; i < lenItem; i++ {
  29. item := items[i]
  30. subItems := make([]*HomeClassifyItem, 0)
  31. subWhere := ``
  32. if maxPermission < 15 && userPermission == 0 {
  33. if permissionStr != "" {
  34. subWhere = ` WHERE parent_id=? AND classify_name IN(` + permissionStr + `) `
  35. } else {
  36. subWhere = ` WHERE parent_id=? `
  37. }
  38. } else {
  39. subWhere = ` WHERE parent_id=? `
  40. }
  41. subSql := `SELECT id ,classify_name FROM classify `
  42. subSql += subWhere
  43. subSql += ` ORDER BY create_time ASC `
  44. _, err = o.Raw(subSql, item.ClassifyId).QueryRows(&subItems)
  45. if err != nil {
  46. fmt.Println("Sub Err:" + err.Error())
  47. return
  48. }
  49. lenSub := len(subItems)
  50. for k := 0; k < lenSub; k++ {
  51. subItem := subItems[k]
  52. reportSql := ` SELECT a.id,a.classify_name,b.*,
  53. (SELECT COUNT(1) FROM report_view_log AS c WHERE c.user_id=? AND c.report_id=b.id ) AS is_show_new_label,
  54. CASE WHEN DATE(b.modify_time)=DATE(NOW()) THEN 1 ELSE 0 END AS is_current_date
  55. FROM classify AS a
  56. INNER JOIN report AS b ON a.id=b.classify_id_second
  57. WHERE b.state=2 AND a.id=?
  58. ORDER BY b.publish_time DESC LIMIT 1 `
  59. report := new(Report)
  60. err = o.Raw(reportSql, userId, subItem.ClassifyId).QueryRow(&report)
  61. if err != nil {
  62. if err.Error() != utils.ErrNoRow() {
  63. return
  64. } else {
  65. err = nil
  66. }
  67. }
  68. subItems[k].Child = report
  69. }
  70. items[i].Child = subItems
  71. }
  72. return
  73. }