Browse Source

用户图表列表-分页

hsun 2 years ago
parent
commit
09482631fe

+ 15 - 2
controller/my_chart/my_chart.go

@@ -9,6 +9,7 @@ import (
 	chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
 	"hongze/hongze_yb/models/tables/yb_config"
 	"hongze/hongze_yb/models/tables/yb_my_chart"
+	"hongze/hongze_yb/services"
 	userService "hongze/hongze_yb/services/user"
 	"hongze/hongze_yb/utils"
 	"strconv"
@@ -36,9 +37,17 @@ func (this *MyChartController) List(c *gin.Context) {
 		cond += ` AND my_chart_classify_id = ?`
 		pars = append(pars, req.ClassifyId)
 	}
+	pageIndex := services.GetCurrPageByClaims(c)
+	pageSize := services.GetPageSizeByClaims(c)
 
 	ob := new(yb_my_chart.YbMyChart)
-	list, e := ob.List(cond, pars)
+	chartTotal, e := ob.Count(cond, pars)
+	if e != nil {
+		response.FailMsg("获取失败", "获取用户图表列表总数失败, Err:"+e.Error(), c)
+		return
+	}
+	total := int(chartTotal)
+	list, e := ob.PageList(cond, pars, pageIndex, pageSize)
 	if e != nil {
 		response.FailMsg("获取失败", "获取用户图表列表失败, Err:"+e.Error(), c)
 		return
@@ -58,7 +67,11 @@ func (this *MyChartController) List(c *gin.Context) {
 			CreateTime:        utils.TimeTransferString(utils.FormatDateTime, list[i].CreateTime),
 		})
 	}
-	response.OkData("获取成功", respList, c)
+
+	response.OkData("获取成功", &responseModel.MyChartListResp{
+		List:   respList,
+		Paging: responseModel.GetPaging(pageIndex, pageSize, total),
+	}, c)
 }
 
 // Collect 收藏图表

+ 2 - 0
models/request/my_chart.go

@@ -45,4 +45,6 @@ type MyChartIsCollectReq struct {
 type MyChartCollectListReq struct {
 	Keyword    string `json:"keyword" form:"keyword" description:"关键词"`
 	ClassifyId int    `json:"classify_id" form:"classify_id" description:"图表分类ID"`
+	CurrPage   int    `json:"curr_page" form:"curr_page"`
+	PageSize   int    `json:"page_size" form:"page_size"`
 }

+ 5 - 17
models/response/my_chart.go

@@ -1,7 +1,5 @@
 package response
 
-import "time"
-
 // MyChartClassifyItem 用户图表分类
 type MyChartClassifyItem struct {
 	MyChartClassifyID   int    `json:"my_chart_classify_id"`
@@ -11,21 +9,6 @@ type MyChartClassifyItem struct {
 	CreateTime          string `json:"create_time"`
 }
 
-type YbMyChart struct {
-	MyChartID         int       `gorm:"primaryKey;column:my_chart_id;type:int(10) unsigned;not null" json:"-"`
-	MyChartClassifyID int       `gorm:"index:idx_classify_id;column:my_chart_classify_id;type:int(10) unsigned;not null;default:0" json:"myChartClassifyId"` // 图表分类ID
-	ChartInfoID       int       `gorm:"column:chart_info_id;type:int(10) unsigned;not null;default:0" json:"chartInfoId"`                                    // 图表ID
-	ChartName         string    `gorm:"column:chart_name;type:varchar(255);not null;default:''" json:"chartName"`                                            // 图表名称
-	UniqueCode        string    `gorm:"column:unique_code;type:varchar(64);not null;default:''" json:"uniqueCode"`                                           // 图表唯一编码
-	ChartImage        string    `gorm:"column:chart_image;type:varchar(255);not null;default:''" json:"chartImage"`                                          // 图表图片
-	UserID            int       `gorm:"index:idx_user_id;column:user_id;type:int(10) unsigned;not null;default:0" json:"userId"`                             // 用户ID
-	ReportID          int       `gorm:"column:report_id;type:int(10) unsigned;not null;default:0" json:"reportId"`                                           // 报告ID(从哪个报告收藏的)
-	ReportChapterID   int       `gorm:"column:report_chapter_id;type:int(10) unsigned;not null;default:0" json:"reportChapterId"`                            // 报告章节ID
-	Source            int       `gorm:"column:source;type:tinyint(4) unsigned;not null;default:1" json:"source"`                                             // 1-ETA图库;2-ETA表格
-	CreateTime        time.Time `gorm:"column:create_time;type:datetime" json:"createTime"`
-	ModifyTime        time.Time `gorm:"column:modify_time;type:datetime" json:"modifyTime"`
-}
-
 // MyChartItem 用户图表
 type MyChartItem struct {
 	MyChartID         int    `json:"my_chart_id"`
@@ -39,3 +22,8 @@ type MyChartItem struct {
 	ReportChapterID   int    `json:"report_chapter_id"`
 	CreateTime        string `json:"create_time"`
 }
+
+type MyChartListResp struct {
+	List   []*MyChartItem `json:"list"`
+	Paging *PagingItem    `json:"paging"`
+}

+ 10 - 0
models/tables/yb_my_chart/model.go

@@ -28,6 +28,16 @@ func (m *YbMyChart) List(condition string, pars []interface{}) (list []*YbMyChar
 	return
 }
 
+func (m *YbMyChart) PageList(condition string, pars []interface{}, pageIndex, pageSize int) (list []*YbMyChart, err error) {
+	offset := (pageIndex - 1) * pageSize
+	err = global.DEFAULT_MYSQL.Model(m).
+		Where(condition, pars...).
+		Offset(offset).Limit(pageSize).
+		Order("create_time DESC").
+		Scan(&list).Error
+	return
+}
+
 func (m *YbMyChart) Fetch(id int) (item *YbMyChart, err error) {
 	err = global.DEFAULT_MYSQL.Model(m).Where("my_chart_id = ?", id).First(&item).Error
 	return