package com.YuyeTech.HeartRate.service.impl;


import com.YuyeTech.HeartRate.dataobject.FriendInfo;
import com.YuyeTech.HeartRate.repository.FriendInfoRepository;
import com.YuyeTech.HeartRate.service.FriendInfoService;
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;

@Service
public class FriendInfoServiceImpl implements FriendInfoService {

    @Autowired
    private FriendInfoRepository friendInfoRepository;

    @Override
    public Page<FriendInfo> getFriendUserInfo(Pageable pageable,String userId) {

        return friendInfoRepository.findAllByUserId(pageable,userId);
    }

    @Override
    public FriendInfo findByUserIdAndFriendId(String userId, String friendId) {
        FriendInfo friendInfo = friendInfoRepository.findByUserIdAndFriendId(userId, friendId).orElse(null);
        return friendInfo;
    }

    @Override
    public FriendInfo addFriendInfo(FriendInfo friendInfo) {
        return friendInfoRepository.save(friendInfo);
    }

    @Override
    public void deleteFriendInfo(String userId, String friendId) {
        friendInfoRepository.deleteByUserIdAndFriendId(userId, friendId);
    }

    /**
     * 这里传入的friendId就是用户自己的userId，用来删除别人添加自己好友的信息
     * @param friendId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFriendInfosByFriendId(String friendId) {
        friendInfoRepository.deleteAllByFriendId(friendId);
    }


    @Override
    public void deleteById(String id) {
        friendInfoRepository.deleteById(id);
    }

}
