package sgc.portal.com.auth.service;

import sgc.portal.com.auth.model.PrvtinfoFindHist;
import sgc.portal.com.auth.persistence.PrvtInfoFindHistMapper;
import sgc.portal.base.exception.BusinessException;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.security.PasswordEncryptionService;
import sgc.portal.com.sym.user.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.com.auth.web.MaskingDesabledController;

import java.util.List;

/**
 * <pre>
 * 시스템명   : Common
 * 프로그램ID :
 * 프로그램명  :
 * 설명 :
 * </pre>
 *
 * @author 이범근
 * @version 2020.01.13
 * @see MaskingDesabledController
 * @since JDK1.8
 */
@Service
@Transactional
public class MaskingDesabledService {

    @Autowired
    private LoginService loginService;

    @Autowired
    private PrvtInfoFindHistMapper prvtInfoFindHistMapper;


    /** 패스워드 암호화 */
    @Autowired
    private PasswordEncryptionService passwordEncryptionService;


    /**
     * Masking desabled check string.
     *
     * @param prvtinfoFindHist the prvtinfo find hist
     * @return the string
     */
    public String maskingDesabledCheck(PrvtinfoFindHist prvtinfoFindHist){

        prvtinfoFindHist.setScrtNo(loginService.decodeStr(prvtinfoFindHist.getScrtNo()));

        String encryptedPwd = passwordEncryptionService.encryptPasswordWithSalt(prvtinfoFindHist.getScrtNo());
        prvtinfoFindHist.setScrtNo(encryptedPwd);

        String maskingAuth = prvtInfoFindHistMapper.selectPrvtInfoDesabledYn(prvtinfoFindHist);

        if("S".equals(maskingAuth)){
            throw new BusinessException("인증사용자 정보가 잘못되었습니다.");
        }


        if("N".equals(maskingAuth)){
            throw new BusinessException("개인정보 조회 권한이 없습니다.");
        }

        prvtInfoFindHistMapper.insertPrvtinfoFindHist(prvtinfoFindHist);
        return "Y";

    }


    /**
     * 개인 정보 조회 이력
     *
     * @param prvtinfoFindHist 기본 모델 제공
     * @return 개인 정보 조회 이력 목록
     */
    public List<EasyHashMap<String, Object>> selectPrvtinfoFindHist(PrvtinfoFindHist prvtinfoFindHist){
        return prvtInfoFindHistMapper.selectPrvtinfoFindHist(prvtinfoFindHist);

    }

}
