SchedulePlanInfoServiceImpl.java 10.7 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.schedule.SchedulePlanInfo;
import com.bsth.repository.LineRepository;
import com.bsth.repository.schedule.SchedulePlanInfoRepository;
import com.bsth.service.schedule.SchedulePlanInfoService;
import com.bsth.service.schedule.datatools.TTInfoDetailForEdit;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.timetable.TimetableExcelData;
import com.bsth.service.schedule.timetable.strategy.impl.TimetableExcelWithPlanInfoExportStrategyImpl;
import com.bsth.service.schedule.timetable.strategy.impl.TimetableExcelWithPlanInfoViewStrategyImpl;
import com.bsth.service.schedule.utils.DataToolsFile;
import com.bsth.service.schedule.utils.DataToolsProperties;
import com.bsth.service.schedule.utils.DataToolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Created by xu on 16/6/16.
 */
@Service
public class SchedulePlanInfoServiceImpl extends BServiceImpl<SchedulePlanInfo, Long> implements SchedulePlanInfoService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private LineRepository lineRepository;

    @Autowired
    @Qualifier(value = "dataToolsServiceImpl")
    private DataToolsService dataToolsService;

    @Autowired
    private DataToolsProperties dataToolsProperties;

    @Override
    public DataToolsFile exportPlanTimetableInfo(Integer xlId, Date scheduleDate) throws ScheduleException {
        TimetableExcelData timetableExcelData = TimetableExcelData.withPlanInfoExcelExportBuilder()
                .setXlId(xlId)
                .setScheduleDate(scheduleDate)
                .setLineRepository(this.lineRepository)
                .setDataToolsService(this.dataToolsService)
                .setDataToolsProperties(this.dataToolsProperties)
                .setTimetableExcelWithPlanInfoExportStrategy(new TimetableExcelWithPlanInfoExportStrategyImpl())
                .build();
        return timetableExcelData.doExportWithPlanInfo();
    }

    @Override
    public TTInfoDetailForEdit.EditInfo getDirectiveDataWithPlanInfo(Integer xlId, Date scheduleDate) throws ScheduleException {
        TimetableExcelData timetableExcelData = TimetableExcelData.withPlanInfoExcelViewBuilder()
                .setXlId(xlId)
                .setScheduleDate(scheduleDate)
                .setLineRepository(this.lineRepository)
                .setDataToolsService(this.dataToolsService)
                .setDataToolsProperties(this.dataToolsProperties)
                .setTimetableExcelWithPlanInfoViewStrategy(new TimetableExcelWithPlanInfoViewStrategyImpl())
                .build();
        return timetableExcelData.doGetDirectiveDataWithPlanInfo();
    }

    @Override
    public SchedulePlanInfo save(SchedulePlanInfo schedulePlanInfo) {
        // 生成计划不是save,使用的是spring batch插入的
        // 这里是单独修改的时候,需要记录修改次数,用于标识被修改过

        Integer currentModifyCount = schedulePlanInfo.getModifyCount();
        schedulePlanInfo.setModifyCount(currentModifyCount == null ? 1 : ++ currentModifyCount);

        return super.save(schedulePlanInfo);
    }

    @Override
    public List<Date> findLastestPlanDate(Integer xlId) {
        String sql = "select max(schedule_date) as sd from bsth_c_s_sp_info " +
                "where xl = ?";

        final List<Date> ld = new ArrayList<>();

        jdbcTemplate.query(sql, new Object[]{xlId}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                if (rs != null && rs.getDate("sd") != null) {
                    ld.add(new Date(rs.getDate("sd").getTime()));
                }
            }
        });

        return ld;

    }

    @Override
    public List<SchedulePlanGroupInfo> findSchedulePlanGroupInfo(Integer xlId, Date scheduleDate) {
        String sql = SchedulePlanGroupInfo.generateSelectSql();
        List<SchedulePlanGroupInfo> schedulePlanGroupInfos = jdbcTemplate.query(sql, new Object[]{xlId, scheduleDate}, new RowMapper<SchedulePlanGroupInfo>() {
            @Override
            public SchedulePlanGroupInfo mapRow(ResultSet resultSet, int i) throws SQLException {
                SchedulePlanGroupInfo schedulePlanGroupInfo = new SchedulePlanGroupInfo();
                schedulePlanGroupInfo.setFromResult(resultSet);
                return schedulePlanGroupInfo;
            }
        });

        // 排序
        Collections.sort(schedulePlanGroupInfos);

        return schedulePlanGroupInfos;
    }

    @Autowired
    private SchedulePlanInfoRepository schedulePlanInfoRepository;

    @Override
    public List<GroupInfo> findGroupInfo(Integer xlid, Date scheduleDate) {
        List<Object[]> groupInfos = schedulePlanInfoRepository.findGroupInfo(xlid, scheduleDate);
        List<GroupInfo> groupInfoList = new ArrayList<>();
        for (Object[] groupInfo : groupInfos) {
            groupInfoList.add(new GroupInfo(groupInfo));
        }
        return groupInfoList;
    }

    @Override
    @Transactional
    public int updateGroupInfo(GroupInfoUpdate groupInfoUpdate) {
        int type = groupInfoUpdate.getType();
        int result = 0;
        if (type == 1) {
            // 换车
            if (groupInfoUpdate.getUpdate().getClId() != groupInfoUpdate.getSrc().getClId()) {
                result = this.schedulePlanInfoRepository.updateGroupInfo_type_1(
                        groupInfoUpdate.getUpdate().getClId(),
                        groupInfoUpdate.getUpdate().getClZbh(),
                        groupInfoUpdate.getSrc().getXlId(),
                        groupInfoUpdate.getSrc().getScheduleDate(),
                        groupInfoUpdate.getSrc().getLpName()
                );
            }

        } else if (type == 2) {
            // 更改出场班次1的时间
            if (!groupInfoUpdate.getUpdate().getCcsj1().equals(groupInfoUpdate.getSrc().getCcsj1())) {
                result = this.schedulePlanInfoRepository.updateGroupInfo_type_2_4(
                        groupInfoUpdate.getUpdate().getCcsj1(),
                        groupInfoUpdate.getSrc().getXlId(),
                        groupInfoUpdate.getUpdate().getScheduleDate(),
                        groupInfoUpdate.getSrc().getLpName(),
                        groupInfoUpdate.getSrc().getCcsj1(),
                        "out",
                        groupInfoUpdate.getSrc().getFcno1()
                );
            }

        } else if (type == 3) {
            // 更改驾驶员1
            if (groupInfoUpdate.getUpdate().getJsy1Id() != groupInfoUpdate.getSrc().getJsy1Id()) {
                result = this.schedulePlanInfoRepository.updateGroupInfo_type_3_5_jsy(
                        groupInfoUpdate.getUpdate().getJsy1Id(),
                        groupInfoUpdate.getUpdate().getJsy1Gh(),
                        groupInfoUpdate.getUpdate().getJsy1Name(),
                        groupInfoUpdate.getSrc().getXlId(),
                        groupInfoUpdate.getSrc().getScheduleDate(),
                        groupInfoUpdate.getSrc().getLpName(),
                        groupInfoUpdate.getSrc().getJsy1Id()
                );
            }
            // 更改售票员1
            if (groupInfoUpdate.getUpdate().getSpy1Id() != groupInfoUpdate.getSrc().getSpy1Id()) {
                result = this.schedulePlanInfoRepository.updateGroupInfo_type_3_5_spy(
                        groupInfoUpdate.getUpdate().getSpy1Id(),
                        groupInfoUpdate.getUpdate().getSpy1Gh(),
                        groupInfoUpdate.getUpdate().getSpy1Name(),
                        groupInfoUpdate.getSrc().getXlId(),
                        groupInfoUpdate.getSrc().getScheduleDate(),
                        groupInfoUpdate.getSrc().getLpName(),
                        groupInfoUpdate.getSrc().getSpy1Id()
                );
            }

        } else if (type == 4) {
            // 更改出场班次2的时间
            if (!groupInfoUpdate.getUpdate().getCcsj2().equals(groupInfoUpdate.getSrc().getCcsj2())) {
                result = this.schedulePlanInfoRepository.updateGroupInfo_type_2_4(
                        groupInfoUpdate.getUpdate().getCcsj2(),
                        groupInfoUpdate.getSrc().getXlId(),
                        groupInfoUpdate.getUpdate().getScheduleDate(),
                        groupInfoUpdate.getSrc().getLpName(),
                        groupInfoUpdate.getSrc().getCcsj2(),
                        "out",
                        groupInfoUpdate.getSrc().getFcno2()
                );
            }

        } else if (type == 5) {
            // 更改驾驶员2
            if (groupInfoUpdate.getUpdate().getJsy2Id() != groupInfoUpdate.getSrc().getJsy2Id()) {
                result = this.schedulePlanInfoRepository.updateGroupInfo_type_3_5_jsy(
                        groupInfoUpdate.getUpdate().getJsy2Id(),
                        groupInfoUpdate.getUpdate().getJsy2Gh(),
                        groupInfoUpdate.getUpdate().getJsy2Name(),
                        groupInfoUpdate.getSrc().getXlId(),
                        groupInfoUpdate.getSrc().getScheduleDate(),
                        groupInfoUpdate.getSrc().getLpName(),
                        groupInfoUpdate.getSrc().getJsy2Id()
                );
            }
            // 更改售票员2
            if (groupInfoUpdate.getUpdate().getSpy2Id() != groupInfoUpdate.getSrc().getSpy2Id()) {
                result = this.schedulePlanInfoRepository.updateGroupInfo_type_3_5_spy(
                        groupInfoUpdate.getUpdate().getSpy2Id(),
                        groupInfoUpdate.getUpdate().getSpy2Gh(),
                        groupInfoUpdate.getUpdate().getSpy2Name(),
                        groupInfoUpdate.getSrc().getXlId(),
                        groupInfoUpdate.getSrc().getScheduleDate(),
                        groupInfoUpdate.getSrc().getLpName(),
                        groupInfoUpdate.getSrc().getSpy2Id()
                );
            }

        } else {
            throw new RuntimeException("未知的更新类型,type=" + type);
        }

        return result;
    }
}