|
@@ -12,14 +12,14 @@ var Product *product
|
|
|
type product struct {
|
|
|
}
|
|
|
|
|
|
-func (*product) CreateProduct(ctx context.Context, newProduct http_model.CreateProductRequest) (*http_model.CreateProductData, error) {
|
|
|
+func (*product) Create(ctx context.Context, newProduct http_model.CreateProductRequest, enterpriseID int64) (*http_model.CreateProductData, error) {
|
|
|
product := gorm_model.YounggeeProduct{
|
|
|
ProductName: newProduct.ProductName,
|
|
|
ProductType: newProduct.ProductType,
|
|
|
ShopAddress: newProduct.ShopAddress,
|
|
|
ProductPrice: newProduct.ProductPrice,
|
|
|
ProductUrl: newProduct.ProductUrl,
|
|
|
- EnterpriseID: newProduct.EnterpriseID,
|
|
|
+ EnterpriseID: enterpriseID,
|
|
|
BrandName: newProduct.BrandName,
|
|
|
}
|
|
|
productID, err := db.CreateProduct(ctx, product)
|
|
@@ -45,3 +45,98 @@ func (*product) CreateProduct(ctx context.Context, newProduct http_model.CreateP
|
|
|
}
|
|
|
return res, nil
|
|
|
}
|
|
|
+
|
|
|
+func (*product) Update(ctx context.Context, newProduct http_model.CreateProductRequest, enterpriseID int64) (*http_model.CreateProductData, error) {
|
|
|
+ product := gorm_model.YounggeeProduct{
|
|
|
+ ProductID: newProduct.ProductId,
|
|
|
+ ProductName: newProduct.ProductName,
|
|
|
+ ProductType: newProduct.ProductType,
|
|
|
+ ShopAddress: newProduct.ShopAddress,
|
|
|
+ ProductPrice: newProduct.ProductPrice,
|
|
|
+ ProductUrl: newProduct.ProductUrl,
|
|
|
+ EnterpriseID: enterpriseID,
|
|
|
+ BrandName: newProduct.BrandName,
|
|
|
+ }
|
|
|
+ productID, err := db.UpdateProduct(ctx, product)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ // 删除该商品之前的所有图片
|
|
|
+ err = db.DeletePhoto(ctx, *productID)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ // 新增图片
|
|
|
+ productPhotos := []gorm_model.YounggeeProductPhoto{}
|
|
|
+ for _, photo := range newProduct.ProductPhotos {
|
|
|
+ productPhoto := gorm_model.YounggeeProductPhoto{
|
|
|
+ PhotoUrl: photo.PhotoUrl,
|
|
|
+ Symbol: photo.Symbol,
|
|
|
+ ProductID: *productID,
|
|
|
+ }
|
|
|
+ productPhotos = append(productPhotos, productPhoto)
|
|
|
+ }
|
|
|
+ err = db.CreateProductPhoto(ctx, productPhotos)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ res := &http_model.CreateProductData{
|
|
|
+ ProductID: *productID,
|
|
|
+ }
|
|
|
+ return res, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (*product) FindAll(ctx context.Context, enterpriseID int64) (*http_model.FindAllProductData, error) {
|
|
|
+ products, err := db.FindAllProduct(ctx, enterpriseID)
|
|
|
+ if err != nil {
|
|
|
+ // 数据库查询error
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ findAllProductData := http_model.FindAllProductData{}
|
|
|
+ for _, product := range products {
|
|
|
+ productData := http_model.ProductInfo{
|
|
|
+ ProductID: product.ProductID,
|
|
|
+ BrandName: product.BrandName,
|
|
|
+ ProductName: product.ProductName,
|
|
|
+ }
|
|
|
+ findAllProductData.ProductInfos = append(findAllProductData.ProductInfos, productData)
|
|
|
+ }
|
|
|
+ return &findAllProductData, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (*product) FindByID(ctx context.Context, productID int64) (*http_model.FindProductData, error) {
|
|
|
+ product, err := db.FindProductByID(ctx, productID)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ productPhotos, err := db.FindAllProductPhoto(ctx, productID)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ findProductData := http_model.FindProductData{
|
|
|
+ ProductID: product.ProductID,
|
|
|
+ ProductName: product.ProductName,
|
|
|
+ ProductType: product.ProductType,
|
|
|
+ ShopAddress: product.ShopAddress,
|
|
|
+ ProductPrice: product.ProductPrice,
|
|
|
+ ProductUrl: product.ProductUrl,
|
|
|
+ EnterpriseID: product.EnterpriseID,
|
|
|
+ BrandName: product.BrandName,
|
|
|
+ }
|
|
|
+ for _, photo := range productPhotos {
|
|
|
+ productPhoto := http_model.ProductPhoto{
|
|
|
+ PhotoUrl: photo.PhotoUrl,
|
|
|
+ Symbol: photo.Symbol,
|
|
|
+ }
|
|
|
+ findProductData.ProductPhotos = append(findProductData.ProductPhotos, productPhoto)
|
|
|
+ }
|
|
|
+ return &findProductData, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (*product) FindByName(ctx context.Context, brandName string, productName string) (*int64, error) {
|
|
|
+ productID, err := db.FindProductByName(ctx, brandName, productName)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return productID, nil
|
|
|
+}
|