package com.YuyeTech.HeartRate.service.impl;

import com.YuyeTech.HeartRate.dataobject.DeviceInfo;
import com.YuyeTech.HeartRate.dto.DeviceBindDTO;
import com.YuyeTech.HeartRate.repository.DeviceInfoRepository;
import com.YuyeTech.HeartRate.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:slambb
 * @date:2020/6/12
 */
@Service
public class DeviceInfoServiceImpl implements DeviceInfoService {

    @Autowired
    private DeviceInfoRepository deviceInfoRepository;

    @Override
    public List<DeviceInfo> addMultipleObjects(List<DeviceInfo> deviceInfos) {
        return deviceInfoRepository.saveAll(deviceInfos);
    }

    @Override
    public List<DeviceInfo> findAllByIdentifier(String identifier) {
        return deviceInfoRepository.findAllByIdentifier(identifier);
    }

    @Override
    public DeviceInfo findByUUID(String uuid) {
        return deviceInfoRepository.findByDeviceUuid(uuid).orElse(null);
    }

    @Override
    public DeviceInfo saveDeviceInfo(DeviceInfo deviceInfo) {
        return deviceInfoRepository.save(deviceInfo);
    }

    @Override
    public List<DeviceInfo> findByUserIdOrUUID(String userId, String uuid) {
        return deviceInfoRepository.findByUserIdOrDeviceUuid(userId, uuid);
    }

    @Override
    public DeviceInfo findByUserIdAndUUID(String userId, String uuid) {
        return deviceInfoRepository.findByUserIdAndDeviceUuid(userId, uuid).orElse(null);
    }

    @Override
    public DeviceInfo findByUserIdAndType(String userId, Integer type) {
        return deviceInfoRepository.findByUserIdAndDeviceType(userId,type).orElse(null);
    }

    @Override
    public DeviceInfo findByUUIDAndType(String uuid, Integer type) {
        return deviceInfoRepository.findByDeviceUuidAndDeviceType( uuid,  type).orElse(null);
    }

    @Override
    public List<DeviceBindDTO> findAllByUserId(String userId) {
        return deviceInfoRepository.findAllByUserId(userId);
    }


    @Override
    public Page<DeviceInfo> findAllDeviceByPageable(Pageable pageable) {
        return deviceInfoRepository.findAll(pageable);
    }


    @Override
    public Page<DeviceInfo> findAllDeviceByPageableAndUUID(Pageable pageable, String uuid) {

        return deviceInfoRepository.findAllByDeviceUuid(uuid,pageable);
    }
}
