package roadshow

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/models/roadshow"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/services"
	roadshowService "hongze/hz_crm_api/services/roadshow"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
	"time"
)

// ResearcherReportList
// @Title 研究员路演统计
// @Description 研究员路演统计接口
// @Param   DataType   query   string  true       "枚举值：week、month、time_interval"
// @Param   StartDate   query   string  true       "开始日期,格式：2022-04-06"
// @Param   EndDate   query   string  true       "结束日期,格式：2022-04-06"
// @Param   CompanyType   query   string  true       "客户类型：'ficc','权益',传空默认为ficc,"
// @Success 200 {object} roadshow.RsReportRecordResp
// @router /report/researcher/list [get]
func (this *CalendarController) ResearcherReportList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	dataType := this.GetString("DataType")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")
	companyType := this.GetString("CompanyType", "ficc")
	var adminDataList []roadshowService.AdminDataMap
	var err error
	//var firstDate time.Time
	//获取列表
	switch dataType {
	case "week":
		adminDataList, _, err = roadshowService.GetWeekData("researcher", "company_status")
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "month":
		adminDataList, _, err = roadshowService.GetMonthData("researcher", "company_status")
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "time_interval":
		if startDate == `` || endDate == `` {
			br.Msg = "开始日期或结束日期不能为空"
			br.ErrMsg = "开始日期或结束日期不能为空，Err:" + err.Error()
			return
		}
		adminDataList, _, err = roadshowService.GetTimeIntervalData(startDate, endDate, "researcher", "company_status")
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	}

	var group []*roadshow.ResearcherGroup
	var researcherList []*roadshow.ResearcherGroup

	switch companyType {
	case utils.COMPANY_CLASSIFY_FICC:
		group, err = roadshow.GetResearcherGroup()
		if err != nil {
			br.Msg = "获取信息失败！"
			br.ErrMsg = "获取分组信息失败！Err:" + err.Error()
			return
		}
		researcherList, err = roadshow.GetResearcherV2()
		if err != nil {
			br.Msg = "获取信息失败！"
			br.ErrMsg = "获取分组信息失败！,GetResearcherV2 Err:" + err.Error()
			return
		}
	case utils.COMPANY_CLASSIFY_RAI:
		mapPermissionUser := make(map[string][]*roadshow.ResearcherGroup)
		listPermission, err := cygx.GetChartPermissionAll()
		if err != nil {
			br.Msg = "获取信息失败"
			br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
			return
		}
		mapPermissionId := make(map[string]int)
		for _, v := range listPermission {
			mapPermissionId[v.PermissionName] = v.ChartPermissionId
		}
		askUserList, err := cygx.GetAskEmailListResearcher()
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败,GetAskEmailListResearcher Err: " + err.Error()
			return
		}
		sysUserList, err := system.GetAdminList()
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取admin列表失败, Err:" + err.Error()
			return
		}
		adminMap := make(map[int]*system.Admin, 0)
		for _, v := range sysUserList {
			adminMap[v.AdminId] = v
		}

		for _, v := range askUserList {
			if admin, ok := adminMap[v.AdminId]; ok {
				item := &roadshow.ResearcherGroup{
					AdminId:      v.AdminId,
					RealName:     v.Name,
					GroupId:      mapPermissionId[v.ChartPermissionName], //行业ID作为组别，进行关系映射
					GroupName:    admin.GroupName,
					RoleTypeCode: admin.RoleTypeCode,
				}
				mapPermissionUser[v.ChartPermissionName] = append(mapPermissionUser[v.ChartPermissionName], item)
				researcherList = append(researcherList, item)
			}
		}
		for k, v := range mapPermissionUser {
			respItem := new(roadshow.ResearcherGroup)
			respItem.GroupName = k + "组"
			respItem.GroupId = mapPermissionId[k]
			respItem.ResearcherList = v
			group = append(group, respItem)
		}
	}
	groupMap := make(map[int][]*roadshow.ResearcherGroup)
	for _, v := range researcherList {
		item := &roadshow.ResearcherGroup{
			GroupId:      v.GroupId,
			GroupName:    v.GroupName,
			AdminId:      v.AdminId,
			RealName:     v.RealName,
			RoleTypeCode: v.RoleTypeCode,
		}
		if findItems, ok := groupMap[v.GroupId]; ok {
			findItems = append(findItems, item)
			groupMap[v.GroupId] = findItems
		} else {
			findItems = append(findItems, item)
			groupMap[v.GroupId] = findItems
		}
		//}
	}
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
	}

	groupReportRecordList := make([]roadshow.GroupReportRecord, 0)
	rsAllReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)
	RsReportRecordResp := roadshow.RsReportRecordResp{
		List:                  groupReportRecordList,
		RsReportRecordNumList: rsAllReportRecordNumList,
	}

	startDateIndexList := make(map[int]string) //开始时间间隔map
	endDateIndexList := make(map[int]string)   //结束时间间隔map

	tmpAllTryOutNumMap := make(map[int]int)
	tmpAllFormalNumMap := make(map[int]int)
	tmpAllMeetingNumMap := make(map[int]int)
	tmpAllRoadShowNumMap := make(map[int]int)
	tmpAllSalonNumMap := make(map[int]int)
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminReportRecord := make([]roadshow.AdminReportRecord, 0)
		tmpGroupRsReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)

		//roadShowMap := make(map[int]int) // 路演总次数
		//salonMap := make(map[int]int)    //沙龙路演次数

		tmpGroupTryOutNumMap := make(map[int]int)
		tmpGroupFormalNumMap := make(map[int]int)
		tmpGroupMeetingNumMap := make(map[int]int)
		tmpGroupRoadShowNumMap := make(map[int]int)
		tmpGroupSalonMapNumMap := make(map[int]int)
		for _, researcher := range groupMap[v.GroupId] {
			//组内研究员数据
			//每个区间的数据
			tmpAdminRsReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)
			for index, adminData := range adminDataList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpTryOutNum, tmpFormalNum, tmpMeetingNum, tmpRoadShowNum, tmpSalonNum int
				if num, ok := adminData.TryOutMap[researcher.AdminId]; ok {
					tmpTryOutNum = num
				}
				if num, ok := adminData.FormalMap[researcher.AdminId]; ok {
					tmpFormalNum = num
				}
				if num, ok := adminData.MeetingMap[researcher.AdminId]; ok {
					tmpMeetingNum = num
				}
				if num, ok := adminData.RoadShowMap[researcher.AdminId]; ok { //路演总次数
					tmpRoadShowNum = num
				}
				if num, ok := adminData.SalonMap[researcher.AdminId]; ok { //路演总次数
					tmpSalonNum = num
				}

				tmpAdminRsReportRecordNum := roadshow.RsReportRecordNum{
					TryOutNum:   tmpTryOutNum,
					FormalNum:   tmpFormalNum,
					MeetingNum:  tmpMeetingNum,
					RoadShowNum: tmpRoadShowNum,
					SalonNum:    tmpSalonNum,
					StartDate:   adminData.StartDate,
					EndDate:     adminData.EndDate,
				}
				tmpAdminRsReportRecordNumList = append(tmpAdminRsReportRecordNumList, tmpAdminRsReportRecordNum)

				//组内数据汇总
				if _, ok := tmpGroupTryOutNumMap[index]; !ok {
					tmpGroupTryOutNumMap[index] = 0
				}
				if _, ok := tmpGroupFormalNumMap[index]; !ok {
					tmpGroupFormalNumMap[index] = 0
				}
				if _, ok := tmpGroupMeetingNumMap[index]; !ok {
					tmpGroupMeetingNumMap[index] = 0
				}
				if _, ok := tmpGroupRoadShowNumMap[index]; !ok {
					tmpGroupRoadShowNumMap[index] = 0
				}
				if _, ok := tmpGroupSalonMapNumMap[index]; !ok {
					tmpGroupSalonMapNumMap[index] = 0
				}
				tmpGroupTryOutNumMap[index] += tmpTryOutNum
				tmpGroupFormalNumMap[index] += tmpFormalNum
				tmpGroupMeetingNumMap[index] += tmpMeetingNum
				tmpGroupRoadShowNumMap[index] += tmpRoadShowNum
				tmpGroupSalonMapNumMap[index] += tmpSalonNum

				//总数据汇总
				if _, ok := tmpAllTryOutNumMap[index]; !ok {
					tmpAllTryOutNumMap[index] = 0
				}
				if _, ok := tmpAllFormalNumMap[index]; !ok {
					tmpAllFormalNumMap[index] = 0
				}
				if _, ok := tmpAllMeetingNumMap[index]; !ok {
					tmpAllMeetingNumMap[index] = 0
				}
				if _, ok := tmpGroupRoadShowNumMap[index]; !ok {
					tmpGroupRoadShowNumMap[index] = 0
				}
				if _, ok := tmpGroupSalonMapNumMap[index]; !ok {
					tmpGroupSalonMapNumMap[index] = 0
				}
				tmpAllTryOutNumMap[index] += tmpTryOutNum
				tmpAllFormalNumMap[index] += tmpFormalNum
				tmpAllMeetingNumMap[index] += tmpMeetingNum
				tmpAllRoadShowNumMap[index] += tmpRoadShowNum
				tmpAllSalonNumMap[index] += tmpSalonNum
			}
			tmpAdminReportRecord := roadshow.AdminReportRecord{
				Name:                  researcher.RealName,
				AdminId:               researcher.AdminId,
				RsReportRecordNumList: tmpAdminRsReportRecordNumList,
			}
			tmpGroupAdminReportRecord = append(tmpGroupAdminReportRecord, tmpAdminReportRecord)
		}

		for i := 0; i < len(tmpGroupMeetingNumMap); i++ {
			tmpGroupRsReportRecordNum := roadshow.RsReportRecordNum{
				TryOutNum:   tmpGroupTryOutNumMap[i],
				FormalNum:   tmpGroupFormalNumMap[i],
				MeetingNum:  tmpGroupMeetingNumMap[i],
				RoadShowNum: tmpGroupRoadShowNumMap[i],
				SalonNum:    tmpGroupSalonMapNumMap[i],
				StartDate:   startDateIndexList[i],
				EndDate:     endDateIndexList[i],
			}
			tmpGroupRsReportRecordNumList = append(tmpGroupRsReportRecordNumList, tmpGroupRsReportRecordNum)
		}
		groupReportRecord := roadshow.GroupReportRecord{
			Item:                  tmpGroupAdminReportRecord,
			Name:                  v.GroupName,
			RsReportRecordNumList: tmpGroupRsReportRecordNumList,
		}
		RsReportRecordResp.List = append(RsReportRecordResp.List, groupReportRecord)
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllFormalNumMap); i++ {
		tmpGroupRsReportRecordNum := roadshow.RsReportRecordNum{
			TryOutNum:   tmpAllTryOutNumMap[i],
			FormalNum:   tmpAllFormalNumMap[i],
			MeetingNum:  tmpAllMeetingNumMap[i],
			RoadShowNum: tmpAllRoadShowNumMap[i],
			SalonNum:    tmpAllSalonNumMap[i],
			StartDate:   startDateIndexList[i],
			EndDate:     endDateIndexList[i],
		}
		rsAllReportRecordNumList = append(rsAllReportRecordNumList, tmpGroupRsReportRecordNum)
	}
	RsReportRecordResp.RsReportRecordNumList = rsAllReportRecordNumList

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = RsReportRecordResp
	return
}

