ChildTaskPlanServiceImpl.java 4.99 KB
package com.bsth.service.realcontrol.impl;

import com.bsth.common.ResponseCode;
import com.bsth.data.BasicData;
import com.bsth.data.Station2ParkBuffer;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.entity.realcontrol.ChildTaskPlan;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.repository.realcontrol.ChildTaskPlanRepository;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.realcontrol.ChildTaskPlanService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.Map;

@Service
public class ChildTaskPlanServiceImpl extends BaseServiceImpl<ChildTaskPlan, Long> implements ChildTaskPlanService {

    @Autowired
    ScheduleRealInfoRepository scheduleRealInfoRepository;

    @Autowired
    ChildTaskPlanRepository childTaskPlanRepository;

    @Autowired
    DayOfSchedule dayOfSchedule;

    @Autowired
    JdbcTemplate jdbcTemplate;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Transactional
    @Override
    public Map<String, Object> save(ChildTaskPlan t) {

        Map<String, Object> rs = new HashMap();
        try {
            ScheduleRealInfo sch = dayOfSchedule.get(t.getSchedule().getId());
            //保存起终点名称
            //String prefix = sch.getXlBm() + "_" + sch.getXlDir() + "_";
            if(StringUtils.isEmpty(t.getStartStationName()))
                t.setStartStationName(getStationName(sch.getXlBm(), t.getStartStation()));
                //t.setStartStationName(BasicData.getStationNameByCode(t.getStartStation(), prefix));

            if(StringUtils.isEmpty(t.getEndStationName()))
                t.setEndStationName(getStationName(sch.getXlBm(), t.getEndStation()));
                //t.setEndStationName(BasicData.getStationNameByCode(t.getEndStation(), prefix));
            if(t.getDestroyReason() == null)
                t.setDestroyReason("");

            //烂班说明,为兼容之前的数据结构
            if(t.isDestroy() && StringUtils.isEmpty(t.getDestroyReason()))
                t.setDestroyReason(t.getReason());

            //先持久化子任务
            rs = super.save(t);
            //关联主任务
            sch.getcTasks().add(t);
            dayOfSchedule.save(sch);
            //直接持久化
            //scheduleRealInfoRepository.save(sch);
            //站到场对照
            t.setSchedule(sch);
            Station2ParkBuffer.put(t);

            rs.put("status", ResponseCode.SUCCESS);
            rs.put("t", sch);
        }catch (Exception e){
            logger.error("", e);
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg",  e.getMessage());
        }
        return rs;
    }

    private String getStationName(String lineCode, String stationCode){
        String name;
        String prefix1 = lineCode + "_" + 0 + "_",
                prefix2 = lineCode + "_" + 1 + "_";

        name = BasicData.getStationNameByCode(stationCode, prefix1);
        if(StringUtils.isEmpty(name))
            name = BasicData.getStationNameByCode(stationCode, prefix2);
        return name;
    }

    @Override
    public Map<String, Object> delete(Long id) {
        Map<String, Object> rs;

        ChildTaskPlan cPlan = childTaskPlanRepository.findOne(id);

        //删除子任务
        rs = super.delete(id);
        //dayOfSchedule.save(sch);
        //解除和主任务关联
        ScheduleRealInfo sch = dayOfSchedule.get(cPlan.getSchedule().getId());
        sch.getcTasks().remove(cPlan);
        rs.put("t", sch);
        return rs;
    }

    @Override
    public Map<String, Object> saveHistory(ChildTaskPlan t) {
        Map<String, Object> rs = new HashMap();
        try {
            ScheduleRealInfo sch = t.getSchedule();
            //保存起终点名称
            String prefix = sch.getXlBm() + "_" + sch.getXlDir() + "_";
            if(StringUtils.isEmpty(t.getStartStationName()))
                t.setStartStationName(BasicData.getStationNameByCode(t.getStartStation(), prefix));

            if(StringUtils.isEmpty(t.getEndStationName()))
                t.setEndStationName(BasicData.getStationNameByCode(t.getEndStation(), prefix));
            //先持久化子任务
            rs = super.save(t);

            rs.put("status", ResponseCode.SUCCESS);
        }catch (Exception e){
            logger.error("", e);
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg",  e.getMessage());
        }
        return rs;
    }

    @Override
    public Map<String, Object> delHistory(Long id) {
        return super.delete(id);
    }
}