package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.CustomCommunicationPreferenceRepository;
import com.crm.manager.service.CustomCommunicationPreferenceService;
import com.crm.rely.backend.core.dto.custom.communication.preference.CustomCommunicationPreferenceDto;
import com.crm.rely.backend.core.entity.custom.communication.preference.CustomCommunicationPreferenceSearchEntity;
import com.crm.rely.backend.core.pojo.table.CustomCommunicationPreferenceTable;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: houn
 * @create: 2020-07-19 16:17
 **/
@Service
public class CustomCommunicationPreferenceServiceImpl implements CustomCommunicationPreferenceService {

    @Autowired
    private CustomCommunicationPreferenceRepository customCommunicationPreferenceRepository;

    @Override
    public List<CustomCommunicationPreferenceDto> searchCustom(CustomCommunicationPreferenceSearchEntity entity) throws ServiceException {

        List<CustomCommunicationPreferenceTable> customCommunicationPreferenceTables = customCommunicationPreferenceRepository.getAllByCustomId(entity.getCustomId());

        return transform(customCommunicationPreferenceTables);
    }

    private List<CustomCommunicationPreferenceDto> transform(List<CustomCommunicationPreferenceTable> customCommunicationPreferenceTables) {

        if (customCommunicationPreferenceTables == null || customCommunicationPreferenceTables.size() <= 0) {
            return null;
        }

        List<CustomCommunicationPreferenceDto> customCommunicationPreferenceDtos = new ArrayList<>(customCommunicationPreferenceTables.size());
        customCommunicationPreferenceTables.forEach(value -> customCommunicationPreferenceDtos.add(transform(value)));

        return customCommunicationPreferenceDtos;
    }

    private CustomCommunicationPreferenceDto transform(CustomCommunicationPreferenceTable customCommunicationPreferenceTable) {
        CustomCommunicationPreferenceDto customCommunicationPreferenceDto = new CustomCommunicationPreferenceDto();
        BeanUtils.copyProperties(customCommunicationPreferenceTable, customCommunicationPreferenceDto);
        return customCommunicationPreferenceDto;
    }
}
