package service

import (
	"cc-officialweb/models"
	"cc-officialweb/utils"
)

// GetIndexProductServe   首页获取产品服务
func GetIndexProductServe(types string) []models.Products {
	var serve []models.Products
	tx := utils.DB.Where("type = ?", types).Where("is_index = ?", true).Order("sort asc").Find(&serve)
	if tx.Error != nil {
		return nil
	}
	return serve
}

// GetProduct   获取所有产品
func GetProduct(ptype string) []models.Products {
	var serve []models.Products
	tx := utils.DB.Where("type = ?", "product").Where("ptype = ?", ptype).Order("sort asc").Find(&serve)
	if tx.Error != nil {
		return nil
	}
	return serve
}

// GetProductDetail   获取产品详情
func GetProductDetail(id int) models.Products {
	var server models.Products
	tx := utils.DB.Where("id = ?", id).First(&server)
	if tx.Error != nil {
		return server
	}
	return server
}

// AddProduct 添加产品
func AddProduct(product models.ProductDto) bool {
	products := models.Products{
		Title:               &product.Title,
		Synopsis:            &product.Synopsis,
		Detail:              &product.Detail,
		Type:                &product.Type,
		Ptype:               &product.Ptype,
		IsIndex:             &product.IsIndex,
		Url:                 &product.Url,
		ProductIntroduction: &product.ProductIntroduction,
		TechnicalParameters: &product.TechnicalParameters,
		Instructions:        &product.Instructions,
		SupportingSoftware:  &product.SupportingSoftware,
		OptionalAccessories: &product.OptionalAccessories,
		IsActive:            &product.IsActive,
	}
	tx := utils.DB.Create(&products)
	if tx.RowsAffected > 0 {
		return true
	}
	return false
}

// AddProductType 添加分类
func AddProductType(productType models.ProductType) bool {
	tx := utils.DB.Create(&productType)
	if tx.RowsAffected > 0 {
		return true
	}
	return false
}

// GetProductType 获取所有分类
func GetProductType() []models.ProductType {
	var serve []models.ProductType
	tx := utils.DB.Find(&serve)
	if tx.Error != nil {
		return nil
	}
	return serve
}

// UpdateProductType 修改分类
func UpdateProductType(id int, productType models.ProductType) bool {
	tx := utils.DB.Model(&productType).Where("id =?", id).Updates(&productType)
	if tx.RowsAffected > 0 {
		return true
	}
	return false
}
func DeleteProduct(id int) bool {
	tx := utils.DB.Delete(&models.Products{}, id)
	if tx.RowsAffected > 0 {
		return true
	}
	return false
}
