package roadshow

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
)

type FeedbackView struct {
	RsCalendarResearcherId int    `orm:"column(rs_calendar_researcher_id);pk"`
	RsCalendarId           int    `description:"日历活动id"`
	SysUserId              int    `description:"创建人id"`
	SysUserRealName        string `description:"创建人名称"`
	ActivityType           string `description:"活动类型"`
	RoadshowType           string `description:"路演形式"`
	RoadshowPlatform       string `description:"路演平台"`
	ResearcherId           int    `description:"研究员id"`
	ResearcherName         string `description:"研究员名称"`
	StartDate              string `description:"开始日期"`
	EndDate                string `description:"结束日期"`
	StartTime              string `description:"开始时间"`
	EndTime                string `description:"结束时间"`
	StartWeek              string `description:"开始日期对应周"`
	EndWeek                string `description:"结束日期对应周"`
	Status                 int    `description:"状态:1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束"`
	ApproveTime            string `description:"接受时间"`
	IsSynced               int    `description:"是否与上海同步 0:未同步 1:已同步"`
	ResearcherSort         int    `description:"研究员新增排序"`
	CompanyId              int    `description:"客户id"`
	CompanyName            string `description:"客户名称"`
	CompanyStatus          string `description:"客户状态"`
	FeedbackStatus         int    `description:"1:未请求,2:待反馈,3:已反馈"`
	FeedbackTime           string `description:"反馈时间"`
	FeedbackContent        string `description:"反馈内容"`
	FeedbackRequestTime    string `description:"反馈请求时间"`
	Province               string `description:"省"`
	ProvinceCode           string `description:"省编码"`
	City                   string `description:"市"`
	CityCode               string `description:"市编码"`
}

func GetFeedbackListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM rs_calendar AS a
			INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id=b.rs_calendar_id
            INNER JOIN admin AS c ON b.researcher_id=c.admin_id
			WHERE 1=1 AND c.enabled=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func GetFeedbackList(condition string, pars []interface{}, startSize, pageSize, feedbackType int) (list []*FeedbackView, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM rs_calendar AS a
			INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id=b.rs_calendar_id
            INNER JOIN admin AS c ON b.researcher_id=c.admin_id
			WHERE 1=1 AND c.enabled=1 `
	if condition != "" {
		sql += condition
	}

	//显示已发送反馈请求的路演
	if feedbackType == 1 {
		sql += ` ORDER BY FIELD(feedback_status,3) DESC,b.feedback_time DESC,FIELD(B.feedback_status,2) DESC,b.feedback_request_time DESC `
	} else {
		sql += ` ORDER BY DATE_FORMAT(CONCAT(b.end_date," ",b.end_time),'%Y-%m-%d %H:%i:%S') DESC `
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)

	return
}

type FeedbackListResp struct {
	Paging *paging.PagingItem
	List   []*FeedbackView
}

func GetSubmitFeedbackList(condition string, pars []interface{}) (list []*FeedbackView, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM rs_calendar AS a
			INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id=b.rs_calendar_id
			WHERE 1=1 `
	if condition != "" {
		sql += condition
	}

	//显示已发送反馈请求的路演
	sql += ` ORDER BY b.feedback_request_time DESC `

	_, err = o.Raw(sql, pars).QueryRows(&list)

	return
}

type WriteFeedbackReq struct {
	RsCalendarResearcherId int    `description:"路演研究员id"`
	FeedbackContent        string `description:"反馈内容"`
}

func WriteFeedback(item *WriteFeedbackReq) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE rs_calendar_researcher SET feedback_content = ? WHERE rs_calendar_researcher_id=? `

	_, err = o.Raw(sql, item.FeedbackContent, item.RsCalendarResearcherId).Exec()
	return
}

type SubmitFeedbackReq struct {
	List []SubmitFeedback
}

type SubmitFeedback struct {
	RsCalendarResearcherId int `description:"路演研究员id"`
}

func SubmitFeedbackInfo(rsCalendarResearcherId int) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE rs_calendar_researcher SET feedback_time = NOW(),feedback_status=3 WHERE rs_calendar_researcher_id=? `

	_, err = o.Raw(sql, rsCalendarResearcherId).Exec()
	return
}

func SendFeedbackInfo(rsCalendarResearcherId int) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE rs_calendar_researcher SET feedback_request_time = NOW(),feedback_status=2 WHERE rs_calendar_researcher_id=? `

	_, err = o.Raw(sql, rsCalendarResearcherId).Exec()
	return
}
