package controllers

import (
	"encoding/json"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/roadshow"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/models/yb"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/services/statistic_report"
	"hongze/hz_crm_api/utils"
	"strings"
	"time"
)

type StatisticYbLogController struct {
	BaseAuthController
}

// SellerYbLog
// @Title 分产品阅读统计
// @Description 分产品阅读统计
// @Param   DataType   query   string  true       "枚举值：week、month、time_interval"
// @Param   ProductType   query   int  true       "产品类型"
// @Param   ProductId   query   int  true         "产品id"
// @Success 200 {object} statistic_report.CompanyReportRecordResp
// @router /report/seller_yb_log [get]
func (s *StatisticYbLogController) SellerYbLog() {
	br := new(models.BaseResponse).Init()
	defer func() {
		s.Data["json"] = br
		s.ServeJSON()
	}()

	sysUser := s.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	dataType := s.GetString("DataType")
	productType, _ := s.GetInt("ProductType")
	productId, _ := s.GetInt("ProductId")
	if dataType == "" {
		br.Msg = "请输入时间类型"
		return
	}

	if productType == 0 {
		br.Msg = "请输入产品类型"
		return
	}
	var companyReportDataMapList []statistic_report.SellerYbLogDataMap
	var err error
	var firstDate time.Time
	switch dataType {
	//获取列表
	case "week":
		nowWeekMonday := utils.GetNowWeekMonday() //本周周一
		//companyReportDataMapList, firstDate, err = statistic_report.GetWeekData()
		companyReportDataMapList, firstDate, err = statistic_report.GetYbLogWeekDataNum(nowWeekMonday, productId, productType, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "month":
		nowMonthFirstDay := utils.GetNowMonthFirstDay() //本月第一天
		companyReportDataMapList, firstDate, err = statistic_report.GetYbLogMonthDataNum(nowMonthFirstDay, productId, productType, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	}

	//获取销售人员分组数据
	var group []services.AdminGroup
	var groupIdRelationMap map[int]int
	var groupMap map[int][]*roadshow.Researcher

	group, groupIdRelationMap, err = services.GetFiccSystemGroup()
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取分组信息失败！Err:" + err.Error()
		return
	}
	researcherList, tmpErr := services.GetFiccSeller(firstDate, groupIdRelationMap)
	if tmpErr != nil {
		err = tmpErr
	}
	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([]statistic_report.GroupYbLogRecord, 0)
	allCompanyReportRecordNum := make([]statistic_report.SellerYbLogRecordNum, 0)
	companyReportRecordResp := statistic_report.SellerYbLogRecordResp{
		List:                     groupReportRecordList,
		SellerYbLogRecordNumList: allCompanyReportRecordNum,
	}

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

	//客户数汇总
	tmpAllTryOutNumMap := make(map[int]int)
	tmpAllFormalNumMap := make(map[int]int)
	tmpAllAllNumMap := make(map[int]int)

	//id集合汇总
	tmpAllTryOutIdMap := make(map[int]string)
	tmpAllFormalIdMap := make(map[int]string)
	tmpAllAllIdMap := make(map[int]string)
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminReportRecord := make([]statistic_report.AdminYbLogRecord, 0)
		tmpGroupCompanyReportRecordNumList := make([]statistic_report.SellerYbLogRecordNum, 0)

		//数量统计
		tmpGroupTryOutNumMap := make(map[int]int)
		tmpGroupFormalNumMap := make(map[int]int)
		tmpGroupAllNumMap := make(map[int]int)

		//id集合
		tmpGroupTryOutIdSliceMap := make(map[int][]string)
		tmpGroupFormalIdSliceMap := make(map[int][]string)
		tmpGroupAllIdSliceMap := make(map[int][]string)

		//id集合
		tmpGroupTryOutIdMap := make(map[int]string)
		tmpGroupFormalIdMap := make(map[int]string)
		tmpGroupAllIdMap := make(map[int]string)

		for _, researcher := range groupMap[v.GroupId] {
			//组内研究员数据

			//每个区间的数据
			tmpCompanyReportRecordNumList := make([]statistic_report.SellerYbLogRecordNum, 0)
			for index, adminData := range companyReportDataMapList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpTryOutNum, tmpFormalNum, tmpAllNum int
				var tmpTryOutIdSlice, tmpFormalIdSlice, tmpAllIdSlice []string
				var tmpTryOutId, tmpFormalId, tmpAllId string

				if num, ok := adminData.TryOutMap[researcher.AdminId]; ok {
					tmpTryOutNum = num
					tmpTryOutIdSlice = append(tmpTryOutIdSlice, adminData.TryOutIdMap[researcher.AdminId])
					tmpTryOutId = strings.Join(tmpTryOutIdSlice, ",")
				}
				if num, ok := adminData.FormalMap[researcher.AdminId]; ok {
					tmpFormalNum = num
					tmpFormalIdSlice = append(tmpFormalIdSlice, adminData.FormalIdMap[researcher.AdminId])
					tmpFormalId = strings.Join(tmpFormalIdSlice, ",")
				}
				if num, ok := adminData.AllMap[researcher.AdminId]; ok {
					tmpAllNum = num
					tmpAllIdSlice = append(tmpAllIdSlice, adminData.AllIdMap[researcher.AdminId])
					tmpAllId = strings.Join(tmpAllIdSlice, ",")
				}

				tmpAdminRsReportRecordNum := statistic_report.SellerYbLogRecordNum{
					TryOutNum: tmpTryOutNum,
					TryOutIds: tmpTryOutId,
					FormalNum: tmpFormalNum,
					FormalIds: tmpFormalId,
					AllNum:    tmpAllNum,
					AllIds:    tmpAllId,
					StartDate: adminData.StartDate,
					EndDate:   adminData.EndDate,
				}
				tmpCompanyReportRecordNumList = append(tmpCompanyReportRecordNumList, tmpAdminRsReportRecordNum)

				//组内数据汇总
				if _, ok := tmpGroupTryOutNumMap[index]; !ok {
					tmpGroupTryOutNumMap[index] = 0
				}
				if _, ok := tmpGroupFormalNumMap[index]; !ok {
					tmpGroupFormalNumMap[index] = 0
				}

				if _, ok := tmpGroupAllNumMap[index]; !ok {
					tmpGroupAllNumMap[index] = 0
				}

				tmpGroupTryOutNumMap[index] += tmpTryOutNum
				tmpGroupFormalNumMap[index] += tmpFormalNum
				tmpGroupAllNumMap[index] += tmpAllNum

				//组内数据汇总
				if tmpTryOutId != "" {
					tmpGroupTryOutIdSliceMap[index] = append(tmpGroupTryOutIdSliceMap[index], tmpTryOutId)
				}
				if tmpFormalId != "" {
					tmpGroupFormalIdSliceMap[index] = append(tmpGroupFormalIdSliceMap[index], tmpFormalId)
				}

				if tmpAllId != "" {
					tmpGroupAllIdSliceMap[index] = append(tmpGroupAllIdSliceMap[index], tmpAllId)
				}

				//总数据汇总
				/*	if _, ok := tmpAllTryOutNumMap[index]; !ok {
						tmpAllTryOutNumMap[index] = 0
					}
					if _, ok := tmpAllFormalNumMap[index]; !ok {
						tmpAllFormalNumMap[index] = 0
					}

					if _, ok := tmpAllAllNumMap[index]; !ok {
						tmpAllAllNumMap[index] = 0
					}*/

				tmpAllTryOutNumMap[index] += tmpTryOutNum
				tmpAllFormalNumMap[index] += tmpFormalNum
				tmpAllAllNumMap[index] += tmpAllNum

				tmpAllTryOutIdMap[index] += tmpTryOutId
				tmpAllFormalIdMap[index] += tmpFormalId
				tmpAllAllIdMap[index] += tmpAllId
			}
			tmpAdminReportRecord := statistic_report.AdminYbLogRecord{
				Name:                     researcher.RealName,
				AdminId:                  researcher.AdminId,
				SellerYbLogRecordNumList: tmpCompanyReportRecordNumList,
			}
			tmpGroupAdminReportRecord = append(tmpGroupAdminReportRecord, tmpAdminReportRecord)
		}

		for i := 0; i < len(tmpGroupAllNumMap); i++ {
			tmpGroupTryOutIdMap[i] = strings.Join(tmpGroupTryOutIdSliceMap[i], ",")
			tmpGroupFormalIdMap[i] = strings.Join(tmpGroupFormalIdSliceMap[i], ",")
			tmpGroupAllIdMap[i] = strings.Join(tmpGroupAllIdSliceMap[i], ",")

			tmpGroupCompanyReportRecordNum := statistic_report.SellerYbLogRecordNum{
				TryOutNum: tmpGroupTryOutNumMap[i],
				TryOutIds: tmpGroupTryOutIdMap[i],
				FormalNum: tmpGroupFormalNumMap[i],
				FormalIds: tmpGroupFormalIdMap[i],
				AllNum:    tmpGroupAllNumMap[i],
				AllIds:    tmpGroupAllIdMap[i],
				StartDate: startDateIndexList[i],
				EndDate:   endDateIndexList[i],
			}
			tmpGroupCompanyReportRecordNumList = append(tmpGroupCompanyReportRecordNumList, tmpGroupCompanyReportRecordNum)
		}
		groupReportRecord := statistic_report.GroupYbLogRecord{
			Item:                     tmpGroupAdminReportRecord,
			Name:                     v.GroupName,
			GroupId:                  v.GroupId,
			SellerYbLogRecordNumList: tmpGroupCompanyReportRecordNumList,
		}
		groupReportRecordList = append(groupReportRecordList, groupReportRecord)
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllFormalNumMap); i++ {
		tmpGroupCompanyReportRecordNum := statistic_report.SellerYbLogRecordNum{
			TryOutNum: tmpAllTryOutNumMap[i],
			TryOutIds: "",
			FormalNum: tmpAllFormalNumMap[i],
			FormalIds: "",
			AllNum:    tmpAllAllNumMap[i],
			AllIds:    "",
			StartDate: startDateIndexList[i],
			EndDate:   endDateIndexList[i],
		}
		allCompanyReportRecordNum = append(allCompanyReportRecordNum, tmpGroupCompanyReportRecordNum)
	}
	companyReportRecordResp.SellerYbLogRecordNumList = allCompanyReportRecordNum
	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.GroupId != adminGroupId {
				tmpList[index].Item = make([]statistic_report.AdminYbLogRecord, 0)
			} else {
				tmpList[index], tmpList[0] = tmpList[0], tmpList[index]
			}
		}
		companyReportRecordResp.List = tmpList
	case utils.ROLE_TYPE_CODE_RAI_GROUP:
		for index, v := range tmpList {
			//如果不是同一个分组，那么就移除该分组下的人员数据
			if v.GroupId != adminGroupId {
				tmpList[index].Item = make([]statistic_report.AdminYbLogRecord, 0)
			} else {
				tmpList[index], tmpList[0] = tmpList[0], tmpList[index]
			}
		}
		companyReportRecordResp.List = tmpList
	case utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_RAI_ADMIN:
		//管理员、超管看全部数据
		companyReportRecordResp.List = tmpList
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = companyReportRecordResp
}