// SellerReportList
// @Title 销售路演统计
// @Description 销售路演统计接口
// @Param   DataType   query   string  true       "枚举值：week、month、time_interval、online、offline"
// @Param   DimensionType   query   string  true       "维度枚举值：company_status、roadshow_type"
// @Param   StartDate   query   string  true       "开始日期,格式：2022-04-06"
// @Param   EndDate   query   string  true       "结束日期,格式：2022-04-06"
// @Param   CompanyType   query   string  true       "客户类型：'ficc','权益',传空默认为ficc,"
// @Success 200 {object} roadshow.RsReportRecordResp
// @router /report/seller/list [get]
func (this *CalendarController) SellerReportList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	dataType := this.GetString("DataType")
	dimensionType := this.GetString("DimensionType")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")
	companyType := this.GetString("CompanyType", "ficc")
	if dimensionType == "" {
		dimensionType = "company_status"
	}
	if companyType == utils.COMPANY_CLASSIFY_RAI {
		dimensionType = "roadshow_rai" // 类型传了权益，就强制查询权益的路演
	}

	var adminDataList []roadshowService.AdminDataMap
	var err error
	var firstDate time.Time
	//获取列表
	switch dataType {
	case "week":
		adminDataList, firstDate, err = roadshowService.GetWeekDataV2("seller", dimensionType, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "month":
		adminDataList, firstDate, err = roadshowService.GetMonthDataV2("seller", dimensionType, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "time_interval":
		if startDate == `` || endDate == `` {
			br.Msg = "开始日期或结束日期不能为空"
			br.ErrMsg = "开始日期或结束日期不能为空，Err:" + err.Error()
			return
		}
		adminDataList, firstDate, err = roadshowService.GetTimeIntervalData(startDate, endDate, "seller", dimensionType)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}

	}

	var group []services.AdminGroup
	var groupIdRelationMap map[int]int
	var researcherList []*roadshow.Researcher
	switch companyType {
	case utils.COMPANY_CLASSIFY_FICC:
		group, groupIdRelationMap, err = services.GetFiccSystemGroup()
		if err != nil {
			br.Msg = "获取信息失败！"
			br.ErrMsg = "获取FICC销售信息失败！Err:" + err.Error()
			return
		}
		researcherList, err = services.GetFiccSeller(firstDate, groupIdRelationMap)
	case utils.COMPANY_CLASSIFY_RAI:
		group, groupIdRelationMap, err = services.GetRaiSystemGroup()
		if err != nil {
			br.Msg = "获取信息失败！"
			br.ErrMsg = "获取分组信息失败！Err:" + err.Error()
			return
		}
		researcherList, err = services.GetRaiSeller(groupIdRelationMap)
		if err != nil {
			br.Msg = "获取信息失败！"
			br.ErrMsg = "获取权益销售信息失败！Err:" + err.Error()
			return
		}
	}

	groupMap := make(map[int][]*roadshow.Researcher)
	for _, v := range researcherList {
		if v.RoleTypeCode == "ficc_admin" {
			findItems := groupMap[1]
			findItems = append(findItems, v)
			groupMap[1] = findItems
		} else {
			if findItems, ok := groupMap[v.GroupId]; ok {
				findItems = append(findItems, v)
				groupMap[v.GroupId] = findItems
			} else {
				findItems = append(findItems, v)
				groupMap[v.GroupId] = findItems
			}
		}
	}

	groupReportRecordList := make([]roadshow.GroupReportRecord, 0)
	rsAllReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)
	RsReportRecordResp := roadshow.RsReportRecordResp{
		List:                  groupReportRecordList,
		RsReportRecordNumList: rsAllReportRecordNumList,
	}

	startDateIndexList := make(map[int]string) //开始时间间隔map
	endDateIndexList := make(map[int]string)   //结束时间间隔map

	tmpAllTryOutNumMap := make(map[int]int)
	tmpAllFormalNumMap := make(map[int]int)
	tmpAllMeetingNumMap := make(map[int]int)
	tmpAllOnlineNumMap := make(map[int]int)
	tmpAllOfflineNumMap := make(map[int]int)
	tmpAllRoadShowNumMap := make(map[int]int)
	tmpAllSalonNumMap := make(map[int]int)
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminReportRecord := make([]roadshow.AdminReportRecord, 0)
		tmpGroupRsReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)

		tmpGroupTryOutNumMap := make(map[int]int)
		tmpGroupFormalNumMap := make(map[int]int)
		tmpGroupMeetingNumMap := make(map[int]int)
		tmpGroupOnlineNumMap := make(map[int]int)
		tmpGroupOfflineNumMap := make(map[int]int)
		tmpGroupRoadShowNumMap := make(map[int]int)
		tmpGroupSalonNumMap := make(map[int]int)
		for _, researcher := range groupMap[v.GroupId] {
			//组内研究员数据
			//每个区间的数据
			tmpAdminRsReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)
			for index, adminData := range adminDataList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpTryOutNum, tmpFormalNum, tmpMeetingNum, tmpOnlineNum, tmpOfflineNum, tmpRoadShowNum, tmpSalonNum int
				if num, ok := adminData.TryOutMap[researcher.AdminId]; ok {
					tmpTryOutNum = num
				}
				if num, ok := adminData.FormalMap[researcher.AdminId]; ok {
					tmpFormalNum = num
				}
				if num, ok := adminData.MeetingMap[researcher.AdminId]; ok {
					tmpMeetingNum = num
				}
				if num, ok := adminData.OnlineMap[researcher.AdminId]; ok {
					tmpOnlineNum = num
				}
				if num, ok := adminData.OfflineMap[researcher.AdminId]; ok {
					tmpOfflineNum = num
				}
				if num, ok := adminData.RoadShowMap[researcher.AdminId]; ok {
					tmpRoadShowNum = num
				}
				if num, ok := adminData.SalonMap[researcher.AdminId]; ok {
					tmpSalonNum = num
				}
				tmpAdminRsReportRecordNum := roadshow.RsReportRecordNum{
					TryOutNum:   tmpTryOutNum,
					FormalNum:   tmpFormalNum,
					MeetingNum:  tmpMeetingNum,
					OnlineNum:   tmpOnlineNum,
					OfflineNum:  tmpOfflineNum,
					RoadShowNum: tmpRoadShowNum,
					SalonNum:    tmpSalonNum,
					StartDate:   adminData.StartDate,
					EndDate:     adminData.EndDate,
				}
				tmpAdminRsReportRecordNumList = append(tmpAdminRsReportRecordNumList, tmpAdminRsReportRecordNum)

				//组内数据汇总
				if _, ok := tmpGroupTryOutNumMap[index]; !ok {
					tmpGroupTryOutNumMap[index] = 0
				}
				if _, ok := tmpGroupFormalNumMap[index]; !ok {
					tmpGroupFormalNumMap[index] = 0
				}
				if _, ok := tmpGroupMeetingNumMap[index]; !ok {
					tmpGroupMeetingNumMap[index] = 0
				}
				if _, ok := tmpGroupOnlineNumMap[index]; !ok {
					tmpGroupOnlineNumMap[index] = 0
				}
				if _, ok := tmpGroupOfflineNumMap[index]; !ok {
					tmpGroupOfflineNumMap[index] = 0
				}
				if _, ok := tmpGroupRoadShowNumMap[index]; !ok {
					tmpGroupRoadShowNumMap[index] = 0
				}
				if _, ok := tmpGroupSalonNumMap[index]; !ok {
					tmpGroupSalonNumMap[index] = 0
				}
				tmpGroupTryOutNumMap[index] += tmpTryOutNum
				tmpGroupFormalNumMap[index] += tmpFormalNum
				tmpGroupMeetingNumMap[index] += tmpMeetingNum
				tmpGroupOnlineNumMap[index] += tmpOnlineNum
				tmpGroupOfflineNumMap[index] += tmpOfflineNum
				tmpGroupRoadShowNumMap[index] += tmpRoadShowNum
				tmpGroupSalonNumMap[index] += tmpSalonNum

				//总数据汇总
				if _, ok := tmpAllTryOutNumMap[index]; !ok {
					tmpAllTryOutNumMap[index] = 0
				}
				if _, ok := tmpAllFormalNumMap[index]; !ok {
					tmpAllFormalNumMap[index] = 0
				}
				if _, ok := tmpAllMeetingNumMap[index]; !ok {
					tmpAllMeetingNumMap[index] = 0
				}
				if _, ok := tmpAllOnlineNumMap[index]; !ok {
					tmpAllOnlineNumMap[index] = 0
				}
				if _, ok := tmpAllOfflineNumMap[index]; !ok {
					tmpAllOfflineNumMap[index] = 0
				}
				if _, ok := tmpAllRoadShowNumMap[index]; !ok {
					tmpAllRoadShowNumMap[index] = 0
				}
				if _, ok := tmpAllSalonNumMap[index]; !ok {
					tmpAllSalonNumMap[index] = 0
				}
				tmpAllTryOutNumMap[index] += tmpTryOutNum
				tmpAllFormalNumMap[index] += tmpFormalNum
				tmpAllMeetingNumMap[index] += tmpMeetingNum
				tmpAllOnlineNumMap[index] += tmpOnlineNum
				tmpAllOfflineNumMap[index] += tmpOfflineNum
				tmpAllRoadShowNumMap[index] += tmpRoadShowNum
				tmpAllSalonNumMap[index] += tmpSalonNum
			}
			tmpAdminReportRecord := roadshow.AdminReportRecord{
				Name:                  researcher.RealName,
				AdminId:               researcher.AdminId,
				RsReportRecordNumList: tmpAdminRsReportRecordNumList,
			}
			tmpGroupAdminReportRecord = append(tmpGroupAdminReportRecord, tmpAdminReportRecord)
		}

		for i := 0; i < len(tmpGroupMeetingNumMap); i++ {
			tmpGroupRsReportRecordNum := roadshow.RsReportRecordNum{
				TryOutNum:   tmpGroupTryOutNumMap[i],
				FormalNum:   tmpGroupFormalNumMap[i],
				MeetingNum:  tmpGroupMeetingNumMap[i],
				OnlineNum:   tmpGroupOnlineNumMap[i],
				OfflineNum:  tmpGroupOfflineNumMap[i],
				RoadShowNum: tmpGroupRoadShowNumMap[i],
				SalonNum:    tmpGroupSalonNumMap[i],
				StartDate:   startDateIndexList[i],
				EndDate:     endDateIndexList[i],
			}
			tmpGroupRsReportRecordNumList = append(tmpGroupRsReportRecordNumList, tmpGroupRsReportRecordNum)
		}
		groupReportRecord := roadshow.GroupReportRecord{
			Item:                  tmpGroupAdminReportRecord,
			Name:                  v.GroupName,
			GruopId:               v.GroupId,
			RsReportRecordNumList: tmpGroupRsReportRecordNumList,
		}
		groupReportRecordList = append(groupReportRecordList, groupReportRecord)
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllFormalNumMap); i++ {
		tmpGroupRsReportRecordNum := roadshow.RsReportRecordNum{
			TryOutNum:   tmpAllTryOutNumMap[i],
			FormalNum:   tmpAllFormalNumMap[i],
			MeetingNum:  tmpAllMeetingNumMap[i],
			OnlineNum:   tmpAllOnlineNumMap[i],
			OfflineNum:  tmpAllOfflineNumMap[i],
			RoadShowNum: tmpAllRoadShowNumMap[i],
			SalonNum:    tmpAllSalonNumMap[i],
			StartDate:   startDateIndexList[i],
			EndDate:     endDateIndexList[i],
		}
		rsAllReportRecordNumList = append(rsAllReportRecordNumList, tmpGroupRsReportRecordNum)
	}
	RsReportRecordResp.RsReportRecordNumList = rsAllReportRecordNumList

	//tmpList := groupReportRecordList
	//因为RoleTypeCode不一样，所以需要重新从数据库取数据
	//adminInfo, _ := system.GetSysAdminById(sysUser.AdminId)
	//adminGroupId := adminInfo.GroupId
	//if tmpGroupId, ok := groupIdRelationMap[adminGroupId]; ok {
	//	adminGroupId = tmpGroupId
	//}
	//switch adminInfo.RoleTypeCode {
	//case utils.ROLE_TYPE_CODE_FICC_GROUP, utils.ROLE_TYPE_CODE_FICC_TEAM:
	//	for index, v := range tmpList {
	//		//如果不是同一个分组，那么就移除该分组下的人员数据
	//		if v.GruopId != adminGroupId {
	//			tmpList[index].Item = make([]roadshow.AdminReportRecord, 0)
	//		}
	//	}
	//	RsReportRecordResp.List = tmpList
	//case utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_ADMIN:
	//	//管理员、超管看全部数据
	//	RsReportRecordResp.List = groupReportRecordList
	//}

	// 20230529-有权限查看该页面的用户权限同admin
	RsReportRecordResp.List = groupReportRecordList

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = RsReportRecordResp
	return
}

