package com.crm.email.service.impl;

import com.crm.email.dao.repository.SysConfigRepository;
import com.crm.email.service.SysConfigService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigRepository configRepository;

    @Override
    public List<SysConfigTable> getByCodes(List<String> codes) throws ServiceException {
        if (codes == null || codes.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<SysConfigTable> sysConfigTables = configRepository.getByCodeIn(codes);

        if (sysConfigTables == null || sysConfigTables.size() < codes.size()) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        return sysConfigTables;
    }

    @Override
    public SysConfigTable getByCode(String code) throws ServiceException {
        if (Strings.isNullOrEmpty(code)) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        SysConfigTable sysConfigTable = configRepository.getByCode(code);

        if (sysConfigTable == null) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        return sysConfigTable;
    }
}
