package yb

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// ActivityType 活动分类表
type ActivityType struct {
	ActivityTypeId    int       `orm:"column(activity_type_id);pk" description:"活动类型ID"`
	ActivityTypeName  string    `description:"活动类型名称"`
	Pid               int       `description:"父级id，默认是：0"`
	ChartPermissionId string    `description:"权限id，多个id用英文逗号分割"`
	Sort              int       `description:"排序字段"`
	FrontIsShow       int       `description:"是否在前台展示，0：不展示，1：展示"`
	CreateTime        time.Time `description:"创建时间"`
}

// TableName 表名变更
func (activityType *ActivityType) TableName() string {
	return "yb_activity_type"
}

// AddActivityType 新增活动类型
func AddActivityType(item *ActivityType) (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(item)
	if err != nil {
		return
	}
	item.ActivityTypeId = int(id)
	return
}

// Update 更新活动类型
func (activityType *ActivityType) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(activityType, cols...)
	return
}

type ActivityTypeList struct {
	ActivityTypeId   int                 `description:"活动类型ID"`
	ActivityTypeName string              `description:"活动类型名称"`
	Pid              int                 `description:"父级id，默认是：0"`
	Sort             int                 `description:"排序字段"`
	FrontIsShow      int8                `description:"是否在前台展示，0：不展示，1：展示"`
	CreateTime       time.Time           `description:"创建时间"`
	ChildList        []*ActivityTypeList `description:"下级活动类型列表"`
}

// GetActivityTypeListByPid 获取最高级的活动分类
func GetActivityTypeListByid(pid int) (list []*ActivityType, err error) {
	o := orm.NewOrm()
	sql := `select * from yb_activity_type where pid=? order by sort asc ,activity_type_id asc`
	_, err = o.Raw(sql, pid).QueryRows(&list)
	return
}

// GetActivityTypeListById 获取分类详情
func GetActivityTypeListById(pid int) (item *ActivityType, err error) {
	o := orm.NewOrm()
	sql := `select * from yb_activity_type where activity_type_id=? `
	err = o.Raw(sql, pid).QueryRow(&item)
	return
}

// GetActivityTypeListByActivityTypePId 根据上级id获取活动分类列表
func GetActivityTypeListByActivityTypePId(pid int) (list []*ActivityTypeList, err error) {
	o := orm.NewOrm()
	sql := `select * from yb_activity_type where pid=? AND front_is_show = 1 order by sort asc ,activity_type_id asc`
	_, err = o.Raw(sql, pid).QueryRows(&list)
	return
}

// GetActivityTypeByName 根据名称获取活动类型
func GetActivityTypeByName(name string) (item *ActivityType, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_activity_type WHERE activity_type_name = ? LIMIT 1`
	err = o.Raw(sql, name).QueryRow(&item)
	return
}

// GetActivityTypeByCondition 获取活动分类列表
func GetActivityTypeByCondition(condition string, pars []interface{}) (items []*ActivityType, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_activity_type WHERE 1 = 1 `
	if condition != `` {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