// ReportCalendarList
// @Title 路演详情列表
// @Description 路演详情列表接口
// @Param   AdminType   query   string  true       "枚举值：researcher、seller、 special：专项路演"
// @Param   DataType   query   string  true       "枚举值：try_out、formal、meeting、online、offline"
// @Param   AdminId   query   int  true       "用户id"
// @Param   StartDate   query   string  true       "开始日期,格式：2022-04-06"
// @Param   EndDate   query   string  true       "结束日期,格式：2022-04-06"
// @Success 200 {object} []roadshow.RsReportRecordList
// @router /report/calendar/list [get]
func (this *CalendarController) ReportCalendarList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	adminType := this.GetString("AdminType")
	dataType := this.GetString("DataType")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")
	adminId, _ := this.GetInt("AdminId")
	if adminId <= 0 {
		br.Msg = "请传入对应的人员"
		return
	}

	//正式客户
	var condition string
	var pars []interface{}
	condition = ` and a.start_date >= ? and a.end_date <= ? `
	pars = append(pars, startDate, endDate)

	//选择的人员类型（发起人还是被发起人）
	if adminType == "seller" {
		condition += ` and a.seller_id = ? `
	} else if adminType == "special" {
		condition += ` and a.seller_id = ? `
		detail, err := company.GetConfigDetailByCode("choice_researcher_adminid")
		if err != nil {
			br.Msg = "获取研究员信息失败！"
			br.ErrMsg = "选择研究员失败，Err:" + err.Error()
			return
		}
		condition += ` and a.researcher_id IN (` + detail.ConfigValue + `) `
	} else {
		condition += ` and a.researcher_id = ? `
	}
	pars = append(pars, adminId)

	//获取列表
	switch dataType {
	case "try_out":
		condition += ` and a.company_status = ? `
		pars = append(pars, "试用")
	case "formal":
		condition += ` and a.company_status = ? `
		pars = append(pars, "正式")
	case "meeting":
		condition += ` and a.activity_type = ? `
		pars = append(pars, "公开会议")
	case "online":
		condition += ` and a.roadshow_type = ? `
		pars = append(pars, "线上")
	case "offline":
		condition += ` and a.roadshow_type = ? `
		pars = append(pars, "线下")
	case "road_show":
		condition += ` and a.activity_type = ? `
		pars = append(pars, "路演")
	case "salon":
		condition += ` and a.activity_type = ? `
		pars = append(pars, "沙龙")
	default:
		br.Msg = "请传入类型"
		br.ErrMsg = "请传入类型DataType"
		return
	}

	list, err := roadshow.GetRsReportRecordList(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,ERR:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
	return
}

