package com.YuyeTech.HeartRate.service.impl;

import com.YuyeTech.HeartRate.dataobject.UserReward;
import com.YuyeTech.HeartRate.repository.UserRewardRepository;
import com.YuyeTech.HeartRate.service.UserRewardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * 用户信息
 *
 * @author:slambb
 * @date:2021/8/5
 */
@Service
public class UserRewardServiceImpl implements UserRewardService {

    @Autowired
    private UserRewardRepository userRewardRepository;

    @Override
    public void saveUserReward(UserReward userReward) {
        userRewardRepository.save(userReward);
    }

    @Override
    public UserReward findByUserIdAndEventId(String userId,Integer eventId) {
        return userRewardRepository.findByUserIdAndEventId(userId,eventId).orElse(null);
    }

    /**
     * 查询用户全部荣誉
     * @param userId
     * @return
     */
    @Override
    public List<UserReward> findAllHonorByAndUserId(String userId) {
        //排序规则和分页
        Specification<UserReward> specification = (Specification<UserReward>) (root, query, cb) -> {
            //增加筛选条件
            Predicate predicate = cb.conjunction();
            predicate.getExpressions().add(cb.notEqual(root.get("honor").as(String.class), 0));
            predicate.getExpressions().add(cb.equal(root.get("userId").as(String.class), userId));
            return predicate;
        };
        List<UserReward> userRewards = userRewardRepository.findAll(specification);
        return userRewards;
    }
}

