示例#1
0
func (this *Article) GetCount(filter map[string]interface{}) int {
	var params []interface{}
	var maps []orm.Params
	var sql bytes.Buffer
	totalCount := 0

	categoryId := filter["categoryId"]
	keywords := filter["keywords"]

	sql.WriteString("select count(*) as totalcount from article where 1=1")

	if categoryId != "" {
		sql.WriteString(" and categoryId in (SELECT b.id FROM articlecategory a INNER JOIN articlecategory b ON a.id=? AND b.innercode LIKE CONCAT(a.innercode, '%'))")
		params = append(params, categoryId)
	}

	if keywords != "" {
		sql.WriteString(" and title like ?")
		params = append(params, utils.Concat("%", keywords.(string), "%"))
	}

	o := orm.NewOrm()
	_, err := o.Raw(sql.String(), params).Values(&maps)
	if err != nil {
		fmt.Println(err)
	} else {
		totalCount, _ = strconv.Atoi(maps[0]["totalcount"].(string))
	}

	return totalCount
}
示例#2
0
func (this *Article) GetList(pageSize int, pageNo int,
	filter map[string]interface{}) []orm.Params {
	var params []interface{}
	var sql bytes.Buffer
	var list []orm.Params
	firstResult := (pageNo - 1) * pageSize

	categoryId := filter["categoryId"]
	keywords := filter["keywords"]

	sql.WriteString("select t.id, t.title, t.categoryid, c.categoryname, t.description, t.publishtime, t.createtime, t.readnum, t.author from article t, articlecategory c where t.categoryId=c.id")

	if categoryId != "" {
		sql.WriteString(" and categoryId in (SELECT b.id FROM articlecategory a INNER JOIN articlecategory b ON a.id=? AND b.innercode LIKE CONCAT(a.innercode, '%'))")
		params = append(params, categoryId)
	}

	if keywords != "" {
		sql.WriteString(" and t.title like ?")
		params = append(params, utils.Concat("%", keywords.(string), "%"))
	}

	sql.WriteString(" order by t.id desc limit ?, ?")
	params = append(params, firstResult)
	params = append(params, pageSize)

	o := orm.NewOrm()
	_, err := o.Raw(sql.String(), params).Values(&list)
	if err != nil {
		fmt.Println(err)
	}

	return list
}