// ChoiceResearcher
// @Title 选择的研究员
// @Description 选择的研究员接口
// @Param   AdminIds   query   string  true       "研究员ID ，多个用 , 隔开"
// @Success 200 操作成功
// @router /report/choice/researcher [post]
func (this *CalendarController) ChoiceResearcher() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req roadshow.ResearcherIds
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.AdminIds == "" {
		req.AdminIds = "0"
	}
	adminIdArr := strings.Split(req.AdminIds, ",")
	for _, v := range adminIdArr {
		_, err := strconv.Atoi(v)
		if err != nil {
			br.Msg = "选择研究员失败！"
			br.ErrMsg = "选择研究员失败，Err:" + err.Error()
			return
		}
	}
	err = company.CrmConfigUpdate(req.AdminIds, "choice_researcher_adminid")
	if err != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// AdminList
// @Title 选择的研究员列表
// @Description 获取选择的研究员列表
// @Success 200 {object} roadshow.Researcher
// @Success 200 操作成功
// @router /report/admin/list [get]
func (this *CalendarController) AdminList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	detail, err := company.GetConfigDetailByCode("choice_researcher_adminid")
	if err != nil {
		br.Msg = "获取研究员信息失败！"
		br.ErrMsg = "选择研究员失败，Err:" + err.Error()
		return
	}
	list, err := roadshow.GetChoiceResearcher(detail.ConfigValue)
	if err != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.Data = list
	return
}

