package com.eactive.eai.rms.data.entity.onl.logger;

import com.eactive.eai.data.RollingTable;
import com.eactive.eai.data.entity.onl.logger.EAILog;
import com.eactive.eai.data.entity.onl.logger.EAILogId;
import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.data.entity.onl.apim.portalstatistics.PortalStatisticsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
@RequiredArgsConstructor
public class EAILogServiceImpl implements EAILogService {

    private final EAILogRepository eaiLogRepository;
    private final ApplicationContext applicationContext;
    private final EAILogDAOForEMS eaiLogDAOForEMS;

    @Override
    public Page<EAILogDto> findAll(Pageable pageable, EAILogSearch eaiLogSearch) {
        long time = CommonUtil.getCalendar(eaiLogSearch.getSearchStartTime()).getTimeInMillis();
        EAILog eaiLog = (EAILog) applicationContext.getBean(RollingTable.class, EAILog.class, time);
        Page<EAILogDto> eaiLogDtos = eaiLogRepository.findAll(eaiLog.getClass(), pageable, eaiLogSearch);
        return eaiLogDtos;
    }

    public Map<String, Long> getTransactionCounts(EAILogSearch eaiLogSearch){
        long time = CommonUtil.getCalendar(eaiLogSearch.getSearchStartTime()).getTimeInMillis();
        EAILog eaiLog = (EAILog) applicationContext.getBean(RollingTable.class, EAILog.class, time);
        Map<String, Long> detailedCounts = eaiLogRepository.countDetailedTransactions(eaiLog.getClass(), eaiLogSearch);
        return detailedCounts;
    }

    @Override
    public EAILogDto getById(EAILogId eaiLogId) {
        long time = CommonUtil.getCalendar(eaiLogId.getMsgdpstyms()).getTimeInMillis();
        EAILog eaiLog = (EAILog) applicationContext.getBean(RollingTable.class, EAILog.class, time);
        return eaiLogRepository.getById(eaiLog.getClass(), eaiLogId);
    }


    public List<PortalStatisticsVO> selectLogList(HashMap<String, Object> paramMap) {
        return eaiLogDAOForEMS.selectLogList(paramMap);
    }

}
