package com.crm.globex.service.impl;

import com.crm.globex.dao.repository.SysEmailTemplateRepository;
import com.crm.globex.service.SysEmailTemplateService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.pojo.table.SysEmailTemplateTable;
import com.crm.rely.backend.core.exception.ServiceException;
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 SysEmailTemplateServiceImpl implements SysEmailTemplateService {

    @Autowired
    private SysEmailTemplateRepository repository;


    @Override
    public List<SysEmailTemplateTable> getAllByCodeIn(List<String> codes) throws ServiceException {
        if (codes == null || codes.size() <= 0) {
            return null;
        }
        List<SysEmailTemplateTable> sysEmailTemplateTables = repository.getAllByCodeIn(codes);
        if (sysEmailTemplateTables == null || sysEmailTemplateTables.size() <= 0 || codes.size() > sysEmailTemplateTables.size()) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        return sysEmailTemplateTables;
    }

    @Override
    public SysEmailTemplateTable getByCode(String code) throws ServiceException {
        if (Strings.isNullOrEmpty(code)) {
            return null;
        }
        SysEmailTemplateTable sysEmailTemplateTable = repository.getFirstByCode(code);
        if (sysEmailTemplateTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        return sysEmailTemplateTable;
    }
}
