package dal import ( "fmt" "xiaoniaokuaiyan.com/xiaoniao/util" ) type SortParam struct { SortBy string SortDir int } type ListResult struct { Totoal int `json:"total"` PageIndex int `json:"pi"` PageSize int `json:"ps"` Data interface{} `json:"data"` } func List(result interface{}, tableName, strSql, orderStr, whereStr string, whereValue []interface{}, pinfo Pager) (*ListResult, error) { count, err := Count("select count(*) as total from "+tableName+" "+whereStr, whereValue) if err != nil { return nil, err } if count == 0 { return &ListResult{ Totoal: 0, PageIndex: pinfo.PageIndex, PageSize: pinfo.PageSize, }, nil } db := util.GetSqlDB() if pinfo.PageIndex < 1 { pinfo.PageIndex = 1 } if pinfo.PageSize == 0 { pinfo.PageSize = 10 } strSql += " " + whereStr + " " + orderStr strSql += fmt.Sprintf(" limit %d,%d ", (pinfo.PageIndex-1)*pinfo.PageSize, pinfo.PageSize) //rows, err := db.Queryx(strSql, whereValue...) err = db.Select(result, strSql, whereValue...) if err != nil { return nil, err } /*err = util.ScanStructOld(rows.Rows, result) if err != nil { return nil, err }*/ return &ListResult{ Totoal: count, PageSize: pinfo.PageSize, PageIndex: pinfo.PageIndex, Data: result, }, nil } func Count(strSql string, params []interface{}) (int, error) { db := util.GetSqlDB() //rows, err := db.NamedQuery(strSql, params) rows, err := db.Queryx(strSql, params...) if err != nil { return 0, err } var count int for rows.Rows.Next() { rows.Rows.Scan(&count) } return count, nil }