package report

import (
	"fmt"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/models/tables/rddp/report"
	"hongze/hongze_yb/models/tables/rddp/report_view_log"
	"time"
)

// GetUnRead 获取未读数
func GetUnRead(classifyIdSeconds []int, userId uint64) (unReadItem map[string]int, err error) {
	firstDay := "2022-01-01"
	newReports, err := report.GetReportsByClassifyIdSecondsAndDate(classifyIdSeconds, firstDay)
	if err != nil {
		return
	}
	var newIds []int
	reportMap := make(map[int]*report.UnReadItem)
	if len(newReports) > 0 {
		for _, v := range newReports {
			newIds = append(newIds, v.Id)
			temp := new(report.UnReadItem)
			temp.ReportID = v.Id
			temp.ClassifyNameFirst = v.ClassifyNameFirst
			reportMap[v.Id] = temp
		}

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

		//过滤已经浏览过的报告ID
		if len(viewReportIds) > 0 {
			for _, item := range viewReportIds {
				if _, ok := reportMap[item]; ok {
					delete(reportMap, item)
				}
			}
		}

		unReadItem = make(map[string]int)
		for _, v := range reportMap {
			unReadItem[v.ClassifyNameFirst] += 1
		}
	}
	return
}

// BatchInsertReportView 批量新增报告已读记录
func BatchInsertReportView(userId uint64, weekReportIds []int, classifyNameFirst string, classifyIdSeconds []int) {
	var err error
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("Task BatchInsertReportView: userId=%d, err:%s, recover:%s", userId, err.Error(), recover()))
		}
	}()
	firstDay := "2022-01-01"
	var newReports []*report.Report
	pars := make([]interface{}, 0)

	var condition string
	// 特殊处理三种分类
	if classifyNameFirst == "大事点评" || classifyNameFirst == "会议纪要" || classifyNameFirst == "年报合集" {
		condition = "state IN (2, 6) and publish_time > ? "
		pars = append(pars, firstDay)
	} else {
		condition = "state IN (2, 6) and classify_name_first = ? and publish_time > ? "
		pars = append(pars, classifyNameFirst, firstDay)
	}

	if classifyNameFirst == "晨报" {
	} else if classifyNameFirst == "周报" {
		condition += " and id in (?)"
		pars = append(pars, weekReportIds)
	} else {
		condition += " and classify_id_second in (?)"
		pars = append(pars, classifyIdSeconds)
	}
	newReports, err = report.GetReportListByCondition(condition, pars)

	if err != nil {
		return
	}
	var newIds []int
	reportMap := make(map[int]int)
	if len(newReports) > 0 {
		for _, v := range newReports {
			newIds = append(newIds, v.Id)
			reportMap[v.Id] = v.Id
		}
		//获取用户的浏览记录
		viewReportIds, tErr := report_view_log.GetByReportIdsAndUserIdAndTime(newIds, userId, firstDay)
		if tErr != nil {
			err = tErr
			return
		}

		//过滤已经浏览过的报告ID
		if len(viewReportIds) > 0 {
			for _, item := range viewReportIds {
				if _, ok := reportMap[item]; ok {
					delete(reportMap, item)
				}
			}
		}

		var reportLogs []*report_view_log.ReportViewLog
		for _, v := range reportMap {
			temp := new(report_view_log.ReportViewLog)
			temp.ReportId = v
			temp.UserId = userId
			temp.CreateTime = time.Now()
			reportLogs = append(reportLogs, temp)
		}
		tErr = report_view_log.CreateInBatches(reportLogs)
		if tErr != nil {
			err = tErr
			return
		}
		//num := len(reportLogs)
	}
	return
}
