package models import ( "fmt" "hongze/hongze_api/utils" "rdluck_tools/orm" ) type HomeList struct { ClassifyId int `orm:"column(id)"` ClassifyName string `description:"分类名称"` Child []*HomeClassifyItem TitleType string `description:"标题类型,FICC或者权益"` } type HomeClassifyItem struct { ClassifyId int `orm:"column(id)"` ClassifyName string `description:"分类名称"` Child *Report } func ListHome(userId, maxPermission, userPermission int, permissionStr string) (items []*HomeList, err error) { o := orm.NewOrm() o.Using("rddp") sql := `SELECT id,classify_name FROM classify WHERE parent_id=0 ORDER BY create_time ASC ` _, err = o.Raw(sql).QueryRows(&items) if err != nil { fmt.Println("Get classify Err:" + err.Error()) return } lenItem := len(items) fmt.Println("lenItem:", lenItem) for i := 0; i < lenItem; i++ { item := items[i] subItems := make([]*HomeClassifyItem, 0) subWhere := `` if maxPermission < 15 && userPermission == 0 { if permissionStr != "" { subWhere = ` WHERE parent_id=? AND classify_name IN(` + permissionStr + `) ` } else { subWhere = ` WHERE parent_id=? ` } } else { subWhere = ` WHERE parent_id=? ` } subSql := `SELECT id ,classify_name FROM classify ` subSql += subWhere subSql += ` ORDER BY create_time ASC ` _, err = o.Raw(subSql, item.ClassifyId).QueryRows(&subItems) if err != nil { fmt.Println("Sub Err:" + err.Error()) return } lenSub := len(subItems) for k := 0; k < lenSub; k++ { subItem := subItems[k] reportSql := ` SELECT a.id,a.classify_name,b.*, (SELECT COUNT(1) FROM report_view_log AS c WHERE c.user_id=? AND c.report_id=b.id ) AS is_show_new_label, CASE WHEN DATE(b.modify_time)=DATE(NOW()) THEN 1 ELSE 0 END AS is_current_date FROM classify AS a INNER JOIN report AS b ON a.id=b.classify_id_second WHERE b.state=2 AND a.id=? ORDER BY b.publish_time DESC LIMIT 1 ` report := new(Report) err = o.Raw(reportSql, userId, subItem.ClassifyId).QueryRow(&report) if err != nil { if err.Error() != utils.ErrNoRow() { return } else { err = nil } } subItems[k].Child = report } items[i].Child = subItems } return }