package roadshow

import "hongze/hz_crm_api/models/roadshow"

func CheckMatters(startDateTime, endDateTime string, researcherId, rsMattersId int) (matterCount int, err error) {
	var matterCondition string
	var matterPars []interface{}
	if rsMattersId > 0 {
		matterCondition += " AND rs_matters_id <> ? "
		matterPars = append(matterPars, rsMattersId)
	}
	matterCondition += " AND sys_user_id = ? "
	matterPars = append(matterPars, researcherId)

	matterCondition += ` AND (((? >=DATE_FORMAT(CONCAT(start_date," ",start_time),'%Y-%m-%d %H:%i:%S') AND ? <= DATE_FORMAT(CONCAT(end_date," ",end_time),'%Y-%m-%d %H:%i:%S')) OR (? >= DATE_FORMAT(CONCAT(start_date," ",start_time),'%Y-%m-%d %H:%i:%S') AND ? <= DATE_FORMAT(CONCAT(end_date," ",end_time),'%Y-%m-%d %H:%i:%S') )))`
	matterPars = append(matterPars, startDateTime)
	matterPars = append(matterPars, startDateTime)
	matterPars = append(matterPars, endDateTime)
	matterPars = append(matterPars, endDateTime)
	matterCount, err = roadshow.CheckMattersCount(matterCondition, matterPars)

	if matterCount <= 0 {
		var matterCondition string
		var matterPars []interface{}
		if rsMattersId > 0 {
			matterCondition += " AND rs_matters_id <> ? "
			matterPars = append(matterPars, rsMattersId)
		}
		matterCondition += " AND sys_user_id = ? "
		matterPars = append(matterPars, researcherId)

		matterCondition += ` AND ? <=DATE_FORMAT(CONCAT(start_date," ",start_time),'%Y-%m-%d %H:%i:%S')
								 AND ? >= DATE_FORMAT(CONCAT(end_date," ",end_time),'%Y-%m-%d %H:%i:%S') `
		matterPars = append(matterPars, startDateTime)
		matterPars = append(matterPars, endDateTime)
		matterCount, err = roadshow.CheckMattersCount(matterCondition, matterPars)
	}

	return
}

func CheckCalendar(startDateTime, endDateTime, status string, researcherId, rsCalendarResearcherId int) (calendarCount int, err error) {
	var researcherCondition string
	var researcherMatterPars []interface{}

	if rsCalendarResearcherId > 0 {
		researcherCondition += " AND rs_calendar_researcher_id <> ? "
		researcherMatterPars = append(researcherMatterPars, rsCalendarResearcherId)
	}

	researcherCondition += " AND researcher_id = ? "
	researcherMatterPars = append(researcherMatterPars, researcherId)

	researcherCondition += " AND status IN (" + status + ") "

	researcherCondition += ` AND (((? >=DATE_FORMAT(CONCAT(start_date," ",start_time),'%Y-%m-%d %H:%i:%S') AND ? <= DATE_FORMAT(CONCAT(end_date," ",end_time),'%Y-%m-%d %H:%i:%S')) OR (? >= DATE_FORMAT(CONCAT(start_date," ",start_time),'%Y-%m-%d %H:%i:%S') AND ? <= DATE_FORMAT(CONCAT(end_date," ",end_time),'%Y-%m-%d %H:%i:%S') )))`
	researcherMatterPars = append(researcherMatterPars, startDateTime)
	researcherMatterPars = append(researcherMatterPars, startDateTime)
	researcherMatterPars = append(researcherMatterPars, endDateTime)
	researcherMatterPars = append(researcherMatterPars, endDateTime)
	calendarCount, err = roadshow.CheckCalendarResearcherCount(researcherCondition, researcherMatterPars)
	if calendarCount <= 0 {
		var researcherCondition string
		var researcherMatterPars []interface{}

		if rsCalendarResearcherId > 0 {
			researcherCondition += " AND rs_calendar_researcher_id <> ? "
			researcherMatterPars = append(researcherMatterPars, rsCalendarResearcherId)
		}

		researcherCondition += " AND researcher_id = ? "
		researcherMatterPars = append(researcherMatterPars, researcherId)

		researcherCondition += " AND status IN (" + status + ") "

		researcherCondition += ` AND ? <=DATE_FORMAT(CONCAT(start_date," ",start_time),'%Y-%m-%d %H:%i:%S')
								 AND ? >= DATE_FORMAT(CONCAT(end_date," ",end_time),'%Y-%m-%d %H:%i:%S') `
		researcherMatterPars = append(researcherMatterPars, startDateTime)
		researcherMatterPars = append(researcherMatterPars, endDateTime)
		calendarCount, err = roadshow.CheckCalendarResearcherCount(researcherCondition, researcherMatterPars)
	}
	return
}
