package models

import (
	"exhibitionCentre/database"
	"exhibitionCentre/logging"

	"gorm.io/gorm"
)

type Exhibit struct {
	gorm.Model
	Number             int     `gorm:"not null"` // 展位编号1开始
	Width              float32 `gorm:"default:1"`
	Height             float32 `gorm:"default:1"`
	OffsetX            float32
	OffsetY            float32
	Url                string
	Title              string `gorm:"type:varchar(256)"`
	Audio              string // 语音介绍
	Introduction       string // 文字介绍
	Details            string // json数据格式[{"media":"http://xxxxxx","detail":"《Three masks》"}]
	AuthorIntroduction string
	StayTime           int  `gorm:"default:3"` // 驻足停留时间，默认值为3s
	Likes              int  // 点赞数
	Flowers            int  // 送花数
	Applauses          int  // 鼓掌数
	RoomIndex          int  `gorm:"not null"` // 1为开幕式，2为展厅，3为演播厅
	ExhibitionId       uint `gorm:"not null"`
}

type ExhibitDetail struct {
	Media  string `json:"media"`
	Detail string `json:"detail"`
}

func init() {
	if err := database.DB.AutoMigrate(&Exhibit{}); err != nil {
		logging.Log.Fatalf("Mysql初始化Exhibit表失败: %s\n", err.Error())
	}
	logging.Log.Info("Mysql初始化Exhibit表完成.")
}

func CreateExhibit(ex *Exhibit) error {
	if err := database.DB.Create(ex).Error; err != nil {
		return err
	}
	return nil // 成功创建
}

func GetExhibit(id int) (*Exhibit, error) {
	// 获取第一个id为指定id的展位
	var exhibit Exhibit
	if err := database.DB.First(&exhibit, "id = ?", uint(id)).Error; err != nil {
		return nil, err
	}
	return &exhibit, nil
}

func GetExhibitByNumber(exhibitionId, roomIndex, number int) (*Exhibit, error) {
	// 获取第一个id为指定id的展位
	var exhibit Exhibit
	if err := database.DB.First(&exhibit, "exhibition_id = ? AND room_index = ? AND number = ?", exhibitionId, roomIndex, number).Error; err != nil {
		return nil, err
	}
	return &exhibit, nil
}

func GetExhibitsByExhibitionId(exhibitionId int) ([]Exhibit, error) {
	var exhibits []Exhibit
	if err := database.DB.Order("number asc").Find(&exhibits, "exhibition_id = ?", exhibitionId).Error; err != nil {
		return exhibits, err
	}
	return exhibits, nil
}

func GetExhibitsByIds(ids []int) ([]Exhibit, error) {
	var exhibits []Exhibit
	if len(ids) == 0 {
		return exhibits, nil
	}
	if err := database.DB.Order("number asc").Find(&exhibits, ids).Error; err != nil {
		return exhibits, err
	}
	return exhibits, nil
}

func GetExhibitsByExhibitionIdAndRoomIndex(exhibitionId, roomIndex int) ([]Exhibit, error) {
	var exhibits []Exhibit
	if err := database.DB.Order("number asc").Find(&exhibits, "exhibition_id = ? AND room_index = ?", exhibitionId, roomIndex).Error; err != nil {
		return exhibits, err
	}
	return exhibits, nil
}

func UpdateExhibit(exhibit *Exhibit) error {
	if err := database.DB.Save(exhibit).Error; err != nil {
		return err
	}
	return nil
}

func DeleteExhibit(id int) error {
	if err := database.DB.Delete(&Exhibit{}, "id = ?", id).Error; err != nil {
		return err
	}
	// 同时删除与之关联的订单
	DeleteOrders(id)
	return nil
}

func DeleteExhibits(exhibits []Exhibit) error {
	if len(exhibits) == 0 {
		return nil
	}
	if err := database.DB.Delete(&exhibits).Error; err != nil {
		return err
	}
	// 同时删除与之关联的订单
	var ids []int
	for _, ex := range exhibits {
		ids = append(ids, int(ex.ID))
	}
	DeleteOrdersByExhibits(ids)
	return nil
}
