package com.tzy.service.impl;

import com.tzy.dto.QuizActivityDto;
import com.tzy.mapper.QuizActivityMapper;
import com.tzy.service.QuizActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class QuizActivityServiceImpl implements QuizActivityService {

    @Autowired
    QuizActivityMapper quizActivityMapper;
    @Override
    public QuizActivityDto getById(Long id) {
        return quizActivityMapper.queryQuizActivity(id);
    }

    @Override
    public long calculateRound(QuizActivityDto quizActivityDto) {
        int refreshCycle = quizActivityDto.getRefreshCycle();
        long millisecondsPerDay = 24 * 60 * 60 * 1000;
        //计算距离开始时间的天数
        Date now = new Date();
        long dayNum=(now.getTime()-quizActivityDto.getStartTime().getTime())/millisecondsPerDay;
        long overDayNum=(now.getTime()-quizActivityDto.getStartTime().getTime())%millisecondsPerDay;
        if (overDayNum>0)
            dayNum=dayNum+1;
        //计算答题轮次
        long round=dayNum/refreshCycle;
        long overRound=dayNum%refreshCycle;
        if (overRound>0)
            round=round+1;
        //计算开始时间到结束时间的总天数
        long totalTime = quizActivityDto.getEndTime().getTime() - quizActivityDto.getStartTime().getTime();
        long totalDay = totalTime / millisecondsPerDay;
        //总天数除以答题周期，计算总共有多少个答题轮次
        long totalRound = totalDay / refreshCycle;
        if (round>totalRound)
            return totalRound;
        return round;
    }
}
