package cygx

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

//活动发布推送的Openid记录

type CygxActivityUserTemplateRecord struct {
	TemplateId int       `orm:"column(template_id);pk"description:"主键ID"`
	ActivityId int       ` description:"活动ID"`
	UserId     int       ` description:"用户ID"`
	OpenId     string    ` description:"open_id "`
	SendType   int       ` description:"类型 "`
	CreateTime time.Time `description:"创建时间"`
}

// AddCygxActivityUserTemplateRecordMulti 批量添加
func AddCygxActivityUserTemplateRecordMulti(items []*CygxActivityUserTemplateRecord) (err error) {
	o, err := orm.NewOrmUsingDB("hz_cygx").Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	//批量插入新的关联数据
	if len(items) > 0 {
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

// GeUserTemplateRecordListBysendType 根据模板消息会议类型，获取已推送的用户的信息
func GeCygxActivityUserTemplateRecordListByActivityId(activityId, sendType int) (items []*CygxActivityUserTemplateRecord, err error) {
	sql := `SELECT * FROM cygx_activity_user_template_record WHERE activity_id = ?  AND  send_type = ?  `
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Raw(sql, activityId, sendType).QueryRows(&items)
	return
}
