package dao

import (
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"gorm.io/gorm"
)

type SoundPeriod struct {
	gorm.Model
	Time    string   `json:"time" gorm:"column:time;comment:'星期'"`
	Period0 IntArray `json:"period0" gorm:"column:period0;comment:'时间音量0';type:json"`
	Period1 IntArray `json:"period1" gorm:"column:period1;comment:'时间音量1';type:json"`
	Period2 IntArray `json:"period2" gorm:"column:period2;comment:'时间音量2';type:json"`
	Period3 IntArray `json:"period3" gorm:"column:period3;comment:'时间音量3';type:json"`
	Period4 IntArray `json:"period4" gorm:"column:period4;comment:'时间音量4';type:json"`
	Period5 IntArray `json:"period5" gorm:"column:period5;comment:'时间音量5';type:json"`
	Period6 IntArray `json:"period6" gorm:"column:period6;comment:'时间音量6';type:json"`
	Period7 IntArray `json:"period7" gorm:"column:period7;comment:'时间音量7';type:json"`
}

func (SoundPeriod) TableName() string {
	return "dev_sound_period"
}

type IntArray []int

// Value 实现 driver.Valuer 接口，用于保存到数据库
func (a IntArray) Value() (driver.Value, error) {
	return json.Marshal(a)
}

// Scan 实现 sql.Scanner 接口，用于从数据库读取值
func (a *IntArray) Scan(value interface{}) error {
	b, ok := value.([]byte)
	if !ok {
		return fmt.Errorf("failed to unmarshal IntArray value")
	}
	return json.Unmarshal(b, &a)
}
