package services

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

// AddctivitySignupDetail  获取当天已结束的活动的报名信息。添加到到会详情
func AddctivitySignupDetail(cont context.Context) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg(" 获取当天已结束的活动的报名信息。添加到到会详情失败， ErrMsg:"+err.Error(), 2)
		}
	}()
	actList, err := models.GetActivitySattusToComplete()
	if err != nil && err.Error() != utils.ErrNoRow() {

		return err
	}
	if len(actList) == 0 {
		return err
	}
	activityIds := make([]int, 0)
	for _, v := range actList {
		activityIds = append(activityIds, v.ActivityId)
	}
	actLen := len(activityIds)

	var condition string
	var pars []interface{}
	if actLen > 0 {
		condition += ` AND activity_id IN (` + utils.GetOrmInReplace(actLen) + `)`
		pars = append(pars, activityIds)
	} else {
		return err
	}
	//condition = ""
	//获取当天已经结束了 活动的报名人数
	signUpList, err := models.GetSignupDetailBySignup(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return err
	}

	signUpDetailList, err := models.GetSignupDetailList(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return err
	}

	mapsignUpDetai := make(map[string]string)
	if len(signUpDetailList) > 0 {
		for _, v := range signUpDetailList {
			mapsignUpDetai[fmt.Sprint("ActivityId", v.ActivityId, "Mobile", v.Mobile)] = v.Mobile
		}
	}
	var items []*models.CygxActivitySignupDetail
	if len(signUpList) > 0 {
		for _, v := range signUpList {
			if _, ok := mapsignUpDetai[fmt.Sprint("ActivityId", v.ActivityId, "Mobile", v.Mobile)]; !ok {
				item := new(models.CygxActivitySignupDetail)
				item.UserId = v.UserId
				item.RealName = v.RealName
				item.SellerName = v.SellerName
				item.ActivityId = v.ActivityId
				item.CreateTime = v.CreateTime
				item.Mobile = v.Mobile
				item.OutboundMobile = v.OutboundMobile
				item.Email = v.Email
				item.CompanyId = v.CompanyId
				item.CompanyName = v.CompanyName
				item.SignupType = v.SignupType
				item.FailType = v.FailType
				item.DoFailType = v.DoFailType
				items = append(items, item)
				if len(items)%2000 == 0 {
					err = models.AddCygxActivitySignupDetail(items)
					if err != nil {
						return
					}
					items = make([]*models.CygxActivitySignupDetail, 0)
				}
			}
		}
	}
	if len(items) > 0 {
		err = models.AddCygxActivitySignupDetail(items)
		if err != nil {
			return
		}
	}
	return err
}

