ScheduleRuleServiceImpl.java 1.67 KB
package com.bsth.service.schedule.rules;

import com.bsth.entity.schedule.temp.SchedulePlanRuleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Created by xu on 17/4/19.
 */
@Service
public class ScheduleRuleServiceImpl implements ScheduleRuleService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<SchedulePlanRuleResult> findLastByXl(String xlid, Date from) {
        String sql = "select * from bsth_c_s_sp_rule_rst a " +
                "where exists (select 1 from " +
                "(select t.rule_id as rid, max(t.schedule_date) as sd from bsth_c_s_sp_rule_rst t " +
                "where t.xl_id = ? and t.schedule_date < ? " +
                "group by t.rule_id) a2 " +
                "where a.rule_id = rid and a.schedule_date = sd) ";

        return jdbcTemplate.query(sql, new Object[]{xlid, from}, new RowMapper<SchedulePlanRuleResult>() {
            @Override
            public SchedulePlanRuleResult mapRow(ResultSet rs, int i) throws SQLException {
                SchedulePlanRuleResult obj = new SchedulePlanRuleResult();
                obj.setRuleId(rs.getString("rule_id"));
                obj.setScheduleDate(rs.getDate("schedule_date"));
                obj.setGidindex(rs.getString("gidindex"));
                obj.setEcindex(rs.getString("ecindex"));

                // 其他字段没用
                return obj;
            }
        });
    }
}