// SellerYbUserLog
// @Title 分产品阅读统计用户详情
// @Description 分产品阅读统计用户详情
// @Param	request	body statisticModels.SellerYbUserLogReq true "type json string"
// @Success 200 {object} statistic_report.CompanyListResp
// @router /report/seller_yb_log/user [post]
func (s *StatisticYbLogController) SellerYbUserLog() {
	br := new(models.BaseResponse).Init()
	defer func() {
		s.Data["json"] = br
		s.ServeJSON()
	}()
	sysUser := s.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req yb.SellerYbUserLogReq
	err := json.Unmarshal(s.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	//todo 校验入参
	if req.SellerIds == "" {
		br.Msg = "请输入销售ID"
		return
	}

	if req.StartDate == "" {
		br.Msg = "请输入开始时间"
		return
	}

	if req.EndDate == "" {
		br.Msg = "请输入截止时间"
		return
	}

	if req.ProductType == 0 {
		br.Msg = "请输入产品类型"
		return
	}

	if req.CompanyStatus == "" {
		br.Msg = "请输入产品状态"
		return
	}
	resp, err, errMsg := statistic_report.YbProductUserList(sysUser, req)
	if err != nil {
		br.Msg = err.Error()
		br.ErrMsg = errMsg
		return
	}

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

// SellerCollectLog
// @Title 收藏统计
// @Description 收藏统计
// @Param   DataType   query   string  true       "枚举值：week、month、time_interval"
// @Success 200 {object} statistic_report.CompanyReportRecordResp
// @router /report/seller_collect_log [get]
func (s *StatisticYbLogController) SellerCollectLog() {
	br := new(models.BaseResponse).Init()
	defer func() {
		s.Data["json"] = br
		s.ServeJSON()
	}()

	sysUser := s.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	dataType := s.GetString("DataType")
	if dataType == "" {
		br.Msg = "请输入时间类型"
		return
	}

	var companyReportDataMapList []statistic_report.SellerYbLogDataMap
	var err error
	var firstDate time.Time
	switch dataType {
	//获取列表
	case "week":
		nowWeekMonday := utils.GetNowWeekMonday() //本周周一
		companyReportDataMapList, firstDate, err = statistic_report.GetCollectLogWeekDataNum(nowWeekMonday, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "month":
		nowMonthFirstDay := utils.GetNowMonthFirstDay() //本月第一天
		companyReportDataMapList, firstDate, err = statistic_report.GetCollectLogMonthDataNum(nowMonthFirstDay, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	}

	//获取销售人员分组数据
	var group []services.AdminGroup
	var groupIdRelationMap map[int]int
	var groupMap map[int][]*roadshow.Researcher

	group, groupIdRelationMap, err = services.GetFiccSystemGroup()
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取分组信息失败！Err:" + err.Error()
		return
	}
	researcherList, tmpErr := services.GetFiccSeller(firstDate, groupIdRelationMap)
	if tmpErr != nil {
		err = tmpErr
	}
	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([]statistic_report.GroupYbLogRecord, 0)
	allCompanyReportRecordNum := make([]statistic_report.SellerYbLogRecordNum, 0)
	companyReportRecordResp := statistic_report.SellerYbLogRecordResp{
		List:                     groupReportRecordList,
		SellerYbLogRecordNumList: allCompanyReportRecordNum,
	}

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

	//客户数汇总
	tmpAllTryOutNumMap := make(map[int]int)
	tmpAllFormalNumMap := make(map[int]int)
	tmpAllAllNumMap := make(map[int]int)

	//id集合汇总
	tmpAllTryOutIdMap := make(map[int]string)
	tmpAllFormalIdMap := make(map[int]string)
	tmpAllAllIdMap := make(map[int]string)
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminReportRecord := make([]statistic_report.AdminYbLogRecord, 0)
		tmpGroupCompanyReportRecordNumList := make([]statistic_report.SellerYbLogRecordNum, 0)

		//数量统计
		tmpGroupTryOutNumMap := make(map[int]int)
		tmpGroupFormalNumMap := make(map[int]int)
		tmpGroupAllNumMap := make(map[int]int)

		//id集合
		tmpGroupTryOutIdSliceMap := make(map[int][]string)
		tmpGroupFormalIdSliceMap := make(map[int][]string)
		tmpGroupAllIdSliceMap := make(map[int][]string)

		//id集合
		tmpGroupTryOutIdMap := make(map[int]string)
		tmpGroupFormalIdMap := make(map[int]string)
		tmpGroupAllIdMap := make(map[int]string)

		for _, researcher := range groupMap[v.GroupId] {
			//组内研究员数据

			//每个区间的数据
			tmpCompanyReportRecordNumList := make([]statistic_report.SellerYbLogRecordNum, 0)
			for index, adminData := range companyReportDataMapList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpTryOutNum, tmpFormalNum, tmpAllNum int
				var tmpTryOutIdSlice, tmpFormalIdSlice, tmpAllIdSlice []string
				var tmpTryOutId, tmpFormalId, tmpAllId string

				if num, ok := adminData.TryOutMap[researcher.AdminId]; ok {
					tmpTryOutNum = num
					tmpTryOutIdSlice = append(tmpTryOutIdSlice, adminData.TryOutIdMap[researcher.AdminId])
					tmpTryOutId = strings.Join(tmpTryOutIdSlice, ",")
				}
				if num, ok := adminData.FormalMap[researcher.AdminId]; ok {
					tmpFormalNum = num
					tmpFormalIdSlice = append(tmpFormalIdSlice, adminData.FormalIdMap[researcher.AdminId])
					tmpFormalId = strings.Join(tmpFormalIdSlice, ",")
				}
				if num, ok := adminData.AllMap[researcher.AdminId]; ok {
					tmpAllNum = num
					tmpAllIdSlice = append(tmpAllIdSlice, adminData.AllIdMap[researcher.AdminId])
					tmpAllId = strings.Join(tmpAllIdSlice, ",")
				}

				tmpAdminRsReportRecordNum := statistic_report.SellerYbLogRecordNum{
					TryOutNum: tmpTryOutNum,
					TryOutIds: tmpTryOutId,
					FormalNum: tmpFormalNum,
					FormalIds: tmpFormalId,
					AllNum:    tmpAllNum,
					AllIds:    tmpAllId,
					StartDate: adminData.StartDate,
					EndDate:   adminData.EndDate,
				}
				tmpCompanyReportRecordNumList = append(tmpCompanyReportRecordNumList, tmpAdminRsReportRecordNum)

				//组内数据汇总
				if _, ok := tmpGroupTryOutNumMap[index]; !ok {
					tmpGroupTryOutNumMap[index] = 0
				}
				if _, ok := tmpGroupFormalNumMap[index]; !ok {
					tmpGroupFormalNumMap[index] = 0
				}

				if _, ok := tmpGroupAllNumMap[index]; !ok {
					tmpGroupAllNumMap[index] = 0
				}

				tmpGroupTryOutNumMap[index] += tmpTryOutNum
				tmpGroupFormalNumMap[index] += tmpFormalNum
				tmpGroupAllNumMap[index] += tmpAllNum

				//组内数据汇总
				if tmpTryOutId != "" {
					tmpGroupTryOutIdSliceMap[index] = append(tmpGroupTryOutIdSliceMap[index], tmpTryOutId)
				}
				if tmpFormalId != "" {
					tmpGroupFormalIdSliceMap[index] = append(tmpGroupFormalIdSliceMap[index], tmpFormalId)
				}

				if tmpAllId != "" {
					tmpGroupAllIdSliceMap[index] = append(tmpGroupAllIdSliceMap[index], tmpAllId)
				}

				tmpAllTryOutNumMap[index] += tmpTryOutNum
				tmpAllFormalNumMap[index] += tmpFormalNum
				tmpAllAllNumMap[index] += tmpAllNum

				tmpAllTryOutIdMap[index] += tmpTryOutId
				tmpAllFormalIdMap[index] += tmpFormalId
				tmpAllAllIdMap[index] += tmpAllId
			}
			tmpAdminReportRecord := statistic_report.AdminYbLogRecord{
				Name:                     researcher.RealName,
				AdminId:                  researcher.AdminId,
				SellerYbLogRecordNumList: tmpCompanyReportRecordNumList,
			}
			tmpGroupAdminReportRecord = append(tmpGroupAdminReportRecord, tmpAdminReportRecord)
		}

		for i := 0; i < len(tmpGroupAllNumMap); i++ {
			tmpGroupTryOutIdMap[i] = strings.Join(tmpGroupTryOutIdSliceMap[i], ",")
			tmpGroupFormalIdMap[i] = strings.Join(tmpGroupFormalIdSliceMap[i], ",")
			tmpGroupAllIdMap[i] = strings.Join(tmpGroupAllIdSliceMap[i], ",")

			tmpGroupCompanyReportRecordNum := statistic_report.SellerYbLogRecordNum{
				TryOutNum: tmpGroupTryOutNumMap[i],
				TryOutIds: tmpGroupTryOutIdMap[i],
				FormalNum: tmpGroupFormalNumMap[i],
				FormalIds: tmpGroupFormalIdMap[i],
				AllNum:    tmpGroupAllNumMap[i],
				AllIds:    tmpGroupAllIdMap[i],
				StartDate: startDateIndexList[i],
				EndDate:   endDateIndexList[i],
			}
			tmpGroupCompanyReportRecordNumList = append(tmpGroupCompanyReportRecordNumList, tmpGroupCompanyReportRecordNum)
		}
		groupReportRecord := statistic_report.GroupYbLogRecord{
			Item:                     tmpGroupAdminReportRecord,
			Name:                     v.GroupName,
			GroupId:                  v.GroupId,
			SellerYbLogRecordNumList: tmpGroupCompanyReportRecordNumList,
		}
		groupReportRecordList = append(groupReportRecordList, groupReportRecord)
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllFormalNumMap); i++ {
		tmpGroupCompanyReportRecordNum := statistic_report.SellerYbLogRecordNum{
			TryOutNum: tmpAllTryOutNumMap[i],
			TryOutIds: "",
			FormalNum: tmpAllFormalNumMap[i],
			FormalIds: "",
			AllNum:    tmpAllAllNumMap[i],
			AllIds:    "",
			StartDate: startDateIndexList[i],
			EndDate:   endDateIndexList[i],
		}
		allCompanyReportRecordNum = append(allCompanyReportRecordNum, tmpGroupCompanyReportRecordNum)
	}
	companyReportRecordResp.SellerYbLogRecordNumList = allCompanyReportRecordNum
	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.GroupId != adminGroupId {
				tmpList[index].Item = make([]statistic_report.AdminYbLogRecord, 0)
			} else {
				tmpList[index], tmpList[0] = tmpList[0], tmpList[index]
			}
		}
		companyReportRecordResp.List = tmpList
	case utils.ROLE_TYPE_CODE_RAI_GROUP:
		for index, v := range tmpList {
			//如果不是同一个分组，那么就移除该分组下的人员数据
			if v.GroupId != adminGroupId {
				tmpList[index].Item = make([]statistic_report.AdminYbLogRecord, 0)
			} else {
				tmpList[index], tmpList[0] = tmpList[0], tmpList[index]
			}
		}
		companyReportRecordResp.List = tmpList
	case utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_RAI_ADMIN:
		//管理员、超管看全部数据
		companyReportRecordResp.List = tmpList
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = companyReportRecordResp
}

