|
|
@@ -6,6 +6,8 @@ import (
|
|
|
"encoding/hex"
|
|
|
"git.qianqiusoft.com/qianqiusoft/go-uuid/uuid"
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
|
|
|
+ "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
|
|
|
+ "github.com/xormplus/xorm"
|
|
|
"net/url"
|
|
|
"strings"
|
|
|
)
|
|
|
@@ -50,3 +52,55 @@ func FormatForBrowse(agent, val string) string {
|
|
|
}
|
|
|
return val
|
|
|
}
|
|
|
+
|
|
|
+func PageQuery(db *xorm.Engine, param models.PageQueryParam, rowsSlicePtr interface{})(*models.PageResult, error){
|
|
|
+ pageResult := models.PageResult{}
|
|
|
+
|
|
|
+ dbSession := db.Table(param.TableName)
|
|
|
+ if len(param.SelectFields) > 0{
|
|
|
+ dbSession = dbSession.Select(param.TableName + ".*")
|
|
|
+ }else{
|
|
|
+ dbSession = dbSession.Select(strings.Join(param.SelectFields, ","))
|
|
|
+ }
|
|
|
+
|
|
|
+ count := 0
|
|
|
+ countSession := db.Table(param.TableName)
|
|
|
+ countSession.Select("count(*) as records")
|
|
|
+
|
|
|
+ for _, join := range param.JoinTables {
|
|
|
+ dbSession = dbSession.Join(join.JoinOperator, join.TabelName, join.Condition)
|
|
|
+ }
|
|
|
+
|
|
|
+ whereStr, datas, err := param.PageInfo.Filter.Parse("")
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ dbSession = dbSession.Where(whereStr, datas...)
|
|
|
+ countSession = countSession.Where(whereStr, datas...)
|
|
|
+
|
|
|
+ sidx := strings.Split(param.PageInfo.Sidx, ",")
|
|
|
+ for _, order := range sidx {
|
|
|
+ order = strings.TrimSpace(order)
|
|
|
+ if len(order) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ if strings.ToLower(param.PageInfo.Sord) == "desc" {
|
|
|
+ dbSession = dbSession.OrderBy(order + " desc")
|
|
|
+ } else {
|
|
|
+ dbSession = dbSession.OrderBy(order + " asc")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ err = dbSession.Limit(param.PageInfo.PageSize, (param.PageInfo.PageNum-1)*param.PageInfo.PageSize).Find(rowsSlicePtr)
|
|
|
+ _, err = countSession.Get(&count)
|
|
|
+
|
|
|
+ pageResult.Content = rowsSlicePtr
|
|
|
+ pageResult.TotalSize = count
|
|
|
+ pageResult.PageNum = param.PageInfo.PageNum
|
|
|
+ pageResult.PageSize = param.PageInfo.PageSize
|
|
|
+
|
|
|
+ return &pageResult, nil
|
|
|
+}
|