package dao

import (
	"errors"
	"github.com/sirupsen/logrus"
	"gorm.io/gorm"
	"youngee_b_api/app/entity"
)

type ProductPhotoDAO struct{}

func (d ProductPhotoDAO) CreateProductPhoto(productPhotos []entity.ProductPhoto) error {
	err := Db.Debug().Create(&productPhotos).Error
	if err != nil {
		return err
	}
	return nil
}

// 获取所属商品的图片
func (d ProductPhotoDAO) GetProductPhotoByProductID(productId int64) ([]entity.ProductPhoto, error) {
	var productPhotos []entity.ProductPhoto
	err := Db.Where("product_id = ?", productId).Order("created_at asc").Find(&productPhotos).Error
	if err != nil {
		return nil, err
	}
	return productPhotos, nil
}

// 获取所属商品的主图详情
func (d ProductPhotoDAO) GetMainProductPhotoInfoByProductID(productId int64) (*entity.ProductPhoto, error) {
	var productPhoto entity.ProductPhoto
	err := Db.Where("product_id = ? AND symbol = ?", productId, 1).First(&productPhoto).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, nil
		} else {
			return nil, err
		}
	}
	return &productPhoto, nil
}

// 获取所属商品的主图
func (d ProductPhotoDAO) GetMainPhotoByProductID(productId int64) (string, error) {
	var productPhoto entity.ProductPhoto
	err := Db.Where("product_id = ? AND symbol = ?", productId, 1).Find(&productPhoto).Error
	if err != nil {
		return "", err
	}
	return productPhoto.PhotoUrl, nil
}

// 获取所属门店的图片
func (d ProductPhotoDAO) GetProductPhotoByStoreID(storeId int64) ([]entity.ProductPhoto, error) {
	var productPhotos []entity.ProductPhoto
	err := Db.Where("store_id = ?", storeId).Order("created_at asc").Find(&productPhotos).Error
	if err != nil {
		return nil, err
	}
	return productPhotos, nil
}

// 获取所属门店的主图
func (d ProductPhotoDAO) GetMainPhotoByStoreID(storeId int64) (string, error) {
	var productPhoto entity.ProductPhoto
	err := Db.Where("store_id = ? AND symbol = ?", storeId, 1).Find(&productPhoto).Error
	if err != nil {
		return "", err
	}
	return productPhoto.PhotoUrl, nil
}

// 获取所属团购的图片
func (d ProductPhotoDAO) GetProductPhotoByTeamBuyingID(teamBuyingId int64) ([]entity.ProductPhoto, error) {
	var productPhotos []entity.ProductPhoto
	err := Db.Where("team_buying_id = ?", teamBuyingId).Order("created_at asc").Find(&productPhotos).Error
	if err != nil {
		return nil, err
	}
	return productPhotos, nil
}

// 获取所属团购的主图
func (d ProductPhotoDAO) GetMainPhotoByTeamBuyingID(teamBuyingId int64) (string, error) {
	var productPhoto entity.ProductPhoto
	err := Db.Where("team_buying_id = ? AND symbol = ?", teamBuyingId, 1).Find(&productPhoto).Error
	if err != nil {
		return "", err
	}
	return productPhoto.PhotoUrl, nil
}

func (d ProductPhotoDAO) GetProductPhotosBySelectionId(selectionId string) ([]*entity.ProductPhoto, error) {
	productId := 0
	err := Db.Model(entity.SelectionInfo{}).Select("product_id").Where("selection_id = ?", selectionId).Find(&productId).Error
	if err != nil {
		logrus.Errorf("[GetProductInfo] error query mysql, err:%+v", err)
		return nil, err
	}
	var productPhotos []*entity.ProductPhoto
	err = Db.Model(entity.ProductPhoto{}).Where("product_id = ?", productId).Find(&productPhotos).Error
	if err != nil {
		logrus.Errorf("[GetProductInfo] error query mysql, err:%+v", err)
		return nil, err
	}
	return productPhotos, nil
}

func (d ProductPhotoDAO) GetProductPhotosByProjectId(projectId string) ([]*entity.ProductPhoto, error) {
	productId := 0
	err := Db.Model(entity.Project{}).Select("product_id").Where("project_id = ?", projectId).Find(&productId).Error
	if err != nil {
		logrus.Errorf("[GetProductInfo] error query mysql, err:%+v", err)
		return nil, err
	}
	var productPhotos []*entity.ProductPhoto
	err = Db.Model(entity.ProductPhoto{}).Where("product_id = ?", productId).Find(&productPhotos).Error
	if err != nil {
		logrus.Errorf("[GetProductInfo] error query mysql, err:%+v", err)
		return nil, err
	}
	return productPhotos, nil
}