// SellerReportList
// @Title 专项路演统计
// @Description 专项路演统计接口
// @Success 200 {object} roadshow.RsReportRecordResp
// @router /report/special/list [get]
func (this *CalendarController) SpecialReportList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	dimensionType := this.GetString("DimensionType")

	if dimensionType == "" {
		dimensionType = "seller_special"
	}

	var adminDataList []roadshowService.AdminDataMap
	var err error
	var firstDate time.Time
	//获取列表
	adminDataList, firstDate, err = roadshowService.GetWeekData("seller", dimensionType)
	if err != nil {
		br.Msg = "数据异常"
		br.ErrMsg = "数据异常，Err:" + err.Error()
		return
	}
	var haveData bool
	detail, err := company.GetConfigDetailByCode("choice_researcher_adminid")
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取选择研究员信息失败，Err:" + err.Error()
		return
	}
	adminMap := make(map[int]int)
	adminIdArr := strings.Split(detail.ConfigValue, ",")
	for _, v := range adminIdArr {
		adminId, err := strconv.Atoi(v)
		if err != nil {
			br.Msg = "获取信息失败！"
			br.ErrMsg = "获取选择研究员信息失败，Err:" + err.Error()
			return
		}
		adminMap[adminId] = adminId
	}

	group, groupIdRelationMap, err := services.GetFiccSystemGroup()
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取分组信息失败！Err:" + err.Error()
		return
	}
	researcherList, err := services.GetFiccSeller(firstDate, groupIdRelationMap)
	groupMap := make(map[int][]*roadshow.Researcher)
	for _, v := range researcherList {
		//if v.RoleTypeCode == "ficc_admin" {
		//	findItems := groupMap[1]
		//	findItems = append(findItems, v)
		//	groupMap[1] = findItems
		//} else {
		if findItems, ok := groupMap[v.GroupId]; ok {
			findItems = append(findItems, v)
			groupMap[v.GroupId] = findItems
		} else {
			findItems = append(findItems, v)
			groupMap[v.GroupId] = findItems
		}
		//}
	}

	groupReportRecordList := make([]roadshow.GroupReportRecord, 0)
	rsAllReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)
	RsReportRecordResp := roadshow.RsReportRecordResp{
		List:                  groupReportRecordList,
		RsReportRecordNumList: rsAllReportRecordNumList,
	}

	startDateIndexList := make(map[int]string) //开始时间间隔map
	endDateIndexList := make(map[int]string)   //结束时间间隔map

	tmpAllTryOutNumMap := make(map[int]int)
	tmpAllFormalNumMap := make(map[int]int)
	tmpAllMeetingNumMap := make(map[int]int)
	tmpAllOnlineNumMap := make(map[int]int)
	tmpAllOfflineNumMap := make(map[int]int)
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminReportRecord := make([]roadshow.AdminReportRecord, 0)
		tmpGroupRsReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)

		tmpGroupTryOutNumMap := make(map[int]int)
		tmpGroupFormalNumMap := make(map[int]int)
		tmpGroupMeetingNumMap := make(map[int]int)
		tmpGroupOnlineNumMap := make(map[int]int)
		tmpGroupOfflineNumMap := make(map[int]int)
		for _, researcher := range groupMap[v.GroupId] {
			//组内研究员数据
			var isLook bool
			//如果是管理员或者本组的可以查看详情
			if sysUser.GroupId == v.GroupId || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
				isLook = true
			}

			//每个区间的数据
			tmpAdminRsReportRecordNumList := make([]roadshow.RsReportRecordNum, 0)
			var isAppend bool
			for index, adminData := range adminDataList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpTryOutNum, tmpFormalNum, tmpMeetingNum, tmpOnlineNum, tmpOfflineNum int
				if num, ok := adminData.TryOutMap[researcher.AdminId]; ok {
					tmpTryOutNum = num
				}
				if num, ok := adminData.FormalMap[researcher.AdminId]; ok {
					tmpFormalNum = num
				}
				if num, ok := adminData.MeetingMap[researcher.AdminId]; ok {
					tmpMeetingNum = num
				}
				if num, ok := adminData.OnlineMap[researcher.AdminId]; ok {
					tmpOnlineNum = num
				}
				if num, ok := adminData.OfflineMap[researcher.AdminId]; ok {
					tmpOfflineNum = num
				}
				tmpAdminRsReportRecordNum := roadshow.RsReportRecordNum{
					TryOutNum:  tmpTryOutNum,
					FormalNum:  tmpFormalNum,
					MeetingNum: tmpMeetingNum,
					OnlineNum:  tmpOnlineNum,
					OfflineNum: tmpOfflineNum,
					StartDate:  adminData.StartDate,
					EndDate:    adminData.EndDate,
					IsLook:     isLook,
				}
				if tmpTryOutNum > 0 || tmpFormalNum > 0 || tmpMeetingNum > 0 || tmpOnlineNum > 0 || tmpOfflineNum > 0 {
					isAppend = true
					haveData = true
				}
				tmpAdminRsReportRecordNumList = append(tmpAdminRsReportRecordNumList, tmpAdminRsReportRecordNum)

				//组内数据汇总
				if _, ok := tmpGroupTryOutNumMap[index]; !ok {
					tmpGroupTryOutNumMap[index] = 0
				}
				if _, ok := tmpGroupFormalNumMap[index]; !ok {
					tmpGroupFormalNumMap[index] = 0
				}
				if _, ok := tmpGroupMeetingNumMap[index]; !ok {
					tmpGroupMeetingNumMap[index] = 0
				}
				if _, ok := tmpGroupOnlineNumMap[index]; !ok {
					tmpGroupOnlineNumMap[index] = 0
				}
				if _, ok := tmpGroupOfflineNumMap[index]; !ok {
					tmpGroupOfflineNumMap[index] = 0
				}
				tmpGroupTryOutNumMap[index] += tmpTryOutNum
				tmpGroupFormalNumMap[index] += tmpFormalNum
				tmpGroupMeetingNumMap[index] += tmpMeetingNum
				tmpGroupOnlineNumMap[index] += tmpOnlineNum
				tmpGroupOfflineNumMap[index] += tmpOfflineNum

				//总数据汇总
				if _, ok := tmpAllTryOutNumMap[index]; !ok {
					tmpAllTryOutNumMap[index] = 0
				}
				if _, ok := tmpAllFormalNumMap[index]; !ok {
					tmpAllFormalNumMap[index] = 0
				}
				if _, ok := tmpAllMeetingNumMap[index]; !ok {
					tmpAllMeetingNumMap[index] = 0
				}
				if _, ok := tmpAllOnlineNumMap[index]; !ok {
					tmpAllOnlineNumMap[index] = 0
				}
				if _, ok := tmpAllOfflineNumMap[index]; !ok {
					tmpAllOfflineNumMap[index] = 0
				}
				tmpAllTryOutNumMap[index] += tmpTryOutNum
				tmpAllFormalNumMap[index] += tmpFormalNum
				tmpAllMeetingNumMap[index] += tmpMeetingNum
				tmpAllOnlineNumMap[index] += tmpOnlineNum
				tmpAllOfflineNumMap[index] += tmpOfflineNum
			}

			tmpAdminReportRecord := roadshow.AdminReportRecord{
				Name:                  researcher.RealName,
				AdminId:               researcher.AdminId,
				RsReportRecordNumList: tmpAdminRsReportRecordNumList,
			}
			if isAppend {
				tmpGroupAdminReportRecord = append(tmpGroupAdminReportRecord, tmpAdminReportRecord)
			}
		}

		for i := 0; i < len(tmpGroupMeetingNumMap); i++ {
			tmpGroupRsReportRecordNum := roadshow.RsReportRecordNum{
				TryOutNum:  tmpGroupTryOutNumMap[i],
				FormalNum:  tmpGroupFormalNumMap[i],
				MeetingNum: tmpGroupMeetingNumMap[i],
				OnlineNum:  tmpGroupOnlineNumMap[i],
				OfflineNum: tmpGroupOfflineNumMap[i],
				StartDate:  startDateIndexList[i],
				EndDate:    endDateIndexList[i],
			}
			tmpGroupRsReportRecordNumList = append(tmpGroupRsReportRecordNumList, tmpGroupRsReportRecordNum)
		}
		groupReportRecord := roadshow.GroupReportRecord{
			Item:                  tmpGroupAdminReportRecord,
			Name:                  v.GroupName,
			GruopId:               v.GroupId,
			RsReportRecordNumList: tmpGroupRsReportRecordNumList,
		}
		if len(tmpGroupAdminReportRecord) > 0 {
			groupReportRecordList = append(groupReportRecordList, groupReportRecord)
		}
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllFormalNumMap); i++ {
		tmpGroupRsReportRecordNum := roadshow.RsReportRecordNum{
			TryOutNum:  tmpAllTryOutNumMap[i],
			FormalNum:  tmpAllFormalNumMap[i],
			MeetingNum: tmpAllMeetingNumMap[i],
			OnlineNum:  tmpAllOnlineNumMap[i],
			OfflineNum: tmpAllOfflineNumMap[i],
			StartDate:  startDateIndexList[i],
			EndDate:    endDateIndexList[i],
		}
		rsAllReportRecordNumList = append(rsAllReportRecordNumList, tmpGroupRsReportRecordNum)
	}
	RsReportRecordResp.RsReportRecordNumList = rsAllReportRecordNumList

	tmpList := groupReportRecordList

	//因为RoleTypeCode不一样，所以需要重新从数据库取数据
	adminInfo, _ := system.GetSysAdminById(sysUser.AdminId)
	adminGroupId := adminInfo.GroupId
	if tmpGroupId, ok := groupIdRelationMap[adminGroupId]; ok {
		adminGroupId = tmpGroupId
	}

	switch adminInfo.RoleTypeCode {
	case utils.ROLE_TYPE_CODE_FICC_GROUP, utils.ROLE_TYPE_CODE_FICC_TEAM:
		for index, v := range tmpList {
			//如果不是同一个分组，那么就移除该分组下的人员数据
			if v.GruopId != adminGroupId {
				tmpList[index].Item = make([]roadshow.AdminReportRecord, 0)
			}
		}
		RsReportRecordResp.List = tmpList
	case utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_ADMIN:
		//管理员、超管看全部数据
		RsReportRecordResp.List = groupReportRecordList
	}
	//判断是否有数据如果没有，处理数据为空
	if !haveData {
		RsReportRecordResp = roadshow.RsReportRecordResp{
			List:                  make([]roadshow.GroupReportRecord, 0),
			RsReportRecordNumList: make([]roadshow.RsReportRecordNum, 0),
		}
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = RsReportRecordResp
	return
}

