package speech_recognition

import (
	"eta/eta_api/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
)

// SpeechRecognitionTagMapping 语音识别-标签关联表
type SpeechRecognitionTagMapping struct {
	Id                  int `orm:"column(id);pk"`
	SpeechRecognitionId int `description:"语音识别ID"`
	TagId               int `description:"标签ID"`
}

var SpeechRecognitionTagMappingCols = struct {
	Id                  string
	SpeechRecognitionId string
	TagId               string
}{
	Id:                  "id",
	SpeechRecognitionId: "speech_recognition_id",
	TagId:               "tag_id",
}

func (m *SpeechRecognitionTagMapping) TableName() string {
	return "speech_recognition_tag_mapping"
}

func (m *SpeechRecognitionTagMapping) PrimaryId() string {
	return SpeechRecognitionTagMappingCols.Id
}

func (m *SpeechRecognitionTagMapping) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.Id = int(id)
	return
}

func (m *SpeechRecognitionTagMapping) CreateMulti(items []*SpeechRecognitionTagMapping) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(items), items)
	return
}

func (m *SpeechRecognitionTagMapping) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

func (m *SpeechRecognitionTagMapping) Del() (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.Id).Exec()
	return
}

func (m *SpeechRecognitionTagMapping) MultiDel(menuIds []int) (err error) {
	if len(menuIds) == 0 {
		return
	}
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
	_, err = o.Raw(sql, menuIds).Exec()
	return
}

func (m *SpeechRecognitionTagMapping) GetItemById(id int) (item *SpeechRecognitionTagMapping, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *SpeechRecognitionTagMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SpeechRecognitionTagMapping, err error) {
	o := orm.NewOrm()
	order := ``
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *SpeechRecognitionTagMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *SpeechRecognitionTagMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SpeechRecognitionTagMapping, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := ``
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *SpeechRecognitionTagMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SpeechRecognitionTagMapping, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := ``
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// ClearMappingBySpeechId 清除转写文件标签关联
func (m *SpeechRecognitionTagMapping) ClearMappingBySpeechId(speechId int) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, m.TableName(), SpeechRecognitionTagMappingCols.SpeechRecognitionId)
	_, err = o.Raw(sql, speechId).Exec()
	return
}

func (m *SpeechRecognitionTagMapping) GetSpeechIdsByTagIds(tagIds []int) (speechIds []int, err error) {
	if len(tagIds) == 0 {
		return
	}
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE %s IN (%s) GROUP BY %s`, SpeechRecognitionTagMappingCols.SpeechRecognitionId, m.TableName(), SpeechRecognitionTagMappingCols.TagId, utils.GetOrmInReplace(len(tagIds)), SpeechRecognitionTagMappingCols.SpeechRecognitionId)
	_, err = o.Raw(sql, tagIds).QueryRows(&speechIds)
	return
}
