package db import ( "context" "youngee_b_api/model/gorm_model" "gorm.io/gorm" ) func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) { db := GetReadDB(ctx) err := db.Create(&product).Error if err != nil { return nil, err } return &product.ProductID, nil } func UpdateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) { db := GetReadDB(ctx) err := db.Model(&product).Updates(product).Error if err != nil { return nil, err } return &product.ProductID, nil } func GetProductByEnterpriseID(ctx context.Context, enterpriseID string) ([]gorm_model.YounggeeProduct, error) { db := GetReadDB(ctx) products := []gorm_model.YounggeeProduct{} err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error if err != nil { return nil, err } return products, nil } func GetProductByID(ctx context.Context, productID int64) (*gorm_model.YounggeeProduct, error) { db := GetReadDB(ctx) product := &gorm_model.YounggeeProduct{} err := db.First(&product, productID).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return product, nil } func GetProductIDByName(ctx context.Context, brandName string, productName string) (*int64, error) { db := GetReadDB(ctx) product := &gorm_model.YounggeeProduct{} err := db.Where("product_name = ? AND brand_name = ?", productName, brandName).First(&product).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, nil } else { return nil, err } } return &product.ProductID, nil }