package orders import ( "context" "database/sql" "time" "backend/app/requests" "backend/database/fields" "backend/database/models/qvyun_v2/public/model" "backend/database/models/qvyun_v2/public/table" "backend/pkg/utils" "backend/providers/otel" . "github.com/go-jet/jet/v2/postgres" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" semconv "go.opentelemetry.io/otel/semconv/v1.4.0" ) // @provider:except type Service struct { db *sql.DB log *log.Entry `inject:"false"` } func (svc *Service) Prepare() error { svc.log = log.WithField("module", "orders.service") _ = Int(1) return nil } // GetUserOrders func (svc *Service) GetOrders(ctx context.Context, pagination *requests.Pagination, filter *UserOrderFilter) ([]model.Orders, int64, error) { _, span := otel.Start(ctx, "users.service.GetUserOrders") defer span.End() span.SetAttributes( attribute.Int64("user.id", filter.UserID), attribute.Int64("page.page", pagination.Page), attribute.Int64("page.limit", pagination.Limit), ) tbl := table.Orders cond := tbl.UserID.EQ(Int64(filter.UserID)) if filter.Status != nil { cond = cond.AND(tbl.Status.EQ(Int16(int16(*filter.Status)))) } if filter.Type != nil { cond = cond.AND(tbl.Type.EQ(Int16(int16(*filter.Type)))) } if filter.CreatedAt != nil { cond = cond.AND(tbl.CreatedAt.LT_EQ(TimestampT(*filter.CreatedAt))) } cntStmt := tbl.SELECT(COUNT(tbl.ID).AS("cnt")).WHERE(cond) var count struct { Cnt int64 } if err := cntStmt.QueryContext(ctx, svc.db, &count); err != nil { return nil, 0, err } stmt := tbl. SELECT(tbl.AllColumns). WHERE(cond). ORDER_BY(tbl.CreatedAt.DESC()). LIMIT(pagination.Limit). OFFSET(pagination.Offset()) span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql())) var orders []model.Orders if err := stmt.QueryContext(ctx, svc.db, &orders); err != nil { return nil, 0, err } return orders, count.Cnt, nil } // CreateOrder func (svc *Service) Create(ctx context.Context, user *model.Users, post *model.Posts) (*model.Orders, error) { _, span := otel.Start(ctx, "users.service.CreateOrder") defer span.End() span.SetAttributes( attribute.Int64("post.id", post.ID), ) price := post.Price if post.Discount != 100 { price = post.Price * int64(post.Discount) / 100 } m := model.Orders{ CreatedAt: time.Now(), UpdatedAt: time.Now(), TenantID: post.TenantID, UserID: user.ID, Type: fields.OrderTypeConsume, Status: fields.OrderStatusPending, OrderSerial: svc.generateCreateOrderSerial(ctx), RemoteOrderSerial: "", RefundSerial: "", RemoteRefundSerial: "", Amount: price, Currency: "CNY", Title: post.Title, Description: new(string), Meta: fields.ToJson(fields.OrderMeta{ ObjectID: post.ID, Price: post.Price, Discount: post.Discount, Coupons: nil, }), } tbl := table.Orders stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns) span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql())) var mm model.Orders if err := stmt.QueryContext(ctx, svc.db, &mm); err != nil { return nil, err } return &mm, nil } // generateCreateOrderSerial func (svc *Service) generateCreateOrderSerial(ctx context.Context) string { return utils.GenerateOrderSerial("O") } // generateRefundOrderSerial func (svc *Service) generateRefundOrderSerial(ctx context.Context) string { return utils.GenerateOrderSerial("R") } // GetByOrderID func (svc *Service) GetByOrderID(ctx context.Context, orderID string) (*model.Orders, error) { _, span := otel.Start(ctx, "users.service.GetByOrderID") defer span.End() span.SetAttributes( attribute.String("order.id", orderID), ) tbl := table.Orders stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.OrderSerial.EQ(String(orderID))) span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql())) var order model.Orders if err := stmt.QueryContext(ctx, svc.db, &order); err != nil { return nil, err } return &order, nil } // GetUserOrderByOrderID func (svc *Service) GetUserOrderByOrderID(ctx context.Context, orderID string, userID int64) (*model.Orders, error) { _, span := otel.Start(ctx, "users.service.GetUserOrderByOrderID") defer span.End() span.SetAttributes( attribute.String("order.id", orderID), attribute.Int64("user.id", userID), ) tbl := table.Orders stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.OrderSerial.EQ(String(orderID)).AND(tbl.UserID.EQ(Int64(userID)))) span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql())) var order model.Orders if err := stmt.QueryContext(ctx, svc.db, &order); err != nil { return nil, err } return &order, nil }