package com.madeu.controller.web.webstatistics;

import com.madeu.constants.Constants;
import com.madeu.init.ManagerDraftAction;
import com.madeu.service.web.webloghistory.WebLogHistoryService;
import com.madeu.service.web.webstatistics.WebStatisticsService;
import com.madeu.util.HttpUtil;
import com.madeu.view.web.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class WebStatisticsController extends ManagerDraftAction {

    @Autowired
    private WebStatisticsService webStatisticsService;

    @Autowired
    private WebLogHistoryService webLogHistoryService;

    @Autowired
    ResourceLoader resourceLoader;

    /**
     * 통계 - 대시보드 화면으로 이동.
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/selectStatisticsIntro.do")
    public String selectStatisticsIntro(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("WebStatisticsController  selectStatisticsIntro START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return "/web/login/logout";
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectStatisticsIntro(paramMap);

                model.addAttribute("selectUseYn", map.get("selectUseYn"));
                model.addAttribute("insertUseYn", map.get("insertUseYn"));
                model.addAttribute("updateUseYn", map.get("updateUseYn"));
                model.addAttribute("deleteUseYn", map.get("deleteUseYn"));
                model.addAttribute("downloadUseYn", map.get("downloadUseYn"));
                model.addAttribute("muMemberId", paramMap.get("loginMemberId"));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return "/web/login/logout";
        }
        log.debug("WebStatisticsController  selectStatisticsIntro END");
        return "/web/statistics/statistics";
    }
    /**
     * 대시보드 매출 조회
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/webstatistics/selectStatistics.do")
    public ModelAndView selectStatistics(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        log.debug("WebStatisticsController selectStatistics START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try{
            if(!webCheckLogin(session)){
                return null;
            }
            else{
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectStatistics(paramMap);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("WebStatisticsController selectStatistics END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
    /**
     * 통계 - 매출통계 화면으로 이동.
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/selectPaymentStatisticsIntro.do")
    public String selectPaymentStatisticsIntro(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("WebStatisticsController  selectPaymentStatisticsIntro START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return "/web/login/logout";
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectPaymentStatisticsIntro(paramMap);

                model.addAttribute("selectUseYn", map.get("selectUseYn"));
                model.addAttribute("insertUseYn", map.get("insertUseYn"));
                model.addAttribute("updateUseYn", map.get("updateUseYn"));
                model.addAttribute("deleteUseYn", map.get("deleteUseYn"));
                model.addAttribute("downloadUseYn", map.get("downloadUseYn"));
                model.addAttribute("muMemberId", paramMap.get("loginMemberId"));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return "/web/login/logout";
        }
        log.debug("WebStatisticsController  selectPaymentStatisticsIntro END");
        return "/web/statistics/statisticsPayment";
    }
    /**
     * 결제통계 조회
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/webstatistics/selectStatisticsPayment.do")
    public ModelAndView selectStatisticsPayment(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        log.debug("WebStatisticsController selectStatisticsPayment START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try{
            if(!webCheckLogin(session)){
                return null;
            }
            else{
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectStatisticsPayment(paramMap);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("WebStatisticsController selectStatisticsPayment END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

    /**
     * 결제통계 상세내역 엑셀 다운로드
     * 페이지 처리 없음
     * request:
     * response : msgCode, msgDesc
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/downloadStatisticsPaymentExcel.do")
    public ModelAndView downloadStatisticsPaymentExcel(HttpSession session, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file, Model model) {
        log.debug("WebStatisticsController downloadStatisticsPaymentExcel START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        List<Map<String, Object>> selectListReport = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return null;
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                selectListReport = webStatisticsService.selectListStatisticsPaymentExcel(paramMap);
                map.put("selectListStatisticsPayment", selectListReport);
                map.put("name", "statisticsPayment");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        log.debug("WebStatisticsController downloadStatisticsPaymentExcel END");
        return new ModelAndView(new WebStatisticsPaymentView(), "map", map);
    }

    /**
     * 통계 - 진료통계 화면으로 이동.
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/selectClinicStatisticsIntro.do")
    public String selectMedicalStatisticsIntro(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("WebStatisticsController  selectClinicStatisticsIntro START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return "/web/login/logout";
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectClinicStatisticsIntro(paramMap);

                model.addAttribute("selectUseYn", map.get("selectUseYn"));
                model.addAttribute("insertUseYn", map.get("insertUseYn"));
                model.addAttribute("updateUseYn", map.get("updateUseYn"));
                model.addAttribute("deleteUseYn", map.get("deleteUseYn"));
                model.addAttribute("downloadUseYn", map.get("downloadUseYn"));
                model.addAttribute("muMemberId", paramMap.get("loginMemberId"));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return "/web/login/logout";
        }
        log.debug("WebStatisticsController  selectClinicStatisticsIntro END");
        return "/web/statistics/statisticsClinic";
    }
    /**
     * 진료통계 조회
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/webstatistics/selectStatisticsClinic.do")
    public ModelAndView selectStatisticsClinic(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        log.debug("WebStatisticsController selectStatisticsClinic START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try{
            if(!webCheckLogin(session)){
                return null;
            }
            else{
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectStatisticsClinic(paramMap);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("WebStatisticsController selectStatisticsClinic END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

    /**
     * 진료통계 상세내역 엑셀 다운로드
     * 페이지 처리 없음
     * request:
     * response : msgCode, msgDesc
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/downloadStatisticsClinicExcel.do")
    public ModelAndView downloadStatisticsClinicExcel(HttpSession session, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file, Model model) {
        log.debug("WebStatisticsController downloadStatisticsClinicExcel START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        List<Map<String, Object>> selectListReport = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return null;
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                selectListReport = webStatisticsService.downloadStatisticsClinicExcel(paramMap);
                map.put("selectListStatisticsClinic", selectListReport);
                map.put("name", "statisticsClinic");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        log.debug("WebStatisticsController downloadStatisticsClinicExcel END");
        return new ModelAndView(new WebStatisticsClinicView(), "map", map);
    }

    /**
     * 통계 - 고객통계 화면으로 이동.
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/selectUserStatisticsIntro.do")
    public String selectUserStatisticsIntro(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("WebStatisticsController  selectUserStatisticsIntro START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return "/web/login/logout";
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectUserStatisticsIntro(paramMap);

                model.addAttribute("selectUseYn", map.get("selectUseYn"));
                model.addAttribute("insertUseYn", map.get("insertUseYn"));
                model.addAttribute("updateUseYn", map.get("updateUseYn"));
                model.addAttribute("deleteUseYn", map.get("deleteUseYn"));
                model.addAttribute("downloadUseYn", map.get("downloadUseYn"));
                model.addAttribute("muMemberId", paramMap.get("loginMemberId"));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return "/web/login/logout";
        }
        log.debug("WebStatisticsController  selectUserStatisticsIntro END");
        return "/web/statistics/statisticsUser";
    }
    /**
     * 고객통계 조회
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/webstatistics/selectStatisticsUser.do")
    public ModelAndView selectStatisticsUser(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        log.debug("WebStatisticsController selectStatisticsUser START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try{
            if(!webCheckLogin(session)){
                return null;
            }
            else{
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectStatisticsUser(paramMap);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("WebStatisticsController selectStatisticsUser END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

    /**
     * 고객통계 상세내역 엑셀 다운로드
     * 페이지 처리 없음
     * request:
     * response : msgCode, msgDesc
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/downloadStatisticsUserExcel.do")
    public ModelAndView downloadStatisticsUserExcel(HttpSession session, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file, Model model) {
        log.debug("WebStatisticsController downloadStatisticsUserExcel START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        List<Map<String, Object>> selectListReport = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return null;
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                selectListReport = webStatisticsService.downloadStatisticsUserExcel(paramMap);
                map.put("selectListStatisticsUser", selectListReport);
                map.put("name", "statisticsUser");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        log.debug("WebStatisticsController downloadStatisticsUserExcel END");
        return new ModelAndView(new WebStatisticsUserView(), "map", map);
    }

    /**
     * 통계 - 푸시통계 화면으로 이동.
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/selectPushStatisticsIntro.do")
    public String selectPushStatisticsIntro(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model model) {
        log.debug("WebStatisticsController  selectPushStatisticsIntro START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return "/web/login/logout";
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectPushStatisticsIntro(paramMap);

                model.addAttribute("selectUseYn", map.get("selectUseYn"));
                model.addAttribute("insertUseYn", map.get("insertUseYn"));
                model.addAttribute("updateUseYn", map.get("updateUseYn"));
                model.addAttribute("deleteUseYn", map.get("deleteUseYn"));
                model.addAttribute("downloadUseYn", map.get("downloadUseYn"));
                model.addAttribute("muMemberId", paramMap.get("loginMemberId"));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return "/web/login/logout";
        }
        log.debug("WebStatisticsController  selectPushStatisticsIntro END");
        return "/web/statistics/statisticsPush";
    }
    /**
     * 푸시통계 조회
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/webstatistics/selectStatisticsPush.do")
    public ModelAndView selectStatisticsPush(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        log.debug("WebStatisticsController selectStatisticsPush START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<String, Object>();

        try{
            if(!webCheckLogin(session)){
                return null;
            }
            else{
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                map = webStatisticsService.selectStatisticsPush(paramMap);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if(Constants.OK == map.get("msgCode")) {

            }
            else{
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if(null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("WebStatisticsController selectStatisticsPush END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

    /**
     * 푸시통계 상세내역 엑셀 다운로드
     * 페이지 처리 없음
     * request:
     * response : msgCode, msgDesc
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/webstatistics/downloadStatisticsPushExcel.do")
    public ModelAndView downloadStatisticsPushExcel(HttpSession session, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file, Model model) {
        log.debug("WebStatisticsController downloadStatisticsPushExcel START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        List<Map<String, Object>> selectListReport = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            if (!webCheckLogin(session)) {
                return null;
            } else {
                paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
                selectListReport = webStatisticsService.downloadStatisticsPushExcel(paramMap);
                map.put("selectListStatisticsPush", selectListReport);
                map.put("name", "statisticsPush");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        log.debug("WebStatisticsController downloadStatisticsPushExcel END");
        return new ModelAndView(new WebStatisticsPushView(), "map", map);
    }

}
