bindbox-game/internal/api/admin/pay_orders_export.go
邹方成 6ee627139c
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 40s
feat: 新增支付测试小程序与微信支付集成
feat(pay): 添加支付API基础结构
feat(miniapp): 创建支付测试小程序页面与配置
feat(wechatpay): 配置微信支付参数与证书
fix(guild): 修复成员列表查询条件
docs: 更新代码规范文档与需求文档
style: 统一前后端枚举显示与注释格式
refactor(admin): 重构用户奖励发放接口参数处理
test(title): 添加称号效果参数验证测试
2025-11-17 00:42:08 +08:00

79 lines
2.8 KiB
Go

package admin
import (
"bytes"
"net/http"
"time"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/validation"
"github.com/tealeg/xlsx"
)
type exportOrdersRequest struct {
Status *int32 `form:"status"`
SourceType *int32 `form:"source_type"`
StartDate string `form:"start_date"`
EndDate string `form:"end_date"`
}
func (h *handler) ExportPayOrders() core.HandlerFunc {
return func(ctx core.Context) {
req := new(exportOrdersRequest)
if err := ctx.ShouldBindForm(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
q := h.readDB.Orders.WithContext(ctx.RequestContext()).ReadDB()
if req.Status != nil {
q = q.Where(h.readDB.Orders.Status.Eq(*req.Status))
}
if req.SourceType != nil {
q = q.Where(h.readDB.Orders.SourceType.Eq(*req.SourceType))
}
if req.StartDate != "" {
if t, err := time.Parse("2006-01-02", req.StartDate); err == nil {
q = q.Where(h.readDB.Orders.CreatedAt.Gte(t))
}
}
if req.EndDate != "" {
if t, err := time.Parse("2006-01-02", req.EndDate); err == nil {
t = t.Add(24 * time.Hour).Add(-time.Second)
q = q.Where(h.readDB.Orders.CreatedAt.Lte(t))
}
}
rows, err := q.Order(h.readDB.Orders.ID.Desc()).Limit(5000).Find()
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 23001, err.Error()))
return
}
file := xlsx.NewFile()
sheet, _ := file.AddSheet("orders")
header := []string{"订单号", "用户ID", "来源", "状态", "总金额", "折扣", "积分抵扣", "实付", "支付时间", "创建时间"}
row := sheet.AddRow()
for _, hname := range header {
cell := row.AddCell()
cell.Value = hname
}
for _, o := range rows {
r := sheet.AddRow()
r.AddCell().Value = o.OrderNo
r.AddCell().SetInt64(o.UserID)
r.AddCell().SetInt(int(o.SourceType))
r.AddCell().SetInt(int(o.Status))
r.AddCell().SetInt64(o.TotalAmount)
r.AddCell().SetInt64(o.DiscountAmount)
r.AddCell().SetInt64(o.PointsAmount)
r.AddCell().SetInt64(o.ActualAmount)
r.AddCell().Value = o.PaidAt.Format("2006-01-02 15:04:05")
r.AddCell().Value = o.CreatedAt.Format("2006-01-02 15:04:05")
}
var buf bytes.Buffer
if err := file.Write(&buf); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 23002, err.Error()))
return
}
ctx.ExcelData("orders.xlsx", buf.Bytes())
}
}