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

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import cn.com.liandisys.ycjt.ggcx.coachBus.dao.BusMainDao;
import cn.com.liandisys.ycjt.ggcx.coachBus.model.BusMain;
import cn.com.liandisys.ycjt.ggcx.coachBus.service.BusMainService;
import cn.com.liandisys.ycjt.ggcx.coachBus.vo.QueryParam;
import cn.com.liandisys.ycjt.ggcx.common.util.StringUtil;

@Service
public class BusMainServiceImpl implements BusMainService {

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

    @Resource(name = "busMainDao")
    private BusMainDao dao;

    @Override
    public Page<BusMain> findAll(Pageable pageable) {

        return dao.findAll(pageable);
    }

    @Override
    public Specification<BusMain> getSpecification(QueryParam queryParam) {

        final String carLocation = queryParam.getCarLocation();
        final String arrLocation = queryParam.getArrLocation();
        //        final Date departTime = queryParam.getDepartTimeVal();

        return new Specification<BusMain>() {

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

                Predicate predicate = cb.conjunction();
                if (!StringUtil.isEmpty(carLocation)) {
                    predicate.getExpressions().add(cb.equal((root.get("carLocation").as(String.class)), carLocation));
                }
                if (arrLocation != null && !StringUtil.isEmpty(arrLocation)) {
                    predicate.getExpressions()
                            .add(cb.like((root.get("arrLocation").as(String.class)), "%" + arrLocation + "%"));

                }
                /*  if (!StringUtil.isEmpty(departTime)) {
                    predicate.getExpressions().add(cb.greaterThanOrEqualTo((root.get("departTime").as(Date.class)), departTime));
                }*/
                return predicate;
            }
        };
    }

}
