package model

import (
	"context"

	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ AuthorizationDocumentModel = (*customAuthorizationDocumentModel)(nil)

type (
	// AuthorizationDocumentModel is an interface to be customized, add more methods here,
	// and implement the added methods in customAuthorizationDocumentModel.
	AuthorizationDocumentModel interface {
		authorizationDocumentModel
		FindByOrderId(ctx context.Context, orderId int64) ([]*AuthorizationDocument, error)
	}

	customAuthorizationDocumentModel struct {
		*defaultAuthorizationDocumentModel
	}
)

// NewAuthorizationDocumentModel returns a model for the database table.
func NewAuthorizationDocumentModel(conn sqlx.SqlConn, c cache.CacheConf) AuthorizationDocumentModel {
	return &customAuthorizationDocumentModel{
		defaultAuthorizationDocumentModel: newAuthorizationDocumentModel(conn, c),
	}
}

// FindByOrderId 根据订单ID查询授权书列表
func (m *customAuthorizationDocumentModel) FindByOrderId(ctx context.Context, orderId int64) ([]*AuthorizationDocument, error) {
	query := `SELECT * FROM authorization_document WHERE order_id = ? AND del_state = 0 ORDER BY create_time DESC`

	var authDocs []*AuthorizationDocument
	err := m.QueryRowsNoCacheCtx(ctx, &authDocs, query, orderId)
	return authDocs, err
}
