package services

import (
	"errors"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"strconv"
	"strings"
	"time"
)

// 获取用户已经报名的活动
func GetActivitySignUpUserMap(activityIds []int, user *models.WxUserItem) (mapUserId map[int]int, err error) {
	userId := user.UserId
	var condition string
	var pars []interface{}
	activityIdsLen := len(activityIds)
	if activityIdsLen > 0 {
		condition += ` AND activity_id IN (` + utils.GetOrmInReplace(activityIdsLen) + `)`
		pars = append(pars, activityIds)
	}
	condition += ` AND user_id = ?  AND do_fail_type = 0 `
	pars = append(pars, userId)
	list, e := models.GetActivitySignuListByUser(condition, pars)
	if e != nil {
		err = errors.New("GetCygxActivitySpecialTripList, Err: " + e.Error())
		return
	}
	mapUid := make(map[int]int)
	for _, v := range list {
		mapUid[v.ActivityId] = v.UserId
	}
	mapUserId = mapUid
	return
}

// 获取用户已经设置会议提醒的活动  cygx_activity_meeting_reminder
func GetActivityReminderUserMasp(activityIds []int, user *models.WxUserItem) (mapUserId map[int]int, err error) {
	userId := user.UserId
	var condition string
	var pars []interface{}
	activityIdsLen := len(activityIds)
	if activityIdsLen > 0 {
		condition += ` AND activity_id IN (` + utils.GetOrmInReplace(activityIdsLen) + `)`
		pars = append(pars, activityIds)
	}
	condition += ` AND user_id = ?  `
	pars = append(pars, userId)
	list, e := models.GetCygxReminderListByUser(condition, pars)
	if e != nil {
		err = errors.New("GetCygxReminderListByUser, Err: " + e.Error())
		return
	}
	mapUid := make(map[int]int)
	for _, v := range list {
		mapUid[v.ActivityId] = v.UserId
	}
	mapUserId = mapUid
	return
}

// 获取用户已经预约纪要的活动
func GetActivityAppointmentUserMap(activityIds []int, user *models.WxUserItem) (mapUserId map[int]int, err error) {
	userId := user.UserId
	var condition string
	var pars []interface{}
	activityIdsLen := len(activityIds)
	if activityIdsLen > 0 {
		condition += ` AND activity_id IN (` + utils.GetOrmInReplace(activityIdsLen) + `)`
		pars = append(pars, activityIds)
	}
	condition += ` AND user_id = ?  `
	pars = append(pars, userId)
	list, e := models.GetCygxAppointmentListByUser(condition, pars)
	if e != nil {
		err = errors.New("GetCygxActivitySpecialTripList, Err: " + e.Error())
		return
	}
	mapUid := make(map[int]int)
	for _, v := range list {
		mapUid[v.ActivityId] = v.UserId
	}
	mapUserId = mapUid
	return
}

