package com.ff.weijinxiao.service;


import com.ff.weijinxiao.domain.UserSetting;
import com.ff.weijinxiao.repository.UserSettingRepository;
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 UserSettingService {

    @Resource
    private UserSettingRepository userSettingRepository;

    /**
     * 创建实体
     *
     * @param userSetting
     */
    public UserSetting saveUserSetting(UserSetting userSetting) {
        return userSettingRepository.save(userSetting);
    }

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

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

    /**
     * 分页条件查询
     *
     * @param page
     * @param size
     * @param param
     * @return
     */
    public Page<UserSetting> 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 userSettingRepository.findAll(SpecificationUtil.buildSpecification(param, UserSetting.class), pageable);
    }

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

    public Page<UserSetting> 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 userSettingRepository.findAll(new Specification<UserSetting>() {
            @Override
            public Predicate toPredicate(Root<UserSetting> 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 UserSetting getSettingByUid(String uid) {
        return userSettingRepository.findByUid(uid);
    }


}
