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

import java.util.ArrayList;
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 com.madeu.constants.Constants;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.dao.web.webproduct.WebProductSqlMapDAO;
import com.madeu.service.web.webproduct.WebProductService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("WebProductService")
public class WebProductServiceImpl implements WebProductService {
	
	@Autowired
	private WebProductSqlMapDAO webProductSqlMapDAO;
	
	@Autowired
	private WebMemberSqlMapDAO webMemberSqlMapDAO;
	
	@Autowired
	private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;
	
	/**
	 * 제품(약품) 리스트 조회 (option)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListProductOption(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebProductServiceImpl selectListProductOption START");

		HashMap<String, Object> map = new HashMap<String, Object>();

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		log.debug("productSearchKeywordParam  :"+paramMap.get("productSearchKeywordParam"));
		log.debug("productSort                :"+paramMap.get("productSort"));
		log.debug("productSort                :"+paramMap.get("productSort"));
		log.debug("productDir                 :"+paramMap.get("productDir"));
		log.debug("productCurrentPage         :"+paramMap.get("productCurrentPage"));
		log.debug("muTreatmentId              :"+paramMap.get("muTreatmentId"));
		
		List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();

		try{
			boolean check  = true;
			
			if(true == check){
				// 사용자 확인
				List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
				int userListMapSize = userListMap.size();
				
				if(1==userListMapSize){
					paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
				}
				else {
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
				}
			}
			
			if(true == check){
				// 정렬 
				if(null==paramMap.get("productDir")||("").equals(paramMap.get("productDir"))){
					
				}
				else{
					String dir = String.valueOf(paramMap.get("dir"));
					if(("A").equals(dir)){
						paramMap.put("productDir", "DESC");
					}
					else if(("B").equals(dir)){
						paramMap.put("productDir", "ASC");
					}
					else {
						paramMap.put("productDir", "DESC");
					}
				}
				
				paramMap.put("useYn", "Y");
				
				List<Map<String, Object>> totalCountListMap = webProductSqlMapDAO.selectTotalProductOptionCount(paramMap);
				int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));
				
				if(0 < totalCount) {
					// 리스트 조회
					listMap = webProductSqlMapDAO.selectListProductOption(paramMap);
				}
				
				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("totalCount", totalCount);
				map.put("rows",listMap);
			}

		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}

		log.debug("WebProductServiceImpl selectListProductOption END");
		return map;
	}
	
	/**
	 * 제품(약품) 리스트 조회
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListProduct(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebProductServiceImpl selectListProduct START");

		HashMap<String, Object> map = new HashMap<String, Object>();

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		log.debug("productSearchKeywordParam  :"+paramMap.get("productSearchKeywordParam"));
		log.debug("productSort                :"+paramMap.get("productSort"));
		log.debug("productStart               :"+paramMap.get("productStart"));
		log.debug("productLimit               :"+paramMap.get("productLimit"));
		log.debug("productSort                :"+paramMap.get("productSort"));
		log.debug("productDir                 :"+paramMap.get("productDir"));
		log.debug("productCurrentPage         :"+paramMap.get("productCurrentPage"));
		
		List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();

		try{
			boolean check  = true;
			
			if(true == check){
				// 사용자 확인
				List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
				int userListMapSize = userListMap.size();
				
				if(1==userListMapSize){
					paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
				}
				else {
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
				}
			}
			
			if(true == check){
				// 정렬 
				if(null==paramMap.get("productDir")||("").equals(paramMap.get("productDir"))){
					
				}
				else{
					String dir = String.valueOf(paramMap.get("dir"));
					if(("A").equals(dir)){
						paramMap.put("productDir", "DESC");
					}
					else if(("B").equals(dir)){
						paramMap.put("productDir", "ASC");
					}
					else {
						paramMap.put("productDir", "DESC");
					}
				}
				
				paramMap.put("useYn", "Y");
				
				List<Map<String, Object>> totalCountListMap = webProductSqlMapDAO.selectTotalProductCount(paramMap);
				int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));
				
				if(0 < totalCount) {
					// 리스트 조회
					listMap = webProductSqlMapDAO.selectListProduct(paramMap);
				}
				
				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("totalCount", totalCount);
				map.put("rows",listMap);
			}

		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}

		log.debug("WebProductServiceImpl selectListProduct END");
		return map;
	}
	
}