// 活动列表的展示  HandleActivityListButton
func HandleActivityListButton(list []*models.ActivityDetail, user *models.WxUserItem) (items []*models.ActivityDetail, err error) {
	var activityIds []int
	var activitySpecilalIds []int
	for k, v := range list {
		if v.SourceType == 2 {
			activitySpecilalIds = append(activitySpecilalIds, v.ActivityId)
			//把专项调研的线下改为活动的线下类型
			if v.ActivityType == 2 {
				list[k].ActivityType = 0
			}
			list[k].IsShowSignup = true
		} else {
			activityIds = append(activityIds, v.ActivityId)
		}
	}
	//处理活动
	if len(activityIds) > 0 {
		//处理用户是否报名
		mapSignUp, e := GetActivitySignUpUserMap(activityIds, user)
		if e != nil {
			err = errors.New("GetActivitySignUpUserMap, Err: " + e.Error())
			return
		}
		for k, v := range list {
			if v.SourceType != 2 {
				if _, ok := mapSignUp[v.ActivityId]; ok {
					list[k].IsSignup = 1
				}
			}
		}
		//处理用户是否预约纪要
		mapAppointment, e := GetActivityAppointmentUserMap(activityIds, user)
		if e != nil {
			err = errors.New("GetActivityAppointmentUserMap, Err: " + e.Error())
			return
		}
		for k, v := range list {
			if v.SourceType != 2 {
				if _, ok := mapAppointment[v.ActivityId]; ok {
					list[k].IsAppointment = 1
				}
			}
		}
		//处理用户是否预约会议提醒
		mapReminder, e := GetActivityReminderUserMasp(activityIds, user)
		if e != nil {
			err = errors.New("GetActivityReminderUserMasp, Err: " + e.Error())
			return
		}
		for k, v := range list {
			if v.SourceType != 2 {
				if _, ok := mapReminder[v.ActivityId]; ok {
					list[k].IsCancelMeetingReminder = 1
				}
			}
		}
	}

	//处理专项产业调研
	if len(activitySpecilalIds) > 0 {
		//处理用户是否报名
		UserMap, e := GetSpecialTripUserMap(activitySpecilalIds, user.UserId)
		if e != nil {
			err = errors.New("GetSpecialTripUserMap, Err: " + e.Error())
			return
		}
		for k, v := range list {
			if v.SourceType == 2 {
				if _, ok := UserMap[v.ActivityId]; ok {
					list[k].IsSignup = 1
				}
			}
		}

		for k, v := range list {
			if v.SourceType == 2 {
				resultTimeStart := utils.StrTimeToTime(v.ActivityTime)  //时间字符串格式转时间格式
				resultTimeEnd := utils.StrTimeToTime(v.ActivityTimeEnd) //时间字符串格式转时间格式
				if resultTimeStart.After(time.Now()) {
					list[k].ActiveState = strconv.Itoa(1)
				} else if time.Now().After(resultTimeEnd) {
					list[k].ActiveState = strconv.Itoa(3)
				} else {
					list[k].ActiveState = strconv.Itoa(2)
				}
			}
		}
	}

	//var isShow bool
	isShow, e := GetShowSustainableNew()
	if e != nil {
		err = errors.New("GetShowSustainableNew, Err: " + e.Error())
		return
	}
	detail, e := models.GetConfigByCode("city_img_url")
	if e != nil {
		err = errors.New("GetConfigByCode, Err: " + e.Error())
		return
	}
	detailChart, e := models.GetConfigByCode("chart_img_url")
	if e != nil {
		err = errors.New("GetConfigByCode, Err: " + e.Error())
		return
	}
	addressList := strings.Split(detail.ConfigValue, "{|}")
	mapAddress := make(map[string]string)
	chartList := strings.Split(detailChart.ConfigValue, "{|}")
	mapChart := make(map[string]string)
	var cityName string
	var chartName string
	var imgUrl string
	var imgUrlChart string
	var mapActivityId []int
	for _, v := range addressList {
		vslice := strings.Split(v, "_")
		cityName = vslice[0]
		imgUrl = vslice[len(vslice)-1]
		mapAddress[cityName] = imgUrl
	}
	for _, v := range chartList {
		vslice := strings.Split(v, "_")
		chartName = vslice[0]
		imgUrlChart = vslice[len(vslice)-1]
		mapChart[chartName] = imgUrlChart
	}
	for k, v := range list {
		if strings.Contains(v.ActivityName, "【") {
			list[k].IsBrackets = 1
		}
		if v.SignupNum > v.LimitPeopleNum {
			list[k].SignupNum = v.LimitPeopleNum
		}
		if isShow && strings.Contains(v.ChartPermissionName, "研选") {
			list[k].IsShowSustainable = true
		}
		if strings.Contains(v.ChartPermissionName, "研选") && v.ActivityTypeId == 1 {
			list[k].ActivityTypeName = "买方研选电话会"
			list[k].ImgUrlText = utils.YAN_XUAN_IMG
		}
		if v.SourceType == 1 {
			if v.ActivityType == 0 {
				if mapAddress[v.City] != "" {
					list[k].ImgUrl = mapAddress[v.City]
				} else {
					list[k].ImgUrl = mapAddress["其它"]
				}
			} else {
				if mapChart[v.ChartPermissionName] != "" {
					list[k].ImgUrl = mapChart[v.ChartPermissionName]
				}
			}
		} else {
			if mapChart[v.ChartPermissionName] != "" {
				list[k].ImgUrl = mapChart[v.ChartPermissionName]
			}
		}

		expertTxt, _ := GetReportContentTextSub(v.Expert)
		list[k].Expert = expertTxt
		if v.IsHideAppointment == 0 {
			list[k].IsShowAppointment = IsShowAppointment(v.ActivityTypeId, v.ChartPermissionName)
		}
		if v.ActivityTypeId == utils.C_CLASS_ACTIVITY_TYPE_ID {
			list[k].IsCClassMeeting = true
		}
		mapActivityId = append(mapActivityId, v.ActivityId)
	}

	//处理音频回放
	mapActivityVoice, e := GetActivityVoiceResp(mapActivityId)
	if e != nil {
		err = errors.New("GetActivityVoiceResp, Err: " + e.Error())
		return
	}

	//处理视频回放
	mapActivityVideo, e := GetActivityVideoResp(mapActivityId)
	if e != nil {
		err = errors.New("GetActivityVoiceResp, Err: " + e.Error())
		return
	}

	//处理不同的报名方式按钮回显
	mapActivitySignup, e := GetActivitySignupResp(mapActivityId, user)
	if e != nil {
		e = errors.New("mapActivitySignup, Err: " + e.Error())
		return
	}
	for k, v := range list {
		if mapActivityVoice[v.ActivityId] != nil {
			list[k].FileType = 1
			list[k].AudioLink = true
			list[k].VoiceList = mapActivityVoice[v.ActivityId]
		}
		if mapActivityVideo[v.ActivityId] != nil {
			list[k].FileType = 2
			list[k].AudioLink = true
			list[k].VideoDetail = mapActivityVideo[v.ActivityId]
		}
		v.SignupType = mapActivitySignup[v.ActivityId]
		items = append(items, ActivityButtonShow(v))
	}
	return
}
