根据条件查询用户信息,比如用户登录、用户列表等的获取也可以使用这个函数 @param p int 页码 @param listRows int 每页显示记录数 @param orderby string 排序,如"id desc" @param fields []string 需要查询的字段 @param cond string 查询条件 @param
(p, listRows int, orderby, fields, cond string, args ...interface{})
| 55 | //@param cond string 查询条件 |
| 56 | //@param args ...interface{} 查询条件参数 |
| 57 | func (u *User) UserList(p, listRows int, orderby, fields, cond string, args ...interface{}) (params []orm.Params, totalRows int, err error) { |
| 58 | o := orm.NewOrm() |
| 59 | if len(orderby) == 0 || orderby == "" { |
| 60 | orderby = "i.Id desc" |
| 61 | } |
| 62 | if len(fields) == 0 { |
| 63 | fields = "*" |
| 64 | } |
| 65 | if len(cond) > 0 { |
| 66 | cond = "where " + cond |
| 67 | } |
| 68 | |
| 69 | sqlCount := fmt.Sprintf("select count(i.Id) cnt from %v u left join %v i on u.Id=i.Id %v limit 1", |
| 70 | GetTableUser(), GetTableUserInfo(), cond, |
| 71 | ) |
| 72 | var one []orm.Params |
| 73 | if rows, err := o.Raw(sqlCount, args...).Values(&one); err == nil && rows > 0 { |
| 74 | totalRows = helper.Interface2Int(one[0]["cnt"]) |
| 75 | } |
| 76 | |
| 77 | sql := fmt.Sprintf("select %v from %v u left join %v i on u.Id=i.Id %v order by %v limit %v offset %v", |
| 78 | fields, GetTableUser(), GetTableUserInfo(), cond, orderby, listRows, (p-1)*listRows, |
| 79 | ) |
| 80 | _, err = o.Raw(sql, args...).Values(¶ms) |
| 81 | return params, totalRows, err |
| 82 | } |
| 83 | |
| 84 | //获取User表的字段 |
| 85 | func (u *User) Fields() map[string]string { |