ScheduleRuleServiceImpl.java 5.45 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
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<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;
            }
        });
    }

    @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);
    }

    @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();
                }
            });
        }

    }
}