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

import com.bsth.entity.schedule.SchedulePlan;
import com.bsth.entity.schedule.SchedulePlanInfo;
import com.bsth.entity.schedule.temp.SchedulePlanRuleResult;
import com.bsth.service.schedule.rules.rerun.RerunRule_input;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xu on 17/4/19.
 */
@Service
public class ScheduleRuleServiceImpl implements ScheduleRuleService {
    /** 日志记录器 */
    private static final Logger logger = LoggerFactory.getLogger(ScheduleRuleServiceImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<RerunRule_input> findRerunrule(Integer xlid) {
        String sql = "select " +
                "rerun_type as type " +
                ", rerun_xl as m_xl " +
                ", rerun_ttinfo as m_ttinfo " +
                ", rerun_lp as m_lp " +
                ", rerun_ttinfodetail_fcsjs as m_fcsjs " +
                ", use_xl as s_dylp_xl " +
                ", use_lp as s_dylp_lp " +
                ", use_type as s_dylp_type " +
                ", use_hrtype as s_dylp_hrtype " +
                ", cl as s_dybc_cl " +
                ", cl_zbh as s_dybc_clzbh " +
                ", j as s_dybc_j " +
                ", j_gh as s_dybc_jgh " +
                ", j_name as s_dybc_jname " +
                ", s as s_dybc_s " +
                ", s_gh as s_dybc_sgh " +
                ", s_name as s_dybc_sname " +
                "from bsth_c_s_rerun_rule " +
                "where is_cancel = 0 and rerun_xl = ? ";

        final List<RerunRule_input> rerunRule_inputs = new ArrayList<>();

        jdbcTemplate.query(sql, new Object[]{xlid}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet resultSet) throws SQLException {
                // 发车时间
                String[] fcsjs = resultSet.getString("m_fcsjs").split(",");
                for (String fcsj: fcsjs) {
                    RerunRule_input rerunRule_input = new RerunRule_input();
                    rerunRule_input.setXl(String.valueOf(resultSet.getInt("m_xl")));
                    rerunRule_input.setTtinfo(String.valueOf(resultSet.getLong("m_ttinfo")));
                    rerunRule_input.setLp(String.valueOf(resultSet.getLong("m_lp")));
                    rerunRule_input.setFcsj(fcsj);

                    rerunRule_input.setType(resultSet.getString("type"));
                    // 对应班次类型
                    if ("dylp".equals(resultSet.getString("type"))) {
                        rerunRule_input.setS_xl(String.valueOf(resultSet.getInt("s_dylp_xl")));
                        rerunRule_input.setS_lp(String.valueOf(resultSet.getLong("s_dylp_lp")));
                        rerunRule_input.setUsetype(resultSet.getString("s_dylp_type"));
                        rerunRule_input.setUserhrtype(resultSet.getString("s_dylp_hrtype"));
                    } else if ("dybc".equals(resultSet.getString("type"))) {
                        rerunRule_input.setCl(resultSet.getInt("s_dybc_cl"));
                        rerunRule_input.setZbh(resultSet.getString("s_dybc_clzbh"));
                        rerunRule_input.setJ(resultSet.getInt("s_dybc_j"));
                        rerunRule_input.setJgh(resultSet.getString("s_dybc_jgh"));
                        rerunRule_input.setJname(resultSet.getString("s_dybc_jname"));
                        rerunRule_input.setS(resultSet.getInt("s_dybc_s"));
                        rerunRule_input.setSgh(resultSet.getString("s_dybc_sgh"));
                        rerunRule_input.setSname(resultSet.getString("s_dybc_sname"));
                    }

                    rerunRule_inputs.add(rerunRule_input);
                }

            }
        });

        return rerunRule_inputs;
    }

    @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.setCcZbh(rs.getString("cc_zbh"));
                obj.setQyrq(rs.getDate("qyrq"));
                obj.setGids(rs.getString("gids"));
                obj.setEcids(rs.getString("ecids"));
                obj.setScheduleDate(rs.getDate("schedule_date"));
                obj.setGidindex(rs.getString("gidindex"));
                obj.setEcindex(rs.getString("ecindex"));

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

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public void deleteSchedulePlanAll(Long id) {
        String sql1 = "delete from bsth_c_s_sp where id = ?";
        String sql2 = "delete from bsth_c_s_sp_info where schedule_plan = ?";

        jdbcTemplate.update(sql2, id);
        jdbcTemplate.update(sql1, id);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public void deelteSchedulePlanInfo(Integer xlid, Date datefrom, Date dateto) {
        String sql = "delete from bsth_c_s_sp_info where xl = ? and schedule_date >= ? and schedule_date <= ?";
        jdbcTemplate.update(sql, xlid, datefrom, dateto);
    }

    @Override
    public void generateRuleResult(List<SchedulePlanRuleResult> schedulePlanRuleResults) {
        // 、批量插入排班规则结果
        List<List<SchedulePlanRuleResult>> lists = new ArrayList<>();
        int batchSize = 2000;
        int loopCount = schedulePlanRuleResults.size() / batchSize;
        int otherCount = schedulePlanRuleResults.size() % batchSize;
        for (int i = 0; i < loopCount; i++) {
            lists.add(schedulePlanRuleResults.subList(i * batchSize, i * batchSize + batchSize));
        }
        if (otherCount > 0) {
            lists.add(schedulePlanRuleResults.subList(loopCount * batchSize, loopCount * batchSize + otherCount));
        }

        for (final List<SchedulePlanRuleResult> list : lists) {
            jdbcTemplate.batchUpdate(SchedulePlanRuleResult.generateInsertSql(), new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                    SchedulePlanRuleResult schedulePlanRuleResult = list.get(i);
                    schedulePlanRuleResult.preparedStatementSet(preparedStatement);
                }
                @Override
                public int getBatchSize() {
                    return list.size();
                }
            });
        }
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    @Override
    public void generateSchedulePlan(final SchedulePlan schedulePlan, final List<SchedulePlanInfo> schedulePlanInfos) {
        // 1、插入排班计划主表,并获取主键id
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(
                        SchedulePlan.generateInsertSql(), new String[] {"id"});
                schedulePlan.preparedStatementSet(ps);

                return ps;
            }
        }, keyHolder);

        SchedulePlan master = new SchedulePlan();
        master.setId(keyHolder.getKey().longValue());
        for (SchedulePlanInfo schedulePlanInfo : schedulePlanInfos) {
            schedulePlanInfo.setSchedulePlan(master);
        }

        // 2、批量插入排班计划从表
        List<List<SchedulePlanInfo>> lists = new ArrayList<>();
        int batchSize = 2000;
        int loopCount = schedulePlanInfos.size() / batchSize;
        int otherCount = schedulePlanInfos.size() % batchSize;
        for (int i = 0; i < loopCount; i++) {
            lists.add(schedulePlanInfos.subList(i * batchSize, i * batchSize + batchSize));
        }
        if (otherCount > 0) {
            lists.add(schedulePlanInfos.subList(loopCount * batchSize, loopCount * batchSize + otherCount));
        }

        for (final List<SchedulePlanInfo> list : lists) {
            jdbcTemplate.batchUpdate(SchedulePlanInfo.generateInsertSql(), new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                    SchedulePlanInfo schedulePlanInfo = list.get(i);
                    schedulePlanInfo.preparedStatementSet(preparedStatement);
                }
                @Override
                public int getBatchSize() {
                    return list.size();
                }
            });
        }

    }
}