package activity

import (
	"hongze/hongze_yb/models/tables/yb_activity"
	"hongze/hongze_yb/models/tables/yb_activity_view_log"
	"time"
)

// GetUnRead 获取未读数
func GetUnRead( activityIds []int, userId uint64) (unRead int, err error) {
	firstDay := "2022-01-01"
	newActivityList, err := yb_activity.GetActivityByIDsAndDate(activityIds, firstDay)
	if err != nil {
		return
	}
	var newIds []int
	activityMap := make(map[int]bool)
	if len(newActivityList) > 0 {
		for _, v := range newActivityList {
			newIds = append(newIds, v.ActivityID)
			activityMap[v.ActivityID] = true
		}

		//获取用户的浏览记录
		viewActivityIds, tErr := yb_activity_view_log.GetByActivityIdsAndUserIdAndTime(newIds, userId, firstDay)
		if tErr != nil {
			err = tErr
			return
		}

		//过滤已经浏览过的活动ID
		if len(viewActivityIds) > 0 {
			for _, item := range viewActivityIds {
				if _, ok := activityMap[item]; ok {
					delete(activityMap, item)
				}
			}
		}

		unRead = len(activityMap)
	}
	return
}

// BatchInsertActivityView 批量新增报告已读记录
func BatchInsertActivityView(activityIds []int, userId uint64) (num int, err error)  {
	firstDay := "2022-01-01"
	newActivityList, err := yb_activity.GetActivityByIDsAndDate(activityIds, firstDay)
	if err != nil {
		return
	}
	var newIds []int
	activityMap := make(map[int]bool)
	if len(newActivityList) > 0 {
		for _, v := range newActivityList {
			newIds = append(newIds, v.ActivityID)
			activityMap[v.ActivityID] = true
		}

		//获取用户的浏览记录
		viewActivityIds, tErr := yb_activity_view_log.GetByActivityIdsAndUserIdAndTime(newIds, userId, firstDay)
		if tErr != nil {
			err = tErr
			return
		}

		//过滤已经浏览过的活动ID
		if len(viewActivityIds) > 0 {
			for _, item := range viewActivityIds {
				if _, ok := activityMap[item]; ok {
					delete(activityMap, item)
				}
			}
		}

		var logs []*yb_activity_view_log.YbActivityViewLog
		for k, _ := range activityMap{
			temp := new(yb_activity_view_log.YbActivityViewLog)
			temp.ActivityId = k
			temp.UserId = userId
			temp.CreateTime = time.Now()
			logs = append(logs, temp)
		}
		tErr = yb_activity_view_log.CreateInBatches(logs)
		if tErr != nil {
			err = tErr
			return
		}
		num = len(logs)
	}
	return
}



