package com.crm.pay.service.impl;

import com.crm.pay.dao.repository.CustomLoginRepository;
import com.crm.pay.service.CustomLoginService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.pojo.table.CustomLoginTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomLoginServiceImpl implements CustomLoginService {

    @Autowired
    private CustomLoginRepository customLoginRepository;

    @Override
    public void save(CustomLoginTable customLoginTable) throws ServiceException {
        customLoginRepository.save(customLoginTable);
    }

    @Override
    public List<CustomLoginTable> getAllByCustomId(Long customId) throws ServiceException {

        return customLoginRepository.getAllByCustomId(customId);

    }

    @Override
    public CustomLoginTable getByCustomIdAndLogin(Long customId, Long login) throws ServiceException {

        CustomLoginTable customLoginTable = customLoginRepository.getFirstByCustomIdAndLogin(customId, login);
        if (customLoginTable == null) {
            throw new ServiceException(Constants.LOGIN_NOT_EXIST_ERROR);
        }
        return customLoginTable;
    }

    @Override
    public CustomLoginTable getByLogin(Long login) throws ServiceException {
        CustomLoginTable customLoginTable = customLoginRepository.getFirstByLogin(login);
        if (customLoginTable == null) {
            throw new ServiceException(Constants.LOGIN_NOT_EXIST_ERROR);
        }
        return customLoginTable;
    }

    @Override
    public List<CustomLoginTable> getByCustomIdAndLogins(Long customId, List<Long> logins) throws ServiceException {

        if (logins != null && logins.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<CustomLoginTable> customLoginTables = customLoginRepository.getByCustomIdAndLoginIn(customId, logins);
        if (customLoginTables == null || customLoginTables.size() != logins.size()) {
            throw new ServiceException(Constants.LOGIN_NOT_EXIST_ERROR);
        }
        return customLoginTables;
    }

}
