package model

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

type ControlBy struct {
	CreateBy int `json:"createBy" gorm:"index;comment:创建者"` // 创建者
	UpdateBy int `json:"updateBy" gorm:"index;comment:更新者"` // 更新者
}

// SetCreateBy 设置创建人id
func (e *ControlBy) SetCreateBy(createBy int) {
	e.CreateBy = createBy
}

// SetUpdateBy 设置修改人id
func (e *ControlBy) SetUpdateBy(updateBy int) {
	e.UpdateBy = updateBy
}

type DeptBy struct {
	DeptId int `json:"deptId" gorm:"index;comment:部门id"` // 部门id
}

// SetCreateBy 设置创建人id
func (e *DeptBy) SetDeptId(deptId int) {
	e.DeptId = deptId
}

type Model struct {
	Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"` // 主键编码
}

type ModelTime struct {
	CreatedAt Time           `json:"createdAt" gorm:"type:timestamp;comment:创建时间"`                  // 创建时间
	UpdatedAt Time           `json:"updatedAt" gorm:"type:timestamp;comment:最后更新时间;autoUpdateTime"` // 最后更新时间
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`
}

const timeFormat = "2006-01-02 15:04:05"
const timezone = "Asia/Shanghai"

// 全局定义
type Time time.Time

func (t Time) MarshalJSON() ([]byte, error) {
	b := make([]byte, 0, len(timeFormat)+2)
	if time.Time(t).IsZero() {
		b = append(b, '"')
		b = append(b, '"')
		return b, nil
	}

	b = append(b, '"')
	b = time.Time(t).AppendFormat(b, timeFormat)
	b = append(b, '"')
	return b, nil
}

func (t *Time) UnmarshalJSON(data []byte) (err error) {
	now, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), time.Local)
	*t = Time(now)
	return
}

func (t Time) String() string {
	if time.Time(t).IsZero() {
		return ""
	}
	return time.Time(t).Format(timeFormat)
}

func (t Time) Local() time.Time {
	loc, _ := time.LoadLocation(timezone)
	return time.Time(t).In(loc)
}

func (t Time) Value() (driver.Value, error) {
	var zeroTime time.Time
	var ti = time.Time(t)
	if ti.UnixNano() == zeroTime.UnixNano() {
		return nil, nil
	}
	return ti, nil
}

func (t *Time) Scan(v interface{}) error {
	value, ok := v.(time.Time)
	if ok {
		*t = Time(value)
		return nil
	}
	return fmt.Errorf("can not convert %v to timestamp", v)
}

type StringList []string
type FloatList []float64
type IntList []int

func (f *IntList) Scan(value interface{}) error {
	if value == nil {
		return nil
	}

	switch v := value.(type) {
	case string:
		return json.Unmarshal([]byte(v), f)
	case []byte:
		return json.Unmarshal(v, f)
	case int:
		*f = IntList{v}
		return nil
	default:
		return errors.New("cannot unmarshal type into FloatList")
	}
}
func (f IntList) Value() (driver.Value, error) {
	return json.Marshal(f)
}
func (f *FloatList) Scan(value interface{}) error {
	if value == nil {
		return nil
	}

	switch v := value.(type) {
	case string:
		return json.Unmarshal([]byte(v), f)
	case []byte:
		return json.Unmarshal(v, f)
	case float64:
		*f = FloatList{v}
		return nil
	default:
		return errors.New("cannot unmarshal type into FloatList")
	}
}

func (f FloatList) Value() (driver.Value, error) {
	return json.Marshal(f)
}
func (e StringList) Value() (driver.Value, error) {
	d, err := json.Marshal(e)
	return string(d), err
}

func (e *StringList) Scan(src interface{}) error {
	return json.Unmarshal(src.([]byte), e)
}
