package controllers

import (
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/roadshow"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/services/statistic_report"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
	"time"
)

// TodoCompanyReport
// @Title 销售Todo任务统计统计数据
// @Description 获取增销售Todo任务统计统计数据接口
// @Param   ProductId   query   string  true       "销售类型，ficc销售：1；权益销售：2"
// @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} statistic_report.companyTodoReportRecordResp
// @router /report/seller_company_todo [get]
func (this *StatisticReportController) TodoCompanyReport() {
	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
	}

	productId, _ := this.GetInt("ProductId")
	dataType := this.GetString("DataType")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")

	//数据处理
	groupTodoReportRecordList := make([]statistic_report.GroupTodoReportRecord, 0)
	allCompanyTodoReportRecordNum := make([]statistic_report.CompanyTodoReportRecordNum, 0)

	//返回参
	companyTodoReportRecordResp := statistic_report.CompanyTodoReportRecordResp{
		List:                           groupTodoReportRecordList,
		CompanyTodoReportRecordNumList: allCompanyTodoReportRecordNum,
	}
	groupId := -1
	dataNum := 6 //目前设置管理员可见页面为6周，销售主管和销售组长可见页面为七周
	switch sysUser.RoleTypeCode {
	case utils.ROLE_TYPE_CODE_ADMIN:
	case utils.ROLE_TYPE_CODE_FICC_ADMIN: //ficc管理员
		productId = 1
	case utils.ROLE_TYPE_CODE_RAI_ADMIN: //权益管理员
		productId = 2
	case utils.ROLE_TYPE_CODE_FICC_TEAM, utils.ROLE_TYPE_CODE_FICC_SELLER, utils.ROLE_TYPE_CODE_FICC_GROUP:
		productId = 1
		groupId = sysUser.GroupId
		//dataNum = 7
	case utils.ROLE_TYPE_CODE_RAI_SELLER, utils.ROLE_TYPE_CODE_RAI_GROUP:
		productId = 2
		groupId = sysUser.GroupId
		//dataNum = 7
	default:
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	if productId <= 0 {
		br.Msg = "请选择销售类型"
		return
	}
	if productId != 1 && productId != 2 {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}

	nowWeekMonday := utils.GetNowWeekMonday()    //本周周一
	firstDate := nowWeekMonday.AddDate(0, 0, -7) //上周周一

	var group []services.AdminGroup
	groupMap := make(map[int][]*roadshow.Researcher)
	var sellerIds string

	group, groupMap, sellerIds, brMsg, brErrMsg := getTodoCompanySallers(productId, groupId, sysUser, firstDate)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	if sellerIds == "" {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	sellerIds = strings.Trim(sellerIds, ",")
	sellerIds = "(" + sellerIds + ")"

	var companyReportDataMapList []statistic_report.CompanyTodoReportDataMap
	var err error
	//获取列表
	switch dataType {
	case "week":
		companyReportDataMapList, err = statistic_report.GetCompanyTodoWeekDataByWeekNumV2(productId, nowWeekMonday, sellerIds, dataNum)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "month":
		nowMonthFirstDay := utils.GetNowMonthFirstDay() //本月第一天
		companyReportDataMapList, err = statistic_report.GetCompanyTodoMonthDataByNum(productId, nowMonthFirstDay, sellerIds, dataNum)
		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
		}
		companyReportDataMapList, err = statistic_report.GetCompanyTodoTimeIntervalDataByNum(productId, startDate, endDate, sellerIds)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	default:
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	// 统计截止日期小于等于当前时间的未完成的任务数
	doingExpiredDataMapList, doingExpiredIdDataMapList, err := statistic_report.GetDoingTodoExpiredNum(productId, sellerIds)
	if err != nil {
		br.Msg = "数据异常"
		br.ErrMsg = "数据异常，Err:" + err.Error()
		return
	}

	// 统计截止日期小于等于当前时间的已完成的任务数
	finishDataMapList, finishIdDataMapList, err := statistic_report.GetFinishTodoNum(productId, sellerIds)
	if err != nil {
		br.Msg = "数据异常"
		br.ErrMsg = "数据异常，Err:" + err.Error()
		return
	}

	// 进行中且未到截止日期的To Do任务统计的任务数
	doingDataMapList, doingIdDataMapList, err := statistic_report.GetDoingTodoNum(productId, sellerIds)
	if err != nil {
		br.Msg = "数据异常"
		br.ErrMsg = "数据异常，Err:" + err.Error()
		return
	}

	adminCompanyIdsMap, err := statistic_report.GetTryStatusCompanyBySellerIds(productId, sellerIds)
	startDateIndexList := make(map[int]string) //开始时间间隔map
	endDateIndexList := make(map[int]string)   //结束时间间隔map

	//客户数汇总
	tmpAllAddMap := make(map[int]int)
	tmpAllApproveMap := make(map[int]int)

	//id集合汇总
	tmpAllDoingExpired := 0 //任务未完成数量（基于分组id）
	tmpAllFinish := 0       //任务已完成数量（基于分组id）
	tmpAllDoing := 0        //任务进行中数量（基于分组id）
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminTodoReportRecord := make([]statistic_report.AdminTodoReportRecord, 0)
		tmpGroupCompanyTodoReportRecordNumList := make([]statistic_report.CompanyTodoReportRecordNum, 0)

		//数量统计
		tmpGroupAddMap := make(map[int]int)
		tmpGroupApproveMap := make(map[int]int)

		//数量统计（基于分组id）
		tmpGroupDoingExpiredMap := make(map[int]int)
		tmpGroupFinishMap := make(map[int]int)
		tmpGroupDoingMap := make(map[int]int)

		//id集合（不区分客户状态）
		tmpGroupAddIdSliceMap := make(map[int][]string)
		tmpGroupApproveIdSliceMap := make(map[int][]string)

		//id集合 string类型（不区分客户状态）
		tmpGroupAddIdMap := make(map[int]string)
		tmpGroupApproveIdMap := make(map[int]string)

		//id集合（基于分组id）
		tmpGroupDoingExpiredIdMap := make(map[int]string)
		tmpGroupFinishIdMap := make(map[int]string)
		tmpGroupDoingIdMap := make(map[int]string)

		for _, researcher := range groupMap[v.GroupId] {

			//每个区间的数据
			tmpCompanyTodoReportRecordNumList := make([]statistic_report.CompanyTodoReportRecordNum, 0)
			// 未完成任务统计
			tmpDoingExpiredNum := 0
			tmpDoingExpiredCompanyIds := ""
			{

				if num, ok := doingExpiredDataMapList[researcher.AdminId]; ok {
					tmpDoingExpiredNum = num
					if tmpCompanyIds, ok2 := doingExpiredIdDataMapList[researcher.AdminId]; ok2 {
						tmpDoingExpiredCompanyIds = tmpCompanyIds
					}
				}
				tmpGroupDoingExpiredMap[v.GroupId] += tmpDoingExpiredNum
				tmpAllDoingExpired += tmpDoingExpiredNum

				if tmpDoingExpiredCompanyIds != "" {
					if tmpGroupDoingExpiredIdMap[v.GroupId] != "" {
						tmpGroupDoingExpiredIdMap[v.GroupId] += "," + tmpDoingExpiredCompanyIds
					} else {
						tmpGroupDoingExpiredIdMap[v.GroupId] += tmpDoingExpiredCompanyIds
					}
				}
			}

			// 已完成任务统计
			tmpFinishNum := 0
			tmpFinishCompanyIds := ""
			{
				if finishNum, ok := finishDataMapList[researcher.AdminId]; ok {
					tmpFinishNum = finishNum
					if tmpCompanyIds, ok2 := finishIdDataMapList[researcher.AdminId]; ok2 {
						tmpFinishCompanyIds = tmpCompanyIds
					}
				}
				tmpGroupFinishMap[v.GroupId] += tmpFinishNum
				tmpAllFinish += tmpFinishNum

				if tmpFinishCompanyIds != "" {
					if tmpGroupFinishIdMap[v.GroupId] != "" {
						tmpGroupFinishIdMap[v.GroupId] += "," + tmpFinishCompanyIds
					} else {
						tmpGroupFinishIdMap[v.GroupId] += tmpFinishCompanyIds
					}
				}
			}

			// 进行中任务统计
			tmpDoingNum := 0
			tmpDoingCompanyIds := ""
			{
				if doingNum, ok := doingDataMapList[researcher.AdminId]; ok {
					tmpDoingNum = doingNum
					if tmpCompanyIds, ok2 := doingIdDataMapList[researcher.AdminId]; ok2 {
						tmpDoingCompanyIds = tmpCompanyIds
					}
				}
				tmpGroupDoingMap[v.GroupId] += tmpDoingNum
				tmpAllDoing += tmpDoingNum

				if tmpDoingCompanyIds != "" {
					if tmpGroupDoingIdMap[v.GroupId] != "" {
						tmpGroupDoingIdMap[v.GroupId] += "," + tmpDoingCompanyIds
					} else {
						tmpGroupDoingIdMap[v.GroupId] += tmpDoingCompanyIds
					}
				}
			}

			// 销售相关的试用客户ID
			tmpAdminCompanyIds := ""
			if ids, ok := adminCompanyIdsMap[researcher.AdminId]; ok {
				tmpAdminCompanyIds = ids
			}

			for index, adminData := range companyReportDataMapList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpAddNum, tmpApproveNum int
				var tmpAddIdSlice, tmpApproveIdSlice []string
				var tmpAddId, tmpApproveId string

				tmpStat := adminData.Stat

				//试用数据
				if num, ok := tmpStat.AddMap[researcher.AdminId]; ok {
					tmpAddNum = num
					tmpAddIdSlice = append(tmpAddIdSlice, tmpStat.AddIdMap[researcher.AdminId])
				}
				if num, ok := tmpStat.ApproveMap[researcher.AdminId]; ok {
					tmpApproveNum = num
					tmpApproveIdSlice = append(tmpApproveIdSlice, tmpStat.ApproveIdMap[researcher.AdminId])
				}
				tmpAddId = strings.Join(tmpAddIdSlice, ",")
				tmpApproveId = strings.Join(tmpApproveIdSlice, ",")

				tmpAdminRsReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
					AddMap:       tmpAddNum,
					AddIdMap:     tmpAddId,
					ApproveMap:   tmpApproveNum,
					ApproveIdMap: tmpApproveId,

					StartDate: adminData.StartDate,
					EndDate:   adminData.EndDate,
				}
				tmpCompanyTodoReportRecordNumList = append(tmpCompanyTodoReportRecordNumList, tmpAdminRsReportRecordNum)
				//组内数据汇总
				if _, ok := tmpGroupAddMap[index]; !ok {
					tmpGroupAddMap[index] = 0
				}
				if _, ok := tmpGroupApproveMap[index]; !ok {
					tmpGroupApproveMap[index] = 0
				}
				tmpGroupAddMap[index] += tmpAddNum
				tmpGroupApproveMap[index] += tmpApproveNum

				//组内数据汇总
				if tmpAddId != "" {
					tmpGroupAddIdSliceMap[index] = append(tmpGroupAddIdSliceMap[index], tmpAddId)
				}
				if tmpApproveId != "" {
					tmpGroupApproveIdSliceMap[index] = append(tmpGroupApproveIdSliceMap[index], tmpApproveId)
				}

				//总数据汇总
				if _, ok := tmpAllAddMap[index]; !ok {
					tmpAllAddMap[index] = 0
				}
				if _, ok := tmpAllApproveMap[index]; !ok {
					tmpAllApproveMap[index] = 0
				}
				tmpAllAddMap[index] += tmpAddNum
				tmpAllApproveMap[index] += tmpApproveNum
			}

			tmpAdminTodoReportRecord := statistic_report.AdminTodoReportRecord{
				Name:                           researcher.RealName,
				AdminId:                        researcher.AdminId,
				CompanyTodoReportRecordNumList: tmpCompanyTodoReportRecordNumList,
				DoingExpiredNum:                tmpDoingExpiredNum,
				DoingExpiredCompanyIds:         tmpDoingExpiredCompanyIds,
				FinishNum:                      tmpFinishNum,
				FinishCompanyIds:               tmpFinishCompanyIds,
				DoingNum:                       tmpDoingNum,
				DoingCompanyIds:                tmpDoingCompanyIds,
				AdminCompanyIds:                tmpAdminCompanyIds,
			}
			tmpGroupAdminTodoReportRecord = append(tmpGroupAdminTodoReportRecord, tmpAdminTodoReportRecord)
		}

		for i := 0; i < len(tmpGroupAddMap); i++ {

			//id集合（不区分客户状态）
			tmpGroupAddIdMap[i] = strings.Join(tmpGroupAddIdSliceMap[i], ",")
			tmpGroupApproveIdMap[i] = strings.Join(tmpGroupApproveIdSliceMap[i], ",")

			//tmpAddIdMap := append(AddIdMap)
			tmpGroupCompanyTodoReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
				AddMap:       tmpGroupAddMap[i],
				AddIdMap:     tmpGroupAddIdMap[i],
				ApproveMap:   tmpGroupApproveMap[i],
				ApproveIdMap: tmpGroupApproveIdMap[i],

				StartDate: startDateIndexList[i],
				EndDate:   endDateIndexList[i],
			}
			tmpGroupCompanyTodoReportRecordNumList = append(tmpGroupCompanyTodoReportRecordNumList, tmpGroupCompanyTodoReportRecordNum)
		}
		groupTodoReportRecord := statistic_report.GroupTodoReportRecord{
			Item:                           tmpGroupAdminTodoReportRecord,
			Name:                           v.GroupName,
			GroupId:                        v.GroupId,
			CompanyTodoReportRecordNumList: tmpGroupCompanyTodoReportRecordNumList,
			DoingExpiredGroupNum:           tmpGroupDoingExpiredMap[v.GroupId],
			DoingExpiredGroupCompanyIds:    tmpGroupDoingExpiredIdMap[v.GroupId],
			FinishNum:                      tmpGroupFinishMap[v.GroupId],
			FinishCompanyIds:               tmpGroupFinishIdMap[v.GroupId],
			DoingNum:                       tmpGroupDoingMap[v.GroupId],
			DoingCompanyIds:                tmpGroupDoingIdMap[v.GroupId],
		}
		groupTodoReportRecordList = append(groupTodoReportRecordList, groupTodoReportRecord)
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllApproveMap); i++ {
		tmpGroupCompanyTodoReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
			AddMap: tmpAllAddMap[i],
			//AddIdMap:     tmpAllTryOutAddIdMap[i],
			ApproveMap: tmpAllApproveMap[i],
			//ApproveIdMap: tmpAllTryOutApproveIdMap[i],

			StartDate: startDateIndexList[i],
			EndDate:   endDateIndexList[i],
		}
		allCompanyTodoReportRecordNum = append(allCompanyTodoReportRecordNum, tmpGroupCompanyTodoReportRecordNum)
	}
	companyTodoReportRecordResp.CompanyTodoReportRecordNumList = allCompanyTodoReportRecordNum

	companyTodoReportRecordResp.List = groupTodoReportRecordList
	companyTodoReportRecordResp.DoingExpiredAllNum = tmpAllDoingExpired
	companyTodoReportRecordResp.FinishAllNum = tmpAllFinish
	companyTodoReportRecordResp.DoingAllNum = tmpAllDoing
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = companyTodoReportRecordResp
}
func (this *StatisticReportController) TodoCompanyReportBak20220610174656() {
	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
	}

	productId, _ := this.GetInt("ProductId")
	dataType := this.GetString("DataType")
	//startDate := this.GetString("StartDate")
	//endDate := this.GetString("EndDate")

	//数据处理
	groupTodoReportRecordList := make([]statistic_report.GroupTodoReportRecord, 0)
	allCompanyTodoReportRecordNum := make([]statistic_report.CompanyTodoReportRecordNum, 0)

	//返回参
	companyTodoReportRecordResp := statistic_report.CompanyTodoReportRecordResp{
		List:                           groupTodoReportRecordList,
		CompanyTodoReportRecordNumList: allCompanyTodoReportRecordNum,
	}
	groupId := -1
	weekNum := 6 //目前设置管理员可见页面为6周，销售主管和销售组长可见页面为七周
	switch sysUser.RoleTypeCode {
	case utils.ROLE_TYPE_CODE_ADMIN:
	case utils.ROLE_TYPE_CODE_FICC_ADMIN: //ficc管理员
		productId = 1
	case utils.ROLE_TYPE_CODE_RAI_ADMIN: //权益管理员
		productId = 2
	case utils.ROLE_TYPE_CODE_FICC_TEAM, utils.ROLE_TYPE_CODE_FICC_SELLER, utils.ROLE_TYPE_CODE_FICC_GROUP:
		productId = 1
		groupId = sysUser.GroupId
		weekNum = 7
	case utils.ROLE_TYPE_CODE_RAI_SELLER, utils.ROLE_TYPE_CODE_RAI_GROUP:
		productId = 2
		groupId = sysUser.GroupId
		weekNum = 7
	default:
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	if productId <= 0 {
		br.Msg = "请选择销售类型"
		return
	}
	if productId != 1 && productId != 2 {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}

	nowWeekMonday := utils.GetNowWeekMonday()    //本周周一
	firstDate := nowWeekMonday.AddDate(0, 0, -7) //上周周一

	var group []services.AdminGroup
	groupMap := make(map[int][]*roadshow.Researcher)
	var sellerIds string

	group, groupMap, sellerIds, brMsg, brErrMsg := getTodoCompanySallers(productId, groupId, sysUser, firstDate)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	if sellerIds == "" {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	sellerIds = strings.Trim(sellerIds, ",")
	sellerIds = "(" + sellerIds + ")"

	var companyReportDataMapList []statistic_report.CompanyTodoReportDataMap
	var err error
	//获取列表
	switch dataType {
	case "week":
		companyReportDataMapList, err = statistic_report.GetCompanyTodoWeekDataByWeekNum(productId, nowWeekMonday, sellerIds, weekNum)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	default:
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	// 统计截止日期小于等于当前时间的未完成的任务数
	doingExpiredDataMapList, doingExpiredIdDataMapList, err := statistic_report.GetDoingTodoExpiredNum(productId, sellerIds)
	if err != nil {
		br.Msg = "数据异常"
		br.ErrMsg = "数据异常，Err:" + err.Error()
		return
	}

	// 统计截止日期小于等于当前时间的已完成的任务数
	finishDataMapList, finishIdDataMapList, err := statistic_report.GetFinishTodoNum(productId, sellerIds)
	if err != nil {
		br.Msg = "数据异常"
		br.ErrMsg = "数据异常，Err:" + err.Error()
		return
	}

	adminCompanyIdsMap, err := statistic_report.GetTryStatusCompanyBySellerIds(productId, sellerIds)
	startDateIndexList := make(map[int]string) //开始时间间隔map
	endDateIndexList := make(map[int]string)   //结束时间间隔map

	//客户数汇总
	tmpAllTryOutAddMap := make(map[int]int)
	tmpAllTryOutApproveMap := make(map[int]int)
	tmpAllFormalAddMap := make(map[int]int)
	tmpAllFormalApproveMap := make(map[int]int)

	//id集合汇总
	tmpAllTryOutAddIdMap := make(map[int]string)
	tmpAllTryOutApproveIdMap := make(map[int]string)
	tmpAllFormalAddIdMap := make(map[int]string)
	tmpAllFormalApproveIdMap := make(map[int]string)
	tmpAllDoingExpired := 0 //任务未完成数量（基于分组id）
	tmpAllFinish := 0       //任务已完成数量（基于分组id）
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminTodoReportRecord := make([]statistic_report.AdminTodoReportRecord, 0)
		tmpGroupCompanyTodoReportRecordNumList := make([]statistic_report.CompanyTodoReportRecordNum, 0)

		//数量统计
		tmpGroupTryOutAddMap := make(map[int]int)
		tmpGroupTryOutApproveMap := make(map[int]int)
		tmpGroupFormalAddMap := make(map[int]int)
		tmpGroupFormalApproveMap := make(map[int]int)

		//数量统计（基于分组id）
		tmpGroupDoingExpiredMap := make(map[int]int)
		tmpGroupFinishMap := make(map[int]int)

		//id集合
		tmpGroupTryOutAddIdSliceMap := make(map[int][]string)
		tmpGroupTryOutApproveIdSliceMap := make(map[int][]string)
		tmpGroupFormalAddIdSliceMap := make(map[int][]string)
		tmpGroupFormalApproveIdSliceMap := make(map[int][]string)
		//id集合（不区分客户状态）
		tmpGroupAddIdSliceMap := make(map[int][]string)
		tmpGroupApproveIdSliceMap := make(map[int][]string)

		//id集合 string类型
		tmpGroupTryOutAddIdMap := make(map[int]string)
		tmpGroupTryOutApproveIdMap := make(map[int]string)
		tmpGroupFormalAddIdMap := make(map[int]string)
		tmpGroupFormalApproveIdMap := make(map[int]string)

		//id集合 string类型（不区分客户状态）
		tmpGroupAddIdMap := make(map[int]string)
		tmpGroupApproveIdMap := make(map[int]string)

		//id集合（基于分组id）
		tmpGroupDoingExpiredIdMap := make(map[int]string)
		tmpGroupFinishIdMap := make(map[int]string)

		for _, researcher := range groupMap[v.GroupId] {

			//每个区间的数据
			tmpCompanyTodoReportRecordNumList := make([]statistic_report.CompanyTodoReportRecordNum, 0)
			tmpDoingExpiredNum := 0
			tmpDoingExpiredCompanyIds := ""
			tmpAdminCompanyIds := ""
			// 未完成任务统计
			if num, ok := doingExpiredDataMapList[researcher.AdminId]; ok {
				tmpDoingExpiredNum = num
				if tmpCompanyIds, ok2 := doingExpiredIdDataMapList[researcher.AdminId]; ok2 {
					tmpDoingExpiredCompanyIds = tmpCompanyIds
				}
			}
			tmpGroupDoingExpiredMap[v.GroupId] += tmpDoingExpiredNum
			tmpAllDoingExpired += tmpDoingExpiredNum

			if tmpDoingExpiredCompanyIds != "" {
				if tmpGroupDoingExpiredIdMap[v.GroupId] != "" {
					tmpGroupDoingExpiredIdMap[v.GroupId] += "," + tmpDoingExpiredCompanyIds
				} else {
					tmpGroupDoingExpiredIdMap[v.GroupId] += tmpDoingExpiredCompanyIds
				}
			}

			// 已完成任务统计
			tmpFinishNum := 0
			tmpFinishCompanyIds := ""
			if finishNum, ok := finishDataMapList[researcher.AdminId]; ok {
				tmpFinishNum = finishNum
				if tmpCompanyIds, ok2 := finishIdDataMapList[researcher.AdminId]; ok2 {
					tmpFinishCompanyIds = tmpCompanyIds
				}
			}
			tmpGroupFinishMap[v.GroupId] += tmpFinishNum
			tmpAllFinish += tmpFinishNum

			if tmpFinishCompanyIds != "" {
				if tmpGroupFinishIdMap[v.GroupId] != "" {
					tmpGroupFinishIdMap[v.GroupId] += "," + tmpFinishCompanyIds
				} else {
					tmpGroupFinishIdMap[v.GroupId] += tmpFinishCompanyIds
				}
			}

			// 销售相关的试用客户ID
			if ids, ok := adminCompanyIdsMap[researcher.AdminId]; ok {
				tmpAdminCompanyIds = ids
			}

			for index, adminData := range companyReportDataMapList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpTryOutAddNum, tmpTryOutApproveNum, tmpFormalAddNum, tmpFormalApproveNum int
				//var tmpTryOutAddIds, tmpTryOutApproveIds, tmpFormalAddIds string
				var tmpTryOutAddIdSlice, tmpTryOutApproveIdSlice, tmpFormalAddIdSlice, tmpFormalApproveIdSlice, tmpAddIdSlice, tmpApproveIdSlice []string
				var tmpTryOutAddId, tmpTryOutApproveId, tmpFormalAddId, tmpFormalApproveId, tmpAddId, tmpApproveId string

				tmpTryOutData := adminData.TryOut
				tmpFormalData := adminData.Formal

				//试用数据
				if num, ok := tmpTryOutData.AddMap[researcher.AdminId]; ok {
					tmpTryOutAddNum = num
					tmpTryOutAddIdSlice = append(tmpTryOutAddIdSlice, tmpTryOutData.AddIdMap[researcher.AdminId])
					tmpAddIdSlice = append(tmpAddIdSlice, tmpTryOutData.AddIdMap[researcher.AdminId])
				}
				if num, ok := tmpTryOutData.ApproveMap[researcher.AdminId]; ok {
					tmpTryOutApproveNum = num
					tmpTryOutApproveIdSlice = append(tmpTryOutApproveIdSlice, tmpTryOutData.ApproveIdMap[researcher.AdminId])
					tmpApproveIdSlice = append(tmpApproveIdSlice, tmpTryOutData.ApproveIdMap[researcher.AdminId])
				}
				if num, ok := tmpFormalData.AddMap[researcher.AdminId]; ok {
					tmpFormalAddNum = num
					tmpFormalAddIdSlice = append(tmpFormalAddIdSlice, tmpFormalData.AddIdMap[researcher.AdminId])
					tmpAddIdSlice = append(tmpAddIdSlice, tmpFormalData.AddIdMap[researcher.AdminId])
				}
				if num, ok := tmpFormalData.ApproveMap[researcher.AdminId]; ok {
					tmpFormalApproveNum = num
					tmpFormalApproveIdSlice = append(tmpFormalApproveIdSlice, tmpFormalData.ApproveIdMap[researcher.AdminId])
					tmpApproveIdSlice = append(tmpApproveIdSlice, tmpFormalData.ApproveIdMap[researcher.AdminId])
				}
				tmpTryOutAddId = strings.Join(tmpTryOutAddIdSlice, ",")
				tmpTryOutApproveId = strings.Join(tmpTryOutApproveIdSlice, ",")
				tmpFormalAddId = strings.Join(tmpFormalAddIdSlice, ",")
				tmpAddId = strings.Join(tmpAddIdSlice, ",")
				tmpApproveId = strings.Join(tmpApproveIdSlice, ",")

				tmpAdminRsReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
					TryOutAddMap:       tmpTryOutAddNum,
					TryOutAddIdMap:     tmpTryOutAddId,
					TryOutApproveMap:   tmpTryOutApproveNum,
					TryOutApproveIdMap: tmpTryOutApproveId,
					FormalAddMap:       tmpFormalAddNum,
					FormalAddIdMap:     tmpFormalAddId,
					FormalApproveMap:   tmpFormalApproveNum,
					FormalApproveIdMap: tmpFormalApproveId,

					AddMap:       tmpTryOutAddNum + tmpFormalAddNum,
					AddIdMap:     tmpAddId,
					ApproveMap:   tmpTryOutApproveNum + tmpFormalApproveNum,
					ApproveIdMap: tmpApproveId,

					StartDate: adminData.StartDate,
					EndDate:   adminData.EndDate,
				}
				tmpCompanyTodoReportRecordNumList = append(tmpCompanyTodoReportRecordNumList, tmpAdminRsReportRecordNum)
				//组内数据汇总
				if _, ok := tmpGroupTryOutAddMap[index]; !ok {
					tmpGroupTryOutAddMap[index] = 0
				}
				if _, ok := tmpGroupTryOutApproveMap[index]; !ok {
					tmpGroupTryOutApproveMap[index] = 0
				}
				if _, ok := tmpGroupFormalAddMap[index]; !ok {
					tmpGroupFormalAddMap[index] = 0
				}
				if _, ok := tmpGroupFormalApproveMap[index]; !ok {
					tmpGroupFormalApproveMap[index] = 0
				}
				tmpGroupTryOutAddMap[index] += tmpTryOutAddNum
				tmpGroupTryOutApproveMap[index] += tmpTryOutApproveNum
				tmpGroupFormalAddMap[index] += tmpFormalAddNum
				tmpGroupFormalApproveMap[index] += tmpFormalApproveNum

				//组内数据汇总
				if _, ok := tmpGroupTryOutAddIdMap[index]; !ok {
					tmpGroupTryOutAddIdMap[index] = ""
				}
				if _, ok := tmpGroupTryOutApproveIdMap[index]; !ok {
					tmpGroupTryOutApproveIdMap[index] = ""
				}
				if _, ok := tmpGroupFormalAddIdMap[index]; !ok {
					tmpGroupFormalAddIdMap[index] = ""
				}
				if tmpTryOutAddId != "" {
					tmpGroupTryOutAddIdSliceMap[index] = append(tmpGroupTryOutAddIdSliceMap[index], tmpTryOutAddId)
					tmpGroupAddIdSliceMap[index] = append(tmpGroupAddIdSliceMap[index], tmpTryOutAddId)
				}
				if tmpTryOutApproveId != "" {
					tmpGroupTryOutApproveIdSliceMap[index] = append(tmpGroupTryOutApproveIdSliceMap[index], tmpTryOutApproveId)
					tmpGroupApproveIdSliceMap[index] = append(tmpGroupApproveIdSliceMap[index], tmpTryOutApproveId)

				}
				if tmpFormalAddId != "" {
					tmpGroupFormalAddIdSliceMap[index] = append(tmpGroupFormalAddIdSliceMap[index], tmpFormalAddId)
					tmpGroupAddIdSliceMap[index] = append(tmpGroupAddIdSliceMap[index], tmpFormalAddId)
				}
				if tmpFormalApproveId != "" {
					tmpGroupFormalApproveIdSliceMap[index] = append(tmpGroupFormalApproveIdSliceMap[index], tmpFormalApproveId)
					tmpGroupApproveIdSliceMap[index] = append(tmpGroupApproveIdSliceMap[index], tmpFormalApproveId)
				}

				//总数据汇总
				if _, ok := tmpAllTryOutAddMap[index]; !ok {
					tmpAllTryOutAddMap[index] = 0
				}
				if _, ok := tmpAllTryOutApproveMap[index]; !ok {
					tmpAllTryOutApproveMap[index] = 0
				}
				if _, ok := tmpAllFormalAddMap[index]; !ok {
					tmpAllFormalAddMap[index] = 0
				}
				if _, ok := tmpAllFormalApproveMap[index]; !ok {
					tmpAllFormalApproveMap[index] = 0
				}
				tmpAllTryOutAddMap[index] += tmpTryOutAddNum
				tmpAllTryOutApproveMap[index] += tmpTryOutApproveNum
				tmpAllFormalAddMap[index] += tmpFormalAddNum
				tmpAllFormalApproveMap[index] += tmpFormalApproveNum
			}

			tmpAdminTodoReportRecord := statistic_report.AdminTodoReportRecord{
				Name:                           researcher.RealName,
				AdminId:                        researcher.AdminId,
				CompanyTodoReportRecordNumList: tmpCompanyTodoReportRecordNumList,
				DoingExpiredNum:                tmpDoingExpiredNum,
				DoingExpiredCompanyIds:         tmpDoingExpiredCompanyIds,
				FinishNum:                      tmpFinishNum,
				FinishCompanyIds:               tmpFinishCompanyIds,
				AdminCompanyIds:                tmpAdminCompanyIds,
			}
			tmpGroupAdminTodoReportRecord = append(tmpGroupAdminTodoReportRecord, tmpAdminTodoReportRecord)
		}

		for i := 0; i < len(tmpGroupFormalAddMap); i++ {
			tmpGroupTryOutAddIdMap[i] = strings.Join(tmpGroupTryOutAddIdSliceMap[i], ",")
			tmpGroupTryOutApproveIdMap[i] = strings.Join(tmpGroupTryOutApproveIdSliceMap[i], ",")
			tmpGroupFormalAddIdMap[i] = strings.Join(tmpGroupFormalAddIdSliceMap[i], ",")
			tmpGroupFormalApproveIdMap[i] = strings.Join(tmpGroupFormalApproveIdSliceMap[i], ",")

			//id集合（不区分客户状态）
			tmpGroupAddIdMap[i] = strings.Join(tmpGroupAddIdSliceMap[i], ",")
			tmpGroupApproveIdMap[i] = strings.Join(tmpGroupApproveIdSliceMap[i], ",")

			//tmpAddIdMap := append(AddIdMap)
			tmpGroupCompanyTodoReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
				TryOutAddMap:       tmpGroupTryOutAddMap[i],
				TryOutAddIdMap:     tmpGroupTryOutAddIdMap[i],
				TryOutApproveMap:   tmpGroupTryOutApproveMap[i],
				TryOutApproveIdMap: tmpGroupTryOutApproveIdMap[i],
				FormalAddMap:       tmpGroupFormalAddMap[i],
				FormalAddIdMap:     tmpGroupFormalAddIdMap[i],
				FormalApproveMap:   tmpGroupFormalApproveMap[i],
				FormalApproveIdMap: tmpGroupFormalApproveIdMap[i],

				AddMap:       tmpGroupTryOutAddMap[i] + tmpGroupFormalAddMap[i],
				AddIdMap:     tmpGroupAddIdMap[i],
				ApproveMap:   tmpGroupTryOutApproveMap[i] + tmpGroupFormalApproveMap[i],
				ApproveIdMap: tmpGroupApproveIdMap[i],

				StartDate: startDateIndexList[i],
				EndDate:   endDateIndexList[i],
			}
			tmpGroupCompanyTodoReportRecordNumList = append(tmpGroupCompanyTodoReportRecordNumList, tmpGroupCompanyTodoReportRecordNum)
		}
		groupTodoReportRecord := statistic_report.GroupTodoReportRecord{
			Item:                           tmpGroupAdminTodoReportRecord,
			Name:                           v.GroupName,
			GroupId:                        v.GroupId,
			CompanyTodoReportRecordNumList: tmpGroupCompanyTodoReportRecordNumList,
			DoingExpiredGroupNum:           tmpGroupDoingExpiredMap[v.GroupId],
			DoingExpiredGroupCompanyIds:    tmpGroupDoingExpiredIdMap[v.GroupId],
			FinishNum:                      tmpGroupFinishMap[v.GroupId],
			FinishCompanyIds:               tmpGroupFinishIdMap[v.GroupId],
		}
		groupTodoReportRecordList = append(groupTodoReportRecordList, groupTodoReportRecord)
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllTryOutApproveMap); i++ {
		tmpGroupCompanyTodoReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
			TryOutAddMap:       tmpAllTryOutAddMap[i],
			TryOutAddIdMap:     tmpAllTryOutAddIdMap[i],
			TryOutApproveMap:   tmpAllTryOutApproveMap[i],
			TryOutApproveIdMap: tmpAllTryOutApproveIdMap[i],
			FormalAddMap:       tmpAllFormalAddMap[i],
			FormalAddIdMap:     tmpAllFormalAddIdMap[i],
			FormalApproveMap:   tmpAllFormalApproveMap[i],
			FormalApproveIdMap: tmpAllFormalApproveIdMap[i],

			AddMap: tmpAllTryOutAddMap[i] + tmpAllFormalAddMap[i],
			//AddIdMap:     tmpAllTryOutAddIdMap[i],
			ApproveMap: tmpAllTryOutApproveMap[i] + tmpAllFormalApproveMap[i],
			//ApproveIdMap: tmpAllTryOutApproveIdMap[i],

			StartDate: startDateIndexList[i],
			EndDate:   endDateIndexList[i],
		}
		allCompanyTodoReportRecordNum = append(allCompanyTodoReportRecordNum, tmpGroupCompanyTodoReportRecordNum)
	}
	companyTodoReportRecordResp.CompanyTodoReportRecordNumList = allCompanyTodoReportRecordNum

	companyTodoReportRecordResp.List = groupTodoReportRecordList
	companyTodoReportRecordResp.DoingExpiredAllNum = tmpAllDoingExpired
	companyTodoReportRecordResp.FinishAllNum = tmpAllFinish
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = companyTodoReportRecordResp
}
func (this *StatisticReportController) TodoCompanyReportBak() {
	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
	}

	productId, _ := this.GetInt("ProductId")
	dataType := this.GetString("DataType")
	//startDate := this.GetString("StartDate")
	//endDate := this.GetString("EndDate")

	//数据处理
	groupTodoReportRecordList := make([]statistic_report.GroupTodoReportRecord, 0)
	allCompanyTodoReportRecordNum := make([]statistic_report.CompanyTodoReportRecordNum, 0)

	//返回参
	companyTodoReportRecordResp := statistic_report.CompanyTodoReportRecordResp{
		List:                           groupTodoReportRecordList,
		CompanyTodoReportRecordNumList: allCompanyTodoReportRecordNum,
	}
	groupId := -1
	switch sysUser.RoleTypeCode {
	case utils.ROLE_TYPE_CODE_ADMIN:
	case utils.ROLE_TYPE_CODE_FICC_ADMIN: //ficc管理员
		productId = 1
	case utils.ROLE_TYPE_CODE_RAI_ADMIN: //权益管理员
		productId = 2
	case utils.ROLE_TYPE_CODE_FICC_TEAM, utils.ROLE_TYPE_CODE_FICC_SELLER, utils.ROLE_TYPE_CODE_FICC_GROUP:
		productId = 1
		groupId = sysUser.GroupId
	case utils.ROLE_TYPE_CODE_RAI_SELLER, utils.ROLE_TYPE_CODE_RAI_GROUP:
		productId = 2
		groupId = sysUser.GroupId
	default:
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	if productId <= 0 {
		br.Msg = "请选择销售类型"
		return
	}
	if productId != 1 && productId != 2 {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}

	nowWeekMonday := utils.GetNowWeekMonday()    //本周周一
	firstDate := nowWeekMonday.AddDate(0, 0, -7) //上周周一

	var group []services.AdminGroup
	groupMap := make(map[int][]*roadshow.Researcher)
	var sellerIds string

	group, groupMap, sellerIds, brMsg, brErrMsg := getTodoCompanySallers(productId, groupId, sysUser, firstDate)
	if brMsg != "" {
		br.Msg = brMsg
		br.ErrMsg = brErrMsg
		return
	}
	if sellerIds == "" {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	sellerIds = strings.Trim(sellerIds, ",")
	sellerIds = "(" + sellerIds + ")"

	var companyReportDataMapList []statistic_report.CompanyTodoReportDataMap
	var err error
	//获取列表
	switch dataType {
	case "week":
		companyReportDataMapList, err = statistic_report.GetCompanyTodoWeekData(productId, nowWeekMonday, sellerIds)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	default:
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = companyTodoReportRecordResp
		return
	}
	// 统计截止日期小于等于当前时间的未完成的任务数
	doingExpiredDataMapList, doingExpiredIdDataMapList, err := statistic_report.GetDoingTodoExpiredNum(productId, sellerIds)
	if err != nil {
		br.Msg = "数据异常"
		br.ErrMsg = "数据异常，Err:" + err.Error()
		return
	}

	// 统计截止日期小于等于当前时间的未完成的任务数
	finishDataMapList, finishIdDataMapList, err := statistic_report.GetFinishTodoNum(productId, sellerIds)
	if err != nil {
		br.Msg = "数据异常"
		br.ErrMsg = "数据异常，Err:" + err.Error()
		return
	}

	adminCompanyIdsMap, err := statistic_report.GetTryStatusCompanyBySellerIds(productId, sellerIds)
	startDateIndexList := make(map[int]string) //开始时间间隔map
	endDateIndexList := make(map[int]string)   //结束时间间隔map

	//客户数汇总
	tmpAllTryOutAddMap := make(map[int]int)
	tmpAllTryOutApproveMap := make(map[int]int)
	tmpAllFormalAddMap := make(map[int]int)
	tmpAllFormalApproveMap := make(map[int]int)

	//id集合汇总
	tmpAllTryOutAddIdMap := make(map[int]string)
	tmpAllTryOutApproveIdMap := make(map[int]string)
	tmpAllFormalAddIdMap := make(map[int]string)
	tmpAllFormalApproveIdMap := make(map[int]string)
	tmpAllDoingExpired := 0
	tmpAllFinish := 0
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminTodoReportRecord := make([]statistic_report.AdminTodoReportRecord, 0)
		tmpGroupCompanyTodoReportRecordNumList := make([]statistic_report.CompanyTodoReportRecordNum, 0)

		//数量统计
		tmpGroupTryOutAddMap := make(map[int]int)
		tmpGroupTryOutApproveMap := make(map[int]int)
		tmpGroupFormalAddMap := make(map[int]int)
		tmpGroupFormalApproveMap := make(map[int]int)
		tmpGroupDoingExpiredMap := make(map[int]int)
		tmpGroupFinishMap := make(map[int]int)
		//id集合
		tmpGroupTryOutAddIdSliceMap := make(map[int][]string)
		tmpGroupTryOutApproveIdSliceMap := make(map[int][]string)
		tmpGroupFormalAddIdSliceMap := make(map[int][]string)
		tmpGroupFormalApproveIdSliceMap := make(map[int][]string)

		//id集合
		tmpGroupTryOutAddIdMap := make(map[int]string)
		tmpGroupTryOutApproveIdMap := make(map[int]string)
		tmpGroupFormalAddIdMap := make(map[int]string)
		tmpGroupFormalApproveIdMap := make(map[int]string)
		tmpGroupDoingExpiredIdMap := make(map[int]string)
		tmpGroupFinishIdMap := make(map[int]string)

		for _, researcher := range groupMap[v.GroupId] {

			//每个区间的数据
			tmpCompanyTodoReportRecordNumList := make([]statistic_report.CompanyTodoReportRecordNum, 0)
			tmpDoingExpiredNum := 0
			tmpDoingExpiredCompanyIds := ""
			tmpAdminCompanyIds := ""
			// 未完成任务统计
			if num, ok := doingExpiredDataMapList[researcher.AdminId]; ok {
				tmpDoingExpiredNum = num
				if tmpCompanyIds, ok2 := doingExpiredIdDataMapList[researcher.AdminId]; ok2 {
					tmpDoingExpiredCompanyIds = tmpCompanyIds
				}
			}
			tmpGroupDoingExpiredMap[v.GroupId] += tmpDoingExpiredNum
			tmpAllDoingExpired += tmpDoingExpiredNum

			if tmpDoingExpiredCompanyIds != "" {
				if tmpGroupDoingExpiredIdMap[v.GroupId] != "" {
					tmpGroupDoingExpiredIdMap[v.GroupId] += "," + tmpDoingExpiredCompanyIds
				} else {
					tmpGroupDoingExpiredIdMap[v.GroupId] += tmpDoingExpiredCompanyIds
				}
			}

			// 已完成任务统计
			tmpFinishNum := 0
			tmpFinishCompanyIds := ""
			if finishNum, ok := finishDataMapList[researcher.AdminId]; ok {
				tmpFinishNum = finishNum
				if tmpCompanyIds, ok2 := finishIdDataMapList[researcher.AdminId]; ok2 {
					tmpFinishCompanyIds = tmpCompanyIds
				}
			}
			tmpGroupFinishMap[v.GroupId] += tmpFinishNum
			tmpAllFinish += tmpFinishNum

			if tmpFinishCompanyIds != "" {
				if tmpGroupFinishIdMap[v.GroupId] != "" {
					tmpGroupFinishIdMap[v.GroupId] += "," + tmpFinishCompanyIds
				} else {
					tmpGroupFinishIdMap[v.GroupId] += tmpFinishCompanyIds
				}
			}

			// 销售相关的试用客户ID
			if ids, ok := adminCompanyIdsMap[researcher.AdminId]; ok {
				tmpAdminCompanyIds = ids
			}

			for index, adminData := range companyReportDataMapList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpTryOutAddNum, tmpTryOutApproveNum, tmpFormalAddNum, tmpFormalApproveNum int
				//var tmpTryOutAddIds, tmpTryOutApproveIds, tmpFormalAddIds string
				var tmpTryOutAddIdSlice, tmpTryOutApproveIdSlice, tmpFormalAddIdSlice, tmpFormalApproveIdSlice []string
				var tmpTryOutAddId, tmpTryOutApproveId, tmpFormalAddId, tmpFormalApproveId string

				tmpTryOutData := adminData.TryOut
				tmpFormalData := adminData.Formal

				//试用数据
				if num, ok := tmpTryOutData.AddMap[researcher.AdminId]; ok {
					tmpTryOutAddNum = num
					tmpTryOutAddIdSlice = append(tmpTryOutAddIdSlice, tmpTryOutData.AddIdMap[researcher.AdminId])
					tmpTryOutAddId = strings.Join(tmpTryOutAddIdSlice, ",")
				}
				if num, ok := tmpTryOutData.ApproveMap[researcher.AdminId]; ok {
					tmpTryOutApproveNum = num
					tmpTryOutApproveIdSlice = append(tmpTryOutApproveIdSlice, tmpTryOutData.ApproveIdMap[researcher.AdminId])
					tmpTryOutApproveId = strings.Join(tmpTryOutApproveIdSlice, ",")
				}
				if num, ok := tmpFormalData.AddMap[researcher.AdminId]; ok {
					tmpFormalAddNum = num
					tmpFormalAddIdSlice = append(tmpFormalAddIdSlice, tmpFormalData.AddIdMap[researcher.AdminId])
					tmpFormalAddId = strings.Join(tmpFormalAddIdSlice, ",")
				}
				if num, ok := tmpFormalData.ApproveMap[researcher.AdminId]; ok {
					tmpFormalApproveNum = num
					tmpFormalApproveIdSlice = append(tmpFormalApproveIdSlice, tmpFormalData.ApproveIdMap[researcher.AdminId])
					tmpFormalApproveId = strings.Join(tmpFormalApproveIdSlice, ",")
				}
				tmpAdminRsReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
					TryOutAddMap:       tmpTryOutAddNum,
					TryOutAddIdMap:     tmpTryOutAddId,
					TryOutApproveMap:   tmpTryOutApproveNum,
					TryOutApproveIdMap: tmpTryOutApproveId,
					FormalAddMap:       tmpFormalAddNum,
					FormalAddIdMap:     tmpFormalAddId,
					FormalApproveMap:   tmpFormalApproveNum,
					FormalApproveIdMap: tmpFormalApproveId,
					StartDate:          adminData.StartDate,
					EndDate:            adminData.EndDate,
				}
				tmpCompanyTodoReportRecordNumList = append(tmpCompanyTodoReportRecordNumList, tmpAdminRsReportRecordNum)
				//组内数据汇总
				if _, ok := tmpGroupTryOutAddMap[index]; !ok {
					tmpGroupTryOutAddMap[index] = 0
				}
				if _, ok := tmpGroupTryOutApproveMap[index]; !ok {
					tmpGroupTryOutApproveMap[index] = 0
				}
				if _, ok := tmpGroupFormalAddMap[index]; !ok {
					tmpGroupFormalAddMap[index] = 0
				}
				if _, ok := tmpGroupFormalApproveMap[index]; !ok {
					tmpGroupFormalApproveMap[index] = 0
				}
				tmpGroupTryOutAddMap[index] += tmpTryOutAddNum
				tmpGroupTryOutApproveMap[index] += tmpTryOutApproveNum
				tmpGroupFormalAddMap[index] += tmpFormalAddNum
				tmpGroupFormalApproveMap[index] += tmpFormalApproveNum

				//组内数据汇总
				if _, ok := tmpGroupTryOutAddIdMap[index]; !ok {
					tmpGroupTryOutAddIdMap[index] = ""
				}
				if _, ok := tmpGroupTryOutApproveIdMap[index]; !ok {
					tmpGroupTryOutApproveIdMap[index] = ""
				}
				if _, ok := tmpGroupFormalAddIdMap[index]; !ok {
					tmpGroupFormalAddIdMap[index] = ""
				}
				if tmpTryOutAddId != "" {
					tmpGroupTryOutAddIdSliceMap[index] = append(tmpGroupTryOutAddIdSliceMap[index], tmpTryOutAddId)
				}
				if tmpTryOutApproveId != "" {
					tmpGroupTryOutApproveIdSliceMap[index] = append(tmpGroupTryOutApproveIdSliceMap[index], tmpTryOutApproveId)
				}
				if tmpFormalAddId != "" {
					tmpGroupFormalAddIdSliceMap[index] = append(tmpGroupFormalAddIdSliceMap[index], tmpFormalAddId)
				}
				if tmpFormalApproveId != "" {
					tmpGroupFormalApproveIdSliceMap[index] = append(tmpGroupFormalApproveIdSliceMap[index], tmpFormalApproveId)
				}

				//总数据汇总
				if _, ok := tmpAllTryOutAddMap[index]; !ok {
					tmpAllTryOutAddMap[index] = 0
				}
				if _, ok := tmpAllTryOutApproveMap[index]; !ok {
					tmpAllTryOutApproveMap[index] = 0
				}
				if _, ok := tmpAllFormalAddMap[index]; !ok {
					tmpAllFormalAddMap[index] = 0
				}
				if _, ok := tmpAllFormalApproveMap[index]; !ok {
					tmpAllFormalApproveMap[index] = 0
				}
				tmpAllTryOutAddMap[index] += tmpTryOutAddNum
				tmpAllTryOutApproveMap[index] += tmpTryOutApproveNum
				tmpAllFormalAddMap[index] += tmpFormalAddNum
				tmpAllFormalApproveMap[index] += tmpFormalApproveNum
			}

			tmpAdminTodoReportRecord := statistic_report.AdminTodoReportRecord{
				Name:                           researcher.RealName,
				AdminId:                        researcher.AdminId,
				CompanyTodoReportRecordNumList: tmpCompanyTodoReportRecordNumList,
				DoingExpiredNum:                tmpDoingExpiredNum,
				DoingExpiredCompanyIds:         tmpDoingExpiredCompanyIds,
				FinishNum:                      tmpFinishNum,
				FinishCompanyIds:               tmpFinishCompanyIds,
				AdminCompanyIds:                tmpAdminCompanyIds,
			}
			tmpGroupAdminTodoReportRecord = append(tmpGroupAdminTodoReportRecord, tmpAdminTodoReportRecord)
		}

		for i := 0; i < len(tmpGroupFormalAddMap); i++ {
			tmpGroupTryOutAddIdMap[i] = strings.Join(tmpGroupTryOutAddIdSliceMap[i], ",")
			tmpGroupTryOutApproveIdMap[i] = strings.Join(tmpGroupTryOutApproveIdSliceMap[i], ",")
			tmpGroupFormalAddIdMap[i] = strings.Join(tmpGroupFormalAddIdSliceMap[i], ",")
			tmpGroupFormalApproveIdMap[i] = strings.Join(tmpGroupFormalApproveIdSliceMap[i], ",")
			tmpGroupCompanyTodoReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
				TryOutAddMap:       tmpGroupTryOutAddMap[i],
				TryOutAddIdMap:     tmpGroupTryOutAddIdMap[i],
				TryOutApproveMap:   tmpGroupTryOutApproveMap[i],
				TryOutApproveIdMap: tmpGroupTryOutApproveIdMap[i],
				FormalAddMap:       tmpGroupFormalAddMap[i],
				FormalAddIdMap:     tmpGroupFormalAddIdMap[i],
				FormalApproveMap:   tmpGroupFormalApproveMap[i],
				FormalApproveIdMap: tmpGroupFormalApproveIdMap[i],
				StartDate:          startDateIndexList[i],
				EndDate:            endDateIndexList[i],
			}
			tmpGroupCompanyTodoReportRecordNumList = append(tmpGroupCompanyTodoReportRecordNumList, tmpGroupCompanyTodoReportRecordNum)
		}
		groupTodoReportRecord := statistic_report.GroupTodoReportRecord{
			Item:                           tmpGroupAdminTodoReportRecord,
			Name:                           v.GroupName,
			GroupId:                        v.GroupId,
			CompanyTodoReportRecordNumList: tmpGroupCompanyTodoReportRecordNumList,
			DoingExpiredGroupNum:           tmpGroupDoingExpiredMap[v.GroupId],
			DoingExpiredGroupCompanyIds:    tmpGroupDoingExpiredIdMap[v.GroupId],
		}
		groupTodoReportRecordList = append(groupTodoReportRecordList, groupTodoReportRecord)
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllTryOutApproveMap); i++ {
		tmpGroupCompanyTodoReportRecordNum := statistic_report.CompanyTodoReportRecordNum{
			TryOutAddMap:       tmpAllTryOutAddMap[i],
			TryOutAddIdMap:     tmpAllTryOutAddIdMap[i],
			TryOutApproveMap:   tmpAllTryOutApproveMap[i],
			TryOutApproveIdMap: tmpAllTryOutApproveIdMap[i],
			FormalAddMap:       tmpAllFormalAddMap[i],
			FormalAddIdMap:     tmpAllFormalAddIdMap[i],
			FormalApproveMap:   tmpAllFormalApproveMap[i],
			FormalApproveIdMap: tmpAllFormalApproveIdMap[i],
			StartDate:          startDateIndexList[i],
			EndDate:            endDateIndexList[i],
		}
		allCompanyTodoReportRecordNum = append(allCompanyTodoReportRecordNum, tmpGroupCompanyTodoReportRecordNum)
	}
	companyTodoReportRecordResp.CompanyTodoReportRecordNumList = allCompanyTodoReportRecordNum

	companyTodoReportRecordResp.List = groupTodoReportRecordList
	companyTodoReportRecordResp.DoingExpiredAllNum = tmpAllDoingExpired
	companyTodoReportRecordResp.FinishAllNum = tmpAllFinish
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = companyTodoReportRecordResp
}

