package com.madeu.controller.web.webcompany;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.madeu.constants.Constants;
import com.madeu.common.service.LogHistoryService;
import com.madeu.service.web.webcompany.WebCompanyService;
import com.madeu.util.HttpUtil;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Controller
public class WebCompanyController{

	@Autowired
	private WebCompanyService webCompanyService;
	
	@Autowired
	private LogHistoryService logHistoryService;

	
	/**
	 * 거래처 리스트 조회 (option)
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/selectListCompanyOption.do")
	public ModelAndView selectListCompanyOption(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebCompanyController selectListCompanyOption START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.selectListCompanyOption(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("WebCompanyController selectListCompanyOption END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 거래처 정보 리스트 화면으로 이동.
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/selectListCompanyIntro.do")
	public String  selectListCompanyIntro(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model) {
		log.debug("WebCompanyController  selectListCompanyIntro START");

		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.selectListCompanyIntro(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"));
		}
		catch (Exception e) {
			e.printStackTrace();
			return "/web/login/logout";
		}
		log.debug("WebCompanyController  selectListCompanyIntro END");
		return "/web/company/companySelectList";
	}
	
	/**
	 * 거래처 정보 리스트 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/selectListCompany.do")
	public ModelAndView selectListCompany(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
			
		log.debug("WebCompanyController selectListCompany START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.selectListCompany(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("WebCompanyController selectListCompany END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**
	 * 거래처 정보 상세 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/selectCompany.do")
	public ModelAndView selectCompany(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
			
		log.debug("WebCompanyController selectCompany START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.selectCompany(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("WebCompanyController selectCompany END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	
	/**	
	 * 거래처 정보 등록
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/insertCompany.do")
	public ModelAndView insertCompany(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
				
		log.debug("WebCompanyController insertCompany START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();

		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
			paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.insertCompany(paramMap);
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		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","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}

						logHistoryService.save("거래처 등록", "거래처 등록", paramMap, map, String.valueOf(errorMsg));
		}
		log.debug("WebCompanyController insertCompany END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	/**
	 * 거래처 정보 수정 화면으로 이동.
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/updateCompanyIntro.do")
	public String  updateCompanyIntro(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model) {
			
		log.debug("WebCompanyController  updateCompanyIntro START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.selectCompanyIntro(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"));
		}
		catch (Exception e) {
			e.printStackTrace();
			return "/web/login/logout";
		}
		log.debug("WebCompanyController  updateCompanyIntro END");
		return "/web/company/companyUpdate";
	}

	/**
	 * 거래처 정보 수정
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/updateCompany.do")
	public ModelAndView updateCompany(HttpSession session,HttpServletRequest request, HttpServletResponse response, MultipartHttpServletRequest file) {
				
		log.debug("WebCompanyController updateCompany START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
			paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.updateCompany(paramMap);
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		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","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
			
						logHistoryService.save("거래처 수정", "거래처 수정", paramMap, map, String.valueOf(errorMsg));
		}
		log.debug("WebCompanyController updateCompany END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
	

	/**
	 * 거래처 정보 삭제
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/deleteCompany.do")
	public ModelAndView deleteCompany(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
				
		log.debug("WebCompanyController deleteCompany START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();

		StringBuffer errorMsg = new StringBuffer();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			paramMap.put("regId", String.valueOf(session.getAttribute("loginMemberId")));
			paramMap.put("modId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.deleteCompany(paramMap);
		}
		catch (Exception e) {
			e.printStackTrace();
			errorMsg.append(e);
		}
		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","정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
				}
			}
			
						logHistoryService.save("거래처 삭제", "거래처 삭제", paramMap, map, String.valueOf(errorMsg));
		}
		log.debug("WebCompanyController deleteCompany END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}

	
	/**
	 * 거래처 정보 및 제품 정보 상세 조회
	 * 
	 * @param request
	 * @param response 
	 * @return
	 */
	@RequestMapping(value="/webcompany/selectCompanyProduct.do")
	public ModelAndView selectCompanyProduct(HttpSession session,HttpServletRequest request, HttpServletResponse response) {
		log.debug("WebCompanyController selectCompanyProduct START");
		
		HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
			map = webCompanyService.selectCompanyProduct(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("WebCompanyController selectCompanyProduct END");
		return HttpUtil.makeHashToJsonModelAndView(map);
	}
}
