package db import ( "context" "github.com/caixw/lib.go/conv" "gorm.io/gorm" "youngee_m_api/model/gorm_model" ) 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 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 GetEnterpriseIDByUserID(ctx context.Context, UserId string) int64 { db := GetReadDB(ctx) enterpriseInfo := gorm_model.Enterprise{} userId := conv.MustInt64(UserId, 0) err := db.Where("user_id = ?", userId).Find(&enterpriseInfo).Error if err != nil { return 0 } enterpriseID := enterpriseInfo.EnterpriseID return enterpriseID } func GetProductByEnterpriseID(ctx context.Context, enterpriseID int64) ([]gorm_model.YounggeeProduct, error) { db := GetReadDB(ctx) var products []gorm_model.YounggeeProduct err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error if err != nil { return nil, err } return products, nil } func GetEnterpriseIDByProductID(ctx context.Context, ProductID int64) int64 { db := GetReadDB(ctx) ProjectInfo := gorm_model.ProjectInfo{} err := db.Where("product_id = ?", ProductID).Find(&ProjectInfo).Error if err != nil { return 0 } enterpriseID := ProjectInfo.EnterpriseID return enterpriseID } 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 }