// ResearcherReportList
// @Title 研究员出差表
// @Description 研究员出差表接口
// @Param   AdminId   query   int  true       "研究员id"
// @Param   WeekQuery   query   int  false       "周查询: 0-本期(本周及下周); 1-前两周; 2-后两周"
// @Param   BaseQueryDate   query   string  false       "周查询时的开始日期"
// @Success 200 {object} roadshow.BusinessTripResp
// @router /report/researcher/business_trip [get]
func (this *CalendarController) ResearcherBusinessTrip() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	adminId, _ := this.GetInt("AdminId")
	weekQuery, _ := this.GetInt("WeekQuery")
	baseQueryDate := this.GetString("BaseQueryDate")
	//fmt.Println(adminId)
	group, err := roadshow.GetResearcherGroup()
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取分组信息失败！Err:" + err.Error()
		return
	}

	researcherList, err := roadshow.GetBusinessTripResearcher()
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取分组信息失败！,GetBusinessTripResearcher Err:" + err.Error()
		return
	}

	////获取当天周日期
	//nowWeekStart := utils.GetNowWeekMonday().AddDate(0, 0, -2)
	////nowWeekEnd := utils.GetNowWeekLastDay()
	////获取下周日期
	//nextWeekStart := utils.GetNextWeekMonday().AddDate(0, 0, -2)
	//nextWeekEnd := utils.GetNextWeekLastDay().AddDate(0, 0, -2)

	var nowWeekStart time.Time
	if baseQueryDate == "" {
		now := time.Now()
		weekDay := int(now.Weekday())
		if weekDay == 6 {
			nowWeekStart, _ = time.ParseInLocation(utils.FormatDate, now.Format(utils.FormatDate), time.Local)
		} else {
			nowWeekStart, _ = time.ParseInLocation(utils.FormatDate, now.AddDate(0, 0, -(weekDay+1)).Format(utils.FormatDate), time.Local)
		}
	}
	// 查询需求是可以无限往前后两周查询, 以入参为本周开始日期
	if baseQueryDate != "" {
		t, e := time.ParseInLocation(utils.FormatDate, baseQueryDate, time.Local)
		if e != nil {
			br.Msg = "查询开始日期格式有误"
			return
		}
		nowWeekStart = t
	}

	// 前/后两周
	if weekQuery == 1 {
		nowWeekStart = nowWeekStart.AddDate(0, 0, -14)
	}
	if weekQuery == 2 {
		nowWeekStart = nowWeekStart.AddDate(0, 0, +14)
	}
	//nowWeekEnd := utils.GetNowWeekLastDay()
	//获取下周日期
	nextWeekStart := nowWeekStart.AddDate(0, 0, +7)
	nextWeekEnd := nowWeekStart.AddDate(0, 0, +13)

	//获取路演信息
	roadshowList, err := roadshow.GetBusinessTripList(adminId, nowWeekStart.Format(utils.FormatDate), nextWeekEnd.Format(utils.FormatDateTime))
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取路演信息失败！,GetBusinessTripList Err:" + err.Error()
		return
	}

	roadshowMap := make(map[int][]*roadshow.BusinessTripResearcherList)

	for _, v := range roadshowList {
		if findVals, ok := roadshowMap[v.ResearcherId]; ok {
			findVals = append(findVals, v)
			roadshowMap[v.ResearcherId] = findVals
		} else {
			roadsItems := make([]*roadshow.BusinessTripResearcherList, 0)
			roadsItems = append(roadsItems, v)
			roadshowMap[v.ResearcherId] = roadsItems
		}
	}

	//fmt.Println("roadshow json start")
	//jsonBody, err := json.Marshal(roadshowMap)
	//fmt.Println("json body:" + string(jsonBody))
	//fmt.Println("roadshow json end")
	groupMap := make(map[int][]*roadshow.BusinessTripResearcher)

	for _, v := range researcherList {
		if findRoadshowList, ok := roadshowMap[v.AdminId]; ok {
			tripList := make([]*roadshow.BusinessTrip, 0)
			researcherMap := make(map[string]string)
			for i := 0; i < 7; i++ {
				newDay, _ := time.ParseInLocation(utils.FormatDate, nowWeekStart.AddDate(0, 0, i).Format(utils.FormatDate), time.Local)
				weekDate := newDay.Format(utils.FormatDate)
				tripItem := new(roadshow.BusinessTrip)
				for _, r := range findRoadshowList {
					startDateT, _ := time.ParseInLocation(utils.FormatDate, r.StartDate, time.Local)
					endDateT, _ := time.ParseInLocation(utils.FormatDate, r.EndDate, time.Local)
					if int(endDateT.Sub(startDateT)) <= 0 {
						skey := strconv.Itoa(r.ResearcherId) + r.StartDate
						if _, sok := researcherMap[skey]; !sok {
							if r != nil && (newDay.Equal(startDateT) || (newDay.Equal(endDateT)) || (newDay.Before(endDateT) && newDay.After(startDateT))) {
								tripItem.City = r.City
								researcherMap[skey] = skey
							}
						}
					} else {
						if r != nil && (newDay.Equal(startDateT) || (newDay.Equal(endDateT)) || (newDay.Before(endDateT) && newDay.After(startDateT))) {
							tripItem.City = r.City
							//researcherMap[skey] = skey
						}
					}

				}
				tripItem.WeekDate = weekDate
				tripItem.WeekType = "current"
				tripItem.Week = newDay.Weekday().String()
				tripList = append(tripList, tripItem)
			}

			for i := 0; i < 7; i++ {
				newDay := nextWeekStart.AddDate(0, 0, i)
				weekDate := newDay.Format(utils.FormatDate)
				tripItem := new(roadshow.BusinessTrip)
				for _, r := range findRoadshowList {
					startDateT, _ := time.ParseInLocation(utils.FormatDate, r.StartDate, time.Local)
					endDateT, _ := time.ParseInLocation(utils.FormatDate, r.EndDate, time.Local)
					if int(endDateT.Sub(startDateT)) <= 0 {
						skey := strconv.Itoa(r.ResearcherId) + r.StartDate
						if _, sok := researcherMap[skey]; !sok {
							if r != nil && (newDay.Equal(startDateT) || (newDay.Equal(endDateT)) || (newDay.Before(endDateT) && newDay.After(startDateT))) {
								tripItem.City = r.City
								researcherMap[skey] = skey
							}
						}
					} else {
						if r != nil && (newDay.Equal(startDateT) || (newDay.Equal(endDateT)) || (newDay.Before(endDateT) && newDay.After(startDateT))) {
							tripItem.City = r.City
							//researcherMap[skey] = skey
						}
					}
				}
				tripItem.WeekDate = weekDate
				tripItem.WeekType = "next"
				tripItem.Week = newDay.Weekday().String()
				tripList = append(tripList, tripItem)
			}

			v.BusinessTripList = tripList

			//if v.RoleTypeCode == "ficc_admin" {
			//	findItems := groupMap[1]
			//	findItems = append(findItems, v)
			//	groupMap[1] = findItems
			//} else {
			if findItems, ok := groupMap[v.GroupId]; ok {
				findItems = append(findItems, v)
				groupMap[v.GroupId] = findItems
			} else {
				findItems = append(findItems, v)
				groupMap[v.GroupId] = findItems
			}
			//}
		}
	}

	groupList := make([]*roadshow.BusinessTripGroup, 0)
	for _, v := range group {
		item := new(roadshow.BusinessTripGroup)
		item.GroupName = v.GroupName
		item.GroupId = v.GroupId
		item.ResearcherList = groupMap[v.GroupId]

		groupList = append(groupList, item)
	}
	resp := new(roadshow.BusinessTripResp)
	resp.GroupList = groupList
	resp.BaseDate = nowWeekStart.Format(utils.FormatDate)
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
	return
}

// OverseasCustomReportCalendarList
// @Title 路演详情列表
// @Description 路演详情列表接口
// @Param   Keyword   query   string  true       "关键字：客户名称"
// @Param   CompanyStatus   query   string  true       "客户状态，枚举值：正式、试用、关闭"
// @Param   SellerId   query   int  true       "销售id列表；多个用英文,分割"
// @Param   ResearcherId   query   int  true       "销售id列表；多个用英文,分割"
// @Param   StartDate   query   string  true       "开始日期,格式：2022-04-06"
// @Param   EndDate   query   string  true       "结束日期,格式：2022-04-06"
// @Param   SortField	query	string		false	"排序字段:start_date"
// @Param   SortDesc	query	int		false	"1:降序,默认,2:升序"
// @Success 200 {object} []roadshow.RsReportRecordList
// @router /overseas_custom/calendar/list [get]
func (this *CalendarController) OverseasCustomReportCalendarList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	keyword := this.GetString("Keyword")
	companyStatus := this.GetString("CompanyStatus")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")
	sellerId := this.GetString("SellerId")
	researcherId := this.GetString("ResearcherId")
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	sortField := this.GetString("SortField")
	sortDesc, _ := this.GetInt("SortDesc")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	switch sortField {
	case "start_date":
	default:
		sortField = "start_date"
	}

	// "1:降序,默认,2:升序"
	sortDesStr := ` desc `
	switch sortDesc {
	case 2:
		sortDesStr = ` asc `
	default:
		sortDesStr = "desc"
	}

	total, list, err := roadshow.GetOverseaCustomCalendarList(keyword, sellerId, researcherId, startDate, endDate, companyStatus, sortField, sortDesStr, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,ERR:" + err.Error()
		return
	}

	page := paging.GetPaging(currentIndex, pageSize, total)

	resp := new(roadshow.OverseaCustomRecordInfoResp)
	resp.Paging = page
	resp.List = list

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
	return
}

