package models

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

type MediaPermissionMapping struct {
	Id           int       `orm:"pk" description:"id"` // 主键
	PermissionId int       `description:"权限ID"`        // 权限 ID
	MediaId      int       `description:"媒体ID"`        // 媒体 ID
	MediaType    MediaType `description:"媒体类型"`        // 媒体类型
	Deleted      int       `description:"是否删除"`
	CreatedTime  time.Time `description:"创建时间"`
	UpdatedTime  time.Time `description:"更新时间"`
}

// TableName returns the table name for this model.
func (mp *MediaPermissionMapping) TableName() string {
	return "media_permission_mappings"
}

func DeleteMappingsById(mediaId int) (err error) {
	o := orm.NewOrm()
	_, err = o.QueryTable(new(MediaPermissionMapping)).Filter("media_id", mediaId).Update(orm.Params{"deleted": 1})
	return
}

func GetMappingsByCondition(condition string, pars []interface{}) (items []int64, err error) {
	o := orm.NewOrm()
	sql := `SELECT distinct  media_id FROM media_permission_mappings WHERE DELETED=0 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
