package incheon.ags.pss.edit.service.impl;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import incheon.ags.pss.edit.mapper.SimulationMapper;
import incheon.ags.pss.edit.service.SimulationService;
import incheon.ags.pss.edit.smlt.service.VRService;
import incheon.ags.pss.edit.vo.SimulationVO;

@Service("simulationService")
public class SimulationServiceImpl implements SimulationService {
    
    @Resource(name = "simulationMapper")
    private SimulationMapper mapper;
    
    @Autowired
    private VRService vrService;
    
    @Override
    public List<SimulationVO> selectSimulationList(SimulationVO vo) throws Exception {
        return mapper.selectSimulationList(vo);
    }
    
    @Override
    public SimulationVO selectSimulation(Long smltNo) throws Exception {
        return mapper.selectSimulation(smltNo);
    }
    
    @Override
    public void insertSimulation(SimulationVO vo) throws Exception {
        mapper.insertSimulation(vo);
    }

    @Override
    public void updateSimulation(SimulationVO vo) throws Exception {
        mapper.updateSimulation(vo);
    }


    @Override
    @Transactional
    public void deleteSimulation(Long smltNo) throws Exception {
    	
    	//삭제 전 V1 타입인지 확인
        SimulationVO vo = mapper.selectSimulation(smltNo);
        if (vo != null && "V1".equals(vo.getSmltTypeCd())) {
            // V1 타입이면 연관된 모든 VR 기록을 먼저 삭제
            vrService.deleteVrRecordsBySmltNo(smltNo);
        }
    	
        // 공용 시뮬레이션 마스터 항목 삭제
        mapper.deleteSimulation(smltNo);
    }
}