package com.ekexiu.portal.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.web.annotation.Path;
import org.jfw.apt.web.annotation.operate.Delete;
import org.jfw.apt.web.annotation.operate.Get;
import org.jfw.apt.web.annotation.operate.Post;
import org.jfw.apt.web.annotation.operate.Put;
import org.jfw.apt.web.annotation.param.JdbcConn;
import org.jfw.apt.web.annotation.param.PathVar;
import org.jfw.util.StringUtil;
import org.jfw.util.io.IoUtil;

import com.ekexiu.portal.dao.ImageDao;
import com.ekexiu.portal.po.Image;

@Path("/images")
public class ImagesService {
	private File tmpPath;
	private File resourcePath;
	@Autowrie
	private ImageDao imageDao;
	
	public File getTmpPath() {
		return tmpPath;
	}

	public void setTmpPath(File tmpPath) {
		this.tmpPath = tmpPath;
	}

	public File getResourcePath() {
		return resourcePath;
	}

	public void setResourcePath(File resourcePath) {
		this.resourcePath = resourcePath;
	}

	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}
	
	@Post
	@Path("/resourceImg")
	public String insert(@JdbcConn(true) Connection con, String resourceId, String fn) throws SQLException, IOException {
		InputStream in = new FileInputStream(new File(this.tmpPath, fn));
		try {
			IoUtil.copy(in, new FileOutputStream(new File(this.resourcePath, resourceId + ".jpg")), false, true);
		} finally {
			in.close();
		}
		Image image1 = this.imageDao.queryOne(con, resourceId);
		if(null == image1){
			Image image = new Image();
			String imageId = StringUtil.buildUUID();
			image.setImageId(imageId);
			String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
			image.setImageSrc(imageSrc);
			image.setResourceId(resourceId);
			this.imageDao.insert(con, image);
			return imageId;
		} else {
			return null;
		}
	}
	
	@Put
	@Path
	public void update(@JdbcConn(true) Connection con, String resourceId) throws SQLException{
		
	}
	
	@Get
	@Path("/{imageId}")
	public Image query(@JdbcConn Connection con, @PathVar String imageId) throws SQLException {
		return this.imageDao.query(con, imageId);
	}
	
	@Get
	@Path("/qa")
	public List<Image> queryAll(@JdbcConn Connection con) throws SQLException {
		return this.imageDao.query(con);		
	}
	
	@Get
	@Path("/qares")
	public List<Image> queryRes(@JdbcConn Connection con, String resourceId) throws SQLException {
		return this.imageDao.queryRes(con, resourceId);
	}
	
	@Delete
	@Path("/{imageId}")
	public void deleteId(@JdbcConn(true) Connection con, @PathVar String imageId) throws SQLException {
		this.imageDao.deleteId(con, imageId);
	}
	
	@Delete
	@Path("/src")
	public void deleteSrc(@JdbcConn(true) Connection con, String imageSrc) throws SQLException {
		this.imageDao.deleteSrc(con, imageSrc);
	}
	
	@Delete
	@Path("/del")
	public void deleteRes(@JdbcConn(true) Connection con, String resourceId) throws SQLException {
		this.imageDao.deleteRes(con, resourceId);
	}
}
