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

import javax.annotation.Resource;

import kr.co.jinwoosi.clfd.conts.banner.mapper.BannerMapper;
import kr.co.jinwoosi.clfd.conts.banner.model.Banner;
import kr.co.jinwoosi.clfd.conts.banner.model.BannerSearch;
import kr.co.jinwoosi.clfd.conts.banner.service.BannerService;
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="bannerService")
public class BannerServiceImpl extends BaseServiceImpl<Banner, BannerSearch, BannerMapper> implements BannerService {

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

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

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