home.go 2.0 KB

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