package services

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

// AddCygxActivitySigninLog 扫码签到日志记录
func AddCygxActivitySigninLog(item *models.CygxActivitySignin) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("扫码签到日志记录，失败,活动ID："+strconv.Itoa(item.ActivityId)+err.Error(), 2)
		}
	}()
	//添加日志记录
	itemLog := new(models.CygxActivitySigninLog)
	itemLog.ActivityId = item.ActivityId
	itemLog.UserId = item.UserId
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.CompanyId = item.CompanyId
	itemLog.RealName = item.RealName
	itemLog.CompanyName = item.CompanyName
	itemLog.IsSignup = item.IsSignup
	itemLog.BusinessCard = item.BusinessCard
	itemLog.CountryCode = item.CountryCode
	itemLog.OpenId = item.OpenId
	itemLog.CreateTime = time.Now()
	err = models.AddCygxActivitySigninLog(itemLog)
	if err != nil {
		return
	}

	err = models.UpdatedateCygxActivitySignin(item)
	return
}

// AddCygxActivityOfflineMeetingDetail 扫码签到日志记录
func AddCygxActivityOfflineMeetingDetail(activityid, userId int) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("扫码签到日志记录，失败,活动ID："+strconv.Itoa(activityid)+err.Error(), 2)
		}
	}()

	var condition string
	var pars []interface{}

	condition = `  AND activity_id = ?`
	pars = append(pars, activityid)
	listOfflineMeeting, err := models.GetOfflineMeetingList(condition, pars)
	if err != nil {
		err = errors.New("GetOfflineMeetingList, Err: " + err.Error())
		return
	}
	mapOfflineMeeting := make(map[int]bool)
	for _, v := range listOfflineMeeting {
		mapOfflineMeeting[v.UserId] = true
	}
	pars = make([]interface{}, 0)
	condition = ` AND do_fail_type = 0 AND activity_id  = ?`
	pars = append(pars, activityid)
	listSignup, e := models.GetActivitySignupList(condition, pars)
	if e != nil {
		err = errors.New("GetResourceDataList, Err: " + e.Error())
		return
	}

	var items []*models.CygxActivityOfflineMeetingDetail
	for _, v := range listSignup {
		if !mapOfflineMeeting[v.UserId] {
			item := new(models.CygxActivityOfflineMeetingDetail)
			item.UserId = v.UserId
			item.ActivityId = v.ActivityId
			item.CreateTime = v.CreateTime
			item.Mobile = v.Mobile
			item.CompanyId = v.CompanyId
			item.CompanyName = v.CompanyName
			if userId == v.UserId {
				item.IsMeeting = 1
				item.SigninTime = time.Now().Format(utils.FormatDateTime)
			}
			items = append(items, item)
		}
	}

	//添加线下到会信息记录
	_, err = models.AddCygxActivityOfflineMeetingDetailList(items)
	if err != nil {
		err = errors.New("AddCygxActivityOfflineMeetingDetailList, Err: " + err.Error())
		return
	}

	//修改活动是否提交到会状态
	err = models.UpdateCygxActivitySubmitMeetingByActivityId(activityid)
	if err != nil {
		err = errors.New("UpdateCygxActivitySubmitMeetingByActivityId, Err: " + err.Error())
		return
	}

	//修改签到时间
	err = models.UpdateCygxActivityOfflineMeetingDetailSigninTime(activityid, userId)
	if err != nil {
		err = errors.New("UpdateCygxActivityOfflineMeetingDetailSigninTime, Err: " + err.Error())
		return
	}
	return
}

// CygxActivityRestrictSignupByTask 定时任务处理未到会人员的爽约
func CygxActivityRestrictSignupByTask(cont context.Context) (err error) {
	//func CygxActivityRestrictSignupByTask() (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("判断是否加入黑名单记录失败"+err.Error(), 2)
		}
	}()
	var pars []interface{}
	var condition string
	startDate := time.Now().Add(-time.Minute * 120).Format(utils.FormatDateTime)
	endDate := time.Now().Add(-time.Minute * 105).Format(utils.FormatDateTime)
	condition = " AND  activity_type_id IN (5,6)  AND signin_img != ''  AND  publish_status = 1  AND activity_time  >= ?  AND  activity_time  <= ? "
	pars = append(pars, startDate, endDate)
	activityList, err := models.GetActivityListByCondition(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	if len(activityList) == 0 {
		return
	}
	for _, v := range activityList {
		go AddCygxActivityRestrictSignupByAdmin(v.ActivityId) // 处理爽约限制
	}
	for _, v := range activityList {
		err = models.AddCygxActivityMeetDetailLogOffline(v.ActivityId)
		if err != nil {
			return
		}
		//修改活动是否提交到会状态
		err = models.UpdateCygxActivitySubmitMeetingByActivityId(v.ActivityId)
		if err != nil {
			err = errors.New("UpdateCygxActivitySubmitMeetingByActivityId, Err: " + err.Error())
			return
		}
		//把报名信息写入签到到会表
		AddCygxActivityOfflineMeetingDetail(v.ActivityId, 0)
	}
	return
}

func CygxActivityRestrictSignupByuid(uid, activityId int) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("判断是否加入黑名单记录失败"+err.Error(), 2)
		}
	}()

	err = models.DeleteCygxActivitySignupBreak(uid, activityId)
	if err != nil {
		err = errors.New("DeleteCygxActivitySignupBreak" + err.Error())
		return err
	}
	totalNomeet, err := models.GetActivitySignupNomeetingCountByUid(uid)
	if err != nil {
		err = errors.New("GetActivitySignupNomeetingCountByUid" + err.Error())
		return err
	}
	if totalNomeet < 3 {
		err = models.DeleteCygxActivityRestrictSignup(uid)
	}
	return
}

// GetUserActivitySigninTimeMap  获取用户的签到时间
func GetUserActivitySigninTimeMap(activityId int) (mapItem map[int]string, err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("扫码签到日志记录，失败,活动ID："+strconv.Itoa(activityId)+err.Error(), 2)
		}
	}()
	var condition string
	var pars []interface{}
	condition = "  AND activity_id = ?  "
	pars = append(pars, activityId)
	userList, e := models.GetCygxActivitySigninList(condition, pars)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("GetCygxActivitySigninList" + e.Error())
		return
	}
	mapItem = make(map[int]string, 0)
	for _, v := range userList {
		if v.UserId == 0 {
			continue
		}
		mapItem[v.UserId] = v.CreateTime.Format(utils.FormatDateTime)
	}
	return
}
