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

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.call.req.StatisticsCallInfoListReq;
import com.icomsys.main_vm.biz.rcp.statistics.call.service.StatisticsCallInfoService;
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.StatisticsCallInfoVO;
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/call")
public class StatisticsCallInfoController {

    private final StatisticsCallInfoService statisticsCallInfoService;

    private final ExcelService excelService;

    private final LogService logService;

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

    @RequestMapping(value = "/list.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<HashMap> statCallInfoList(@RequestBody StatisticsCallInfoVO vo) {
        List<HashMap> result = statisticsCallInfoService.selectStatCallInfo(vo);

        return result;
    }

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

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

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

        excelService.ExcelDownload("콜통계" + nowTime, req.getStatisticsCallInfoExcelVOList(), req.getTitleList());

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

}
