package roadshow

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

type RsCalendarMeetingLabelGroup struct {
	LabelId      int       `orm:"column(label_id);pk" description:"主键ID"`
	RsCalendarId int       `description:"路演ID"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
	AdminId      int       `description:"管理员ID"`
	AdminName    string    `description:"管理员姓名"`
	TagType      int       `description:"标签类型，1产业，2标的，3行业权限"`
	TagId        int       `description:"标签ID"`
}

// 新增
func AddRsCalendarMeetingLabelGroup(item *RsCalendarMeetingLabelGroup) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

// AddRsCalendarMeetingLabelGroupMulti 批量添加
func AddRsCalendarMeetingLabelGroupMulti(items []*RsCalendarMeetingLabelGroup, rsCalendarId int) (err error) {
	if len(items) == 0 {
		return
	}
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()

	//删除历史记录
	sql := " DELETE FROM rs_calendar_meeting_label_group  WHERE rs_calendar_id = ? "
	_, err = o.Raw(sql, rsCalendarId).Exec()
	if err != nil {
		return
	}

	//批量插入
	_, err = o.InsertMulti(len(items), items)
	if err != nil {
		return
	}
	return
}

// 获取数量
func GetRsCalendarMeetingLabelGroupByRsCalendarId(rsCalendarId int) (count int, err error) {
	o := orm.NewOrm()
	sqlCount := ` SELECT COUNT(1) AS count  FROM rs_calendar_meeting_label_group WHERE   rs_calendar_id   = ?`
	err = o.Raw(sqlCount, rsCalendarId).QueryRow(&count)
	return
}

// 获取详情
func GetRsCalendarMeetingLabelGroupDetailByRsCalendarId(rsCalendarId int) (item *RsCalendarMeetingLabelGroup, err error) {
	o := orm.NewOrm()
	sql := ` SELECT *  FROM rs_calendar_meeting_label_group WHERE   rs_calendar_id   = ? `
	err = o.Raw(sql, rsCalendarId).QueryRow(&item)
	return
}

// 根据多个路演ID获取研信息
func GetRsCalendarMeetingLabelGroupListByRsCalendarId(rsCalendarId int) (item []*RsCalendarMeetingLabelGroup, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM rs_calendar_meeting_label_group WHERE rs_calendar_id  = ? `
	_, err = o.Raw(sql, rsCalendarId).QueryRows(&item)
	return
}

// 根据多个路演ID获取研信息
func GetRsCalendarMeetingLabelGroupDetailByRsCalendarIds(rsCalendarIds []int) (item []*RsCalendarMeetingLabelGroup, err error) {
	if len(rsCalendarIds) == 0 {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT * FROM rs_calendar_meeting_label_group WHERE rs_calendar_id IN (` + utils.GetOrmInReplace(len(rsCalendarIds)) + `) `
	_, err = o.Raw(sql, rsCalendarIds).QueryRows(&item)
	return
}

// 修改
func UpdateProductInterior(item *RsCalendarMeetingLabelGroup) (err error) {
	o := orm.NewOrm()
	updateParams := make(map[string]interface{})
	updateParams["AdminId"] = item.AdminId
	updateParams["AdminName"] = item.AdminName
	updateParams["TagType"] = item.TagType
	updateParams["TagId"] = item.TagId
	updateParams["ModifyTime"] = item.ModifyTime
	ptrStructOrTableName := "rs_calendar_meeting_label_group"
	whereParam := map[string]interface{}{"rs_calendar_id": item.RsCalendarId}
	qs := o.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

// 根据类型、id 获取对应的分组信息
func GetRsCalendarMeetingLabelGroupDetailByTag(tagType, tagId int) (item []*RsCalendarMeetingLabelGroup, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM rs_calendar_meeting_label_group WHERE tag_type = ? AND tag_id  = ?  `
	_, err = o.Raw(sql, tagType, tagId).QueryRows(&item)
	return
}

// 根据类型、id 获取对应的分组信息
func GetRsCalendarMeetingLabelGroupDetailByTagIds(tagType int, tagIds []int) (item []*RsCalendarMeetingLabelGroup, err error) {
	lenArr := len(tagIds)
	if lenArr == 0 {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT * FROM rs_calendar_meeting_label_group WHERE tag_type = ? AND tag_id IN  (` + utils.GetOrmInReplace(lenArr) + `)  `
	_, err = o.Raw(sql, tagType, tagIds).QueryRows(&item)
	return
}