// AddctivitySignupDetailByJmcjNew 通过进门财经添加用户参数信息
func AddctivitySignupDetailByJmcjNew(itemsDetail []*models.CygxActivityAttendanceDetail, activityIds []int) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("通过进门财经添加用户参数信息失败 ErrMsg:"+err.Error(), 2)
		}
	}()
	actLen := len(activityIds)
	var condition string
	var pars []interface{}
	if actLen > 0 {
		condition += ` AND activity_id IN (` + utils.GetOrmInReplace(actLen) + `)`
		pars = append(pars, activityIds)
	}

	signUpDetailList, err := models.GetSignupDetailList(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return err
	}

	mapsignUpDetai := make(map[string]string)
	if len(signUpDetailList) > 0 {
		for _, v := range signUpDetailList {
			mapsignUpDetai[fmt.Sprint("ActivityId", v.ActivityId, "Mobile", v.Mobile)] = v.Mobile
		}
	}

	//获取当天已经结束了 活动的报名人数
	signUpList, err := models.GetSignupDetailBySignup(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return err
	}
	//建立外呼号与手机号的绑定
	mapsignUp := make(map[string]string)
	if len(signUpList) > 0 {
		for _, v := range signUpList {
			mapsignUp[fmt.Sprint(v.OutboundMobile)] = v.Mobile
		}
	}

	var itemsAdd []*models.CygxActivitySignupDetail
	var itemsUpdate []*models.CygxActivitySignupDetail

	var mobiles []string
	mobileMap := make(map[string]bool)
	mobileUserMap := make(map[string]int)
	for _, v := range itemsDetail {
		if !mobileMap[v.Mobile] {
			mobiles = append(mobiles, v.Mobile)
		}
		mobileMap[v.Mobile] = true
	}
	//mobiles = make([]string, 0)
	//mobiles = append(mobiles, "13054631616")
	//根据手机号获取这些用户的信息
	listUser, e := models.GetWxUserByOutboundMobiles(mobiles)
	if e != nil {
		err = errors.New("GetWxUserOutboundMobiles, Err: " + e.Error())
		return
	}

	for _, v := range listUser {
		mobileUserMap[v.Mobile] = v.UserId
	}

	for _, v := range itemsDetail {
		var item = new(models.CygxActivitySignupDetail)
		item.ActivityId = v.ActivityId
		item.RealName = v.RealName
		//如果手机号存在那么就是报名的，不存在就是直接参与的
		if _, ok := mapsignUp[v.Mobile]; ok {
			item.Mobile = mapsignUp[v.Mobile]
		} else {
			item.Mobile = v.Mobile
		}
		item.OutboundMobile = v.Mobile
		item.CompanyName = v.CompanyName
		item.CompanyId = v.CompanyId
		item.SellerName = v.SellerName
		item.FirstMeetingTime = v.FirstMeetingTime
		item.LastMeetingTime = v.LastMeetingTime
		item.Duration = v.Duration
		item.MeetingTypeStr = v.MeetingTypeStr
		item.MeetingAuthentication = v.MeetingAuthentication
		item.MeetingStatusStr = v.MeetingStatusStr
		item.CreateTime = v.CreateTime
		item.Position = v.Position
		item.IsMeeting = 1
		item.UseridEntity = v.UseridEntity
		item.ActivityTime = v.ActivityTime
		item.CrmCompanyMapStatusId = v.CrmCompanyMapStatusId
		item.UserId = mobileUserMap[v.Mobile]

		if _, ok := mapsignUpDetai[fmt.Sprint("ActivityId", v.ActivityId, "Mobile", item.Mobile)]; !ok {
			itemsAdd = append(itemsAdd, item)
		} else {
			itemsUpdate = append(itemsUpdate, item)
		}
	}
	if len(itemsAdd) > 0 {
		err = models.AddCygxActivitySignupDetail(itemsAdd)
		if err != nil {
			return
		}
	}
	if len(itemsUpdate) > 0 {
		err = models.UpdateActivitySignupDetailMulti(itemsUpdate)
		if err != nil {
			return
		}
	}

	//获取所有研选扣点的活动，如果是就行扣点流水处理
	mapActivityPoints, _ := GetActivityPointsAll()
	for _, v := range activityIds {
		if mapActivityPoints[v] {
			YanXuanActivityPointsBillSubmitMeeting(v, 0)
		}
	}

	//添加到Redis队列消息中，处理用户标签
	for _, v := range itemsAdd {
		if v.UserId > 0 {
			ActivityWxUserRaiLabelRedisAdd(v.ActivityId, v.UserId, v.CreateTime)
		}
	}
	return err
}

//func init() {
//	var err error
//	defer func() {
//		if err != nil {
//			fmt.Println(err)
//		}
//	}()
//	var condition string
//	var pars []interface{}
//	condition = " AND  user_id = 0 AND company_id > 1 AND mobile != '' "
//	signUpDetailList, e := models.GetSignupDetailList(condition, pars)
//	if e != nil && e.Error() != utils.ErrNoRow() {
//		err = errors.New("GetSignupDetailList, Err: " + e.Error())
//		return
//	}
//	var mobiles []string
//	mobileMap := make(map[string]bool)
//	for _, v := range signUpDetailList {
//		if !mobileMap[v.Mobile] {
//			mobiles = append(mobiles, v.Mobile)
//		}
//		mobileMap[v.Mobile] = true
//	}
//	//mobiles = make([]string, 0)
//	//mobiles = append(mobiles, "13054631616")
//	//根据手机号获取这些用户的信息
//	listUser, e := models.GetWxUserByOutboundMobiles(mobiles)
//	if e != nil {
//		err = errors.New("GetWxUserOutboundMobiles, Err: " + e.Error())
//		return
//	}
//
//	var itemsUpdate []*models.CygxActivitySignupDetail
//	for _, v := range listUser {
//		item := new(models.CygxActivitySignupDetail)
//		item.UserId = v.UserId
//		item.Mobile = v.Mobile
//		itemsUpdate = append(itemsUpdate, item)
//	}
//	fmt.Println(len(itemsUpdate))
//	err = models.UpdateActivitySignupDetailMultiMobileAndUserId(itemsUpdate)
//}
