package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.File;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

@Repository("fileRepository")
public class FileRepository extends BasicGenericDao<File, Long> {

	public FileRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
		super(jdbcTemplate, npJdbcTemplate);
	}

	public File queryByUuid(String uuid) throws Exception {
		String sql = "select  file as file_bytes,uuid,file_name,gmt_create,creator_id from ci_file where uuid = :uuid";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		List<File> list = this.list(File.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public File queryByFileName(String filename) throws Exception {
		String sql = "select file as file_bytes,uuid,file_name,gmt_create,creator_id from ci_file where file_name = :filename";
		Map<String, Object> params = MapTools.simpleMap("filename", filename);
		List<File> list = this.list(File.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

}
