package yb

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

// ComeinEvent 全时会议表
type ComeinEvent struct {
	ComeinEventId int       `orm:"column(comein_event_id);pk" description:"自增id"`
	RoadshowId    int       `description:"路演ID"`
	ConferenceId  string    `description:"电话会议ID"`
	Title         string    `description:"活动标题"`
	StartTime     time.Time `description:"路演开始时间"`
	EndTime       time.Time `description:"路演结束时间"`
	People        int       `description:"实际入会人数"`
	CreateTime    time.Time `description:"创建时间"`
	ShareSellerId int       `description:"分享销售ID"`
}

// TableName 表名变更
func (comeinEventInfo *ComeinEvent) TableName() string {
	return "comein_event"
}

// GetComeinEventByRoadshowId 根据进门会议的路演id获取进门会议（已同步）
func GetComeinEventByRoadshowId(comeinId int) (item *ComeinEvent, err error) {
	o := orm.NewOrm()
	sql := "select * from comein_event where roadshow_id=? "
	err = o.Raw(sql, comeinId).QueryRow(&item)
	return
}

// GetComeInEventById 主键获取进门到会
func GetComeInEventById(comeInEventId int) (item *ComeinEvent, err error) {
	o := orm.NewOrm()
	sql := "select * from comein_event where comein_event_id = ? "
	err = o.Raw(sql, comeInEventId).QueryRow(&item)
	return
}

// Update 更新进门会议
func (comeinEventInfo *ComeinEvent) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(comeinEventInfo, cols...)
	return
}

// AddComeinEvent 新增进门会议
func AddComeinEvent(comeinEvent *ComeinEvent) (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(comeinEvent)
	if err != nil {
		return
	}
	comeinEvent.ComeinEventId = int(id)
	return
}

type ComeinEventItem struct {
	ComeinEventId   int       `orm:"column(comein_event_id);pk" description:"自增id"`
	RoadshowId      int       `description:"路演ID"`
	ConferenceId    string    `description:"电话会议ID"`
	Title           string    `description:"活动标题"`
	StartTime       time.Time `description:"路演开始时间"`
	EndTime         time.Time `description:"路演结束时间"`
	People          int       `description:"实际入会人数"`
	CreateTime      time.Time `description:"创建时间"`
	ShareSellerId   int       `description:"分享销售ID"`
	ShareSellerName string    `description:"分享销售姓名"`
}

// GetComeinList 获取进门列表数据
func GetComeinList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*ComeinEventItem, err error) {
	o := orm.NewOrm()
	sql := "select a.*,c.real_name as share_seller_name from comein_event a left join admin c on a.share_seller_id = c.admin_id " +
		" where 1=1  "
	sql += condition
	sql += ` order by a.start_time DESC,comein_event_id desc `

	totalSql := `select count(1) total from (` + sql + `) z `
	err = o.Raw(totalSql, pars).QueryRow(&total)
	if err != nil {
		return
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}

// AddComeinEventAndUser 添加进门活动和用户
func AddComeinEventAndUser(comeinEventInfo *ComeinEvent, userList []*ComeinEventUser) {
	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	id, err := to.Insert(comeinEventInfo)
	if err != nil {
		return
	}
	comeinEventInfo.ComeinEventId = int(id)

	// 联系人入库
	if len(userList) > 0 {
		for _, v := range userList {
			v.ComeinEventId = comeinEventInfo.ComeinEventId
		}
		_, tmpErr := to.InsertMulti(len(userList), userList)
		if tmpErr != nil {
			err = tmpErr
			return
		}

	}
}
