package com.interplug.qcast.biz.canvasBasicSetting;

import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.interplug.qcast.biz.canvasBasicSetting.dto.CanvasBasicSettingInfo;
import com.interplug.qcast.biz.canvasBasicSetting.dto.CanvasBasicSettingResponse;
import com.interplug.qcast.biz.canvasBasicSetting.dto.RoofMaterialsAddInfo;

@Service
@RequiredArgsConstructor
public class CanvasBasicSettingService {
	private final CanvasBasicSettingMapper canvasBasicSettingMapper;

	// Canvas Basic Setting 조회(objectNo)
	public List<CanvasBasicSettingResponse> selectCanvasBasicSetting(String objectNo) {
		return canvasBasicSettingMapper.selectCanvasBasicSetting(objectNo);
	}

	// Canvas Basic Setting 등록
	public Map<String, String> insertCanvasBasicSetting(CanvasBasicSettingInfo csi) {
		
		Map<String, String> response = new HashMap<>();

		try {
			
			// 도면/치수/각도 정보 insert/update
			canvasBasicSettingMapper.insertCanvasBasicSetting(csi);
			
			// 기존 지붕재추가 정보 삭제 후 insert
			canvasBasicSettingMapper.deleteRoofMaterialsAdd(csi.getObjectNo());
			
			int roofSeq = 1;
			 // for-each 루프를 사용하여 지붕재추가 Setting
	        for (RoofMaterialsAddInfo rma : csi.getRoofMaterialsAddList()) {
	        	
	        	rma.setObjectNo(csi.getObjectNo());
	        	rma.setRoofSeq(roofSeq++); //roofSeq는 순차적으로 새로 생성하여 insert
	            
	        	// 신규 지붕재추가 정보 insert
	            canvasBasicSettingMapper.insertRoofMaterialsAdd(rma);
	        }
	        response.put("objectNo", csi.getObjectNo());
	        response.put("returnMessage", "common.message.confirm.mark");
			
		} catch (Exception e) {
			response.put("objectNo", csi.getObjectNo());
			response.put("returnMessage", "common.message.save.error");
		}

		// 생성된 objectNo 반환
		return response;

	}

}
