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

import com.bsth.data.BasicData;
import com.bsth.data.match.Arrival2Schedule;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

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

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

	/*@Autowired
	ScheduleRealInfoServiceImpl scheduleRealInfoService;*/

	@Autowired
	ScheduleRealInfoRepository scheduleRealInfoRepository;
	
	@Autowired
	ChildTaskPlanRepository childTaskPlanRepository;
	
	@Autowired
	DayOfSchedule dayOfSchedule;
	
	@Autowired
	Arrival2Schedule arrival2Schedule;

	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Transactional
	@Override
	public Map<String, Object> save(ChildTaskPlan t) {
		Map<String, Object> rs;
		//保存起终点名称
		Map<String, String> map = BasicData.stationCode2NameMap;
		
		t.setStartStationName(map.get(t.getStartStation()));
		t.setEndStationName(map.get(t.getEndStation()));
		//先持久化子任务
		rs = super.save(t);
		//再关联主任务
		ScheduleRealInfo sch = dayOfSchedule.get(t.getSchedule().getId());
		sch.getcTasks().add(t);
		dayOfSchedule.save(sch);
		
		rs.put("t", sch);
		
		if(sch.getZdsjActual() == null)
			arrival2Schedule.removeExpect(sch.getClZbh());
		return rs;
	}

	@Override
	public Map<String, Object> delete(Long id) {
		Map<String, Object> rs;
		
		ChildTaskPlan cPlan = childTaskPlanRepository.findOne(id);
		//解除和主任务关联
		ScheduleRealInfo sch = dayOfSchedule.get(cPlan.getSchedule().getId());
		sch.getcTasks().remove(cPlan);
		//删除关联表数据
		jdbcTemplate.execute("delete from bsth_c_s_sp_info_real_c_tasks where bsth_c_s_sp_info_real="+sch.getId()+" and c_tasks="+cPlan.getId());

		//删除子任务
		rs = super.delete(id);
		dayOfSchedule.save(sch);

		rs.put("t", sch);
		return rs;
	}
}