package kr.co.jinwoosi.clfd.conts.popup.service.impl;

import javax.annotation.Resource;

import kr.co.jinwoosi.clfd.conts.popup.mapper.PopupMapper;
import kr.co.jinwoosi.clfd.conts.popup.model.Popup;
import kr.co.jinwoosi.clfd.conts.popup.model.PopupSearch;
import kr.co.jinwoosi.clfd.conts.popup.service.PopupService;
import kr.co.jinwoosi.clfd.framework.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import kr.co.jinwoosi.clfd.framework.file.model.FileInfo;
import kr.co.jinwoosi.clfd.framework.file.model.FileItem;
import kr.co.jinwoosi.clfd.framework.file.service.FileItemService;
import kr.co.jinwoosi.clfd.framework.file.service.FileService;

@Service(value="popupService")
public class PopupServiceImpl extends BaseServiceImpl<Popup, PopupSearch, PopupMapper> implements PopupService {

	@Resource (name="fileItemService")
	private FileItemService fileItemService;
	
	@Resource (name="fileService")
	private FileService fileService;
	
	@Override
	@Resource (name="popupMapper")
	protected void setMapper (PopupMapper mapper) {
		super.setMapper (mapper);
	}

	@Override
	public void insert(Popup popup, Long itemId) {
		FileInfo file = new FileInfo();
		fileService.insert(file);
		
		FileItem fileItem = new FileItem();
		fileItem.setFileId(file.getFileId());
		fileItem.setItemId(itemId);
		fileItemService.updateFileId(fileItem);
		
		popup.setFileId(file.getFileId());
		insert(popup);
	}

	@Override
	public void update(Popup popup, Long itemId) {
		if(itemId != null) { //첨부파일 교체 시
			fileService.deleteFileItem(popup.getFileId());
			
			FileItem fileItem = new FileItem();
			fileItem.setFileId(popup.getFileId());
			fileItem.setItemId(itemId);
			fileItemService.updateFileId(fileItem);
		}
		
		update(popup);
	}

}
