2023-11-06 17:55:46 +08:00
|
|
|
package handler
|
|
|
|
|
2024-05-06 14:41:27 +08:00
|
|
|
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
|
|
|
|
// * Use of this source code is governed by a Apache-2.0 license
|
|
|
|
// * that can be found in the LICENSE file.
|
|
|
|
// * @Author yangjian102621@163.com
|
|
|
|
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
2023-11-06 17:55:46 +08:00
|
|
|
import (
|
2024-05-06 14:41:27 +08:00
|
|
|
"geekai/core"
|
|
|
|
"geekai/core/types"
|
|
|
|
"geekai/store/model"
|
|
|
|
"geekai/store/vo"
|
|
|
|
"geekai/utils"
|
|
|
|
"geekai/utils/resp"
|
2024-03-03 10:40:32 +08:00
|
|
|
|
2023-11-06 17:55:46 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrderHandler struct {
|
|
|
|
BaseHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrderHandler(app *core.AppServer, db *gorm.DB) *OrderHandler {
|
2024-03-19 18:25:01 +08:00
|
|
|
return &OrderHandler{BaseHandler: BaseHandler{App: app, DB: db}}
|
2023-11-06 17:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrderHandler) List(c *gin.Context) {
|
|
|
|
var data struct {
|
|
|
|
Page int `json:"page"`
|
|
|
|
PageSize int `json:"page_size"`
|
|
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&data); err != nil {
|
|
|
|
resp.ERROR(c, types.InvalidArgs)
|
|
|
|
return
|
|
|
|
}
|
2024-03-03 10:40:32 +08:00
|
|
|
userId := h.GetLoginUserId(c)
|
2024-03-19 18:25:01 +08:00
|
|
|
session := h.DB.Session(&gorm.Session{}).Where("user_id = ? AND status = ?", userId, types.OrderPaidSuccess)
|
2023-11-06 17:55:46 +08:00
|
|
|
var total int64
|
|
|
|
session.Model(&model.Order{}).Count(&total)
|
|
|
|
var items []model.Order
|
|
|
|
var list = make([]vo.Order, 0)
|
|
|
|
offset := (data.Page - 1) * data.PageSize
|
|
|
|
res := session.Order("id DESC").Offset(offset).Limit(data.PageSize).Find(&items)
|
|
|
|
if res.Error == nil {
|
|
|
|
for _, item := range items {
|
|
|
|
var order vo.Order
|
|
|
|
err := utils.CopyObject(item, &order)
|
|
|
|
if err == nil {
|
|
|
|
order.Id = item.Id
|
|
|
|
order.CreatedAt = item.CreatedAt.Unix()
|
|
|
|
order.UpdatedAt = item.UpdatedAt.Unix()
|
|
|
|
list = append(list, order)
|
|
|
|
} else {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.SUCCESS(c, vo.NewPage(total, data.Page, data.PageSize, list))
|
|
|
|
}
|