package com.tsiwebserver.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.tsiwebserver.mapper.HistoryMapper;
import com.tsiwebserver.pageService.PageClass;
import com.tsiwebserver.service.HistoryService;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Slf4j
public class HistoryServiceImpl implements HistoryService{

    private final HistoryMapper historyMapper;
   
 	// API접속일자별 조회
    public PageClass searchDateRecodeInvoke(String searchStartDate, String searchEndDate, String pageNo) {
        LocalDate start = LocalDate.parse(searchStartDate); 
        LocalDate end = LocalDate.parse(searchEndDate);

        Map<String, Object> list = new HashMap<String, Object>();  //Map은 인터페이스라 객체생성을 못한다 때문에 자식인 HashMap으로 객체를 생성한다, 값이 2개일경우 map으로 받아서 키값으로 넘겨야만 Mybatis - mapper.xml parameter값으로 사용가능
        list.put("start",start);
        list.put("end",end);
        
        if (pageNo == null) pageNo = "1";

        PageClass pc = new PageClass(Integer.parseInt(pageNo), 22, historyMapper.getSearchInvokeCnt(list));
        list.put("firstRow", pc.getFirstRow());
        list.put("countPerPage", pc.getPagePerCount());
        pc.setTablelist(historyMapper.searchDateRecodeInvoke(list));
        return pc;         
    }

    //API 사용자별 조회
    public PageClass searchCustomerInvoke(String userId, String pageNo) {
    	 Map<String, Object> list = new HashMap<String, Object>();

         if (pageNo == null) pageNo = "1";

         list.put("userId", userId);
         PageClass pc = new PageClass(Integer.parseInt(pageNo), 22, historyMapper.getSearchInvokeUserCnt(list));
         list.put("firstRow", pc.getFirstRow());
         list.put("countPerPage", pc.getPagePerCount());
         pc.setTablelist(historyMapper.searchCustomerInvoke(list));
         return pc;         
    }
    
    // 시스템리소스 날짜별조회
    public PageClass SystemResourceDateSearch(String searchStartDate, String searchEndDate, String pageNo) {
        LocalDateTime start = LocalDateTime.parse(searchStartDate + "T00:00:00");
        LocalDateTime end = LocalDateTime.parse(searchEndDate + "T23:59:59");

        Map<String, Object> list = new HashMap<String, Object>();
        list.put("start",start);
        list.put("end",end);

        if (pageNo == null) pageNo = "1";
        
        PageClass pc = new PageClass(Integer.parseInt(pageNo), 100, historyMapper.getSearchResourceDailyCnt(list));
        list.put("firstRow", pc.getFirstRow());
        list.put("countPerPage", pc.getPagePerCount());
        pc.setTablelist(historyMapper.SystemResourceDateSearch(list));
        return pc;         
    }
    
    //시스템 사용자별 조회
    public PageClass systemResourceUserSearch(String userId, String pageNo) {
    	Map<String, Object> list = new HashMap<String, Object>(); 
        
        if (pageNo == null) pageNo = "1";
        
        list.put("userId", userId);
        PageClass pc = new PageClass(Integer.parseInt(pageNo), 22, historyMapper.getSearchResourceUserCnt(list));
        list.put("firstRow", pc.getFirstRow());
        list.put("countPerPage", pc.getPagePerCount());
        pc.setTablelist(historyMapper.systemResourceUserSearch(list));
        return pc;   
    }

    public List<String> setOptionApiUser() {
		return historyMapper.setOptionApiUser();
	}
	
	public List<String> setOptionResourceUser() {
		return historyMapper.setOptionResourceUser();
	}

    
}
