package roadshow

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

type AddRsCalendarMeetingUserReq struct {
	RsCalendarId int   `description:"日程ID"`
	UserId       []int // 用户ID
	TagList      []*AddRsCalendarMeetingUserTagReq
}

type AddRsCalendarMeetingUserTagReq struct {
	TagType int `description:"标签类型"`
	TagId   int `description:"标签ID"`
}

type RsCalendarMeetingUser struct {
	RsCalendarMeetingUserId int       `orm:"column(rs_calendar_meeting_user_id);pk" description:"主键ID"`
	RsCalendarId            int       `description:"日程ID"`
	UserId                  int       `description:"用户ID"`
	Mobile                  string    `description:"手机号"`
	Email                   string    `description:"邮箱"`
	CompanyId               int       `description:"公司ID"`
	CompanyName             string    `description:"公司名称"`
	RealName                string    `description:"用户实际名称"`
	Position                string    `description:"职位"`
	AdminId                 int       `description:"管理员ID"`
	AdminName               string    `description:"管理员姓名"`
	CreateTime              time.Time `description:"创建时间"`
	ModifyTime              time.Time `description:"修改时间"`
}

type RsCalendarMeetingUserResp struct {
	RsCalendarMeetingUserId int    `description:"参会名单主键ID"`
	RealName                string `description:"用户实际名称"`
	Position                string `description:"职位"`
}

type DeleteRsCalendarMeetingUserReq struct {
	RsCalendarMeetingUserId int `description:"参会名单主键ID"`
}

type RsCalendarMeetingUserListResp struct {
	List      []*RsCalendarMeetingUserResp
	ListLebel []*cygx.RaiServeTagResp
}

// MultiAddRsCalendarMeetingUser 批量添加RsCalendarMeetingUser
func MultiAddRsCalendarMeetingUser(items []*RsCalendarMeetingUser) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(items), items)
	return
}

// 删除
func DeleteRsCalendarMeetingUser(rsCalendarMeetingUserID int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM rs_calendar_meeting_user WHERE rs_calendar_meeting_user_id=? `
	_, err = o.Raw(sql, rsCalendarMeetingUserID).Exec()
	return err
}

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

// 获取数量
func GetRsCalendarMeetingUserCountByUserModel(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sqlCount := ` SELECT COUNT(1) AS count  FROM rs_calendar_meeting_user as r
                  WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取数量
func GetRsCalendarMeetingUserCountByUserModelWeekly(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sqlCount := ` SELECT COUNT(1) AS count  FROM 	rs_calendar_meeting_user AS r
			INNER JOIN user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2 
		WHERE 1 = 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
func GetRsCalendarMeetingUserList(condition string, pars []interface{}) (items *RsCalendarMeetingUserResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM rs_calendar_meeting_user WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&items)
	return
}

// 通过路演ID获取参会人员列表
func GetRsCalendarMeetingUserListByRsCalendarId(rsCalendarId int) (items []*RsCalendarMeetingUserResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM rs_calendar_meeting_user WHERE  rs_calendar_id = ?  `
	_, err = o.Raw(sql, rsCalendarId).QueryRows(&items)
	return
}

type RsCalendarMeetingUserCountResp struct {
	RsCalendarId int `description:"日程ID"`
	Total        int `description:"数量"`
}

// 路演参会人员数量
func GetRsCalendarMeetingUserListCount(rsCalendarIds []int) (items []*RsCalendarMeetingUserCountResp, err error) {
	lenNum := len(rsCalendarIds)
	if lenNum <= 0 {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT  rs_calendar_id ,COUNT(1) as total  FROM rs_calendar_meeting_user WHERE  rs_calendar_id IN ( ` + utils.GetOrmInReplace(lenNum) + `)   GROUP BY rs_calendar_id  `
	_, err = o.Raw(sql, rsCalendarIds).QueryRows(&items)
	return
}

type RsCalendarMeetingUserByRai struct {
	RsCalendarMeetingUserId int    `orm:"column(rs_calendar_meeting_user_id);pk" description:"主键ID"`
	RsCalendarId            int    `description:"日程ID"`
	UserId                  int    `description:"用户ID"`
	Mobile                  string `description:"手机号"`
	Email                   string `description:"邮箱"`
	CompanyId               int    `description:"公司ID"`
	CompanyName             string `description:"公司名称"`
	RealName                string `description:"用户实际名称"`
	StartDate               string `description:"开始时间"`
	EndDate                 string `description:"结束时间"`
	EndTime                 string `description:"开始时间"`
	StartTime               string `description:"结束时间"`
	StartWeek               string `description:"起始周"`
	RoadshowType            string `description:"路演形式"`
	ResearcherName          string `description:"研究员姓名"`
	TagType                 int    `description:"标签类型，1产业，2标的，3行业权限"`
}

// 权益联系人列表获取相关信息
func GetRsCalendarMeetingUserByRai(condition string, startSize, pageSize int) (total int, items []*RsCalendarMeetingUserByRai, err error) {
	o := orm.NewOrm()
	var sql string
	sql += `SELECT
			r.company_id,
			r.company_name,
			r.real_name,
			r.mobile,
			b.start_date,
			b.end_date,
			b.start_time,
			b.end_time,
			b.start_week,
			b.researcher_name,
			a.roadshow_type,
			a.rs_calendar_id,
			g.tag_type
		FROM
			rs_calendar_meeting_user AS r
			INNER JOIN rs_calendar AS a ON a.rs_calendar_id = r.rs_calendar_id
			INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id = b.rs_calendar_id 
			LEFT JOIN rs_calendar_meeting_label_group AS g ON g.rs_calendar_id = b.rs_calendar_id 
		WHERE
			1 = 1 ` + condition + ` GROUP  BY a.rs_calendar_id  `

	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
	err = o.Raw(totalSql).QueryRow(&total)
	if err != nil {
		return
	}

	sql += ` ORDER BY  b.start_date   DESC ,b.start_time   DESC , a.rs_calendar_id DESC  `
	if startSize > 0 || pageSize > 0 {
		sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetRsCalendarMeetingUserByRaiWeekly(condition string, startSize, pageSize int) (total int, items []*RsCalendarMeetingUserByRai, err error) {
	o := orm.NewOrm()
	var sql string
	sql += `SELECT
			r.company_id,
			r.company_name,
			r.real_name,
			r.mobile,
			b.start_date,
			b.end_date,
			b.start_time,
			b.end_time,
			b.start_week,
			b.researcher_name,
			a.roadshow_type,
			a.rs_calendar_id
		FROM
			rs_calendar_meeting_user AS r
			INNER JOIN rs_calendar AS a ON a.rs_calendar_id = r.rs_calendar_id
			INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id = b.rs_calendar_id 
			INNER JOIN user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2 
		WHERE
			1 = 1 ` + condition

	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
	err = o.Raw(totalSql).QueryRow(&total)
	if err != nil {
		return
	}

	sql += `ORDER BY  b.start_date   DESC ,b.start_time   DESC , a.rs_calendar_id DESC   `
	if startSize > 0 || pageSize > 0 {
		sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}
