package com.takensoft.cms.cntnStats.web;

import com.takensoft.cms.cntnStats.dto.CntnStatsDTO;
import com.takensoft.cms.cntnStats.dto.CntnStatsExcelDTO;
import com.takensoft.cms.cntnStats.service.CntnStatsService;
import com.takensoft.common.excel.service.ExcelService;
import com.takensoft.common.util.ResponseData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @author  : takensoft
 * @since   : 2024.05.23
 *
 * 접속 통계 관련 컨트롤러
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping(value = "/sys/cntnStats")
public class CntnStatsController {
    private final CntnStatsService cntnStatsService;
    private final ExcelService excelService;

    /**
     * @author takensoft
     * @since 2024.05.23
     * @param cntnStatsDTO
     * @return
     * @throws Exception
     *
     * 접속 통게 등록
     */
    @PostMapping("/saveProc.json")
    public ResponseEntity<?> saveProc(@RequestBody CntnStatsDTO cntnStatsDTO) throws Exception {
        int result = cntnStatsService.cntnStatsSave(cntnStatsDTO);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 등록 처리되었습니다.");
            responseData.setData(result);
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("등록에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author 박정하
     * @since 2024.05.29
     * @param
     * @return
     * @throws Exception
     *
     * 사용자 접속 통계
     */
    @PostMapping("/selectUserCntnStatsProc.json")
    public ResponseEntity<?> selectUserCntnStatsProc(@RequestBody HashMap<String, Object> params) throws Exception {
        // 사용자 접속 통계 조회
        HashMap<String, Object> result = cntnStatsService.userCntnStatsSelect(params);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author 박정하
     * @since 2024.05.30
     * @param
     * @return
     * @throws Exception
     *
     * 메뉴별 접속 통계
     */
    @PostMapping("/selectMenuCntnStatsProc.json")
    public ResponseEntity<?> selectMenuCntnStatsProc(@RequestBody HashMap<String, Object> params) throws Exception {
        // 메뉴별 접속 통계 조회
        HashMap<String, Object> result = cntnStatsService.menuCntnStatsSelect(params);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author 박정하
     * @since 2024.05.30
     * @param
     * @return
     * @throws Exception
     *
     * 게시판 접속 통계
     */
    @PostMapping("/selectBbsCntnStatsProc.json")
    public ResponseEntity<?> selectBbsCntnStatsProc(@RequestBody HashMap<String, Object> params) throws Exception {
        // 게시판 접속 통계 조회
        HashMap<String, Object> result = cntnStatsService.bbsCntnStatsSelect(params);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author 박정하
     * @since 2024.05.30
     * @param
     * @return
     * @throws Exception
     *
     * 접속 통계 엑셀 다운로드
     */
    @PostMapping("/excelDownload.file")
    public void excelDownload(HttpServletResponse response, @RequestPart List<CntnStatsExcelDTO> cntnStats, @RequestPart String chart) throws Exception {
        excelService.cntnStatsExcelDownload(response, cntnStats, chart);
    }
}