package cn.com.liandisys.ycjt.ggcx.publicBicycleSat.service.impl;

import java.util.List;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import cn.com.liandisys.ycjt.ggcx.common.util.StringUtil;
import cn.com.liandisys.ycjt.ggcx.publicBicycleSat.dao.PublicBicycleSatDao;
import cn.com.liandisys.ycjt.ggcx.publicBicycleSat.model.PublicBicycleSatEntity;
import cn.com.liandisys.ycjt.ggcx.publicBicycleSat.service.PublicBicycleSatService;
import cn.com.liandisys.ycjt.ggcx.publicBicycleSat.vo.PublicBicycleSatVo;

/**
 * 
 *     
 * 项目名称：ycjt-ggcx    
 * 类名称：BusLineServiceImpl    
 * 类描述：    公共自行车站
 * 创建人：wangqx    
 * 创建时间：2016年11月23日 下午2:42:35    
 * 修改人：wangqx    
 * 修改时间：2016年11月23日 下午2:42:35    
 * 修改备注：    
 * @version     
 *
 */
@Service
public class PublicBicycleSatServiceImpl implements PublicBicycleSatService {

    private Logger logger = LoggerFactory.getLogger(PublicBicycleSatServiceImpl.class);

    /* @Resource(name = "publicBicycleSatDao")*/
    @Autowired
    private PublicBicycleSatDao dao;

    @Override
    public Page<PublicBicycleSatEntity> findAll(Specification<PublicBicycleSatEntity> spec, Pageable pageable) {

        return dao.findAll(spec, pageable);
    }

    /**
     * 动态查询条件拼接
     * @param queryParam
     * @return
     */
    public Specification<PublicBicycleSatEntity> getSpecification(PublicBicycleSatVo queryParam) {

        final String localName = queryParam.getName();
        final String LocalAddress = queryParam.getAddress();

        return new Specification<PublicBicycleSatEntity>() {

            @Override
            public Predicate toPredicate(Root<PublicBicycleSatEntity> root, CriteriaQuery<?> query,
                    CriteriaBuilder cb) {

                Predicate predicate = cb.conjunction();
                //通过名称查找
                if (!StringUtil.isEmpty(localName)) {
                    predicate.getExpressions()
                            .add(cb.like((root.get("stationName").as(String.class)), "%" + localName + "%"));
                }
                //通过地址查找
                if (!StringUtil.isEmpty(LocalAddress)) {
                    predicate.getExpressions()
                            .add(cb.like((root.get("location").as(String.class)), "%" + LocalAddress + "%"));
                }
                return predicate;
            }
        };
    }

    public Page<PublicBicycleSatEntity> getBibycleList(PageRequest pageRequest) {

        return dao.findAll(pageRequest);

    }

    @Override
    public List<PublicBicycleSatEntity> getBibycleList() {

        return dao.findAll();
    }

}
