package user

import (
	logger "eta/eta_mini_ht_api/common/component/log"
	userDao "eta/eta_mini_ht_api/models/user"
)

type RecordCountDTO struct {
	UserId     int
	Mobile     string
	SourceId   int
	SourceType userDao.SourceType
	IpAddress  string
	Location   string
	Referer    string
	Additional string
}

func CountReport(record RecordCountDTO) (err error) {
	dao := convertUserToReportFlow(record)
	err = userDao.CountSourceClicks(dao)
	if err != nil {
		logger.Error("插入用户研报点击记录失败:%v", err)
		return
	}
	_ = GetUserAndCountReadTimes(record.UserId)
	return
}

func CountMedia(record RecordCountDTO) (err error) {
	dao := convertUserToMediaFlow(record)
	err = userDao.CountSourceClicks(dao)
	if err != nil {
		logger.Error("插入用户研报点击记录失败:%v", err)
		return
	}
	_ = GetUserAndCountReadTimes(record.UserId)
	return
}

type HotReportDTO struct {
	ReportId int `gorm:"column:report_id"`
	Count    int `gorm:"column:count"`
}

func GetHotReports(begin string, end string, limit int) (dtoList []HotReportDTO) {
	counts, err := userDao.GetTimeDurationReportCountsById(begin, end, limit, userDao.ReportSourceType)
	if err != nil {
		logger.Error("获取最热研报数据失败:%v", err)
		return
	}
	if counts == nil {
		return []HotReportDTO{}
	}
	for _, count := range counts {
		dtoList = append(dtoList, convertUserToHotReportDTO(count))
	}
	return
}
func convertUserToReportFlow(report RecordCountDTO) userDao.UserSourceClickFlow {
	return userDao.UserSourceClickFlow{
		UserID:         report.UserId,
		Mobile:         report.Mobile,
		SourceId:       report.SourceId,
		SourceType:     userDao.ReportSourceType,
		IPAddress:      report.IpAddress,
		Location:       report.Location,
		Referer:        report.Referer,
		AdditionalData: report.Additional,
	}
}
func convertUserToMediaFlow(media RecordCountDTO) userDao.UserSourceClickFlow {
	return userDao.UserSourceClickFlow{
		UserID:     media.UserId,
		Mobile:     media.Mobile,
		SourceId:   media.SourceId,
		SourceType: media.SourceType,
	}
}
func convertUserToHotReportDTO(flow userDao.CountClickFlowById) HotReportDTO {
	return HotReportDTO{
		ReportId: flow.SourceId,
		Count:    flow.Count,
	}
}
