package com.eactive.eai.rms.onl.apim.portalstatistics;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Controller
public class PortalStatisticsApiTableManController {

    private final PortalStatisticsApiTableManService portalStatisticsApiTableManService;

    public PortalStatisticsApiTableManController(PortalStatisticsApiTableManService portalStatisticsApiTableManService) {
        this.portalStatisticsApiTableManService = portalStatisticsApiTableManService;
    }

    @GetMapping(value = "/onl/apim/portalstatistics/portalStatisticsApiTableMan.view")
    public void view() {
        // view
    }

    @PostMapping(value = "/onl/apim/portalstatistics/portalStatisticsApiTableMan.json", params = "cmd=LIST")
    public ResponseEntity<Map<String, Object>> selectStatisticsList(
            @RequestParam String startDate,
            @RequestParam String endDate) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate start = LocalDate.parse(startDate, formatter);
        LocalDate end = LocalDate.parse(endDate, formatter);

        Map<String, Object> result = portalStatisticsApiTableManService.selectStatisticsList(start, end);
        return ResponseEntity.ok(result);
    }
}