Explorar el Código

operationHis分页

longan hace 2 años
padre
commit
2816a02880

+ 5 - 3
app/system/controller/handleHistoryController.go

@@ -6,6 +6,7 @@ import (
 	"iot_manager_service/app/system/model"
 	service2 "iot_manager_service/app/system/service"
 	"iot_manager_service/util/common"
+	"math"
 	"net/http"
 	"strconv"
 )
@@ -30,17 +31,18 @@ func (c *handleHistoryCtl) List(ctx *gin.Context) {
 	if size <= 0 || size > 100 {
 		size = 10
 	}
-	list, err := service2.OperationHisService.List(claims.TenantId, handleContent, operationType, moduleType, current,
+	list, counts, err := service2.OperationHisService.List(claims.TenantId, handleContent, operationType, moduleType, current,
 		size)
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
 		return
 	}
-	pages := 20 //math.Ceil(float64(len(list)) / float64(size))
+
+	pages := int(math.Ceil(float64(counts) / float64(size))) //math.Ceil(float64(len(list)) / float64(size))
 	rsp := model.RsqOperationHisList{
 		Current: current,
 		Size:    size,
-		Total:   100,
+		Total:   counts,
 		Pages:   pages,
 		Records: list,
 	}

+ 4 - 3
app/system/dao/operationHisDao.go

@@ -25,8 +25,9 @@ func (c *OperationHistory) Create() error {
 	return Db.Debug().Model(&c).Save(&c).Error
 }
 
-func (c OperationHistory) GetHistories(offset, limit int) ([]OperationHistory, error) {
+func (c OperationHistory) GetHistories(offset, limit int) ([]OperationHistory, int64, error) {
 	var list []OperationHistory
+	var counts int64
 	db := Db.Debug().Model(&c)
 	if c.OperationType > 0 {
 		db = db.Where("operation_type = ?", c.OperationType)
@@ -39,6 +40,6 @@ func (c OperationHistory) GetHistories(offset, limit int) ([]OperationHistory, e
 	}
 
 	err := db.Offset(offset).Limit(limit).Find(&list).Error
-
-	return list, err
+	db.Count(&counts)
+	return list, counts, err
 }

+ 1 - 1
app/system/model/OperationHis.go

@@ -17,5 +17,5 @@ type RsqOperationHisList struct {
 	Current int                  `json:"current"` //当前分页
 	Size    int                  `json:"size"`    //每页数量
 	Pages   int                  `json:"pages"`   //总页数
-	Total   int                  `json:"total"`   //总数
+	Total   int64                `json:"total"`   //总数
 }

+ 6 - 6
app/system/service/operationHisService.go

@@ -33,30 +33,30 @@ func (s *operationHisService) Save(userId int64, tenantId int, handleType, handl
 }
 
 func (s *operationHisService) List(tenantId int, handleContent, operationType, moduleType string, current,
-	size int) ([]model.OperationHisDetail, *common.Errors) {
+	size int) ([]model.OperationHisDetail, int64, *common.Errors) {
 	his := dao.OperationHistory{
 		Object: handleContent,
 	}
 	if operationType != "" {
 		operation, err := strconv.Atoi(operationType)
 		if err != nil {
-			return nil, common.FailResponse(err.Error(), nil)
+			return nil, 0, common.FailResponse(err.Error(), nil)
 		}
 		his.OperationType = operation
 	}
 	if moduleType != "" {
 		module, err := strconv.Atoi(moduleType)
 		if err != nil {
-			return nil, common.FailResponse(err.Error(), nil)
+			return nil, 0, common.FailResponse(err.Error(), nil)
 		}
 		his.ModuleType = module
 	}
 
 	offset := (current - 1) * size
 	limit := size
-	list, err := his.GetHistories(offset, limit)
+	list, counts, err := his.GetHistories(offset, limit)
 	if err != nil {
-		return nil, common.FailResponse(err.Error(), nil)
+		return nil, 0, common.FailResponse(err.Error(), nil)
 	}
 	var details []model.OperationHisDetail
 	for _, his := range list {
@@ -68,5 +68,5 @@ func (s *operationHisService) List(tenantId int, handleContent, operationType, m
 		detail.ModuleTypeName = DictService.GetModuleName(tenantId, his.ModuleType)
 		details = append(details, detail)
 	}
-	return details, nil
+	return details, counts, nil
 }