// 获取销售to-do统计的销售员
func getTodoCompanySallers(productId int, groupId int, sysUser *system.Admin, firstDate time.Time) (group []services.AdminGroup, groupMap map[int][]*roadshow.Researcher, sellerIds string, brMsg, brErrMsg string) {
	var groupIdRelationMap map[int]int
	var researcherList []*roadshow.Researcher
	var err error
	groupMap = make(map[int][]*roadshow.Researcher)

	//获取销售人员分组数据
	if productId == 1 { //ficc
		if groupId > 0 { //ficc 只查某个组相关的销售
			findItem := &roadshow.Researcher{
				AdminId:      sysUser.AdminId,
				RealName:     sysUser.RealName,
				GroupId:      sysUser.GroupId,
				GroupName:    sysUser.GroupName,
				RoleTypeCode: sysUser.RoleTypeCode,
			}
			if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER { //销售，只查自己的客户
				var findItems []*roadshow.Researcher
				findItems = append(findItems, findItem)
				groupMap[sysUser.GroupId] = findItems
				sellerIds = strconv.Itoa(sysUser.AdminId)

				//处理group
				adminGroup := services.AdminGroup{
					GroupId:        sysUser.GroupId,
					GroupName:      sysUser.GroupName,
					ResearcherList: findItems,
				}
				group = append(group, adminGroup)

			} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_TEAM { //销售组长只查本组员和本人
				roleTypeCode := "('ficc_seller')"
				groupIds := "(" + strconv.Itoa(groupId) + ")"
				researcherList, err = roadshow.GetSellerList(roleTypeCode, groupIds)
				if err != nil {
					brMsg = "获取销售失败！"
					brErrMsg = "获取销售失败！Err:" + err.Error()
					return
				}
				researcherList = append(researcherList, findItem)
				for _, v := range researcherList {
					sellerIds += "," + strconv.Itoa(v.AdminId)
				}
				groupMap[sysUser.GroupId] = researcherList

				//处理group
				adminGroup := services.AdminGroup{
					GroupId:        sysUser.GroupId,
					GroupName:      sysUser.GroupName,
					ResearcherList: researcherList,
				}
				group = append(group, adminGroup)
			} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP { //如果主管在三级小组里，查找到归属的二级分组
				groupIds := ""
				groupIdsSlice, err := services.GetFiccSystemGroupIdsByGroupId(sysUser.GroupId)
				if err != nil {
					brMsg = "获取销售失败！"
					brErrMsg = "获取销售失败！Err:" + err.Error()
					return
				}
				for _, v := range groupIdsSlice {
					groupIds += "," + strconv.Itoa(v)
				}
				groupIds = strings.Trim(groupIds, ",")
				groupIds = "(" + groupIds + ")"
				roleTypeCode := "('ficc_seller','ficc_team')"
				researcherList, err = roadshow.GetSellerList(roleTypeCode, groupIds)
				if err != nil {
					brMsg = "获取销售失败！"
					brErrMsg = "获取销售失败！Err:" + err.Error()
					return
				}
				researcherList = append(researcherList, findItem)
				for _, v := range researcherList {
					sellerIds += "," + strconv.Itoa(v.AdminId)
				}
				groupMap[sysUser.GroupId] = researcherList

				//处理group
				adminGroup := services.AdminGroup{
					GroupId:        sysUser.GroupId,
					GroupName:      sysUser.GroupName,
					ResearcherList: researcherList,
				}
				group = append(group, adminGroup)
			}
		} else {
			group, groupIdRelationMap, err = services.GetFiccSystemGroup()
			if err != nil {
				brMsg = "获取信息失败！"
				brErrMsg = "获取分组信息失败！Err:" + err.Error()
				return
			}
			researcherList, err = services.GetFiccSeller(firstDate, groupIdRelationMap)
			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
					}
				}
				sellerIds += "," + strconv.Itoa(v.AdminId)
			}
		}
	} else if productId == 2 { //权益
		if groupId > 0 {
			findItem := &roadshow.Researcher{
				AdminId:      sysUser.AdminId,
				RealName:     sysUser.RealName,
				GroupId:      sysUser.GroupId,
				GroupName:    sysUser.GroupName,
				RoleTypeCode: sysUser.RoleTypeCode,
			}
			if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER { //销售，只查自己的客户
				var findItems []*roadshow.Researcher
				findItems = append(findItems, findItem)
				groupMap[sysUser.GroupId] = findItems
				sellerIds = strconv.Itoa(sysUser.AdminId)

				//处理group
				adminGroup := services.AdminGroup{
					GroupId:        sysUser.GroupId,
					GroupName:      sysUser.GroupName,
					ResearcherList: findItems,
				}
				group = append(group, adminGroup)

			} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
				roleTypeCode := "('rai_seller')"
				groupIds := "(" + strconv.Itoa(groupId) + ")"
				researcherList, err = roadshow.GetRaiSellerList(roleTypeCode, groupIds)
				if err != nil {
					brMsg = "获取销售失败！"
					brErrMsg = "获取销售失败！Err:" + err.Error()
					return
				}
				researcherList = append(researcherList, findItem)
				for _, v := range researcherList {
					sellerIds += "," + strconv.Itoa(v.AdminId)
				}
				groupMap[sysUser.GroupId] = researcherList

				//处理group
				adminGroup := services.AdminGroup{
					GroupId:        sysUser.GroupId,
					GroupName:      sysUser.GroupName,
					ResearcherList: researcherList,
				}
				group = append(group, adminGroup)
			}
		} else {
			group, groupIdRelationMap, err = services.GetRaiSystemGroup()
			if err != nil {
				brMsg = "获取信息失败！"
				brErrMsg = "获取分组信息失败！Err:" + err.Error()
				return
			}
			researcherList, err = services.GetRaiSeller(groupIdRelationMap)
			for _, v := range researcherList {
				if v.RoleTypeCode == "rai_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
					}
				}
				sellerIds += "," + strconv.Itoa(v.AdminId)
			}
		}
	}
	return
}
