package dao

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

type ProductDAO struct{}

func (d ProductDAO) GetProductsByEnterpriseID(enterpriseId string, page int, pageSize int) ([]entity.Product, int64, error) {
	var products []entity.Product
	var total int64
	Db.Model(&entity.Product{}).Where("enterprise_id = ? AND is_deleted = 0", enterpriseId).Count(&total)
	// 计算偏移量
	offset := (page - 1) * pageSize
	// 分页查询
	err := Db.Where("enterprise_id = ? AND is_deleted = 0", enterpriseId).Order("created_at desc").Offset(offset).Limit(pageSize).Find(&products).Error
	if err != nil {
		return nil, 0, err
	}
	return products, total, nil
}

func (d ProductDAO) GetProductsByEnterpriseIDAndTypeTitle(enterpriseId string, productType int64, productTitle string, page int, pageSize int) ([]entity.Product, int64, error) {
	var products []entity.Product
	var total int64
	query := Db.Model(&entity.Product{}).Where("enterprise_id = ? AND product_type = ? AND is_deleted = 0", enterpriseId, productType)
	if productTitle != "" {
		query = query.Where("product_name LIKE ?", "%"+productTitle+"%")
	}
	query.Count(&total)
	// 计算偏移量
	offset := (page - 1) * pageSize
	// 分页查询
	err := query.Order("created_at desc").Offset(offset).Limit(pageSize).Find(&products).Error
	if err != nil {
		return nil, 0, err
	}
	return products, total, nil
}

func (d ProductDAO) GetProductsBySubAccountId(subAccountId int64, page int, pageSize int) ([]entity.Product, int64, error) {
	var products []entity.Product
	var total int64
	Db.Model(&entity.Product{}).Where("sub_account_id = ? AND is_deleted = 0", subAccountId).Count(&total)
	// 计算偏移量
	offset := (page - 1) * pageSize
	// 分页查询
	err := Db.Where("sub_account_id = ? AND is_deleted = 0", subAccountId).Order("created_at desc").Offset(offset).Limit(pageSize).Find(&products).Error
	if err != nil {
		return nil, 0, err
	}
	return products, total, nil
}

func (d ProductDAO) CreateProduct(product entity.Product) (int64, error) {
	err := Db.Create(&product).Error
	if err != nil {
		return 0, err
	}
	return product.ProductID, nil
}

func (d ProductDAO) UpdateProduct(product entity.Product) (int64, error) {
	err := Db.Model(&entity.Product{}).Where("product_id = ?", product.ProductID).Updates(product).Error
	if err != nil {
		return 0, err
	}
	return product.ProductID, nil
}

func (d ProductDAO) DeleteProduct(productId int64) error {
	err := Db.Where("product_id = ?", productId).Delete(&entity.Product{}).Error
	return err
}

func (d ProductDAO) GetProductByID(productId int64) (*entity.Product, error) {
	var product entity.Product
	err := Db.Debug().Where("product_id = ?", productId).First(&product).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, nil
		} else {
			return nil, err
		}
	}
	return &product, nil
}

func (d ProductDAO) GetProductBySelectionId(selectionId string) (*entity.Product, 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
	}
	productInfo := entity.Product{}
	err = Db.Model(entity.Product{}).Where("product_id = ?", productId).Find(&productInfo).Error
	if err != nil {
		logrus.Errorf("[GetProductInfo] error query mysql, err:%+v", err)
		return nil, err
	}
	return &productInfo, nil
}

func (d ProductDAO) GetProductByProjectId(projectId string) (*entity.Product, 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
	}
	productInfo := entity.Product{}
	err = Db.Model(entity.Product{}).Where("product_id = ?", productId).Find(&productInfo).Error
	if err != nil {
		logrus.Errorf("[GetProductInfo] error query mysql, err:%+v", err)
		return nil, err
	}
	return &productInfo, nil
}

// 获取商品类目
func (d ProductDAO) GetProductCategorys() ([]entity.InfoProductCategory, error) {
	var infoProductCategorys []entity.InfoProductCategory
	err := Db.Model(&entity.InfoProductCategory{}).Find(&infoProductCategorys).Order("id").Error
	return infoProductCategorys, err
}

// 判断当前商家快手商品是否存在
func (d ProductDAO) CheckKuaishouProduct(enterpriseId string, kuaishouProductId string) int64 {
	var count int64
	Db.Model(&entity.Product{}).Where("enterprise_id = ? and kuaishou_product_id = ?", enterpriseId, kuaishouProductId).Count(&count)
	return count
}
