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

import java.util.ArrayList;
import java.util.List;

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.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import cn.com.liandisys.ycjt.ggcx.busline.dao.BusLineDao;
import cn.com.liandisys.ycjt.ggcx.busline.model.BusLineEntity;
import cn.com.liandisys.ycjt.ggcx.busline.service.BusLineService;
import cn.com.liandisys.ycjt.ggcx.busline.vo.BusLine;
import cn.com.liandisys.ycjt.ggcx.common.util.StringUtil;

/**
 * 
 *     
 * 项目名称：ycjt-ggcx    
 * 类名称：BusLineServiceImpl    
 * 类描述：    公交线路
 * 创建人：yangd    
 * 创建时间：2016年11月23日 下午2:42:35    
 * 修改人：yangd    
 * 修改时间：2016年11月23日 下午2:42:35    
 * 修改备注：    
 * @version     
 *
 */
@Service
public class BusLineServiceImpl implements BusLineService {

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

    @Resource(name = "busLineDao")
    private BusLineDao dao;

    @Autowired
    private JdbcTemplate jdbcTemplate;

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

        return dao.findAll(spec, pageable);
    }

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

        final String linename = queryParam.getLineName();
        final String site = queryParam.getSite();

        return new Specification<BusLineEntity>() {

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

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

    @Override
    public List<BusLineEntity> getBusLineName(String site) {

        List<BusLineEntity> list_all = new ArrayList<>();

        //创建sql语句获得线路名称
        String sql_busline =
                "SELECT LINE_NAME FROM  ISN_LINE_MAIN  WHERE replace(replace(SITE, '（', '('), '）', ')') like " + "'%"
                        + site + "%'";

        List list = jdbcTemplate.queryForList(sql_busline);
        return list;
    }

}
