package cygx

import (
	"errors"
	"fmt"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services/alarm_msg"
	"hongze/hz_crm_api/utils"
)

// 获取线上活动到会更新时间
func GetActivityMeetUpdateTime(activityIdS []int) (mapResp map[int]string, err error) {
	defer func() {
		if err != nil {
			fmt.Println("err：", err)
			go alarm_msg.SendAlarmMsg("获取线上活动到会更新时间失败，Err："+err.Error()+fmt.Sprint(activityIdS), 3)
		}
	}()
	lenNum := len(activityIdS)
	if lenNum == 0 {
		return
	}
	var condition string
	var pars []interface{}
	condition = ` AND a.activity_id IN (` + utils.GetOrmInReplace(lenNum) + `)`
	pars = append(pars, activityIdS)
	condition += ` GROUP BY a.activity_id  `
	list, e := cygx.GetCygxActivityAttendanceDetailList(condition, pars, 0, lenNum)
	if e != nil {
		err = errors.New("GetCygxActivityAttendanceDetailList，Err" + e.Error())
		return
	}
	mapResp = make(map[int]string, 0)
	for _, v := range list {
		mapResp[v.ActivityId] = v.CreateTime.Format(utils.FormatDateTime)
	}
	return
}

func AddCygxActivityMeetDetailLogOffline(meetingUids string, activityId int) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("err：", err)
			go alarm_msg.SendAlarmMsg("添加线下到会记录失败，Err："+err.Error()+fmt.Sprint(activityId), 3)
		}
	}()
	if meetingUids == "" {
		return
	}

	list, e := cygx.GetOfflineMeetingListByUser(meetingUids, activityId)
	if e != nil {
		err = errors.New("GetOfflineMeetingListByUser" + e.Error())
		return
	}
	var companyIdArr []int
	var mobileArr []string
	var items []*cygx.CygxActivityMeetDetailLog

	if err != nil {
		return
	}
	//插入新的数据
	for _, v := range list {
		item := new(cygx.CygxActivityMeetDetailLog)
		item.ActivityId = v.ActivityId
		item.Mobile = v.Mobile
		item.CompanyName = v.CompanyName
		item.CompanyId = v.CompanyId
		item.CreateTime = v.CreateTime
		if v.Mobile != "" {
			mobileArr = append(mobileArr, v.Mobile)
		}
		if v.CompanyId > 0 {
			companyIdArr = append(companyIdArr, v.CompanyId)
		}
		items = append(items, item)
	}

	var condition string
	var pars []interface{}
	var itemsUpdateCompany []*cygx.MeetDetailLoggGroupCount
	lencompanyIdArr := len(companyIdArr)

	if lencompanyIdArr > 0 {
		condition = ` AND company_id IN (` + utils.GetOrmInReplace(lencompanyIdArr) + `)  GROUP BY company_id  `
		pars = append(pars, companyIdArr)
		listCompanyIdCount, e := cygx.GetMeetDetailLogGroupCount(condition, pars)
		if e != nil {
			err = errors.New("GetMeetDetailLogGroupCount" + e.Error())
			return
		}

		for _, v := range listCompanyIdCount {
			item := new(cygx.MeetDetailLoggGroupCount)
			item.CompanyId = v.CompanyId
			item.Count = v.Count
			itemsUpdateCompany = append(itemsUpdateCompany, item)
		}
	}
	fmt.Println(111)
	var itemsUpdateMobile []*cygx.MeetDetailLoggGroupCount
	lenmobileArr := len(mobileArr)
	if lenmobileArr > 0 {
		pars = make([]interface{}, 0)
		condition = ` AND mobile IN (` + utils.GetOrmInReplace(lenmobileArr) + `)  GROUP BY mobile  `
		pars = append(pars, mobileArr)
		listMobileCount, e := cygx.GetMeetDetailLogGroupCount(condition, pars)
		if e != nil {
			err = errors.New("GetMeetDetailLogGroupCount" + e.Error())
			return
		}
		for _, v := range listMobileCount {
			item := new(cygx.MeetDetailLoggGroupCount)
			item.Mobile = v.Mobile
			item.Count = v.Count
			itemsUpdateMobile = append(itemsUpdateMobile, item)
		}
	}

	e = cygx.UpdatecygxActivityMeetDetailLogOffline(items, itemsUpdateCompany, itemsUpdateMobile, activityId)
	if e != nil {
		err = errors.New("UpdatecygxActivityMeetDetailLogOffline" + e.Error())
		return
	}
	return
}

// AddctivitySignupDetailList
func AddctivitySignupDetailList(itemsDetail []*cygx.CygxActivitySignupDetail, activityId int) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("通过进门财经添加用户参数信息失败 ErrMsg:"+err.Error(), 2)
		}
	}()

	activityIds := make([]int, 0)
	activityIds = append(activityIds, 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)
	}

	signUpDetailList, err := cygx.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 := cygx.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 []*cygx.CygxActivitySignupDetail
	var itemsUpdate []*cygx.CygxActivitySignupDetail

	for _, v := range itemsDetail {
		var item = new(cygx.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.UserId = v.UserId
		item.RealName = v.RealName
		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 = v.IsMeeting
		item.UseridEntity = v.UseridEntity
		item.ActivityTime = v.ActivityTime
		item.CrmCompanyMapStatusId = v.CrmCompanyMapStatusId

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

	if len(itemsUpdate) > 0 {
		err = cygx.UpdateActivitySignupDetailMulti(itemsUpdate)
		if err != nil {
			return
		}
	}

	return err
}
