// Code generated by goctl. DO NOT EDIT.
// goctl 1.8.1

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 teamMembersModel interface {
	Insert(ctx context.Context, data *TeamMembers) error
	FindOne(ctx context.Context, id string) (*TeamMembers, error)
	Update(ctx context.Context, data *TeamMembers) (*mongo.UpdateResult, error)
	Delete(ctx context.Context, id string) (int64, error)
}

type defaultTeamMembersModel struct {
	conn *mon.Model
}

func newDefaultTeamMembersModel(conn *mon.Model) *defaultTeamMembersModel {
	return &defaultTeamMembersModel{conn: conn}
}

func (m *defaultTeamMembersModel) Insert(ctx context.Context, data *TeamMembers) error {
	if data.ID.IsZero() {
		data.ID = primitive.NewObjectID()
		data.CreateTime = time.Now()
	}

	_, err := m.conn.InsertOne(ctx, data)
	return err
}

func (m *defaultTeamMembersModel) FindOne(ctx context.Context, id string) (*TeamMembers, error) {
	oid, err := primitive.ObjectIDFromHex(id)
	if err != nil {
		return nil, ErrInvalidObjectId
	}

	var data TeamMembers

	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 *defaultTeamMembersModel) Update(ctx context.Context, data *TeamMembers) (*mongo.UpdateResult, error) {
	res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data})
	return res, err
}

func (m *defaultTeamMembersModel) 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
}
