package com.crm.ucard.service.impl;


import com.crm.rely.backend.core.constant.CustomConstant;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.dao.repository.SysCustomBlackRepository;
import com.crm.ucard.service.SysCustomBlackService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysCustomBlackServiceImpl implements SysCustomBlackService {

    @Autowired
    SysCustomBlackRepository repository;

    @Override
    public boolean validateEmail(String email) throws ServiceException {
        return validateEmail(email, false);
    }

    @Override
    public boolean validateIdentity(String identity) throws ServiceException {
        return validateIdentity(identity, false);
    }

    @Override
    public boolean validateEmail(String email, boolean isException) throws ServiceException {
        if (Strings.isNullOrEmpty(email)) {
            return false;
        }
        boolean bl = repository.existsByEmailIsNotNullAndEmail(email);
        if (isException) {
            validate(bl);
        }
        return bl;
    }

    @Override
    public boolean validateIdentity(String identity, boolean isException) throws ServiceException {
        if (Strings.isNullOrEmpty(identity)) {
            return false;
        }
        boolean bl = repository.existsByIdentityIsNotNullAndIdentity(identity);
        if (isException) {
            validate(bl);
        }
        return bl;
    }

    private void validate(boolean bl) throws ServiceException {
        if (bl) {
            throw ServiceException.exception(CustomConstant.CUSTOM_BLACKLIST_ERROR);
        }
    }

}
