package cokr.xit.fims.sprt.service.bean;

import java.util.List;

import javax.annotation.Resource;

import org.assertj.core.util.Arrays;
import org.springframework.stereotype.Service;

import cokr.xit.base.file.service.FileQuery;
import cokr.xit.base.file.service.FileService;
import cokr.xit.fims.crdn.dao.CrdnInfoMapper;
import cokr.xit.fims.sprt.Sprt;
import cokr.xit.fims.sprt.service.Sprt03Service;
import cokr.xit.foundation.component.AbstractServiceBean;
import cokr.xit.foundation.data.DataObject;

/**미디어 업무지원 서비스 인터페이스의 구현체
 * @author leebj
 */
@Service("sprt03Service")
public class Sprt03ServiceBean extends AbstractServiceBean implements Sprt03Service {

	@Resource(name = "crdnInfoMapper")
	private CrdnInfoMapper crdnInfoMapper;

	@Resource(name="fileService")
	FileService fileService;

	@Override
	public DataObject getVideoInfo(String crdnId) {
		DataObject fileInfo = new DataObject();

		DataObject cvlcptInfo = crdnInfoMapper.selectCrdnInfo(crdnId);
		String linkTblNm = cvlcptInfo.string("LINK_TBL_NM");
		if(!linkTblNm.equals("")) {
			FileQuery fileQuery = new FileQuery();

			if(linkTblNm.equals("TB_ESB_INTERFACE")) {
				fileQuery.setInfoType("010");
			} else if(linkTblNm.equals("TB_SAEOL")) {
				fileQuery.setInfoType("020");
			} else {
				throw new RuntimeException("파일 조회 중 오류가 발생하였습니다.");
			}
			fileQuery.setInfoKeys(cvlcptInfo.string("LINK_ID"));

			List<DataObject> fileList = fileService.getFileList(fileQuery);
			if(fileList != null && !fileList.isEmpty()) {
				for(int i=0; i < fileList.size() ;i++) {
					DataObject file = fileList.get(i);
					String mimeType = file.string("MIME_TYPE");
					if(Arrays.asList(Sprt.VIDEO_MIME_TYPE).contains(mimeType)){
						fileInfo = file;
						break;
					}
				}
			}
		}

		return fileInfo;
	}

}
