package com.madeu.service.web.webstockclosedetail.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.webstockclose.WebStockCloseSqlMapDAO;
import com.madeu.dao.web.webstockclosedetail.WebStockCloseDetailSqlMapDAO;
import com.madeu.dao.web.webstockhistory.WebStockHistorySqlMapDAO;
import com.madeu.service.web.webstockclosedetail.WebStockCloseDetailService;
import com.madeu.util.ValidationCheckUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("WebStockCloseDetailService")
public class WebStockCloseDetailServiceImpl implements WebStockCloseDetailService {
	
	@Autowired
	private WebMemberSqlMapDAO webMemberSqlMapDAO;
	
	@Autowired
	private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;
	
	@Autowired
	private WebStockCloseDetailSqlMapDAO webStockCloseDetailSqlMapDAO;
	
	/**
	 * 재고마감 상세 리스트 조회 화면으로 이동.
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListStockCloseDetailIntro(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebStockCloseDetailServiceImpl selectListStockCloseDetailIntro START");

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

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		try{
			boolean check  = true;
			
			String menuClass    = String.valueOf(paramMap.get("menuClass"));
			
			// 정렬
			if(true != ValidationCheckUtil.emptyCheck(menuClass)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","메뉴 정보가 없습니다.");
			}

			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){
				// 권한 조회
				HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
				authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
				authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
				List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
				int authMenuRelationlistMapSize = authMenuRelationlistMap.size();
				
				if(1==authMenuRelationlistMapSize){
					map.put("msgCode", Constants.OK);
					map.put("success","true");
					
					map.put("selectUseYn", authMenuRelationlistMap.get(0).get("selectUseYn"));
					map.put("insertUseYn", authMenuRelationlistMap.get(0).get("insertUseYn"));
					map.put("updateUseYn", authMenuRelationlistMap.get(0).get("updateUseYn"));
					map.put("deleteUseYn", authMenuRelationlistMap.get(0).get("deleteUseYn"));
					map.put("downloadUseYn", authMenuRelationlistMap.get(0).get("downloadUseYn"));
				}
				else {
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "권한 정보가 없습니다.");
				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}

		log.debug("WebStockCloseDetailServiceImpl selectListStockCloseDetailIntro END");
		return map;
	}
	/**
	 * 현재고 품목수, 품목별재고수, 거래처수, 재고총액 조회 (현재고 상단)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectTotalStockCloseDetail(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebStockSumServiceImpl selectTotalStockCloseDetail START");

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

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

		log.debug("menuClass           :"+paramMap.get("menuClass"));

		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){
				List<Map<String, Object>> listMap = webStockCloseDetailSqlMapDAO.selectTotalStockCloseDetail(paramMap);

				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}

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

		log.debug("WebStockSumServiceImpl selectTotalStockCloseDetail END");
		return map;
	}
	/**
	 * 재고마감 리스트 조회
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListStockCloseDetail(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebStockCloseDetailServiceImpl selectListStockCloseDetail START");

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

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		log.debug("stockCloseDetailSearchKeywordParam  :"+paramMap.get("stockCloseDetailSearchKeywordParam"));
		log.debug("stockCloseDetailSort                :"+paramMap.get("stockCloseDetailSort"));
		log.debug("stockCloseDetailStart               :"+paramMap.get("stockCloseDetailStart"));
		log.debug("stockCloseDetailLimit               :"+paramMap.get("stockCloseDetailLimit"));
		log.debug("stockCloseDetailSort                :"+paramMap.get("stockCloseDetailSort"));
		log.debug("stockCloseDetailDir                 :"+paramMap.get("stockCloseDetailDir"));
		log.debug("stockCloseDetailCurrentPage         :"+paramMap.get("stockCloseDetailCurrentPage"));
		
		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("stockCloseDetailDir")||("").equals(paramMap.get("stockCloseDetailDir"))){
					
				}
				else{
					String dir = String.valueOf(paramMap.get("dir"));
					if(("A").equals(dir)){
						paramMap.put("stockCloseDetailDir", "DESC");
					}
					else if(("B").equals(dir)){
						paramMap.put("stockCloseDetailDir", "ASC");
					}
					else {
						paramMap.put("stockCloseDetailDir", "DESC");
					}
				}
				
				paramMap.put("useYn", "Y");
				
				List<Map<String, Object>> totalCountListMap = webStockCloseDetailSqlMapDAO.selectTotalStockCloseDetailCount(paramMap);
				int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));
				
				if(0 < totalCount) {
					Map<String, Object> dateMap = webStockCloseDetailSqlMapDAO.selectDate(paramMap);
					paramMap.put("YEAR", dateMap.get("YEAR"));
					paramMap.put("MONTH", dateMap.get("MONTH"));
					paramMap.put("YEAR_MONTH", dateMap.get("YEAR") + "-" +dateMap.get("MONTH"));
					String year = "";
					String month = "";
					int r_year = Integer.parseInt(dateMap.get("YEAR").toString()) - 1;
					int r_month = Integer.parseInt(dateMap.get("MONTH").toString()) - 1;
					if(r_month == 0) {
						r_month = 12;
					}else {
						r_year += 1; 
					}
					if(r_month < 10) {
						month = "0" + r_month;
						year = r_year + "";
					}else {
						month = r_month + "";
						year = r_year + "";
					}
					paramMap.put("R_YEAR", year);
					paramMap.put("R_MONTH", month);
					paramMap.put("R_YEAR_MONTH", year + "-" + month);
					
					// 리스트 조회
					listMap = webStockCloseDetailSqlMapDAO.selectListStockCloseDetail(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("WebStockCloseDetailServiceImpl selectListStockCloseDetail END");
		return map;
	}
	
	/**
	 * 재고마감 리스트 조회 (Excel)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListStockCloseDetailExcel(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("WebStockCloseDetailServiceImpl selectListStockCloseDetailExcel START");

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

		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		log.debug("menuClass           :"+paramMap.get("menuClass"));
		
		log.debug("stockCloseDetailSearchKeywordParam  :"+paramMap.get("stockCloseDetailSearchKeywordParam"));
		log.debug("stockCloseDetailSort                :"+paramMap.get("stockCloseDetailSort"));
		log.debug("stockCloseDetailStart               :"+paramMap.get("stockCloseDetailStart"));
		log.debug("stockCloseDetailLimit               :"+paramMap.get("stockCloseDetailLimit"));
		log.debug("stockCloseDetailSort                :"+paramMap.get("stockCloseDetailSort"));
		log.debug("stockCloseDetailDir                 :"+paramMap.get("stockCloseDetailDir"));
		log.debug("stockCloseDetailCurrentPage         :"+paramMap.get("stockCloseDetailCurrentPage"));
		
		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("stockCloseDetailDir")||("").equals(paramMap.get("stockCloseDetailDir"))){
					
				}
				else{
					String dir = String.valueOf(paramMap.get("dir"));
					if(("A").equals(dir)){
						paramMap.put("stockCloseDetailDir", "DESC");
					}
					else if(("B").equals(dir)){
						paramMap.put("stockCloseDetailDir", "ASC");
					}
					else {
						paramMap.put("stockCloseDetailDir", "DESC");
					}
				}
				
				paramMap.put("useYn", "Y");
				Map<String, Object> dateMap = webStockCloseDetailSqlMapDAO.selectDate(paramMap);
				paramMap.put("YEAR", dateMap.get("YEAR"));
				paramMap.put("MONTH", dateMap.get("MONTH"));
				paramMap.put("YEAR_MONTH", dateMap.get("YEAR") + "-" +dateMap.get("MONTH"));
				String year = "";
				String month = "";
				int r_year = Integer.parseInt(dateMap.get("YEAR").toString()) - 1;
				int r_month = Integer.parseInt(dateMap.get("MONTH").toString()) - 1;
				if(r_month == 0) {
					r_month = 12;
				}else {
					r_year += 1; 
				}
				if(r_month < 10) {
					month = "0" + r_month;
					year = r_year + "";
				}else {
					month = r_month + "";
					year = r_year + "";
				}
				paramMap.put("R_YEAR", year);
				paramMap.put("R_MONTH", month);
				paramMap.put("R_YEAR_MONTH", year + "-" + month);
				List<Map<String, Object>> listMap = webStockCloseDetailSqlMapDAO.selectListStockCloseDetailExcel(paramMap);
				
				map.put("year", dateMap.get("YEAR"));
				map.put("month", dateMap.get("MONTH"));
				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}

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

		log.debug("WebStockCloseDetailServiceImpl selectListStockCloseDetailExcel END");
		return map;
	}
}
