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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.web.annotation.Path;
import org.jfw.apt.web.annotation.operate.Get;
import org.jfw.apt.web.annotation.operate.Post;
import org.jfw.apt.web.annotation.param.JdbcConn;
import org.jfw.util.StringUtil;
import org.jfw.util.exception.JfwBaseException;
import org.jfw.util.io.IoUtil;

import com.ekexiu.portal.dao.AccessoryDao;
import com.ekexiu.portal.po.Accessory;

@Path("/accessory")
public class AccessoryService {
	private File tmpPath;
	private File accessoryPath;
	@Autowrie
	private AccessoryDao accessoryDao;

	public AccessoryDao getAccessoryDao() {
		return accessoryDao;
	}
	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}
	
	
	public File getTmpPath() {
		return tmpPath;
	}
	public void setTmpPath(File tmpPath) {
		this.tmpPath = tmpPath;
	}

	public File getAccessoryPath() {
		return accessoryPath;
	}
	public void setAccessoryPath(File accessoryPath) {
		this.accessoryPath = accessoryPath;
	}
	
//	/**
//	 * 保存附件功能
//	 * @param fn 临时文件名
//	 * @param operationId 资源申请ID
//	 * @param fileName 文件名
//	 * @throws IOException
//	 * @throws JfwBaseException
//	 */
//	public void saveAccessory(String fn, String operationId, String fileName) throws IOException, JfwBaseException {
//		InputStream in = new FileInputStream(new File(this.tmpPath, fn));
//		//以月为单位创建时间目录
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
//		String date = df.format(new Date());
//		String datePath = this.accessoryPath.getPath() + "/" +  date;
//		File dateFile = new File(datePath);
//		if (!datePath.exists()) {
//			// 创建日期目录
//			datePath.mkdir();
//		}
//		try {
//			IoUtil.copy(in, new FileOutputStream(new File(datePath, operationId + fileName)), false, true);
//		} finally {
//			in.close();
//		}
//	}

	@Post
	@Path("/save")
	public String insert(@JdbcConn(true) Connection con, String operationId, String professorId,  String fileName, 
			String fn) throws SQLException, IOException, JfwBaseException {
		InputStream in = new FileInputStream(new File(this.tmpPath, fn));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		String date = df.format(new Date());
		String datePath = this.accessoryPath + "/" +  date;
		String realFileName = operationId + "-" +fileName;
		File dateFile = new File(datePath);
		if (!dateFile.exists()) {
			// 创建日期目录
			dateFile.mkdir();
		}
		try {
			IoUtil.copy(in, new FileOutputStream(new File(dateFile, realFileName)), false, true);
		} finally {
			in.close();
		}
		String accessorySrc = datePath + "/" + realFileName;
		Accessory accessory = new Accessory();
		accessory.setAccessoryId(StringUtil.buildUUID());
		accessory.setAccessorySrc(accessorySrc);
		accessory.setOperationId(operationId);
		accessory.setProfessorId(professorId);
		accessory.setAccessoryName(fileName);
		this.accessoryDao.insert(con, accessory);
		return accessory.getAccessoryId();
	}
	
	@Get
	@Path("/qaoper")
	public List<Accessory> queryOper(@JdbcConn Connection con, String operationId) throws SQLException {
		return this.accessoryDao.queryOper(con, operationId);
	}
	
	@Get
	@Path("/qapro")
	public List<Accessory> quetyPro(@JdbcConn Connection con, String professorId) throws SQLException {
		return this.accessoryDao.queryPro(con, professorId);
	}
}
