package com.uas.platform.b2b.manage.service.impl;


import com.uas.platform.b2b.manage.core.util.StringUtils;
import com.uas.platform.b2b.manage.dao.ListenDao;
import com.uas.platform.b2b.manage.model.Listen;
import com.uas.platform.b2b.manage.page.PageInfo;
import com.uas.platform.b2b.manage.page.criteria.CriterionExpression;
import com.uas.platform.b2b.manage.page.criteria.SimpleExpression;
import com.uas.platform.b2b.manage.service.ListenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * @Author: huj
 * @Date: Created in 16:31 2018/11/27.
 */
@Service
public class ListenServiceImpl implements ListenService {

    @Autowired
    ListenDao listenDao;

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public Page<Listen> findAll(PageInfo info, String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            SimpleExpression name = new SimpleExpression("name", keyword, CriterionExpression.Operator.LIKE, true);
            info.expression(name);
        }
        return listenDao.findAll(new Specification<Listen>() {
            @Override
            public Predicate toPredicate(Root<Listen> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                query.where(info.getPredicates(root, query, builder));
                return null;
            }
        }, info);
    }
}
