package gmodel

import (
	"context"
	"gorm.io/gorm"
)

type FsProduct struct {
	Id                   int64   `gorm:"primary_key" json:"id"`
	Sn                   *string `gorm:"default:''" json:"sn"`                     // 商品编号 P98f087j
	Type                 *int64  `gorm:"default:0" json:"type"`                    // 分类ID
	Title                *string `gorm:"default:''" json:"title"`                  // 名称
	TitleCn              *string `gorm:"default:''" json:"title_cn"`               // 中文名称
	Cover                *string `gorm:"default:''" json:"cover"`                  // 封面图
	Imgs                 *string `gorm:"default:''" json:"imgs"`                   // 一个或多个介绍图或视频
	Keywords             *string `gorm:"default:''" json:"keywords"`               // 关键字
	Intro                *string `gorm:"default:''" json:"intro"`                  // 简要描述
	Sort                 *int64  `gorm:"default:0" json:"sort"`                    // 排序
	SelledNum            *int64  `gorm:"default:0" json:"selled_num"`              // 已卖数量
	Ctime                *int64  `gorm:"default:0" json:"ctime"`                   // 添加时间
	View                 *int64  `gorm:"default:0" json:"view"`                    // 浏览量
	SizeIds              *string `gorm:"default:''" json:"size_ids"`               // 尺寸 1,2,3,4
	MaterialIds          *string `gorm:"default:''" json:"material_ids"`           // 材质 1,2,3
	TagIds               *string `gorm:"default:''" json:"tag_ids"`                // 标签 逗号间隔
	Status               *int64  `gorm:"default:1" json:"status"`                  // 状态位  弃用
	ProduceDays          *int64  `gorm:"default:0" json:"produce_days"`            // 生产天数
	DeliveryDays         *int64  `gorm:"default:0" json:"delivery_days"`           // 运送天数
	CoverImg             *string `gorm:"default:''" json:"cover_img"`              // 背景图
	IsShelf              *int64  `gorm:"default:1" json:"is_shelf"`                // 是否上架
	IsRecommend          *int64  `gorm:"default:1" json:"is_recommend"`            // 是否推荐
	IsHot                *int64  `gorm:"default:1" json:"is_hot"`                  // 是否热销
	IsProtection         *int64  `gorm:"default:1" json:"is_protection"`           // 是否环保
	IsMicrowave          *int64  `gorm:"default:1" json:"is_microwave"`            // 是否可微波炉
	IsDel                *int64  `gorm:"default:0" json:"is_del"`                  // 是否删除
	RecommendProduct     *string `gorm:"default:''" json:"recommend_product"`      // 推荐产品id例如: 1,3,4,5
	RecommendProductSort *string `gorm:"default:''" json:"recommend_product_sort"` // 推荐排序例如:1324
	SceneIds             *string `gorm:"default:''" json:"scene_ids"`              // 关联的场景id
}
type FsProductModel struct {
	db *gorm.DB
}

func NewFsProductModel(db *gorm.DB) *FsProductModel {
	return &FsProductModel{db}
}

func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) {
	if len(productIds) == 0 {
		return
	}
	db := p.db.Model(&FsProduct{}).
		Where("`id` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?", productIds, 0, 1, 1)
	switch sort {
	case "sort-asc":
		db = db.Order("`sort` ASC")
	case "sort-desc":
		db = db.Order("`sort` DESC")
	}
	if err = db.Find(&resp).Error; err != nil {
		return nil, err
	}
	return
}

func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTypes []int64, sort string) (resp []FsProduct, err error) {
	if len(productTypes) == 0 {
		return
	}
	db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`type` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?", productTypes, 0, 1, 1)
	switch sort {
	case "sort-asc":
		db = db.Order("`sort` ASC")
	case "sort-desc":
		db = db.Order("`sort` DESC")
	}
	err = db.Find(&resp).Error
	if err != nil {
		return nil, err
	}
	return
}
func (p *FsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) {
	err = p.db.WithContext(ctx).Model(&FsProduct{}).
		Where("`is_del` =? and `is_shelf` = ?", 0, 1).Order("RAND()").Limit(limit).Find(&resp).Error
	if err != nil {
		return nil, err
	}
	return
}