package com.madeu.service.web.weblogin.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.madeu.constants.Constants;
import com.madeu.dao.web.weblogin.WebLoginSqlMapDAO;
import com.madeu.service.web.weblogin.WebLoginService;
import com.madeu.util.SecurityUtil;
import com.madeu.util.ValidationCheckUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("WebLoginService")
public class WebLoginServiceImpl implements WebLoginService{
	
	@Autowired
	private WebLoginSqlMapDAO webLoginSqlMapDAO;

	/**
	 * 로그인 
	 * 
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> memberLogin(
			HashMap<String, Object> paramMap,  HashMap<String, Object> visitLogParamMap) throws Exception {
		log.debug("WebLoginServiceImpl memberLogin START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		log.debug("loginId    :"+paramMap.get("loginId"));
		log.debug("password   :"+paramMap.get("password"));
		
		
		try{
			boolean check  = true;
			
			String loginId           = String.valueOf(paramMap.get("loginId"));
			String password          = String.valueOf(paramMap.get("password"));
			
			if(true != ValidationCheckUtil.emptyCheck(loginId)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc", "아이디 정보가 없습니다.");
			}
			
			if(true != ValidationCheckUtil.emptyCheck(password)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc", "비밀번호 정보가 없습니다.");
			}
			
			if(true == check){
				paramMap.put("password", SecurityUtil.encryptSHA256((String) paramMap.get("password")));
				List<Map<String, Object>> listMap = webLoginSqlMapDAO.memberLogin(paramMap);
				int listMapSize = listMap.size();
				if(null==listMap ||0 == listMapSize){
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
				}
				else if(1 < listMapSize){
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "중복된 사용자가 있습니다. 관리자에게 문의하시기 바랍니다.");
				}
				else{
					String muMemberId                = String.valueOf(listMap.get(0).get("muMemberId"));
					String name                      = String.valueOf(listMap.get(0).get("name"));
					String type                      = String.valueOf(listMap.get(0).get("type"));
					String status                    = String.valueOf(listMap.get(0).get("status"));

					if(("Y").equals(status)){
						map.put("msgCode", Constants.OK);
						map.put("muMemberId", muMemberId);
						map.put("loginId", loginId);
						map.put("memberName", name);
						map.put("memberType", type);
						
						paramMap.put("muMemberId", muMemberId);
						paramMap.put("modId", muMemberId);
						paramMap.put("visitIp", visitLogParamMap.get("visitIp"));
						// 최근 로그인 날짜 저장
						webLoginSqlMapDAO.updateLastLoginDate(paramMap);
					}
					else{
						map.put("msgCode", Constants.FAIL);
						map.put("msgDesc","서비스 제한 상태입니다. 관리자에게 문의하시기 바랍니다.");
					}
				}
			}
		}
		catch(Exception e){
			throw new Exception(e.getMessage());
		}
		log.debug("WebLoginServiceImpl memberLogin END");
		return map;
	}
	
	/**
	 * 로그인 아이디 중복체크 (아이디 중복확인)
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	public HashMap<String, Object> checkloginId(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebLoginServiceImpl checkloginId START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		log.debug("loginId        :"+paramMap.get("loginId"));
		log.debug("muMemberId     :"+paramMap.get("muMemberId"));

		try{
			List<Map<String, Object>> listMap = webLoginSqlMapDAO.checkloginId(paramMap);
			int listMapSize = listMap.size();
			log.debug("listMapSize    : "+listMapSize);
			
			if(listMapSize>=1) {
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","이미 사용중인 아이디입니다. 다른 아이디를 입력해주세요.");
			}
			else {
				map.put("msgCode", Constants.OK);
				map.put("success","true");
			}
		}
		catch(Exception e){
			throw new Exception(e.getMessage());
		}
		log.debug("WebLoginServiceImpl checkloginId END");
		return map;
	}
}
