package com.YuyeTech.HeartRate.game.bluetoothinfo.service;

import com.YuyeTech.HeartRate.common.service.CommonServiceImpl;
import com.YuyeTech.HeartRate.game.bluetoothinfo.projo.BluetoothInfo;
import com.YuyeTech.HeartRate.dto.BLToothDTO;
import com.YuyeTech.HeartRate.game.bluetoothinfo.repository.BluetoothInfoRepository;
import com.YuyeTech.HeartRate.game.bluetoothinfo.vo.BluetoothInfoVo;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author:slambb
 * @date:2020/6/12
 */
@Service
@Transactional
public class BluetoothInfoServiceImpl extends CommonServiceImpl<BluetoothInfoVo, BluetoothInfo, Integer> implements BluetoothInfoService {

    //@PersistenceContext
    //private EntityManager em;

    @Autowired
    private BluetoothInfoRepository bluetoothInfoRepository;

    @Override
    public BluetoothInfo findByMac(String mac) {
        return bluetoothInfoRepository.findByBluetoothMac(mac).orElse(null);
    }


    @Override
    public BluetoothInfo findByUserIdAndType(String userId, Integer type) {
        return bluetoothInfoRepository.findByUserIdAndBluetoothType(userId,type).orElse(null);

    }

    @Override
    public BluetoothInfo findByMacAndType(String mac, Integer type) {
        return bluetoothInfoRepository.findByBluetoothMacAndBluetoothType(mac,  type).orElse(null);
    }

    @Override
    public BluetoothInfo saveBluetoothInfo(BluetoothInfo bluetoothInfo) {
        return bluetoothInfoRepository.save(bluetoothInfo);
    }

    @Override
    public List<BluetoothInfo> addMoreBluetoothInfos(List<BluetoothInfo> bluetoothInfos) {
        return bluetoothInfoRepository.saveAll(bluetoothInfos);
    }

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

    @Override
    public Page<BluetoothInfo> findAllBLEInfoByPageable(Pageable request) {
        return bluetoothInfoRepository.findAll(request);
    }


    @Override
    public Page<BluetoothInfo> findAllBLEByPageableAndMac(Pageable pageable, String mac) {
        return bluetoothInfoRepository.findAllByBluetoothMac(mac,pageable);
    }

    @Override
    public String deleteBluetoothInfoById(Integer bleId) {
        bluetoothInfoRepository.deleteById(bleId);
        return "success";
    }

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