product.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package db
  2. import (
  3. "context"
  4. "github.com/sirupsen/logrus"
  5. "youngee_b_api/model/gorm_model"
  6. "gorm.io/gorm"
  7. )
  8. func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
  9. db := GetReadDB(ctx)
  10. err := db.Create(&product).Error
  11. if err != nil {
  12. return nil, err
  13. }
  14. return &product.ProductID, nil
  15. }
  16. func UpdateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
  17. db := GetReadDB(ctx)
  18. err := db.Model(&product).Updates(product).Error
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &product.ProductID, nil
  23. }
  24. func GetProductByEnterpriseID(ctx context.Context, enterpriseID string) ([]gorm_model.YounggeeProduct, error) {
  25. db := GetReadDB(ctx)
  26. products := []gorm_model.YounggeeProduct{}
  27. err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error
  28. if err != nil {
  29. return nil, err
  30. }
  31. return products, nil
  32. }
  33. func GetProductByID(ctx context.Context, productID int64) (*gorm_model.YounggeeProduct, error) {
  34. db := GetReadDB(ctx)
  35. product := &gorm_model.YounggeeProduct{}
  36. err := db.First(&product, productID).Error
  37. if err != nil {
  38. if err == gorm.ErrRecordNotFound {
  39. return nil, nil
  40. } else {
  41. return nil, err
  42. }
  43. }
  44. return product, nil
  45. }
  46. func GetProductIDByName(ctx context.Context, brandName string, productName string) (*int64, error) {
  47. db := GetReadDB(ctx)
  48. product := &gorm_model.YounggeeProduct{}
  49. err := db.Where("product_name = ? AND brand_name = ?", productName, brandName).First(&product).Error
  50. if err != nil {
  51. if err == gorm.ErrRecordNotFound {
  52. return nil, nil
  53. } else {
  54. return nil, err
  55. }
  56. }
  57. return &product.ProductID, nil
  58. }
  59. func GetProductInfoBySelectionId(ctx context.Context, selectionId string) (*gorm_model.YounggeeProduct, error) {
  60. db := GetReadDB(ctx)
  61. productId := 0
  62. err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error
  63. if err != nil {
  64. logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
  65. return nil, err
  66. }
  67. productInfo := gorm_model.YounggeeProduct{}
  68. err = db.Model(gorm_model.YounggeeProduct{}).Where("product_id = ?", productId).Find(&productInfo).Error
  69. if err != nil {
  70. logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
  71. return nil, err
  72. }
  73. return &productInfo, nil
  74. }
  75. func GetProductPhotoInfoBySelectionId(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeProductPhoto, error) {
  76. db := GetReadDB(ctx)
  77. productId := 0
  78. err := db.Model(gorm_model.YounggeeSelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error
  79. if err != nil {
  80. logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
  81. return nil, err
  82. }
  83. var productPhotoInfo []*gorm_model.YounggeeProductPhoto
  84. err = db.Model(gorm_model.YounggeeProductPhoto{}).Where("product_id = ?", productId).Find(&productPhotoInfo).Error
  85. if err != nil {
  86. logrus.WithContext(ctx).Errorf("[GetProductInfo] error query mysql, err:%+v", err)
  87. return nil, err
  88. }
  89. return productPhotoInfo, nil
  90. }