package com.qmth.qrzk.dbf;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFWriter;

public class WriteDBF {
	public static void writeToDBF(List<com.qmth.qrzk.paper.model.Paper> list,String path) {
		OutputStream fos = null;
		try {
			// 定义DBF文件字段
			DBFField[] fields = new DBFField[10];
			// 分别定义各个字段信息，setFieldName和setName作用相同,
			// 只是setFieldName已经不建议使用
			// fields[0].setFieldName("emp_code");
			fields[0] = new DBFField();
			fields[0].setName("SJBH");
			fields[0].setDataType(DBFField.FIELD_TYPE_C);
			fields[0].setFieldLength(14);
			
			
			fields[1] = new DBFField();
			fields[1].setName("QY");
			fields[1].setDataType(DBFField.FIELD_TYPE_C);
			fields[1].setFieldLength(20);
			
			
			fields[2] = new DBFField();
			fields[2].setName("KCDM");
			fields[2].setDataType(DBFField.FIELD_TYPE_C);
			fields[2].setFieldLength(6);
			
			fields[3] = new DBFField();
			fields[3].setName("KCMC");
			fields[3].setDataType(DBFField.FIELD_TYPE_C);
			fields[3].setFieldLength(50);
			
			fields[4] = new DBFField();
			fields[4].setName("JC");
			fields[4].setDataType(DBFField.FIELD_TYPE_C);
			fields[4].setFieldLength(80);

			fields[5] = new DBFField();
			fields[5].setName("BZ");
			fields[5].setDataType(DBFField.FIELD_TYPE_C);
			fields[5].setFieldLength(20);
			
			fields[6] = new DBFField();
			fields[6].setName("CBS");
			fields[6].setDataType(DBFField.FIELD_TYPE_C);
			fields[6].setFieldLength(30);
			
			fields[7] = new DBFField();
			fields[7].setName("BC");
			fields[7].setDataType(DBFField.FIELD_TYPE_C);
			fields[7].setFieldLength(30);
			
			fields[8] = new DBFField();
			fields[8].setName("XXMC");
			fields[8].setDataType(DBFField.FIELD_TYPE_C);
			fields[8].setFieldLength(30);
			
			fields[9] = new DBFField();
			fields[9].setName("ZK");
			fields[9].setDataType(DBFField.FIELD_TYPE_C);
			fields[9].setFieldLength(5);
			
			
			// DBFWriter writer = new DBFWriter(new File(path));
			// 定义DBFWriter实例用来写DBF文件
			DBFWriter writer = new DBFWriter();
			// 把字段信息写入DBFWriter实例，即定义表结构
			writer.setFields(fields);
			// 一条条的写入记录
			
			for(int i=0;i<list.size();i++){
				Object[] rowData = new Object[10];
				rowData[2]=String.valueOf(list.get(i).getCourseCode());
				rowData[3]=list.get(i).getCourseName();
				rowData[4]=list.get(i).getBookName();
				rowData[5]=list.get(i).getBookAuthor();
				
				rowData[8]=list.get(i).getSchool();
				rowData[6]=list.get(i).getBookPress();
				rowData[7]=list.get(i).getBookPressYear();
				rowData[0]=list.get(i).getCode();
				String QY=list.get(i).getStatus();
				if("DRAFT".equals(QY)){
					rowData[1]="毛坯";
				}else if("FORMAL".equals(QY)){
					rowData[1]="清样";
				}else{
					rowData[1]="抽卷";
				}
				if(list.get(i).getIsSpecialAnswerSheet()){
					rowData[9]="是";
				}else{
					rowData[9]="不详";
				}
				writer.addRecord(rowData);
				
			}
			fos = new FileOutputStream(path);
			writer.write(fos);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(fos!=null){
				try {
					fos.flush();
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
