package com.dev.swms.server.model.base;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import com.dev.energy.server.model.Entity;
import com.dev.swms.server.model.inventory.Inventory;
import com.dev.swms.server.model.receiving.ASN;
import com.dev.swms.server.model.shipping.Label;
import com.dev.swms.server.model.shipping.PickTicket;
import com.dev.swms.server.service.GlobalParamUtils;

public class SignFile extends Entity {

	private static final long serialVersionUID = 1L;
	private Label label; //贴标上传
	private Item item; //商品
	private ASN asn;//入库
	private PickTicket pickTicket;//出库
	private Inventory inventory;//库存
	private String fileUrl;//记录文件存放目录的html代码
	private String fileName; //附件名称
	private String imageType;//影像类型
	private Date fileDate;
	/**对象类型*/
	private String objectType;
	/** 操作人*/
	private String operate;
	/** 操作时间*/
	private Date operateDate;
	/**对象序号*/
	private Long objectId;
	/**显示名称*/
	private String viewName;
	/**备注*/
	private String remark;
	
	
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	public ASN getAsn() {
		return asn;
	}
	public void setAsn(ASN asn) {
		this.asn = asn;
	}
	public PickTicket getPickTicket() {
		return pickTicket;
	}
	public void setPickTicket(PickTicket pickTicket) {
		this.pickTicket = pickTicket;
	}
	public Inventory getInventory() {
		return inventory;
	}
	public void setInventory(Inventory inventory) {
		this.inventory = inventory;
	}
	public String getViewName() {
		return viewName;
	}
	public void setViewName(String viewName) {
		this.viewName = viewName;
	}
	
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public Long getObjectId() {
		return objectId;
	}
	public void setObjectId(Long objectId) {
		this.objectId = objectId;
	}
	public String getObjectType() {
		return objectType;
	}
	public void setObjectType(String objectType) {
		this.objectType = objectType;
	}
	public String getOperate() {
		return operate;
	}
	public void setOperate(String operate) {
		this.operate = operate;
	}
	public Date getOperateDate() {
		return operateDate;
	}
	public void setOperateDate(Date operateDate) {
		this.operateDate = operateDate;
	}
	public Date getFileDate() {
		return fileDate;
	}
	public void setFileDate(Date fileDate) {
		this.fileDate = fileDate;
	}
	public String getImageType() {
		return imageType;
	}
	public void setImageType(String imageType) {
		this.imageType = imageType;
	}
	protected SignFile(){}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	
 
	public Label getLabel() {
		return label;
	}
	public void setLabel(Label label) {
		this.label = label;
	}
/*	public boolean equals(final Object other) {
		if (!(other instanceof SignFile))
			return false;
		SignFile castOther = (SignFile) other;
		return new EqualsBuilder().append(label, castOther.getLabel()).append(
				fileName, castOther.getFileName()).isEquals();
	}
	public int hashCode() {
		return new HashCodeBuilder().append(label).append(fileName)
				.toHashCode();
	}*/
	public String getFileUrl() {
		return fileUrl;
	}
	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}
	
	
	/**
	 *
	 *@date: 2008-6-10 下午12:21:21
	 *@description: 将源回单影像移动至目标文件夹内,并删除源文件夹下的回单影像
	 *
	 */
	public void moveSignImage(Map map){
		
		String imageTarget = map.get("imageTarget").toString();
		String imageSource = map.get("imageSource").toString();
		
		String existStr = GlobalParamUtils.getGloableStringValue("imageTarget");
		File existFile = new File(existStr);
	
		if(!existFile.exists()){
			existFile.mkdirs();
		}
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		File source_file = null;
		try {
			source_file = new File(imageSource);
			
			in = new BufferedInputStream(
								new FileInputStream(source_file));
			out = new BufferedOutputStream(
								new FileOutputStream(
										new File(imageTarget)));
			
			int len;
			while((len = in.read())!= -1){
				out.write(len);
			}
		}catch(IOException e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
				out.close();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		System.gc();
		source_file.delete();
	}
	
	/**
	 *
	 *@date: 2008-6-11 下午03:40:42
	 *@description: 处理图片FileUrl 参数,传入图片路径
	 *
	 */
	public void processFileUrl(String imageTarget){
		String fileUrl = "<img src=\"*.asnImageServlet?imageSourcePath=" + imageTarget + "\" ";
		if(imageTarget.indexOf(".PDF")>0||imageTarget.indexOf(".pdf")>0){
			fileUrl ="<a class=\"media\" href=\"*.asnImageServlet?imageSourcePath=" + imageTarget + "\" "; 
		}
		fileUrl = fileUrl + " title=" + this.fileName + " />";
		this.fileUrl = fileUrl;
	}
}
