package cn.com.liandisys.ycjt.ggcx.ferryship.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.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.common.util.StringUtil;
import cn.com.liandisys.ycjt.ggcx.ferryship.dao.FerryShipDao;
import cn.com.liandisys.ycjt.ggcx.ferryship.model.FerryShip;
import cn.com.liandisys.ycjt.ggcx.ferryship.service.FerryShipService;
import cn.com.liandisys.ycjt.ggcx.ferryship.vo.FerryShipVo;

@Service
public class FerryShipServiceImpl implements FerryShipService {

    @Resource(name = "ferryShipDao")
    private FerryShipDao dao;

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

        final String departurePort = queryParam.getDeparturePort();
        final String arrivalPort = queryParam.getArrivalPort();

        return new Specification<FerryShip>() {

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

                Predicate predicate = cb.conjunction();
                //通过名称查找
                if (!StringUtil.isEmpty(departurePort)) {
                    predicate.getExpressions()
                            .add(cb.like((root.get("departurePort").as(String.class)), '%' + departurePort + '%'));
                }
                if (!StringUtil.isEmpty(arrivalPort)) {
                    predicate.getExpressions()
                            .add(cb.like((root.get("arrivalPort").as(String.class)), '%' + arrivalPort + '%'));
                }
                return predicate;
            }
        };
    }

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

        return dao.findAll(spec, pageable);
    }

}
