package db import ( "context" "github.com/sirupsen/logrus" "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 } func GetProductInfoBySelectionId(ctx context.Context, selectionId string) (*gorm_model.YounggeeProduct, error) { db := GetReadDB(ctx) productId := 0 err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error if err != nil { logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err) return nil, err } productInfo := gorm_model.YounggeeProduct{} err = db.Model(gorm_model.YounggeeProduct{}).Where("product_id = ?", productId).Find(&productInfo).Error if err != nil { logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err) return nil, err } return &productInfo, nil } func GetProductPhotoInfoBySelectionId(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeProductPhoto, error) { db := GetReadDB(ctx) productId := 0 err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error if err != nil { logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err) return nil, err } var productPhotoInfo []*gorm_model.YounggeeProductPhoto err = db.Model(gorm_model.YounggeeProductPhoto{}).Where("product_id = ?", productId).Find(&productPhotoInfo).Error if err != nil { logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err) return nil, err } return productPhotoInfo, nil }