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

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.intent.req.IntentAnalysisItemListReq;
import com.icomsys.main_vm.biz.rcp.statistics.intent.service.StatisticsIntentService;
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.MonitoringIntentAnalysisVO;
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/intent")
public class StatisticsIntentItemController {

    private final StatisticsIntentService statisticsIntentService;

    private final ExcelService excelService;

    private final LogService logService;

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

    @RequestMapping(value = "/item/list.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<HashMap> statIntentItemList(@RequestBody MonitoringIntentAnalysisVO vo) {
        List<HashMap> result = statisticsIntentService.selectStatIntentItem(vo);

        return result;
    }

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

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

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

        excelService.ExcelDownload("항목별_인텐트_통계" + nowTime, req.getIntentAnalysisItemList(), req.getTitleList());

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

}
