You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/service/admin/order.go

76 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package admin
import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
"strings"
)
// OrderBatchService 订单批量操作服务
type OrderBatchService struct {
ID []uint `json:"id" binding:"min=1"`
}
// Delete 删除订单
func (service *OrderBatchService) Delete(c *gin.Context) serializer.Response {
if err := model.DB.Where("id in (?)", service.ID).Delete(&model.Order{}).Error; err != nil {
return serializer.DBErr("无法删除订单", err)
}
return serializer.Response{}
}
// Orders 列出订单
func (service *AdminListService) Orders() serializer.Response {
var res []model.Order
total := 0
tx := model.DB.Model(&model.Order{})
if service.OrderBy != "" {
tx = tx.Order(service.OrderBy)
}
for k, v := range service.Conditions {
tx = tx.Where(k+" = ?", v)
}
if len(service.Searches) > 0 {
search := ""
for k, v := range service.Searches {
search += k + " like '%" + v + "%' OR "
}
search = strings.TrimSuffix(search, " OR ")
tx = tx.Where(search)
}
// 计算总数用于分页
tx.Count(&total)
// 查询记录
tx.Limit(service.PageSize).Offset((service.Page - 1) * service.PageSize).Find(&res)
// 查询对应用户同时计算HashID
users := make(map[uint]model.User)
for _, file := range res {
users[file.UserID] = model.User{}
}
userIDs := make([]uint, 0, len(users))
for k := range users {
userIDs = append(userIDs, k)
}
var userList []model.User
model.DB.Where("id in (?)", userIDs).Find(&userList)
for _, v := range userList {
users[v.ID] = v
}
return serializer.Response{Data: map[string]interface{}{
"total": total,
"items": res,
"users": users,
}}
}