package newbond

import (
	"database/sql/driver"
	"fmt"
	"time"
)

type BondData struct {
	SecurityCode     string `json:"SECURITY_CODE" db:"SecurityCode"`
	SecuCode         string `json:"SECUCODE" db:"SecuCode"`
	TradeMarket      string `json:"TRADE_MARKET" db:"TradeMarket"`
	SecurityNameAbbr string `json:"SECURITY_NAME_ABBR" db:"SecurityNameAbbr"`
}

type CustomTime struct {
	time.Time
}

// 实现 driver.Valuer 接口
func (ct CustomTime) Value() (driver.Value, error) {
	return ct.Time, nil
}

// 实现 sql.Scanner 接口
func (ct *CustomTime) Scan(value interface{}) error {
	if value == nil {
		ct.Time = time.Time{}
		return nil
	}
	switch v := value.(type) {
	case time.Time:
		ct.Time = v
		return nil
	default:
		return fmt.Errorf("cannot scan type %T into CustomTime", value)
	}
}

// 实现 UnmarshalJSON 方法以支持自定义时间格式
func (ct *CustomTime) UnmarshalJSON(b []byte) error {
	str := string(b)
	str = str[1 : len(str)-1] // 去掉引号

	if str == "null" {
		return nil
	}

	// 使用正确的时间格式进行解析
	parsedTime, err := time.Parse("2006-01-02 15:04:05", str)
	if err != nil {
		return err
	}

	ct.Time = parsedTime
	return nil
}

type BondResponse struct {
	Version string     `json:"version"`
	Result  BondResult `json:"result"`
	Success bool       `json:"success"`
	Message string     `json:"message"`
	Code    int        `json:"code"`
}

type BondResult struct {
	Pages int        `json:"pages"`
	Data  []BondData `json:"data"`
	Count int        `json:"count"`
}
