package yb

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/utils"
)

type VoiceSection struct {
	SectionId   int    `orm:"column(section_id);pk" description:"板块id"`
	SectionName string `description:"板块名称"`
	ImgUrl      string `description:"背景图url"`
	VarietyId   int    `description:"品种id"`
	VarietyName string `description:"品种名称"`
	Status      int    `description:"角色状态"`
	CreateTime  string `description:"创建时间"`
}

// TableName 表名变更
func (voiceSection *VoiceSection) TableName() string {
	return "yb_voice_section"
}

func GetVoiceSectionList(startSize, pageSize int) (list []*VoiceSection, err error) {
	o := orm.NewOrm()
	sql := "SELECT * FROM yb_voice_section ORDER BY create_time DESC limit ?, ? "
	_, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
	return
}

func AddVoiceSection(item VoiceSection) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(&item)
	if err != nil {
		return err
	}
	return
}

func GetVoiceSectionTotal() (total int, err error) {
	o := orm.NewOrm()
	sql := "SELECT COUNT(1) AS ct FROM yb_voice_section "
	err = o.Raw(sql).QueryRow(&total)
	return
}

func GetVoiceSectionEnableTotal() (total int, err error) {
	o := orm.NewOrm()
	sql := "SELECT COUNT(1) AS ct FROM yb_voice_section WHERE status=1 "
	err = o.Raw(sql).QueryRow(&total)
	return
}

func EditVoiceSection(condition string, pars []interface{}) (err error) {
	o := orm.NewOrm()
	sql := "UPDATE yb_voice_section SET status=? WHERE 1=1 "

	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).Exec()
	return
}

// Update 更新语音播报板块
func (voiceSection *VoiceSection) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(voiceSection, cols...)
	return
}

// GetAllEnableVoiceSectionList 获取所有启用的语言播报版块
func GetAllEnableVoiceSectionList() (list []*VoiceSection, err error) {
	o := orm.NewOrm()
	sql := "SELECT * FROM yb_voice_section where status=1 ORDER BY create_time ASC "
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetVoiceSectionById 根据版块id来获取版块信息
func GetVoiceSectionById(sectionId int) (item *VoiceSection, err error) {
	o := orm.NewOrm()
	sql := "SELECT * FROM yb_voice_section WHERE section_id= ? "
	err = o.Raw(sql, sectionId).QueryRow(&item)
	return
}

// GetVoiceSectionByIds 根据IDs获取板块
func GetVoiceSectionByIds(ids []int) (list []*VoiceSection, err error) {
	arrLen := len(ids)
	if arrLen == 0 {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_voice_section WHERE section_id IN (` + utils.GetOrmInReplace(arrLen) + `)`
	_, err = o.Raw(sql, ids).QueryRows(&list)
	return
}