package db

import (
	"context"
	"gorm.io/gorm"
	"youngee_b_api/model/gorm_model"
)

// CreateSupplier 新建服务商
func CreateSupplier(ctx context.Context, newSupplier gorm_model.YoungeeSupplier) (*int, error) {
	db := GetWriteDB(ctx)
	err := db.Create(&newSupplier).Error
	if err != nil {
		return nil, err
	}
	return &newSupplier.SupplierId, nil
}

// GetSupplierByUserID 用户ID查找服务商信息
func GetSupplierByUserID(ctx context.Context, userID int64) (*gorm_model.YoungeeSupplier, error) {
	db := GetReadDB(ctx)
	var supplierInfo *gorm_model.YoungeeSupplier
	err := db.Where("user_id = ?", userID).First(&supplierInfo).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, err
		}
	}
	return supplierInfo, nil
}
