代码拉取完成,页面将自动刷新
package page
import (
"fmt"
"gitee.com/leijmdas/gobase/goconfig/common/base/baseconfig"
"gitee.com/leijmdas/gobase/goconfig/common/base/baseconsts"
"gitee.com/leijmdas/gobase/goconfig/common/base/basedto"
"gitee.com/leijmdas/gobase/goconfig/common/base/baseutils"
"gitee.com/leijmdas/gobase/goconfig/common/base/goutils"
"gitee.com/leijmdas/gobase/goconfig/common/base/jsonutils"
"gitee.com/leijmdas/gobase/goconfig/common/dbcontent/database"
"gitee.com/leijmdas/gobase/goconfig/common/gocontext"
"gitee.com/leijmdas/gobase/goconfig/common/golog"
"gitee.com/leijmdas/gobase/goconfig/common/golog/ichublog"
"gitee.com/leijmdas/gobase/goconfig/common/ichubconfig"
"gitee.com/leijmdas/goweb/commonpage/base"
"github.com/gogf/gf/v2/util/gconv"
"reflect"
"strings"
"gitee.com/leijmdas/goweb/commonpage/dto"
"github.com/duke-git/lancet/strutil"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
)
/*
@Title 文件名称: page_request.go
@Description 描述: PageRequest
@Author 作者: leijianming@163.com 时间(2024-02-21 22:38:21)
@Update 作者: leijianming@163.com 时间(2024-02-21 22:38:21)
*/
const (
SORT_TYPE_SCORE = "NewScoreSort"
)
type PageRequest struct {
basedto.BaseEntity `json:"-"`
RequestId string `json:"request_id"`
//每页记录数
PageSize int `json:"page_size"`
//当前页码
PageCurrent int `json:"current"`
//排序字段数组
OrderBys []*dto.OrderByDto `json:"order_by"`
//查询字段条件
Fields []*dto.QueryField `json:"fields"`
EsBoolType int `json:"es_bool_type"`
IfSnake bool
*baseconfig.DbClientDto `json:"-"`
}
func NewPageRequest(pageSize int, pageCurrent int) *PageRequest {
var pageRequest = &PageRequest{
PageSize: pageSize,
PageCurrent: pageCurrent,
IfSnake: true,
OrderBys: []*dto.OrderByDto{},
Fields: make([]*dto.QueryField, 0),
}
pageRequest.InitProxy(pageRequest)
return pageRequest
}
func Default() *PageRequest {
return NewPageRequest(
baseconsts.PAGE_SIZE_DEFAULT,
baseconsts.PAGE_CURRENT)
}
func (self *PageRequest) InitPage() {
if self.PageSize <= baseconsts.PAGE_SIZE_ZERO {
self.PageSize = baseconsts.PAGE_SIZE_DEFAULT
} else if self.PageSize > baseconsts.PAGE_SIZE_MAX {
self.PageSize = baseconsts.PAGE_SIZE_MAX
}
if self.PageCurrent <= 0 {
self.PageCurrent = baseconsts.PAGE_CURRENT
}
}
func (self *PageRequest) GetDB() *gorm.DB {
if self.DbClientDto == nil {
self.DbClientDto = ichubconfig.FindBeanIchubConfig().ReadIchubDb()
}
return database.FindDbClient(self.DbClientDto).Db.Debug()
}
func (self *PageRequest) Clear() {
self.PageSize = baseconsts.PAGE_SIZE_DEFAULT
self.PageCurrent = baseconsts.PAGE_CURRENT
self.OrderBys = []*dto.OrderByDto{}
self.Fields = make([]*dto.QueryField, 0)
}
func (self *PageRequest) FindFieldSign(op int) base.FieldSign {
return base.OpSign[op]
}
func (self *PageRequest) Start() int {
return (self.PageCurrent - 1) * self.PageSize
}
func (self *PageRequest) Limit() int {
return self.PageSize
}
func (self *PageRequest) keySnake(key string) string {
if self.IfSnake {
return strutil.SnakeCase(key)
}
return key
}
func (self *PageRequest) CheckType(field string, value interface{}) {
baseutils.CheckType(value)
}
func (self *PageRequest) QueryFields(f string, opType int, opValues []interface{}) *PageRequest {
var field = dto.NewFields(self.keySnake(f), opType, opValues)
self.Fields = append(self.Fields, field)
return self
}
func (self *PageRequest) Eq(field string, opValues any) *PageRequest {
return self.QueryFields(field, base.Eq, []any{opValues})
}
func (self *PageRequest) Ge(field string, opValue any) *PageRequest {
return self.QueryFields(field, base.Ge, []any{opValue})
}
func (self *PageRequest) Le(field string, opValue any) *PageRequest {
return self.QueryFields(field, base.Le, []any{opValue})
}
func (self *PageRequest) Lt(field string, opValue any) *PageRequest {
return self.QueryFields(field, base.Lt, []any{opValue})
}
func (self *PageRequest) Gt(field string, opValue any) *PageRequest {
return self.QueryFields(field, base.Gt, []any{opValue})
}
func (self *PageRequest) In(field string, opValues []any) *PageRequest {
return self.QueryFields(field, base.In, opValues)
}
func (self *PageRequest) NotIn(field string, opValues []any) *PageRequest {
return self.QueryFields(field, base.NotIn, opValues)
}
func (self *PageRequest) IsNull(field string) *PageRequest {
return self.QueryFields(field, base.IsNull, []any{})
}
func (self *PageRequest) IsNotNull(field string) *PageRequest {
return self.QueryFields(field, base.IsNotNull, []any{})
}
func (self *PageRequest) Like(field string, opValue any) *PageRequest {
return self.QueryFields(field, base.Like, []any{opValue})
}
func (self *PageRequest) NotLike(field string, opValue any) *PageRequest {
return self.QueryFields(field, base.NotLike, []any{opValue})
}
func (self *PageRequest) Between(field string, opValues []any) *PageRequest {
return self.QueryFields(field, base.Between, opValues)
}
func (self *PageRequest) NotBetween(field string, opValues []any) *PageRequest {
return self.QueryFields(field, base.NotBetween, opValues)
}
func (self *PageRequest) OrderBy(field string, sort string) *PageRequest {
var orderby = dto.OrderBy(field, sort)
self.OrderBys = append(self.OrderBys, orderby)
return self
}
func (self *PageRequest) OrderByDesc(field string) *PageRequest {
return self.OrderBy(field, "desc")
}
func (self *PageRequest) OrderByAsc(field string) *PageRequest {
return self.OrderBy(field, "asc")
}
func (this *PageRequest) OrderByAscScore(field string) *PageRequest {
return this.OrderBySortType(field, "asc", SORT_TYPE_SCORE)
}
func (this *PageRequest) OrderByDescSort(sortType string, field string) *PageRequest {
return this.OrderBySortType(field, "desc", sortType)
}
func (this *PageRequest) OrderBySortType(field string, sort string, sortType string) *PageRequest {
var orderby = dto.OrderBy(field, sort)
orderby.SortType = sortType
this.OrderBys = append(this.OrderBys, orderby)
return this
}
func (self *PageRequest) ToMap() (*map[string]interface{}, error) {
var str = jsonutils.ToJsonPretty(self)
m, err := jsonutils.MapFromJson(str)
if err != nil {
logrus.Error(err)
}
return m, err
}
//update
func (self *PageRequest) FindByTable(table string, result any) error {
dbc := self.GetDB().Table(table)
dbc = self.BuildWhere(dbc)
dbc = self.Order(dbc)
dbc = self.SetLimit(dbc)
dbc = dbc.Find(result)
return dbc.Error
}
func (self *PageRequest) Insert(model any) (any, error) {
err := self.GetDB().Create(model).Error
if err != nil {
logrus.Error(err.Error())
}
return model, err
}
func (self *PageRequest) Update(model any, pkey int64) (any, error) {
err := self.GetDB().Model(model).Where("rule_id=?", pkey).Updates(model).Error
if err != nil {
logrus.Error(err.Error())
return -1, err
}
return model, err
}
func (self *PageRequest) FindBy(model any, result any) error {
dbc := self.GetDB().Model(model)
dbc = self.BuildWhere(dbc)
dbc = self.Order(dbc)
dbc = self.SetLimit(dbc)
dbc = dbc.Find(result)
return dbc.Error
}
func (self *PageRequest) TransOpType(field *dto.QueryField) {
if field.OpType == "notin" {
field.OpType = "not in"
}
if field.OpType == "notlike" {
field.OpType = "not like"
}
if field.OpType == "notbetween" {
field.OpType = "not between"
}
}
func (self *PageRequest) BuildWhere(db *gorm.DB) *gorm.DB {
self.InitPage()
if self.Fields == nil || len(self.Fields) == 0 {
return db
}
for _, field := range self.Fields {
self.TransOpType(field)
db = self.BuildOne(db, field)
}
return db
}
func (self *PageRequest) BuildOne(db *gorm.DB, field *dto.QueryField) *gorm.DB {
switch field.OpType {
case base.OpSign[base.Between]:
db = db.Where(fmt.Sprintf("%s BETWEEN ? AND ?", field.Field), field.Values[0], field.Values[1])
case base.OpSign[base.NotBetween]:
db = db.Where(fmt.Sprintf("%s NOT BETWEEN ? AND ?", field.Field), field.Values[0], field.Values[1])
case base.OpSign[base.Ge]:
db = db.Where(fmt.Sprintf("%s >= ?", field.Field), field.Values[0])
case base.OpSign[base.Gt]:
db = db.Where(fmt.Sprintf("%s > ?", field.Field), field.Values[0])
case base.OpSign[base.Le]:
db = db.Where(fmt.Sprintf("%s <= ?", field.Field), field.Values[0])
case base.OpSign[base.Lt]:
db = db.Where(fmt.Sprintf("%s < ?", field.Field), field.Values[0])
case base.OpSign[base.Eq]:
db = db.Where(fmt.Sprintf("%s = ?", field.Field), field.Values[0])
case base.OpSign[base.Ne]:
db = db.Where(fmt.Sprintf("%s != ?", field.Field), field.Values[0])
case base.OpSign[base.In]:
valuesStr := field.Values2InStr()
db = db.Where(fmt.Sprintf("%s IN (?)", field.Field), valuesStr)
case base.OpSign[base.NotIn]:
valuesStr := field.Values2InStr()
db = db.Where(fmt.Sprintf("%s NOT IN (?)", field.Field), valuesStr)
case base.OpSign[base.Like]:
var sval = gconv.String(field.Values[0])
db = db.Where(fmt.Sprintf("%s LIKE ?", field.Field), "%"+sval+"%")
case base.OpSign[base.NotLike]:
var sval = gconv.String(field.Values[0])
db = db.Where(fmt.Sprintf("%s NOT LIKE ?", field.Field), "%"+sval+"%")
case base.OpSign[base.IsNull]:
db = db.Where(fmt.Sprintf("%s IS NULL", field.Field))
case base.OpSign[base.IsNotNull]:
db = db.Where(fmt.Sprintf("%s IS NOT NULL", field.Field))
}
return db
}
//func (this *PageRequest) BuildWhere(db *gorm.DB) *gorm.DB {
//
// this.InitPage()
// if this.Fields == nil {
// return db
// }
// for _, field := range this.Fields {
// this.TransOpType(field)
// switch field.OpType {
// //if notbetween noin notlike
// case base.OpSign[base.Between]:
// db = db.Where(fmt.Sprintf("%s BETWEEN ? and ?", field.Field),
// field.Values[0], field.Values[1])
// case base.OpSign[base.NotBetween]:
// db = db.Where(fmt.Sprintf("%s Not BETWEEN ? and ?", field.Field),
// field.Values[0], field.Values[1])
// case base.OpSign[base.Ge]:
// db = db.Where(fmt.Sprintf("%s >= ?", field.Field), field.Values[0])
//
// case base.OpSign[base.Gt]:
// db = db.Where(fmt.Sprintf("%s > ?", field.Field), field.Values[0])
//
// case base.OpSign[base.Le]:
// db = db.Where(fmt.Sprintf("%s <= ?", field.Field), field.Values[0])
//
// case base.OpSign[base.Lt]:
// db = db.Where(fmt.Sprintf("%s < ?", field.Field), field.Values[0])
//
// case base.OpSign[base.Eq]:
// db = db.Where(fmt.Sprintf("%s = ?", field.Field), field.Values[0])
//
// case base.OpSign[base.Ne]:
// db = db.Where(fmt.Sprintf("%s != ?", field.Field), field.Values[0])
//
// case base.OpSign[base.In]:
//
// db = db.Where(fmt.Sprintf("%s in (%s)", field.Field, field.Values2InStr()))
//
// case base.OpSign[base.NotIn]:
// db = db.Where(fmt.Sprintf("%s not in (%s)", field.Field, field.Values2InStr()))
//
// case base.OpSign[base.Like]:
// var sval = gconv.String(field.Values[0])
// db = db.Where(fmt.Sprintf("%s like ?", field.Field), "'%"+sval+"%'")
//
// case base.OpSign[base.NotLike]:
// var sval = gconv.String(field.Values[0])
// db = db.Where(fmt.Sprintf("%s not like ?", field.Field), "'%"+sval+"%'")
//
// case base.OpSign[base.IsNull]:
//
// db = db.Where(fmt.Sprintf("%s is null", field.Field))
// case base.OpSign[base.IsNotNull]:
//
// db = db.Where(fmt.Sprintf("%s is not null", field.Field))
//
// }
//
// }
//
// return db
//}
func (self *PageRequest) SetLimit(dbc *gorm.DB) *gorm.DB {
self.InitPage()
return dbc.Offset(self.Start()).Limit(self.Limit())
}
func (self *PageRequest) Order(dbc *gorm.DB) *gorm.DB {
if len(self.OrderBys) > 0 {
for _, orderBy := range self.OrderBys {
dbc = dbc.Order(orderBy.ToOrderBy())
}
}
return dbc
}
func (self *PageRequest) Count(model any) (int, error) {
dbc := self.GetDB().Model(model)
dbc = self.BuildWhere(dbc).Offset(0).Limit(1)
var count int
if err := dbc.Count(&count).Error; err != nil {
logrus.Error(err)
return 0, err
}
logrus.Info("\ncount=", count)
return count, nil
}
func (self *PageRequest) CountTable(table string) (int, error) {
dbc := self.GetDB().Table(table)
dbc = self.BuildWhere(dbc).Offset(0).Limit(1)
var count int
if err := dbc.Count(&count).Error; err != nil {
logrus.Error(err)
return 0, err
}
logrus.Info("\ncount=", count)
return count, nil
}
func (self *PageRequest) QueryTable(table string, models any) *PageResult {
self.InitPage()
//self.WriteDaofile(models, self)
count, err := self.CountTable(table)
if err != nil {
logrus.Error(err)
return NewPageResultError(err.Error())
}
if count > 0 {
err = self.FindByTable(table, models)
if err != nil {
logrus.Error(err)
return NewPageResultError(err.Error())
}
}
var result = PageResultOf(self)
result.Total = count
result.Data = models
//self.WriteDaofile(models, result)
return result
}
func (self *PageRequest) Query(entity any, entities any) *PageResult {
self.InitPage()
if err := self.WriteDaofile(entity, self); err != nil {
goutils.Error(err)
// return NewPageResultError(err.Error())
}
count, err := self.Count(entity)
if err != nil {
logrus.Error(err)
return NewPageResultError(err.Error())
}
if count > 0 {
err = self.FindBy(entity, entities)
if err != nil {
logrus.Error(err)
return NewPageResultError(err.Error())
}
}
var result = PageResultOf(self)
result.Total = count
result.Data = entities
//self.WriteDaofile(entity, result)
return result
}
func (self *PageRequest) WriteDaofile(model any, param any) error {
var paramname = baseutils.NameOfType(param)
var fileName = baseutils.NameOfType(model) + paramname + ".json"
fileName = strings.ReplaceAll(fileName, "*", "")
var err = gocontext.FindBeanGoCommonContext().WriteDaoFile(fileName, jsonutils.ToJsonPretty(param))
ichublog.Info(param)
return err
}
func (self *PageRequest) FindById(model any, key int64) (bool, error) {
db := self.GetDB().First(model, key)
return db.RecordNotFound(), db.Error
}
func (self *PageRequest) SetPageSize(size int) *PageRequest {
self.PageSize = size
return self
}
func (self *PageRequest) SetPageCurrent(current int) *PageRequest {
self.PageCurrent = current
return self
}
func (self *PageRequest) CreateDynacStru(table string) reflect.Value {
dbFields := []reflect.StructField{
{
Name: "Id",
Type: reflect.TypeOf(int64(0)), // string 类型的字段
},
{
Name: "Name",
Type: reflect.TypeOf(""), // int 类型的字段
},
}
dbtableType := reflect.StructOf(dbFields)
dbentity := reflect.New(dbtableType).Elem()
return dbentity
}
func (self *PageRequest) TakeTable(table string, model interface{}) *PageResult {
///fileName := table + "_pagerequest.json"
self.InitPage()
count, err := self.CountTable(table)
if err != nil {
golog.Error(err)
return ResultFailedPageResultErr(err)
}
if count > 0 {
err = self.FindTakeTable(table, model)
if err != nil {
logrus.Error(err)
return NewPageResultError(err.Error())
}
}
var result = PageResultOf(self)
result.Total = count
result.Data = model
return result
}
func (self *PageRequest) FindTakeTable(table string, result interface{}) error {
dbc := self.GetDB().Table(table) //.Select("id,name")
dbc = self.BuildWhere(dbc)
dbc = self.Order(dbc)
dbc = self.SetLimit(dbc)
dbc = dbc.Take(result)
return dbc.Error
}
func (self *PageRequest) IfOrderBys() bool {
return len(self.OrderBys) > 0
}
func (self *PageRequest) IfExistsWhere() bool {
self.InitPage()
return self.Fields != nil && len(self.Fields) > 0
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。