package com.icomsys.main_vm.biz.rcp.statistics.scenario;

import com.icomsys.main_vm.biz.common.common.service.ExcelService;
import com.icomsys.main_vm.biz.common.common.service.LogService;
import com.icomsys.main_vm.biz.common.common.service.LogVO;
import com.icomsys.main_vm.biz.rcp.statistics.scenario.req.StatisticsScenarioDayListReq;
import com.icomsys.main_vm.biz.rcp.statistics.scenario.service.StatisticsScenarioService;
import com.icomsys.main_vm.common.code.enumresource.ActionResource;
import com.icomsys.main_vm.common.code.enumresource.MenuResource;
import com.icomsys.main_vm.db.mybatis.alias.MonitoringOutscenarioVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/rcp/statistics/scenario")
public class StatisticsScenarioDayController {

    private final StatisticsScenarioService statisticsScenarioService;

    private final ExcelService excelService;

    private final LogService logService;

    @GetMapping("/day/manage.do")
    public String day(){
    return "/adm/rcp/statistics/scenario/day";
    }

    @RequestMapping(value = "/day/list.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<HashMap> statScenarioDayList(@RequestBody MonitoringOutscenarioVO vo) {
        List<HashMap> result = statisticsScenarioService.selectStatScenarioDay(vo);

        return result;
    }

    @ResponseBody
    @Transactional
    @RequestMapping(value = "/day/exceldownload.do")
    public void statScenarioDayExcelDownload(HttpServletResponse response, @ModelAttribute StatisticsScenarioDayListReq req) throws IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd/HH:mm:ss");

        String nowTime = sdf.format(System.currentTimeMillis());

        excelService.ExcelDownload("날짜별_시나리오_통계" + nowTime, req.getStatisticsScenarioDayVoList(), req.getTitleList());

        logService.LogSave(LogVO.builder()
                .menuAuth(MenuResource.valueOf(req.getMenuVal()))
                .actionCode(String.valueOf((ActionResource.download)))
                .serviceGroup(req.getOprMngCode())
                .remark("엑셀 다운로드")
                .build());
    }

}
