product.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package db
  2. import (
  3. "context"
  4. "youngee_b_api/model/gorm_model"
  5. "gorm.io/gorm"
  6. )
  7. func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
  8. db := GetReadDB(ctx)
  9. err := db.Create(&product).Error
  10. if err != nil {
  11. return nil, err
  12. }
  13. return &product.ProductID, nil
  14. }
  15. func UpdateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
  16. db := GetReadDB(ctx)
  17. err := db.Model(&product).Updates(product).Error
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &product.ProductID, nil
  22. }
  23. func FindAllProduct(ctx context.Context, enterpriseID int64) ([]gorm_model.YounggeeProduct, error) {
  24. db := GetReadDB(ctx)
  25. products := []gorm_model.YounggeeProduct{}
  26. err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error
  27. if err != nil {
  28. return nil, err
  29. }
  30. return products, nil
  31. }
  32. func FindProductByID(ctx context.Context, productID int64) (*gorm_model.YounggeeProduct, error) {
  33. db := GetReadDB(ctx)
  34. product := &gorm_model.YounggeeProduct{}
  35. err := db.First(&product, productID).Error
  36. if err != nil {
  37. if err == gorm.ErrRecordNotFound {
  38. return nil, nil
  39. } else {
  40. return nil, err
  41. }
  42. }
  43. return product, nil
  44. }
  45. func FindProductByName(ctx context.Context, brandName string, productName string) (*int64, error) {
  46. db := GetReadDB(ctx)
  47. product := &gorm_model.YounggeeProduct{}
  48. err := db.Where("product_name = ? AND brand_name = ?", productName, brandName).First(&product).Error
  49. if err != nil {
  50. if err == gorm.ErrRecordNotFound {
  51. return nil, nil
  52. } else {
  53. return nil, err
  54. }
  55. }
  56. return &product.ProductID, nil
  57. }