You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.9 KiB
Go

8 months ago
package service
import (
e "errors"
7 months ago
"github.com/xuri/excelize/v2"
8 months ago
"gold-shop/errors"
"gold-shop/global"
"gold-shop/model"
"gold-shop/request"
7 months ago
"gold-shop/request/manage"
8 months ago
"gold-shop/utils"
7 months ago
"gold-shop/utils/excel"
8 months ago
"gorm.io/gorm"
7 months ago
"time"
8 months ago
)
var OrderService = orderService{}
type orderService struct {
}
func (orderService) Order(orderRequest request.OrderRequest, user *model.User) (*model.Order, error) {
product := &model.Product{}
8 months ago
err := global.DB.Where("id", orderRequest.ProductID).First(product).Error
8 months ago
if e.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.NewBusinessError("商品不存在")
}
totalAmount := float64(orderRequest.Num) * product.Price
if user.Balance < totalAmount {
return nil, errors.NewBusinessError("账号余额不足")
}
orderNo := utils.GenerateNo("order")
tx := global.DB.Begin()
order := model.Order{}
order.OrderNo = orderNo
order.Amount = totalAmount
order.UserID = user.ID
err = global.DB.Save(&order).Error
if err != nil {
tx.Rollback()
return nil, errors.NewBusinessError("保存数据失败")
}
orderProduct := model.OrderProduct{}
orderProduct.ProductID = orderRequest.ProductID
orderProduct.OrderNo = orderNo
orderProduct.Price = product.Price
orderProduct.Amount = totalAmount
orderProduct.Num = orderRequest.Num
err = global.DB.Save(&orderProduct).Error
if err != nil {
tx.Rollback()
return nil, errors.NewBusinessError("保存数据失败")
}
global.DB.Model(user).Update("balance", gorm.Expr("balance - ?", totalAmount))
tx.Commit()
return &order, nil
}
7 months ago
7 months ago
func (orderService) GetUserOrders(userID, page, pageSize int) ([]model.Order, int64) {
7 months ago
orders := make([]model.Order, pageSize)
offset := (page - 1) * pageSize
var total int64
global.DB.Where("user_id", userID).Preload("OrderProducts").Offset(offset).Limit(pageSize).Find(&orders)
global.DB.Model(&model.Order{}).Where("user_id", userID).Count(&total)
return orders, total
}
7 months ago
func (s *orderService) buildQuery(req manage.OrderQueryRequest) *gorm.DB {
tx := global.DB
if req.Mobile != "" {
var userIds []int
global.DB.Model(&model.User{}).Where("mobile", req.Mobile).Pluck("id", &userIds)
tx = tx.Where("user_id", userIds)
}
if req.OrderNo != "" {
tx = tx.Where("order_no", req.OrderNo)
}
if req.UserID != 0 {
tx = tx.Where("user_id", req.UserID)
}
return tx
}
func (s *orderService) GetOrders(req manage.OrderQueryRequest) ([]model.Order, int64) {
orders := make([]model.Order, req.PageSize)
offset := (req.Page - 1) * req.PageSize
var total int64
tx := s.buildQuery(req)
tx.Preload("OrderProducts").Preload("User").Order("id desc").Offset(offset).Limit(req.PageSize).Find(&orders)
tx.Model(&model.Order{}).Count(&total)
return orders, total
}
func (s *orderService) GenerateOrdersExcel(req manage.OrderQueryRequest) *excelize.File {
pageSize := 200
lastID := 0
tx := s.buildQuery(req)
f := excel.NewFile()
headers := []string{"订单号", "用户手机号", "购买份数", "订单总金额", "下单时间"}
excel.SetSimpleHeaders(headers, "Sheet1", f)
for {
orders := make([]model.Order, pageSize)
tx.Preload("OrderProducts").Preload("User").Where("id > ?", lastID).Limit(pageSize).Find(&orders)
count := len(orders)
if count == 0 {
break
}
s.buildOrdersExcel(orders, "Sheet1", f)
lastIndex := count - 1
lastID = orders[lastIndex].ID
}
return f
}
func (orderService) buildOrdersExcel(orders []model.Order, sheet string, f *excelize.File) {
row := 2
for _, order := range orders {
col := 0
num := 0
for _, orderProduct := range order.OrderProducts {
num += orderProduct.Num
}
f.SetCellValue(sheet, excel.CellKey(row, &col), order.OrderNo)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.User.Mobile)
f.SetCellValue(sheet, excel.CellKey(row, &col), num)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.Amount)
f.SetCellValue(sheet, excel.CellKey(row, &col), order.CreatedAt.Format(time.DateTime))
row++
}
}