// OverseasCustomCalendarAdminList
// @Title 海外客户路演用户列表接口
// @Description 海外客户路演用户列表接口
// @Param   AdminType   query   string  true       "枚举值：researcher、seller"
// @Success 200 {object} []roadshow.AdminInfo
// @router /overseas_custom/sys_user/list [get]
func (this *CalendarController) OverseasCustomCalendarAdminList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	adminType := this.GetString("AdminType")

	var list []*roadshow.AdminInfo
	var err error
	//选择的人员类型（发起人还是被发起人）
	if adminType == "seller" {
		list, err = roadshow.GetOverseaCustomCalendarSellerList()
	} else if adminType == "researcher" {
		list, err = roadshow.GetOverseaCustomCalendarResearcherList()
	} else {
		br.Msg = "错误的类型"
		br.IsSendEmail = false
	}

	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,ERR:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
	return
}

// OverseasSellerReportList
// @Title 海外客户销售路演统计
// @Description 海外客户销售路演统计接口
// @Param   DataType   query   string  true       "枚举值：week、month、time_interval、online、offline"
// @Param   DimensionType   query   string  true       "维度枚举值：company_status、roadshow_type"
// @Param   StartDate   query   string  true       "开始日期,格式：2022-04-06"
// @Param   EndDate   query   string  true       "结束日期,格式：2022-04-06"
// @Success 200 {object} roadshow.RsReportRecordResp
// @router /overseas_custom/seller/list [get]
func (this *CalendarController) OverseasSellerReportList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	dataType := this.GetString("DataType")
	dimensionType := this.GetString("DimensionType")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")

	if dimensionType == "" {
		dimensionType = "company_status"
	}

	var adminDataList []roadshowService.OverseasAdminDataMap
	var err error

	//获取列表
	switch dataType {
	case "week":
		adminDataList, _, err = roadshowService.GetOverseasWeekDataV2("seller", dimensionType, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "month":
		adminDataList, _, err = roadshowService.GetOverseasMonthDataV2("seller", dimensionType, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "time_interval":
		if startDate == `` || endDate == `` {
			br.Msg = "开始日期或结束日期不能为空"
			br.ErrMsg = "开始日期或结束日期不能为空，Err:" + err.Error()
			return
		}
		adminDataList, _, err = roadshowService.GetOverseasTimeIntervalData(startDate, endDate, "seller", dimensionType)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}

	}

	sellerList, err := roadshow.GetOverseaCustomCalendarSellerList()

	adminReportRecordList := make([]roadshow.OverseasAdminReportRecord, 0)
	rsAllReportRecordNumList := make([]roadshow.RsOverseasReportRecordNum, 0)
	RsReportRecordResp := roadshow.RsOverseasReportRecordResp{
		List:                  adminReportRecordList,
		RsReportRecordNumList: rsAllReportRecordNumList,
	}

	startDateIndexList := make(map[int]string) //开始时间间隔map
	endDateIndexList := make(map[int]string)   //结束时间间隔map

	tmpAllTryOutNumMap := make(map[int]int)
	tmpAllFormalNumMap := make(map[int]int)
	tmpAllCloseNumMap := make(map[int]int)
	tmpAllOnlineNumMap := make(map[int]int)
	tmpAllOfflineNumMap := make(map[int]int)

	for _, researcher := range sellerList {
		//每个区间的数据
		tmpAdminRsReportRecordNumList := make([]roadshow.RsOverseasReportRecordNum, 0)
		for index, adminData := range adminDataList {
			startDateIndexList[index] = adminData.StartDate
			endDateIndexList[index] = adminData.EndDate

			var tmpTryOutNum, tmpFormalNum, tmpCloseNum, tmpOnlineNum, tmpOfflineNum int
			if num, ok := adminData.TryOutMap[researcher.AdminId]; ok {
				tmpTryOutNum = num
			}
			if num, ok := adminData.FormalMap[researcher.AdminId]; ok {
				tmpFormalNum = num
			}
			if num, ok := adminData.CloseMap[researcher.AdminId]; ok {
				tmpCloseNum = num
			}
			if num, ok := adminData.OnlineMap[researcher.AdminId]; ok {
				tmpOnlineNum = num
			}
			if num, ok := adminData.OfflineMap[researcher.AdminId]; ok {
				tmpOfflineNum = num
			}
			tmpAdminRsReportRecordNum := roadshow.RsOverseasReportRecordNum{
				TryOutNum:  tmpTryOutNum,
				FormalNum:  tmpFormalNum,
				CloseNum:   tmpCloseNum,
				OnlineNum:  tmpOnlineNum,
				OfflineNum: tmpOfflineNum,
				StartDate:  adminData.StartDate,
				EndDate:    adminData.EndDate,
			}
			tmpAdminRsReportRecordNumList = append(tmpAdminRsReportRecordNumList, tmpAdminRsReportRecordNum)

			//总数据汇总
			if _, ok := tmpAllTryOutNumMap[index]; !ok {
				tmpAllTryOutNumMap[index] = 0
			}
			if _, ok := tmpAllFormalNumMap[index]; !ok {
				tmpAllFormalNumMap[index] = 0
			}
			if _, ok := tmpAllCloseNumMap[index]; !ok {
				tmpAllCloseNumMap[index] = 0
			}
			if _, ok := tmpAllOnlineNumMap[index]; !ok {
				tmpAllOnlineNumMap[index] = 0
			}
			if _, ok := tmpAllOfflineNumMap[index]; !ok {
				tmpAllOfflineNumMap[index] = 0
			}
			tmpAllTryOutNumMap[index] += tmpTryOutNum
			tmpAllFormalNumMap[index] += tmpFormalNum
			tmpAllCloseNumMap[index] += tmpCloseNum
			tmpAllOnlineNumMap[index] += tmpOnlineNum
			tmpAllOfflineNumMap[index] += tmpOfflineNum
		}

		// 数据都为0的时候不显示
		adminTotalNum := 0
		for _, v := range tmpAdminRsReportRecordNumList {
			adminTotalNum += v.TryOutNum + v.CloseNum + v.FormalNum
		}
		if adminTotalNum > 0 {
			tmpAdminReportRecord := roadshow.OverseasAdminReportRecord{
				Name:                  researcher.RealName,
				AdminId:               researcher.AdminId,
				RsReportRecordNumList: tmpAdminRsReportRecordNumList,
			}
			RsReportRecordResp.List = append(RsReportRecordResp.List, tmpAdminReportRecord)
		}
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllFormalNumMap); i++ {
		tmpGroupRsReportRecordNum := roadshow.RsOverseasReportRecordNum{
			TryOutNum:  tmpAllTryOutNumMap[i],
			FormalNum:  tmpAllFormalNumMap[i],
			CloseNum:   tmpAllCloseNumMap[i],
			OnlineNum:  tmpAllOnlineNumMap[i],
			OfflineNum: tmpAllOfflineNumMap[i],
			StartDate:  startDateIndexList[i],
			EndDate:    endDateIndexList[i],
		}
		rsAllReportRecordNumList = append(rsAllReportRecordNumList, tmpGroupRsReportRecordNum)
	}
	RsReportRecordResp.RsReportRecordNumList = rsAllReportRecordNumList

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = RsReportRecordResp
	return
}

