package com.caliverse.admin.logs.logservice.indicators;

import com.caliverse.admin.logs.Indicatordomain.AuMongoLog;
import com.caliverse.admin.logs.logservice.businesslogservice.BusinessLogAuByLangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.caliverse.admin.Indicators.Indicatordomain.IndicatorsLog;
import com.caliverse.admin.Indicators.entity.AuPerMinLogInfo;
import com.caliverse.admin.Indicators.indicatorrepository.IndicatorAuPerMinRepository;

import lombok.extern.slf4j.Slf4j;

import java.util.List;


@Slf4j
@Service
public class IndicatorsAuService extends IndicatorAuServiceBase {

    @Autowired private IndicatorAuPerMinRepository indicatorAuPerMinRepository;
    @Autowired private BusinessLogAuByLangService businessLogAuByLangService;

    public void collectActiveUser(String startTime, String endTime)
    {
        List<AuMongoLog> auByLanglogs = businessLogAuByLangService.loadBusinessLogData(startTime, endTime, AuMongoLog.class);

        for(AuMongoLog log : auByLanglogs) {
            String logMin = log.getLogMinute();

            AuPerMinLogInfo indicatorLog = new AuPerMinLogInfo(logMin, log.getLanguageType(), log.getUserGuidList(), log.getUserGuidListCount());

            saveStatLogData(logMin, indicatorLog);
        }

    }

    protected void saveStatLogData(String logMin, IndicatorsLog indicatorsLog) {

        
        if (indicatorsLog instanceof AuPerMinLogInfo) {
            AuPerMinLogInfo auLogInfo = (AuPerMinLogInfo) indicatorsLog;
            try {
                indicatorAuPerMinRepository.save(auLogInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.error("Not instanceof AuPerMinLogInfo");
        }
        
        
    }

    
}
