2024-10-25 16:42:58 +00:00
|
|
|
// Code generated by goctl. DO NOT EDIT.
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type orderModel interface {
|
|
|
|
Insert(ctx context.Context, data *Order) error
|
|
|
|
FindOne(ctx context.Context, id string) (*Order, error)
|
|
|
|
Update(ctx context.Context, data *Order) (*mongo.UpdateResult, error)
|
|
|
|
Delete(ctx context.Context, id string) (int64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type defaultOrderModel struct {
|
|
|
|
conn *mon.Model
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDefaultOrderModel(conn *mon.Model) *defaultOrderModel {
|
|
|
|
return &defaultOrderModel{conn: conn}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *defaultOrderModel) Insert(ctx context.Context, data *Order) error {
|
|
|
|
if data.ID.IsZero() {
|
2024-10-25 17:17:32 +00:00
|
|
|
now := time.Now().UTC().UnixNano()
|
2024-10-25 16:42:58 +00:00
|
|
|
data.ID = primitive.NewObjectID()
|
2024-10-25 17:17:32 +00:00
|
|
|
data.CreateTime = now
|
|
|
|
data.UpdateTime = now
|
2024-10-25 16:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := m.conn.InsertOne(ctx, data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *defaultOrderModel) FindOne(ctx context.Context, id string) (*Order, error) {
|
|
|
|
oid, err := primitive.ObjectIDFromHex(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInvalidObjectId
|
|
|
|
}
|
|
|
|
|
|
|
|
var data Order
|
|
|
|
|
|
|
|
err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid})
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &data, nil
|
|
|
|
case mon.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *defaultOrderModel) Update(ctx context.Context, data *Order) (*mongo.UpdateResult, error) {
|
2024-10-25 17:17:32 +00:00
|
|
|
data.UpdateTime = time.Now().UTC().UnixNano()
|
2024-10-25 16:42:58 +00:00
|
|
|
|
|
|
|
res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data})
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *defaultOrderModel) Delete(ctx context.Context, id string) (int64, error) {
|
|
|
|
oid, err := primitive.ObjectIDFromHex(id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, ErrInvalidObjectId
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid})
|
|
|
|
return res, err
|
|
|
|
}
|