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

import java.sql.ResultSet;
import java.sql.SQLException;
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.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import cn.com.liandisys.ycjt.ggcx.common.util.StringUtil;
import cn.com.liandisys.ycjt.ggcx.train.dao.TrainDao;
import cn.com.liandisys.ycjt.ggcx.train.model.Train;
import cn.com.liandisys.ycjt.ggcx.train.service.TrainService;
import cn.com.liandisys.ycjt.ggcx.train.vo.QueryParam;

@Service("TrainService")
public class TrainServiceImpl implements TrainService {

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

    @Autowired
    private TrainDao trainDao;
    
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
    @Override
    public List<Train> getTrainList() {
        //return trainDao.findAll();
    	try {
            
        	RowMapper<Train> mapper = new RowMapper<Train>() {
                public Train mapRow(ResultSet rs, int rowNum) throws SQLException {
                	Train result = new Train();
                	result.setId(rs.getString("id"));
                	result.setTrainNum(rs.getString("train_num"));
                    result.setTimes(rs.getInt("times"));
                    result.setStation(rs.getString("station"));
                    result.setEstTime(rs.getString("est_time"));
                    result.setDepartTime(rs.getString("depart_time"));
                    result.setMile(rs.getFloat("mile"));
                    result.setDays(rs.getInt("days"));
                    result.setRemark(rs.getString("remark"));
                    result.setCountTime(rs.getDate("count_time"));
                    result.setInsertTime(rs.getDate("insert_time"));
                    return result;
                }
            };
            String sql = "select * from ISN_TRAIN_DES ORDER BY train_num,times";

            List<Train> list = jdbcTemplate.query(sql, mapper);
            return list;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return null;
    }
    }

    @Override
    public List<Train> getTrainList(String id) {

        return trainDao.findById(id);
    }

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

        return trainDao.findAll(pageable);
    }

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

        final String stationS = queryParam.getStationS();
        final String stationE = queryParam.getStationE();

        return new Specification<Train>() {

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

                Predicate predicate = cb.conjunction();
                if (!StringUtil.isEmpty(stationS)) {
                    predicate.getExpressions().add(cb.like((root.get("station").as(String.class)), "%"+stationS+"%"));
                }
                if (!StringUtil.isEmpty(stationE)) {
                    predicate.getExpressions().add(cb.like((root.get("station").as(String.class)), "%"+stationE+"%"));
                }
                return predicate;
            }
        };
    }

}
