/**
   * 설문템플릿를 수정처리 한다.
   *
   * @param multiRequest
   * @param searchVO
   * @param commandMap
   * @param qustnrTmplatManageVO
   * @param bindingResult
   * @param model
   * @return "/uss/olp/qtm/EgovQustnrTmplatManageModifyActor"
   * @throws Exception
   */
  @RequestMapping(value = "/uss/olp/qtm/EgovQustnrTmplatManageModifyActor.do")
  public String QustnrTmplatManageModifyActor(
      final MultipartHttpServletRequest multiRequest,
      @ModelAttribute("searchVO") ComDefaultVO searchVO,
      Map commandMap,
      @ModelAttribute("qustnrTmplatManageVO") QustnrTmplatManageVO qustnrTmplatManageVO,
      BindingResult bindingResult,
      ModelMap model)
      throws Exception {

    // 0. Spring Security 사용자권한 처리
    Boolean isAuthenticated = EgovUserDetailsHelper.isAuthenticated();
    if (!isAuthenticated) {
      model.addAttribute("message", egovMessageSource.getMessage("fail.common.login"));
      return "uat/uia/EgovLoginUsr";
    }

    // 로그인 객체 선언
    LoginVO loginVO = (LoginVO) EgovUserDetailsHelper.getAuthenticatedUser();

    // 서버  validate 체크
    beanValidator.validate(qustnrTmplatManageVO, bindingResult);
    if (bindingResult.hasErrors()) {
      model.addAttribute(
          "resultList",
          egovQustnrTmplatManageService.selectQustnrTmplatManageDetail(qustnrTmplatManageVO));
      return "/uss/olp/qtm/EgovQustnrTmplatManageModify";
    }

    // 아이디 설정
    qustnrTmplatManageVO.setFrstRegisterId((String) loginVO.getUniqId());
    qustnrTmplatManageVO.setLastUpdusrId((String) loginVO.getUniqId());

    final Map<String, MultipartFile> files = multiRequest.getFileMap();

    if (!files.isEmpty()) {
      for (MultipartFile file : files.values()) {
        System.out.println("getName =>" + file.getName());
        System.out.println("getOriginalFilename =>" + file.getOriginalFilename());
        // log.info("getOriginalFilename =>" + file.getOriginalFilename() );
        if (file.getName().equals("qestnrTmplatImage") && !file.getOriginalFilename().equals("")) {
          qustnrTmplatManageVO.setQestnrTmplatImagepathnm(file.getBytes());
        }
      }
    }
    egovQustnrTmplatManageService.updateQustnrTmplatManage(qustnrTmplatManageVO);

    return "redirect:/uss/olp/qtm/EgovQustnrTmplatManageList.do";
  }
  /**
   * 설문템플릿 목록을 조회한다.
   *
   * @param searchVO
   * @param commandMap
   * @param qustnrTmplatManageVO
   * @param model
   * @return "/uss/olp/qtm/EgovQustnrTmplatManageList"
   * @throws Exception
   */
  @RequestMapping(value = "/uss/olp/qtm/EgovQustnrTmplatManageList.do")
  public String EgovQustnrTmplatManageList(
      @ModelAttribute("searchVO") ComDefaultVO searchVO,
      Map commandMap,
      QustnrTmplatManageVO qustnrTmplatManageVO,
      ModelMap model)
      throws Exception {

    String sCmd = commandMap.get("cmd") == null ? "" : (String) commandMap.get("cmd");

    if (sCmd.equals("del")) {
      egovQustnrTmplatManageService.deleteQustnrTmplatManage(qustnrTmplatManageVO);
    }

    /** EgovPropertyService.sample */
    searchVO.setPageUnit(propertiesService.getInt("pageUnit"));
    searchVO.setPageSize(propertiesService.getInt("pageSize"));

    /** pageing */
    PaginationInfo paginationInfo = new PaginationInfo();
    paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
    paginationInfo.setRecordCountPerPage(searchVO.getPageUnit());
    paginationInfo.setPageSize(searchVO.getPageSize());

    searchVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
    searchVO.setLastIndex(paginationInfo.getLastRecordIndex());
    searchVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());

    // List sampleList = egovQustnrTmplatManageService.selectQustnrTmplatManageList(searchVO);
    model.addAttribute(
        "resultList", egovQustnrTmplatManageService.selectQustnrTmplatManageList(searchVO));

    model.addAttribute(
        "searchKeyword",
        commandMap.get("searchKeyword") == null ? "" : (String) commandMap.get("searchKeyword"));
    model.addAttribute(
        "searchCondition",
        commandMap.get("searchCondition") == null
            ? ""
            : (String) commandMap.get("searchCondition"));

    int totCnt = (Integer) egovQustnrTmplatManageService.selectQustnrTmplatManageListCnt(searchVO);
    paginationInfo.setTotalRecordCount(totCnt);
    model.addAttribute("paginationInfo", paginationInfo);

    return "/uss/olp/qtm/EgovQustnrTmplatManageList";
  }
  /**
   * 설문템플릿 목록을 상세조회 조회한다.
   *
   * @param searchVO
   * @param qustnrTmplatManageVO
   * @param commandMap
   * @param model
   * @return "/uss/olp/qtm/EgovQustnrTmplatManageDetail"
   * @throws Exception
   */
  @RequestMapping(value = "/uss/olp/qtm/EgovQustnrTmplatManageDetail.do")
  public String EgovQustnrTmplatManageDetail(
      @ModelAttribute("searchVO") ComDefaultVO searchVO,
      QustnrTmplatManageVO qustnrTmplatManageVO,
      Map commandMap,
      ModelMap model)
      throws Exception {

    String sLocationUrl = "/uss/olp/qtm/EgovQustnrTmplatManageDetail";

    String sCmd = commandMap.get("cmd") == null ? "" : (String) commandMap.get("cmd");

    if (sCmd.equals("del")) {
      egovQustnrTmplatManageService.deleteQustnrTmplatManage(qustnrTmplatManageVO);
      sLocationUrl = "redirect:/uss/olp/qtm/EgovQustnrTmplatManageList.do";
    } else {
      model.addAttribute(
          "resultList",
          egovQustnrTmplatManageService.selectQustnrTmplatManageDetail(qustnrTmplatManageVO));
    }

    return sLocationUrl;
  }
  /**
   * 설문템플릿를 수정한다.
   *
   * @param searchVO
   * @param commandMap
   * @param qustnrTmplatManageVO
   * @param model
   * @return "/uss/olp/qtm/EgovQustnrTmplatManageModify"
   * @throws Exception
   */
  @RequestMapping(value = "/uss/olp/qtm/EgovQustnrTmplatManageModify.do")
  public String QustnrTmplatManageModify(
      @ModelAttribute("searchVO") ComDefaultVO searchVO,
      Map commandMap,
      QustnrTmplatManageVO qustnrTmplatManageVO,
      ModelMap model)
      throws Exception {
    String sLocationUrl = "/uss/olp/qtm/EgovQustnrTmplatManageModify";

    String sCmd = commandMap.get("cmd") == null ? "" : (String) commandMap.get("cmd");

    // List sampleList =
    // egovQustnrTmplatManageService.selectQustnrTmplatManageDetail(qustnrTmplatManageVO);
    model.addAttribute(
        "resultList",
        egovQustnrTmplatManageService.selectQustnrTmplatManageDetail(qustnrTmplatManageVO));

    return sLocationUrl;
  }
  /**
   * 설문템플릿를 등록 처리 한다. / 등록처리
   *
   * @param multiRequest
   * @param searchVO
   * @param qustnrTmplatManageVO
   * @param model
   * @return "/uss/olp/qtm/EgovQustnrTmplatManageRegistActor"
   * @throws Exception
   */
  @RequestMapping(value = "/uss/olp/qtm/EgovQustnrTmplatManageRegistActor.do")
  public String QustnrTmplatManageRegistActor(
      final MultipartHttpServletRequest multiRequest,
      @ModelAttribute("searchVO") ComDefaultVO searchVO,
      QustnrTmplatManageVO qustnrTmplatManageVO,
      ModelMap model)
      throws Exception {
    // 0. Spring Security 사용자권한 처리
    Boolean isAuthenticated = EgovUserDetailsHelper.isAuthenticated();
    if (!isAuthenticated) {
      model.addAttribute("message", egovMessageSource.getMessage("fail.common.login"));
      return "uat/uia/EgovLoginUsr";
    }

    // 로그인 객체 선언
    LoginVO loginVO = (LoginVO) EgovUserDetailsHelper.getAuthenticatedUser();

    // 아이디 설정
    qustnrTmplatManageVO.setFrstRegisterId((String) loginVO.getUniqId());
    qustnrTmplatManageVO.setLastUpdusrId((String) loginVO.getUniqId());

    final Map<String, MultipartFile> files = multiRequest.getFileMap();

    if (!files.isEmpty()) {
      for (MultipartFile file : files.values()) {
        // log.info("getName =>" + file.getName() );
        // log.info("getOriginalFilename =>" + file.getOriginalFilename() );
        if (file.getName().equals("qestnrTmplatImage")) {
          qustnrTmplatManageVO.setQestnrTmplatImagepathnm(file.getBytes());
        }
      }
    }

    // log.info("qestnrTmplatImagepathnm =>" + qustnrTmplatManageVO.getQestnrTmplatImagepathnm() );

    egovQustnrTmplatManageService.insertQustnrTmplatManage(qustnrTmplatManageVO);

    return "redirect:/uss/olp/qtm/EgovQustnrTmplatManageList.do";
  }
  /**
   * 설문템플릿 목록을 상세조회 조회한다.
   *
   * @param request
   * @param response
   * @param qustnrTmplatManageVO
   * @param commandMap
   * @return "/uss/olp/qtm/EgovQustnrTmplatManageImg"
   * @throws Exception
   */
  @RequestMapping(value = "/uss/olp/qtm/EgovQustnrTmplatManageImg.do")
  public void EgovQustnrTmplatManageImg(
      HttpServletRequest request,
      HttpServletResponse response,
      QustnrTmplatManageVO qustnrTmplatManageVO,
      Map commandMap)
      throws Exception {
    Map mapResult =
        egovQustnrTmplatManageService.selectQustnrTmplatManageTmplatImagepathnm(
            qustnrTmplatManageVO);
    byte[] img = (byte[]) mapResult.get("QUSTNR_TMPLAT_IMAGE_INFOPATHNM");
    String imgtype = "jpeg";
    String type = "";

    if (imgtype != null && !"".equals(imgtype)) {
      type = "image/" + imgtype;
    }

    response.setHeader("Content-Type", imgtype);
    response.setHeader("Content-Length", "" + img.length);
    response.getOutputStream().write(img);
    response.getOutputStream().flush();
    response.getOutputStream().close();
  }