package service
import (
"container_cloud/pkg/api"
"container_cloud/pkg/apiserver/query"
"container_cloud/pkg/domain"
"database/sql"
"encoding/json"
"fmt"
"github.com/google/uuid"
"k8s.io/klog"
"strings"
"time"
)
type OrderService struct {
Db *sql.DB
}
func NewOrderService(db *sql.DB) *OrderService{
return &OrderService{Db: db}
}
func (o *OrderService) CreateOrder(order domain.Order) {
order.CreateTime = time.Now()
var orderType uint8 = 1
order.OrderType = &orderType
uuid,_ := uuid.NewRandom()
order.Id = strings.ReplaceAll(uuid.String(), "-", "")
jsonbyte, _ := json.Marshal(order.OrderItem)
order.OrderItemJson = string(jsonbyte)
o.insertData(order)
}
func (o *OrderService) insertData(order domain.Order) {
stmt, _ := o.Db.Prepare(`INSERT INTO t_order (id, username, service_type, order_type, status, reason, order_item, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
defer stmt.Close()
ret, err := stmt.Exec(order.Id, order.Username, order.ServiceType, order.OrderType, order.Status, order.Reason, order.OrderItemJson, order.CreateTime)
if err != nil {
fmt.Printf("insert data error: %v\n", err)
return
}
if LastInsertId, err := ret.LastInsertId(); nil == err {
fmt.Println("LastInsertId:", LastInsertId)
}
if RowsAffected, err := ret.RowsAffected(); nil == err {
fmt.Println("RowsAffected:", RowsAffected)
}
}
func (o *OrderService) ListOrders(query *query.Query, username string) (*api.ListResult, error){
// 查询总数量
totalRow, err := o.Db.Query("SELECT COUNT(*) FROM t_order WHERE username = ?", username)
if err != nil {
klog.Error("query orders count error", err)
return nil, err
}
total := 0
for totalRow.Next() {
err := totalRow.Scan(
&total,
)
if err != nil {
klog.Error("query orders count error", err)
continue
}
}
totalRow.Close()
// 查询订单列表
rows, err := o.Db.Query("select * from t_order where username = ? order by create_time desc limit ? offset ? ", username, query.Pagination.Limit, query.Pagination.Offset)
defer func() {
if rows != nil {
rows.Close()
}
}()
if err != nil {
klog.Error("query orders error", err)
return nil, err
}
items := make([]interface{}, 0)
for rows.Next() {
order := new(domain.Order)
err = rows.Scan(&order.Id, &order.Username, &order.ServiceType, &order.OrderType, &order.Status, &order.Reason, &order.OrderItemJson, &order.CreateTime)
if err != nil {
klog.Error("query orders error", err)
return nil, err
}
order.OrderItemJson = ""
items = append(items, *order)
}
return &api.ListResult{
TotalItems: total,
Items: items,
}, nil
}
func (o *OrderService) GetOrder(id string) (*domain.Order, error) {
order := new(domain.Order)
row := o.Db.QueryRow("select order_item from t_order where id = ?", id)
if err := row.Scan(&order.OrderItemJson); err != nil {
klog.Error(err)
return nil, err
}
orderItems := &[]domain.OrderItem{}
json.Unmarshal([]byte(order.OrderItemJson), orderItems)
order.OrderItemJson = ""
order.OrderItem = *orderItems
return order, nil
}
func (o *OrderService) ListUserOrders(username string) (*[]domain.Order, error){
// 查询订单列表
rows, err := o.Db.Query("select * from t_order where username = ? order by create_time desc", username)
defer func() {
if rows != nil {
rows.Close()
}
}()
if err != nil {
klog.Error("query orders error", err)
return nil, err
}
items := make([]domain.Order,0)
for rows.Next() {
order := new(domain.Order)
err = rows.Scan(&order.Id, &order.Username, &order.ServiceType, &order.OrderType, &order.Status, &order.Reason, &order.OrderItemJson, &order.CreateTime)
if err != nil {
klog.Error("query orders error", err)
return nil, err
}
order.OrderItemJson = ""
items = append(items, *order)
}
return &items,nil
}
|