package com.uas.platform.b2b.service.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uas.platform.b2b.core.BaseUtil;
import com.uas.platform.b2b.dao.AttachDao;
import com.uas.platform.b2b.model.Attach;
import com.uas.platform.b2b.model.FileUpload;
import com.uas.platform.b2b.service.AttachService;


@Service
public class AttachServiceImpl implements AttachService{

	@Autowired
	private AttachDao attachDao;
	
	@Override
	public Attach getAttachById(Long id) {
		return attachDao.findAttachByAtId(id);
	}

	@Override
	public Attach saveAttachPath(Attach attach) {
		return attachDao.save(attach);
	}

	@Override
	public Attach uploadAndSaveAttach(FileUpload uploadItem) {
		String filename = uploadItem.getFile().getOriginalFilename();
		long size = uploadItem.getFile().getSize();
		if (size > 104857600) {
			return null;//文件过大，上传失败
		}
		String path = getFilePath(filename, "postattach", "bussinessCodeImg");
		File file = new File(path);
		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		try {
			bos = new BufferedOutputStream(new FileOutputStream(file));
			bis = new BufferedInputStream(uploadItem.getFile().getInputStream());
			int c;
			while ((c = bis.read()) != -1) {
				bos.write(c);
				bos.flush();
			}
			bis.close();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Attach attach = new Attach();
		attach.setAtPath(path);
		attach.setAtDescription("客户营业执照复印件或照片");
		attach.setAtName(filename);
		Attach at = saveAttachPath(attach);
		return at;
	}
	
	@Override
	public Attach uploadAttach(FileUpload uploadItem) {
		String filename = uploadItem.getFile().getOriginalFilename();
		long size = uploadItem.getFile().getSize();
		if (size > 104857600) {
			return null;//文件过大，上传失败
		}
		String path = getFilePath(filename, "postattach", "bussinessCodeImg");
		File file = new File(path);
		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		try {
			bos = new BufferedOutputStream(new FileOutputStream(file));
			bis = new BufferedInputStream(uploadItem.getFile().getInputStream());
			int c;
			while ((c = bis.read()) != -1) {
				bos.write(c);
				bos.flush();
			}
			bis.close();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Attach attach = new Attach();
		attach.setAtPath(path);
		attach.setAtDescription("客户营业执照复印件或照片");
		attach.setAtName(filename);
		return attach;
	};

	@Override
	public String getFilePath(String fileName, String dirName, String userName) {
		String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
		String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length())
				: "";
		String path = BaseUtil.FILEPATH + dirName;
		File file = new File(path);
		if (!file.isDirectory()) {
			file.mkdir();
			path = path + File.separator + userName;
			new File(path).mkdir();
		} else {
			path = path + File.separator + userName;
			file = new File(path);
			if (!file.isDirectory()) {
				file.mkdir();
			}
		}
		return path + File.separator + uuid + suffix;
	}

}