// SellerYbUserLog
// @Title 收藏统计用户详情
// @Description 分产品阅读统计用户详情
// @Param	request	body statisticModels.SellerYbUserLogReq true "type json string"
// @Success 200 {object} statistic_report.CompanyListResp
// @router /report/seller_collect_log/user [post]
func (s *StatisticYbLogController) SellerCollectUserLog() {
	br := new(models.BaseResponse).Init()
	defer func() {
		s.Data["json"] = br
		s.ServeJSON()
	}()
	sysUser := s.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req yb.SellerCollectUserLogReq
	err := json.Unmarshal(s.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	//todo 校验入参
	if req.SellerIds == "" {
		br.Msg = "请输入销售ID"
		return
	}

	if req.StartDate == "" {
		br.Msg = "请输入开始时间"
		return
	}

	if req.EndDate == "" {
		br.Msg = "请输入截止时间"
		return
	}

	if req.CompanyStatus == "" {
		br.Msg = "请输入产品状态"
		return
	}

	resp, err, errMsg := statistic_report.YbCollectUserList(sysUser, req)
	if err != nil {
		br.Msg = err.Error()
		br.ErrMsg = errMsg
		return
	}

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

// CollectDetailList
// @Title 收藏详情
// @Description 收藏详情
// @Param	request	body statisticModels.SellerYbUserLogReq true "type json string"
// @Success 200 {object} statistic_report.CompanyListResp
// @router /report/seller_collect_log/detail [post]
func (s *StatisticYbLogController) CollectList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		s.Data["json"] = br
		s.ServeJSON()
	}()
	sysUser := s.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req yb.CollectListReq
	err := json.Unmarshal(s.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	//todo 校验入参
	if req.UserId == 0 {
		br.Msg = "请输入联系人ID"
		return
	}

	if req.SellerIds == "" {
		br.Msg = "请输入销售ID"
		return
	}

	if req.StartDate == "" {
		br.Msg = "请输入开始时间"
		return
	}

	if req.EndDate == "" {
		br.Msg = "请输入截止时间"
		return
	}

	if req.CompanyStatus == "" {
		br.Msg = "请输入产品状态"
		return
	}

	resp, err, errMsg := statistic_report.YbCollectList(sysUser, req)
	if err != nil {
		br.Msg = err.Error()
		br.ErrMsg = errMsg
		return
	}

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

// SellerYbChartCollectLog
// @Title 图收藏统计
// @Description 图收藏统计
// @Param   DataType   query   string  true       "枚举值：week、month、time_interval"
// @Success 200 {object} statistic_report.CompanyReportRecordResp
// @router /report/seller_chart_collect_log [get]
func (s *StatisticYbLogController) SellerYbChartCollectLog() {
	br := new(models.BaseResponse).Init()
	defer func() {
		s.Data["json"] = br
		s.ServeJSON()
	}()

	sysUser := s.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	dataType := s.GetString("DataType")
	if dataType == "" {
		br.Msg = "请输入时间类型"
		return
	}

	var companyReportDataMapList []statistic_report.SellerYbLogDataMap
	var err error
	var firstDate time.Time
	switch dataType {
	//获取列表
	case "week":
		nowWeekMonday := utils.GetNowWeekMonday() //本周周一
		companyReportDataMapList, firstDate, err = statistic_report.GetYbChartCollectLogWeekDataNum(nowWeekMonday, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	case "month":
		nowMonthFirstDay := utils.GetNowMonthFirstDay() //本月第一天
		companyReportDataMapList, firstDate, err = statistic_report.GetYbChartCollectLogMonthDataNum(nowMonthFirstDay, 6)
		if err != nil {
			br.Msg = "数据异常"
			br.ErrMsg = "数据异常，Err:" + err.Error()
			return
		}
	}

	//获取销售人员分组数据
	var group []services.AdminGroup
	var groupIdRelationMap map[int]int
	var groupMap map[int][]*roadshow.Researcher

	group, groupIdRelationMap, err = services.GetFiccSystemGroup()
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取分组信息失败！Err:" + err.Error()
		return
	}
	researcherList, tmpErr := services.GetFiccSeller(firstDate, groupIdRelationMap)
	if tmpErr != nil {
		err = tmpErr
	}
	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([]statistic_report.GroupYbLogRecord, 0)
	allCompanyReportRecordNum := make([]statistic_report.SellerYbLogRecordNum, 0)
	companyReportRecordResp := statistic_report.SellerYbLogRecordResp{
		List:                     groupReportRecordList,
		SellerYbLogRecordNumList: allCompanyReportRecordNum,
	}

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

	//客户数汇总
	tmpAllTryOutNumMap := make(map[int]int)
	tmpAllFormalNumMap := make(map[int]int)
	tmpAllAllNumMap := make(map[int]int)

	//id集合汇总
	tmpAllTryOutIdMap := make(map[int]string)
	tmpAllFormalIdMap := make(map[int]string)
	tmpAllAllIdMap := make(map[int]string)
	for _, v := range group {
		v.ResearcherList = groupMap[v.GroupId]
		tmpGroupAdminReportRecord := make([]statistic_report.AdminYbLogRecord, 0)
		tmpGroupCompanyReportRecordNumList := make([]statistic_report.SellerYbLogRecordNum, 0)

		//数量统计
		tmpGroupTryOutNumMap := make(map[int]int)
		tmpGroupFormalNumMap := make(map[int]int)
		tmpGroupAllNumMap := make(map[int]int)

		//id集合
		tmpGroupTryOutIdSliceMap := make(map[int][]string)
		tmpGroupFormalIdSliceMap := make(map[int][]string)
		tmpGroupAllIdSliceMap := make(map[int][]string)

		//id集合
		tmpGroupTryOutIdMap := make(map[int]string)
		tmpGroupFormalIdMap := make(map[int]string)
		tmpGroupAllIdMap := make(map[int]string)

		for _, researcher := range groupMap[v.GroupId] {
			//组内研究员数据

			//每个区间的数据
			tmpCompanyReportRecordNumList := make([]statistic_report.SellerYbLogRecordNum, 0)
			for index, adminData := range companyReportDataMapList {
				startDateIndexList[index] = adminData.StartDate
				endDateIndexList[index] = adminData.EndDate

				var tmpTryOutNum, tmpFormalNum, tmpAllNum int
				var tmpTryOutIdSlice, tmpFormalIdSlice, tmpAllIdSlice []string
				var tmpTryOutId, tmpFormalId, tmpAllId string

				if num, ok := adminData.TryOutMap[researcher.AdminId]; ok {
					tmpTryOutNum = num
					tmpTryOutIdSlice = append(tmpTryOutIdSlice, adminData.TryOutIdMap[researcher.AdminId])
					tmpTryOutId = strings.Join(tmpTryOutIdSlice, ",")
				}
				if num, ok := adminData.FormalMap[researcher.AdminId]; ok {
					tmpFormalNum = num
					tmpFormalIdSlice = append(tmpFormalIdSlice, adminData.FormalIdMap[researcher.AdminId])
					tmpFormalId = strings.Join(tmpFormalIdSlice, ",")
				}
				if num, ok := adminData.AllMap[researcher.AdminId]; ok {
					tmpAllNum = num
					tmpAllIdSlice = append(tmpAllIdSlice, adminData.AllIdMap[researcher.AdminId])
					tmpAllId = strings.Join(tmpAllIdSlice, ",")
				}

				tmpAdminRsReportRecordNum := statistic_report.SellerYbLogRecordNum{
					TryOutNum: tmpTryOutNum,
					TryOutIds: tmpTryOutId,
					FormalNum: tmpFormalNum,
					FormalIds: tmpFormalId,
					AllNum:    tmpAllNum,
					AllIds:    tmpAllId,
					StartDate: adminData.StartDate,
					EndDate:   adminData.EndDate,
				}
				tmpCompanyReportRecordNumList = append(tmpCompanyReportRecordNumList, tmpAdminRsReportRecordNum)

				//组内数据汇总
				if _, ok := tmpGroupTryOutNumMap[index]; !ok {
					tmpGroupTryOutNumMap[index] = 0
				}
				if _, ok := tmpGroupFormalNumMap[index]; !ok {
					tmpGroupFormalNumMap[index] = 0
				}

				if _, ok := tmpGroupAllNumMap[index]; !ok {
					tmpGroupAllNumMap[index] = 0
				}

				tmpGroupTryOutNumMap[index] += tmpTryOutNum
				tmpGroupFormalNumMap[index] += tmpFormalNum
				tmpGroupAllNumMap[index] += tmpAllNum

				//组内数据汇总
				if tmpTryOutId != "" {
					tmpGroupTryOutIdSliceMap[index] = append(tmpGroupTryOutIdSliceMap[index], tmpTryOutId)
				}
				if tmpFormalId != "" {
					tmpGroupFormalIdSliceMap[index] = append(tmpGroupFormalIdSliceMap[index], tmpFormalId)
				}

				if tmpAllId != "" {
					tmpGroupAllIdSliceMap[index] = append(tmpGroupAllIdSliceMap[index], tmpAllId)
				}

				tmpAllTryOutNumMap[index] += tmpTryOutNum
				tmpAllFormalNumMap[index] += tmpFormalNum
				tmpAllAllNumMap[index] += tmpAllNum

				tmpAllTryOutIdMap[index] += tmpTryOutId
				tmpAllFormalIdMap[index] += tmpFormalId
				tmpAllAllIdMap[index] += tmpAllId
			}
			tmpAdminReportRecord := statistic_report.AdminYbLogRecord{
				Name:                     researcher.RealName,
				AdminId:                  researcher.AdminId,
				SellerYbLogRecordNumList: tmpCompanyReportRecordNumList,
			}
			tmpGroupAdminReportRecord = append(tmpGroupAdminReportRecord, tmpAdminReportRecord)
		}

		for i := 0; i < len(tmpGroupAllNumMap); i++ {
			tmpGroupTryOutIdMap[i] = strings.Join(tmpGroupTryOutIdSliceMap[i], ",")
			tmpGroupFormalIdMap[i] = strings.Join(tmpGroupFormalIdSliceMap[i], ",")
			tmpGroupAllIdMap[i] = strings.Join(tmpGroupAllIdSliceMap[i], ",")

			tmpGroupCompanyReportRecordNum := statistic_report.SellerYbLogRecordNum{
				TryOutNum: tmpGroupTryOutNumMap[i],
				TryOutIds: tmpGroupTryOutIdMap[i],
				FormalNum: tmpGroupFormalNumMap[i],
				FormalIds: tmpGroupFormalIdMap[i],
				AllNum:    tmpGroupAllNumMap[i],
				AllIds:    tmpGroupAllIdMap[i],
				StartDate: startDateIndexList[i],
				EndDate:   endDateIndexList[i],
			}
			tmpGroupCompanyReportRecordNumList = append(tmpGroupCompanyReportRecordNumList, tmpGroupCompanyReportRecordNum)
		}
		groupReportRecord := statistic_report.GroupYbLogRecord{
			Item:                     tmpGroupAdminReportRecord,
			Name:                     v.GroupName,
			GroupId:                  v.GroupId,
			SellerYbLogRecordNumList: tmpGroupCompanyReportRecordNumList,
		}
		groupReportRecordList = append(groupReportRecordList, groupReportRecord)
	}

	//总体汇总数据
	for i := 0; i < len(tmpAllFormalNumMap); i++ {
		tmpGroupCompanyReportRecordNum := statistic_report.SellerYbLogRecordNum{
			TryOutNum: tmpAllTryOutNumMap[i],
			TryOutIds: "",
			FormalNum: tmpAllFormalNumMap[i],
			FormalIds: "",
			AllNum:    tmpAllAllNumMap[i],
			AllIds:    "",
			StartDate: startDateIndexList[i],
			EndDate:   endDateIndexList[i],
		}
		allCompanyReportRecordNum = append(allCompanyReportRecordNum, tmpGroupCompanyReportRecordNum)
	}
	companyReportRecordResp.SellerYbLogRecordNumList = allCompanyReportRecordNum
	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.GroupId != adminGroupId {
				tmpList[index].Item = make([]statistic_report.AdminYbLogRecord, 0)
			} else {
				tmpList[index], tmpList[0] = tmpList[0], tmpList[index]
			}
		}
		companyReportRecordResp.List = tmpList
	case utils.ROLE_TYPE_CODE_RAI_GROUP:
		for index, v := range tmpList {
			//如果不是同一个分组，那么就移除该分组下的人员数据
			if v.GroupId != adminGroupId {
				tmpList[index].Item = make([]statistic_report.AdminYbLogRecord, 0)
			} else {
				tmpList[index], tmpList[0] = tmpList[0], tmpList[index]
			}
		}
		companyReportRecordResp.List = tmpList
	case utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_RAI_ADMIN:
		//管理员、超管看全部数据
		companyReportRecordResp.List = tmpList
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = companyReportRecordResp
}

// SellerChartCollectUserLog
// @Title 图收藏统计用户详情
// @Description 图收藏统计用户详情
// @Param	request	body statisticModels.SellerYbUserLogReq true "type json string"
// @Success 200 {object} statistic_report.CompanyListResp
// @router /report/seller_chart_collect_log/user [post]
func (s *StatisticYbLogController) SellerChartCollectUserLog() {
	br := new(models.BaseResponse).Init()
	defer func() {
		s.Data["json"] = br
		s.ServeJSON()
	}()
	sysUser := s.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req yb.SellerChartCollectUserLogReq
	err := json.Unmarshal(s.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	//todo 校验入参
	if req.SellerIds == "" {
		br.Msg = "请输入销售ID"
		return
	}

	if req.StartDate == "" {
		br.Msg = "请输入开始时间"
		return
	}

	if req.EndDate == "" {
		br.Msg = "请输入截止时间"
		return
	}

	if req.CompanyStatus == "" {
		br.Msg = "请输入产品状态"
		return
	}

	resp, err, errMsg := statistic_report.YbChartCollectUserList(sysUser, req)
	if err != nil {
		br.Msg = err.Error()
		br.ErrMsg = errMsg
		return
	}

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

// CollectDetailList
// @Title 图收藏详情
// @Description 图收藏详情
// @Param	request	body statisticModels.SellerYbUserLogReq true "type json string"
// @Success 200 {object} statistic_report.CompanyListResp
// @router /report/seller_chart_collect_log/detail [post]
func (s *StatisticYbLogController) ChartCollectList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		s.Data["json"] = br
		s.ServeJSON()
	}()
	sysUser := s.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req yb.ChartCollectListReq
	err := json.Unmarshal(s.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	//todo 校验入参
	if req.UserId == 0 {
		br.Msg = "请输入联系人ID"
		return
	}

	if req.SellerIds == "" {
		br.Msg = "请输入销售ID"
		return
	}

	if req.StartDate == "" {
		br.Msg = "请输入开始时间"
		return
	}

	if req.EndDate == "" {
		br.Msg = "请输入截止时间"
		return
	}

	if req.CompanyStatus == "" {
		br.Msg = "请输入产品状态"
		return
	}

	resp, err, errMsg := statistic_report.YbChartCollectList(sysUser, req)
	if err != nil {
		br.Msg = err.Error()
		br.ErrMsg = errMsg
		return
	}

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