package models

import "time"

type Welcome struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	UserId    string    `gorm:"size:100 not null default:'' index:'user_id'" json:"user_id"`
	Content   string    `gorm:"size:512 not null default:''" json:"content"`
	IsDefault uint      `gorm:"size:3 not null default:0" json:"is_default"`
	CreatedAt time.Time `json:"ctime"`
}

func CreateWelcome(userId string, content string) uint {
	if userId == "" || content == "" {
		return 0
	}
	w := &Welcome{
		UserId:  userId,
		Content: content,
	}
	DB.Create(w)
	return w.ID
}
func UpdateWelcome(userId string, id string, content string) uint {
	if userId == "" || content == "" {
		return 0
	}
	w := &Welcome{
		Content: content,
	}
	DB.Model(w).Where("user_id = ? and id = ?", userId, id).Updates(w)
	return w.ID
}
func FindWelcomeByUserId(userId interface{}) Welcome {
	var w Welcome
	DB.Where("user_id = ? and is_default=?", userId, 1).First(&w)
	return w
}
func FindWelcomesByUserId(userId interface{}) []Welcome {
	var w []Welcome
	DB.Where("user_id = ?", userId).Find(&w)
	return w
}
func DeleteWelcome(userId interface{}, id string) {
	DB.Where("user_id = ? and id = ?", userId, id).Delete(Welcome{})
}
