package com.YuyeTech.TPlat.service.impl;

import com.YuyeTech.TPlat.dataobject.UserReward;
import com.YuyeTech.TPlat.dataobject.UserWallets;
import com.YuyeTech.TPlat.enums.ResultEnum;
import com.YuyeTech.TPlat.exception.UserException;
import com.YuyeTech.TPlat.repository.UserRewardRepository;
import com.YuyeTech.TPlat.repository.UserWalletsRepository;
import com.YuyeTech.TPlat.service.UserRewardService;
import com.YuyeTech.TPlat.service.UserWalletsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
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;
    }
}

