/** 
 * AdminServiceImpl.java
 * @author 임새미 
 * @since 2016. 10. 28.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 28. 	    임새미				       최초생성
 * 
 */
package kcg.imis.member.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import kcg.imis.member.mapper.MemberMapper;
import kcg.imis.member.vo.MemberSearchVO;
import kcg.imis.member.vo.MemberVO;
import kcg.imis.sec.LoginUserVO;

import org.springframework.stereotype.Service;

/** 
 * 사용자 정보 비지니스로직.
 * 
 * AdminServiceImpl.java
 * @author 임새미 
 * @since 2016. 10. 28.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 28. 	    임새미				       최초생성
 * 
 */
/**
 * @author kimnomin
 *
 */
@Service("memberService")
public class MemberServiceImpl implements MemberService {

	/**
	 * 사용자 Mapper 객체.
	 */
	@Resource(name = "memberMapper")
	private MemberMapper memberMapper;

	@Override
	public List<MemberVO> getMemberListAll(MemberSearchVO searchVO) throws Exception {
		return memberMapper.getMemberListAll(searchVO);
	}

	@Override
	public MemberVO getMemberInfo(String userid) throws Exception {
		return memberMapper.getMemberInfo(userid);
	}
	
		
	@Override
	public MemberVO getHeadMember() throws Exception {
		return memberMapper.getHeadMember();
	}
	
	@Override
	public MemberVO getSectionMember() throws Exception {
		return memberMapper.getSectionMember();
	}
	
	
	@Override
	public HashMap<String, Object> getMemberListUserVo(final MemberSearchVO searchVO) throws Exception {
		
		MemberVO headVO = getHeadMember();
		MemberVO sectionVO = getSectionMember();
		
		List<LoginUserVO> userList = memberMapper.getMemberListUserVO(searchVO);
		Iterator<LoginUserVO> it = userList.iterator();
		while (it.hasNext()) {
			LoginUserVO user = it.next();
			/* 담당관 */
			if (headVO != null) {
				String head = headVO.getUserid();
				user.setAuthHead(head != null && head.equals(user.getUserid()) ? 1 : 0);
			}
			/* 팀장 */
			if (sectionVO != null) {
				String section = sectionVO.getUserid();
				user.setAuthSection(section != null && section.equals(user.getUserid()) ? 1 : 0);
			}
			
		}
		
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("head", headVO);
		result.put("section", sectionVO);
		result.put("resultList", userList);
		
		return result;
	}
	
	@Override
	public List<MemberVO> getMemberList(MemberSearchVO searchVO) throws Exception {
		return memberMapper.getMemberList(searchVO);
	}
	
	@Override
	public int getMemberListCnt(final MemberSearchVO searchVO) throws Exception {
		return memberMapper.getMemberListCnt(searchVO);
	}

	@Override
	public int getMemberListAllCnt(final MemberSearchVO searchVO) throws Exception {
		return memberMapper.getMemberListAllCnt(searchVO);
	}
	@Override
	public int updateMemberAuth(MemberVO memberVO) throws Exception {
		return memberMapper.updateUsrLv(memberVO);
	}

	@Override
	public int updateMemberRpt(MemberVO memberVO) throws Exception {
		return memberMapper.updateRtpAuth(memberVO);
	}

	@Override
	public int updateSection(MemberVO memberVO) throws Exception {
		
		MemberVO section = memberMapper.getSectionMember();
		
		if(section != null){
			section.setUsegrade(6);
			memberMapper.updateUsrLv(section);
		}
		// 20180517 권한관련 팀장4, 담당관3 수정
		memberVO.setUsegrade(4);
		return memberMapper.updateUsrLv(memberVO);
	}

	@Override
	public int updateHead(MemberVO memberVO) throws Exception {
		
		MemberVO head = memberMapper.getHeadMember();
		if(head != null){
			head.setUsegrade(6);
			memberMapper.updateUsrLv(head);
		}
		// 20180517 권한관련 팀장4, 담당관3 수정
		memberVO.setUsegrade(3);
		return memberMapper.updateUsrLv(memberVO);
	}

	@Override
	public int insertUserInfo(MemberVO vo) throws Exception {
		return memberMapper.insertUserInfo(vo);
	}

	@Override
	public int selectCheckId(String userid) throws Exception {
		return memberMapper.selectCheckId(userid);
	}

	@Override
	public int updateUserInfo(MemberVO vo) throws Exception {
		return memberMapper.updateUserInfo(vo);
	}

	@Override
	public List<MemberVO> getMemberListForExel(MemberSearchVO searchVO) throws Exception {
		return memberMapper.getMemberListForExel(searchVO);
	}
		
}
