package speech_recognition

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

// SpeechRecognitionTag 语音识别-标签表
type SpeechRecognitionTag struct {
	SpeechRecognitionTagId int       `orm:"column(speech_recognition_tag_id);pk"`
	UniqueCode             string    `description:"唯一编码"`
	TagName                string    `description:"标签名称"`
	MenuId                 int       `description:"目录ID"`
	Sort                   int       `description:"排序"`
	CreateTime             time.Time `description:"创建时间"`
	ModifyTime             time.Time `description:"修改时间"`
}

var SpeechRecognitionTagCols = struct {
	SpeechRecognitionTagId string
	UniqueCode             string
	TagName                string
	MenuId                 string
	MenuPath               string
	Sort                   string
	CreateTime             string
	ModifyTime             string
}{
	SpeechRecognitionTagId: "speech_recognition_tag_id",
	UniqueCode:             "unique_code",
	TagName:                "tag_name",
	MenuId:                 "menu_id",
	MenuPath:               "menu_path",
	Sort:                   "sort",
	CreateTime:             "create_time",
	ModifyTime:             "modify_time",
}

func (m *SpeechRecognitionTag) TableName() string {
	return "speech_recognition_tag"
}

func (m *SpeechRecognitionTag) PrimaryId() string {
	return SpeechRecognitionTagCols.SpeechRecognitionTagId
}

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

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

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

func (m *SpeechRecognitionTag) 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.SpeechRecognitionTagId).Exec()
	return
}

func (m *SpeechRecognitionTag) 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 *SpeechRecognitionTag) GetItemById(id int) (item *SpeechRecognitionTag, 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 *SpeechRecognitionTag) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SpeechRecognitionTag, 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 *SpeechRecognitionTag) 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 *SpeechRecognitionTag) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SpeechRecognitionTag, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	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 *SpeechRecognitionTag) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SpeechRecognitionTag, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	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
}

// SpeechRecognitionTagItem 语音识别标签
type SpeechRecognitionTagItem struct {
	UniqueCode string `description:"唯一编码"`
	TagId      int    `description:"标签ID"`
	TagName    string `description:"标签名称"`
	MenuId     int    `description:"目录ID"`
	Sort       int    `description:"排序"`
	CreateTime string `description:"创建时间"`
}

func FormatSpeechRecognitionTag2Item(origin *SpeechRecognitionTag) (item *SpeechRecognitionTagItem) {
	if origin == nil {
		return
	}
	item = new(SpeechRecognitionTagItem)
	item.UniqueCode = origin.UniqueCode
	item.TagId = origin.SpeechRecognitionTagId
	item.TagName = origin.TagName
	item.MenuId = origin.MenuId
	item.Sort = origin.Sort
	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
	return
}

// SpeechRecognitionTagAddReq 新增标签
type SpeechRecognitionTagAddReq struct {
	MenuId  int    `description:"目录ID"`
	TagName string `description:"标签名称"`
}

// SpeechRecognitionTagEditReq 编辑标签
type SpeechRecognitionTagEditReq struct {
	TagId   int    `description:"标签ID"`
	TagName string `description:"标签名称"`
}

// SpeechRecognitionTagRemoveReq 删除标签
type SpeechRecognitionTagRemoveReq struct {
	TagId int `description:"标签ID"`
}

// UpdateSortByMenuId 根据分类ID更新排序
func (m *SpeechRecognitionTag) UpdateSortByMenuId(menuId, nowSort int, prevTagId int, updateSort string) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`UPDATE %s SET %s = %s WHERE %s = ? `, m.TableName(), SpeechRecognitionTagCols.Sort, updateSort, SpeechRecognitionTagCols.MenuId)
	if prevTagId > 0 {
		sql += fmt.Sprintf(` AND (%s > ? OR (%s > %d AND %s = %d))`, SpeechRecognitionTagCols.Sort, SpeechRecognitionTagCols.SpeechRecognitionTagId, prevTagId, SpeechRecognitionTagCols.Sort, nowSort)
	} else {
		sql += fmt.Sprintf(` AND %s > ?`, SpeechRecognitionTagCols.Sort)
	}
	_, err = o.Raw(sql, menuId, nowSort).Exec()
	return
}

// GetMaxSortByMenuId 获取分类下最大Sort
func (m *SpeechRecognitionTag) GetMaxSortByMenuId(menuId int) (sort int, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT MAX(sort) AS sort FROM %s WHERE %s = ?`, m.TableName(), SpeechRecognitionTagCols.MenuId)
	err = o.Raw(sql, menuId).QueryRow(&sort)
	return
}

// GetFirstByMenuId 获取目录下排序第一的数据
func (m *SpeechRecognitionTag) GetFirstByMenuId(menuId int) (item *SpeechRecognitionTag, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? ORDER BY %s ASC, %s ASC LIMIT 1`, m.TableName(), SpeechRecognitionTagCols.MenuId, SpeechRecognitionTagCols.Sort, SpeechRecognitionTagCols.SpeechRecognitionTagId)
	err = o.Raw(sql, menuId).QueryRow(&item)
	return
}

func (m *SpeechRecognitionTag) GetTagIdsByMenuIds(menuIds []int) (tagIds []int, err error) {
	if len(menuIds) == 0 {
		return
	}
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE %s IN (%s) GROUP BY %s`, SpeechRecognitionTagCols.SpeechRecognitionTagId, m.TableName(), SpeechRecognitionTagCols.MenuId, utils.GetOrmInReplace(len(menuIds)), SpeechRecognitionTagCols.SpeechRecognitionTagId)
	_, err = o.Raw(sql, menuIds).QueryRows(&tagIds)
	return
}
