package com.ff.weijinxiao.service;


import com.ff.weijinxiao.domain.User;
import com.ff.weijinxiao.repository.UserRepository;
import com.ff.weijinxiao.util.SpecificationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class UserService {

    @Resource
    private UserRepository userRepository;

    /**
     * 创建实体
     *
     * @param user
     */
    public User saveUser(User user) {
        return userRepository.save(user);
    }

    /**
     * 查找实体
     *
     * @param id
     * @return
     */
    public User findById(Integer id) {
        Optional<User> optional = userRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    public void deleteById(Integer id) {
        userRepository.deleteById(id);
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param size
     * @param param
     * @return
     */
    public Page<User> findPage(int page, int size, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(page - 1, size, direction, sort);
        return userRepository.findAll(SpecificationUtil.buildSpecification(param, User.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<User> findList(Map<String, Object> param) {
        return userRepository.findAll(SpecificationUtil.buildSpecification(param, User.class));
    }

    public Page<User> findPage(int page, int size, String tag) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(page - 1, size, direction, sort);
        return userRepository.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.greaterThan(root.get("id").as(Integer.class), 0));
                if (null != tag && tag.length() > 0) {
                    Predicate p1 = criteriaBuilder.like(root.get("name").as(String.class), "%" + tag + "%");
                    Predicate orp = criteriaBuilder.or(p1);
                    p = criteriaBuilder.and(p, orp);
                }
                return p;
            }
        }, pageable);
    }

    public User findUserByAccount(String account) {
        Optional<User> optional = userRepository.findOne(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.equal(root.get("account").as(String.class), account));
                return p;
            }
        });
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

}