// OverseasResearcherReportList
// @Title 研究员路演统计
// @Description 研究员路演统计接口
// @Param   DataType   query   string  true       "枚举值：week、month、time_interval"
// @Param   StartDate   query   string  true       "开始日期,格式：2022-04-06"
// @Param   EndDate   query   string  true       "结束日期,格式：2022-04-06"
// @Success 200 {object} roadshow.RsReportRecordResp
// @router /overseas_custom/researcher/list [get]
func (this *CalendarController) OverseasResearcherReportList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	dataType := this.GetString("DataType")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")

	var adminDataList []roadshowService.OverseasAdminDataMap
	var err error
	//var firstDate time.Time
	//获取列表
	switch dataType {
	case "week":
		adminDataList, _, err = roadshowService.GetOverseasWeekData("researcher", "company_status")
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "month":
		adminDataList, _, err = roadshowService.GetOverseasMonthData("researcher", "company_status")
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "time_interval":
		if startDate == `` || endDate == `` {
			br.Msg = "开始日期或结束日期不能为空"
			br.ErrMsg = "开始日期或结束日期不能为空，Err:" + err.Error()
			return
		}
		adminDataList, _, err = roadshowService.GetOverseasTimeIntervalData(startDate, endDate, "researcher", "company_status")
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}

	}

	researcherList, err := roadshow.GetOverseaCustomCalendarResearcherList()
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取分组信息失败！,GetResearcherV2 Err:" + err.Error()
		return
	}

	adminReportRecordList := make([]roadshow.OverseasAdminReportRecord, 0)
	rsAllReportRecordNumList := make([]roadshow.RsOverseasReportRecordNum, 0)
	RsReportRecordResp := roadshow.RsOverseasReportRecordResp{
		List:                  adminReportRecordList,
		RsReportRecordNumList: rsAllReportRecordNumList,
	}

	startDateIndexList := make(map[int]string) //开始时间间隔map
	endDateIndexList := make(map[int]string)   //结束时间间隔map

	tmpAllTryOutNumMap := make(map[int]int)
	tmpAllFormalNumMap := make(map[int]int)
	tmpAllMeetingNumMap := make(map[int]int)

	for _, researcher := range researcherList {
		//组内研究员数据

		//每个区间的数据
		tmpAdminRsReportRecordNumList := make([]roadshow.RsOverseasReportRecordNum, 0)
		for index, adminData := range adminDataList {
			startDateIndexList[index] = adminData.StartDate
			endDateIndexList[index] = adminData.EndDate

			var tmpTryOutNum, tmpFormalNum, tmpCloseNum int
			if num, ok := adminData.TryOutMap[researcher.AdminId]; ok {
				tmpTryOutNum = num
			}
			if num, ok := adminData.FormalMap[researcher.AdminId]; ok {
				tmpFormalNum = num
			}
			if num, ok := adminData.CloseMap[researcher.AdminId]; ok {
				tmpCloseNum = num
			}
			tmpAdminRsReportRecordNum := roadshow.RsOverseasReportRecordNum{
				TryOutNum: tmpTryOutNum,
				FormalNum: tmpFormalNum,
				CloseNum:  tmpCloseNum,
				StartDate: adminData.StartDate,
				EndDate:   adminData.EndDate,
			}
			tmpAdminRsReportRecordNumList = append(tmpAdminRsReportRecordNumList, tmpAdminRsReportRecordNum)

			//总数据汇总
			if _, ok := tmpAllTryOutNumMap[index]; !ok {
				tmpAllTryOutNumMap[index] = 0
			}
			if _, ok := tmpAllFormalNumMap[index]; !ok {
				tmpAllFormalNumMap[index] = 0
			}
			if _, ok := tmpAllMeetingNumMap[index]; !ok {
				tmpAllMeetingNumMap[index] = 0
			}
			tmpAllTryOutNumMap[index] += tmpTryOutNum
			tmpAllFormalNumMap[index] += tmpFormalNum
			tmpAllMeetingNumMap[index] += tmpCloseNum
		}
		// 数据都为0的时候不显示
		adminTotalNum := 0
		for _, v := range tmpAdminRsReportRecordNumList {
			adminTotalNum += v.TryOutNum + v.CloseNum + v.FormalNum
		}
		if adminTotalNum > 0 {
			tmpAdminReportRecord := roadshow.OverseasAdminReportRecord{
				Name:                  researcher.RealName,
				AdminId:               researcher.AdminId,
				RsReportRecordNumList: tmpAdminRsReportRecordNumList,
			}
			RsReportRecordResp.List = append(RsReportRecordResp.List, tmpAdminReportRecord)
		}
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllFormalNumMap); i++ {
		tmpGroupRsReportRecordNum := roadshow.RsOverseasReportRecordNum{
			TryOutNum: tmpAllTryOutNumMap[i],
			FormalNum: tmpAllFormalNumMap[i],
			CloseNum:  tmpAllMeetingNumMap[i],
			StartDate: startDateIndexList[i],
			EndDate:   endDateIndexList[i],
		}
		rsAllReportRecordNumList = append(rsAllReportRecordNumList, tmpGroupRsReportRecordNum)
	}
	RsReportRecordResp.RsReportRecordNumList = rsAllReportRecordNumList

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = RsReportRecordResp
	return
}

// OverseasCalendarList
// @Title 海外客户路演详情列表
// @Description 海外客户路演详情列表接口
// @Param   AdminType   query   string  true       "枚举值：researcher、seller、 special：专项路演"
// @Param   DataType   query   string  true       "枚举值：try_out、formal、close、online、offline"
// @Param   AdminIds   query   string  true       "用户ids"
// @Param   StartDate   query   string  true       "开始日期,格式：2022-04-06"
// @Param   EndDate   query   string  true       "结束日期,格式：2022-04-06"
// @Success 200 {object} []roadshow.RsReportRecordList
// @router /overseas/calendar/list [get]
func (this *CalendarController) OverseasCalendarList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	adminType := this.GetString("AdminType")
	dataType := this.GetString("DataType")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")
	adminIds := this.GetString("AdminIds")
	if adminIds == "" {
		br.Msg = "请传入对应的人员"
		return
	}

	//adminIdsArr := make([]int, 0)
	//adminIdSlice := strings.Split(adminIds, ",")
	//for _, v := range adminIdSlice {
	//	id,_ := strconv.Atoi(v)
	//	adminIdsArr = append(adminIdsArr, id)
	//}

	//正式客户
	//var condition string
	//var pars []interface{}
	//condition = ` and a.start_date >= ? and a.end_date <= ? `
	//pars = append(pars, startDate, endDate)

	var sellerIds, researcherIds string
	//选择的人员类型（发起人还是被发起人）
	if adminType == "seller" {
		//condition += ` and a.seller_id IN (` + utils.GetOrmInReplace(len(adminIdsArr)) + `)`
		sellerIds = adminIds
	} else {
		//condition += ` and a.researcher_id IN (` + utils.GetOrmInReplace(len(adminIdsArr)) + `)`
		researcherIds = adminIds
	}
	//pars = append(pars, adminIdsArr)

	var status string
	//获取列表
	switch dataType {
	case "try_out":
		//condition += ` and a.company_status = ? )`
		//pars = append(pars, "试用")
		status = "试用"
	case "formal":
		//condition += ` and a.company_status = ? )`
		//pars = append(pars, "正式")
		status = "正式"
	case "close":
		//condition += ` and a.company_status = ? )`
		//pars = append(pars, "关闭")
		status = "关闭"
	//case "online":
	//	condition += ` and a.roadshow_type = ? `
	//	pars = append(pars, "线上")
	//case "offline":
	//	condition += ` and a.roadshow_type = ? `
	//	pars = append(pars, "线下")
	default:
		br.Msg = "请传入类型"
		br.ErrMsg = "请传入类型DataType"
		return
	}

	list, err := roadshow.GetOverseaCustomCalendarList2(sellerIds, researcherIds, startDate, endDate, status)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,ERR:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
	return
}
