package cn.com.qmth.stmms.ms.commons.utils.specification;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

/**
 * 构建动态查询、分页、排序
 * @author xudengqi
 * 2016年8月18日
 */
public abstract class PagingAndSortingSpecification implements QuerySpecification{
	
	private Integer page;
    private Integer size;
    private String order;
    private String[] properties;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String[] getProperties() {
        return properties;
    }

    public void setProperties(String[] properties) {
        this.properties = properties;
    }

    @Override
    public abstract Specification<?> getSpecification();

    @Override
    public PageRequest getPageRequest(){
        PageRequest pageRequest = null;

        if(order == null || order.trim().length() == 0){
            order = "ASC";
        }
        if(properties != null){
            Sort sort = new Sort(Sort.Direction.valueOf(order),properties);
            pageRequest = new PageRequest(page,size,sort);
        }
        else if(page != null && size != null){
            pageRequest = new PageRequest(page,size);
        }
        else{
            //TODO 暂时返还一条数据
            pageRequest = new PageRequest(0,1);
        }
        return pageRequest;
    }
}
