package services

import (
	"errors"
	"fmt"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"time"
)

// GetActivitySignupResp 处理用户的报名方式
func GetActivitySignupResp(activityIdS []int, user *models.WxUserItem) (mapItem map[int]int, err error) {
	var condition string
	var pars []interface{}
	lenActivityId := len(activityIdS)
	if lenActivityId == 0 || user.Mobile == "" {
		return
	}
	condition = ` AND do_fail_type = 0 AND activity_id IN (` + utils.GetOrmInReplace(lenActivityId) + `) AND mobile = ?`
	pars = append(pars, activityIdS, user.Mobile)
	listSignup, e := models.GetActivitySignupList(condition, pars)
	if e != nil {
		err = errors.New("GetResourceDataList, Err: " + e.Error())
		return
	}
	mapItem = make(map[int]int, 0)
	for _, v := range listSignup {
		mapItem[v.ActivityId] = v.SignupType
	}
	return
}

// CheckActivitySignUpLimit 校验报名限制
func CheckActivitySignUpLimit(user *models.WxUserItem, activityInfo *models.ActivityDetail) (signupStatus, popupMsg string, failType int, err error) {
	//判断优先级：总人数限制→单机构2人限制→爽约3次限制
	signupStatus = "Success"
	activityId := activityInfo.ActivityId
	//弘则下面的用户不做单机构两人限制、不受报名总人数限制
	if user.CompanyId != utils.HZ_COMPANY_ID {
		totaSignupPeopleNum, e := models.GetActivitySignupSuccessByUserCountNoHz(activityId)
		if e != nil {
			err = errors.New("GetActivitySignupSuccessByUserCountNoHz, Err: " + e.Error())
			return
		}
		if totaSignupPeopleNum >= activityInfo.LimitPeopleNum {
			signupStatus = "FullStarffed"
			popupMsg = "此活动报名人数已满，请留意下期活动"
			failType = 1
			return
		}
		totalSignupCompany, e := models.GetActivitySignupCompanyCount(activityId, user.CompanyId)
		if e != nil {
			err = errors.New("GetActivitySignupCompanyCount, Err: " + e.Error())
			return
		}
		if totalSignupCompany >= 2 {
			signupStatus = "TwoPeople"
			popupMsg = "单机构最多2人报名同一活动，您所在机构报名人数已满"
			failType = 2
			return
		}
	}
	//totalRestrict, e := models.GetUserRestrictCount(user.Mobile)
	//if e != nil {
	//	err = errors.New("GetUserRestrictCount, Err: " + e.Error())
	//	return
	//}
	//if totalRestrict >= 1 {
	//	signupStatus = "BreakPromise"
	//	popupMsg = "由于爽约次数过多，您暂时被限制报名资格，请联系对口销售"
	//	failType = 3
	//	return
	//}
	return
}

// 校验报名截止时间
func CheckSiginupDeadline(activityInfo *models.ActivityDetail) (checkTime bool, popupMsg string) {
	checkTime = true
	if activityInfo.SiginupDeadline != utils.FormatDateTimeInit && activityInfo.SiginupDeadline != "" {
		timeResp := utils.StrTimeToTime(activityInfo.SiginupDeadline)
		if timeResp.Before(time.Now()) {
			checkTime = false
			popupMsg = "该活动已截止报名\n\n若想参加，请联系对口销售"
		}
	}
	return
}

// 校验报名点数
func CheckActivityPoints(activityInfo *models.ActivityDetail, wxUser *models.WxUserItem) (checkPoints bool, popupMsg, companyPoints, activityPoints string, err error) {
	checkPoints = true
	if activityInfo.IsResearchPoints {
		//获取活动对用户要扣的点
		userPointsNum, e := models.GetCygxActivityPointsSetUserNum(activityInfo.ActivityId)
		if e != nil {
			err = errors.New("GetCygxActivityPointsSetUserNum, Err: " + e.Error())
			return
		}
		// 获取用户所在公司剩余的点
		companyPointsNum, e := models.GetCygxActivitySpecialPermissionPoints(wxUser.CompanyId)
		if e != nil && e.Error() != utils.ErrNoRow() {
			err = errors.New("GetCompanyPoints, Err: " + e.Error())
			return
		}
		if companyPointsNum-userPointsNum < 0 {
			checkPoints = false
			popupMsg = "点数不足，若想报名，\n请联系对口销售充值"
		}
		companyPoints = fmt.Sprint(companyPointsNum)
		activityPoints = fmt.Sprint(userPointsNum)
	}
	return
}

// 校验报名是否需要绑定邮箱
func CheckActivityUserEmail(activityInfo *models.ActivityDetail, wxUser *models.WxUserItem) (checkEmail bool, popupMsg string) {
	checkEmail = true
	if activityInfo.IsNeedEmail == 1 {
		if wxUser.Email == "" {
			checkEmail = false
			popupMsg = "应上市公司要求，该会议报名需\n提供邮箱，请填写您的工作邮箱"
		}
	}
	return
}

// 处理取消报名截止时间的弹窗文案
func ActivityCancelDeadlineMsg(activityInfo *models.ActivityDetail) (popupMsg string, err error) {
	if !activityInfo.IsResearchPoints {
		return
	}
	activityId := activityInfo.ActivityId
	//获取活动是否扣点以及扣点规则明细
	activityPointsSetDetail, e := models.GetCygxActivityPointsSetDetail(activityId)
	if e != nil {
		err = errors.New("GetCygxActivityPointsSetDetail" + e.Error())
		return
	}
	cancelDeadlineType := activityPointsSetDetail.CancelDeadlineType
	popupMsg = "活动开始前1小时取消报名，可返还点数"
	//if activityInfo.CancelDeadlineType == 0 {
	//	popupMsg = "活动开始前1小时取消报名，可返还点数"
	//}
	if cancelDeadlineType == 1 && activityInfo.SiginupDeadline != utils.FormatDateTimeInit && activityInfo.SiginupDeadline != "" {
		siginupDeadline := utils.GetTimeDateRemoveYearAndSecond(activityInfo.SiginupDeadline)
		popupMsg = siginupDeadline + "前取消报名，可返还点数"
	}
	if cancelDeadlineType == 2 {
		popupMsg = "活动开始前24小时取消报名，可返还点数"
	}
	if cancelDeadlineType == 3 {
		popupMsg = "活动开始前48小时取消报名，可返还点数"
	}
	return
}

// 校验取消报名截止时间
func CheckCancelDeadline(activityInfo *models.ActivityDetail) (popupMsg string) {
	if !activityInfo.IsResearchPoints {
		return
	}
	//获取活动是否扣点以及扣点规则明细
	if activityInfo.CancelDeadline != utils.FormatDateTimeInit {
		timeResp := utils.StrTimeToTime(activityInfo.CancelDeadline)
		if timeResp.Before(time.Now()) {
			popupMsg = "当前时间点已无法取消报名，\n\n若想取消，请联系对口销售"
		}
	}
	return
}

// // 校验报名顺序 截止时间>点数>邮箱
func CheckActivityUserAll(activityInfo *models.ActivityDetail, wxUser *models.WxUserItem) (popupMsg string, err error) {
	_, popupMsg = CheckSiginupDeadline(activityInfo)
	if popupMsg != "" {
		return
	}
	_, popupMsg, _, _, err = CheckActivityPoints(activityInfo, wxUser)
	if popupMsg != "" {
		return
	}
	_, popupMsg = CheckActivityUserEmail(activityInfo, wxUser)
	if popupMsg != "" {
		return
	}
	return
}
