package com.qmth.qrzk.dbf;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.qmth.qrzk.repository.common.enumeration.PaperStatusEnum;
import com.qmth.qrzk.repository.user.model.DbfPaperInfo;
import com.qmth.qrzk.repository.user.model.Paper;
import com.qmth.qrzk.repository.utils.General;

public class ReadDBF {
	
	public static List<Paper> readDBFFile(InputStream fis) throws DbfImportException{
		List<Paper> ls=new ArrayList<Paper>();
		int KCDMIndex = -1;
		int KCMCIndex = -1;
		int JCIndex = -1;
		int BZIndex = -1;
		int XXMCIndex = -1;
		int CBSIndex = -1;
		int BCIndex = -1;
		int SJBHIndex = -1;
		int QYIndex = -1;
		int ZKIndex = -1;
		try {
			DBFReader reader = new DBFReader(fis);			
			reader.setCharactersetName("GBK");
			int count=reader.getFieldCount();
			for(int i=0;i<count;i++){
				DBFField field=reader.getField(i);
				String name=field.getName();
				if(name.equals("KCDM")){
					KCDMIndex=i;
				}else if(name.equals("KCMC")){
					KCMCIndex=i;
				}else if(name.equals("JC")){
					JCIndex=i;
				}else if(name.equals("BZ")){
					BZIndex=i;
				}else if(name.equals("XXMC")){
					XXMCIndex=i;
				}else if(name.equals("CBS")){
					CBSIndex=i;
				}else if(name.equals("BC")){
					BCIndex=i;
				}else if(name.equals("SJBH")){
					SJBHIndex=i;
				}else if(name.equals("QY")){
					QYIndex=i;
				}else if(name.equals("ZK")){
					ZKIndex=i;
				}
			}

			if(KCDMIndex == -1 || JCIndex == -1 || BZIndex == -1 || XXMCIndex == -1 || CBSIndex == -1 || BCIndex == -1 || SJBHIndex == -1 || QYIndex == -1 ){
				throw new DbfImportException("dbf文件缺少必要的列！");
			}
			Object[] rowValues;
			// 一条条取出path文件中记录
			while ((rowValues = reader.nextRecord()) != null) {
				String KCDM=rowValues[KCDMIndex].toString().trim();						//科目代码
				String KCMC=rowValues[KCMCIndex].toString().trim();						//科目名称
				String JC=rowValues[JCIndex].toString().trim();							//教材名称
				String BZ=rowValues[BZIndex].toString().trim();							//编者
				String XXMC=rowValues[XXMCIndex].toString().trim();						//学校名称
				String CBS=rowValues[CBSIndex].toString().trim();						//出版社
				String BC=rowValues[BCIndex].toString().trim();							//版次
				String SJBH=rowValues[SJBHIndex].toString().trim();						//试卷编号
				String QY=rowValues[QYIndex].toString().trim();							//清样
				int ZK=0;
				if(ZKIndex!=-1){
					String str=rowValues[ZKIndex].toString().trim();
					if(str.equals("")||str==null){
						ZK=0;
					}else{
						String zkStr=rowValues[ZKIndex].toString().trim();
						if(zkStr.equals("1")){
							ZK=1;
						}else {
							ZK=0;
						}
					}
				}
				
				
				String status;
				if(QY.equals("1")){
					status=PaperStatusEnum.PAPER_FORMAL.getKey();
				}else {
					status=PaperStatusEnum.PAPER_DRAFT.getKey();
				}
				
				int isSpecialAnswerSheet;
				if(ZK==1){
					isSpecialAnswerSheet=1;
				}else{
					isSpecialAnswerSheet=0;
				}
				Paper p=new Paper(KCDM,KCMC,JC,BZ,CBS,BC,XXMC,SJBH,status,isSpecialAnswerSheet);
				ls.add(p);
			}
			
		} catch (DbfImportException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ls;
	}
	
	public static List<Paper> readXlsFile(InputStream fis) throws XlsImportException{
		List<Paper> ls=new ArrayList<Paper>();
		int KCDMIndex = -1;
		int KCMCIndex = -1;
		int JCIndex = -1;
		int BZIndex = -1;
		int XXMCIndex = -1;
		int CBSIndex = -1;
		int BCIndex = -1;
		int SJBHIndex = -1;
		int QYIndex = -1;
		int ZKIndex = -1;
		try {
			HSSFWorkbook wb = new HSSFWorkbook(fis);  
			HSSFSheet sheet = wb.getSheetAt(0);
			HSSFRow titleRow = sheet.getRow(0);
			
			int count = titleRow.getPhysicalNumberOfCells();
			for(int i=0; i<count; i++){
				HSSFCell cell = titleRow.getCell(i);
				String name = General.getCellTextForShow(cell);
				if(name.equals("KCDM")){
					KCDMIndex=i;
				}else if(name.equals("KCMC")){
					KCMCIndex=i;
				}else if(name.equals("JC")){
					JCIndex=i;
				}else if(name.equals("BZ")){
					BZIndex=i;
				}else if(name.equals("XXMC")){
					XXMCIndex=i;
				}else if(name.equals("CBS")){
					CBSIndex=i;
				}else if(name.equals("BC")){
					BCIndex=i;
				}else if(name.equals("SJBH")){
					SJBHIndex=i;
				}else if(name.equals("QY")){
					QYIndex=i;
				}else if(name.equals("ZK")){
					ZKIndex=i;
				}
			}

			if(KCDMIndex == -1 || JCIndex == -1 || BZIndex == -1 || XXMCIndex == -1 || CBSIndex == -1 || BCIndex == -1 || SJBHIndex == -1 || QYIndex == -1 ){
				throw new XlsImportException("xls文件缺少必要的列！");
			}
			// 一条条取出path文件中记录
			Iterator<Row> rit = sheet.rowIterator();
			if(rit.hasNext()){//跳过标题行
				rit.next();
			}
			while (rit.hasNext()){
				HSSFRow row = (HSSFRow) rit.next();
				String KCDM = General.getCellTextForShow(row.getCell(KCDMIndex));		//科目代码
				String KCMC = General.getCellTextForShow(row.getCell(KCMCIndex));		//科目名称
				String JC = General.getCellTextForShow(row.getCell(JCIndex));				//教材名称
				String BZ = General.getCellTextForShow(row.getCell(BZIndex));				//编者
				String XXMC = General.getCellTextForShow(row.getCell(XXMCIndex));			//学校名称
				String CBS = General.getCellTextForShow(row.getCell(CBSIndex));			//出版社
				String BC = General.getCellTextForShow(row.getCell(BCIndex));				//版次
				String SJBH = General.getCellTextForShow(row.getCell(SJBHIndex));			//试卷编号
				String QY = General.getCellTextForShow(row.getCell(QYIndex));				//清样
				int ZK=0;
				if(ZKIndex != -1){
					String str = General.getCellTextForShow(row.getCell(ZKIndex));
					if(str.equals("")||str==null){
						ZK=0;
					}else{
						String zkStr = General.getCellTextForShow(row.getCell(ZKIndex));
						if(zkStr.equals("1")){
							ZK=1;
						}else {
							ZK=0;
						}
					}
				}
				
				
				String status;
				if(QY.equals("1")){
					status=PaperStatusEnum.PAPER_FORMAL.getKey();
				}else {
					status=PaperStatusEnum.PAPER_DRAFT.getKey();
				}
				
				int isSpecialAnswerSheet;
				if(ZK==1){
					isSpecialAnswerSheet=1;
				}else{
					isSpecialAnswerSheet=0;
				}
				Paper p=new Paper(KCDM,KCMC,JC,BZ,CBS,BC,XXMC,SJBH,status,isSpecialAnswerSheet);
				ls.add(p);
			}
			
		} catch (XlsImportException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ls;
	}
	
	public static List<DbfPaperInfo> readDbfPaperInfo(InputStream fis) throws DbfImportException {
		List<DbfPaperInfo> ls=new ArrayList<DbfPaperInfo>();
		try {
			// 根据输入流初始化一个DBFReader实例，用来读取DBF文件信息
			DBFReader reader = new DBFReader(fis);
			int KCDMIndex = -1;
			int KCMCIndex = -1;
			int JCIndex = -1;
			int BZIndex = -1;
			int CBSIndex = -1;
			int BCIndex = -1;
			
			int count=reader.getFieldCount();
			for(int i=0;i<count;i++){
				DBFField field=reader.getField(i);
				String name=field.getName();
				if(name.equals("KCDM")){
					KCDMIndex=i;
				}else if(name.equals("KCMC")){
					KCMCIndex=i;
				}else if(name.equals("JC")){
					JCIndex=i;
				}else if(name.equals("BZ")){
					BZIndex=i;
				}else if(name.equals("CBS")){
					CBSIndex=i;
				}else if(name.equals("BC")){
					BCIndex=i;
				}
			}
			
			if(KCDMIndex == -1 || JCIndex == -1 || BZIndex == -1 || CBSIndex == -1 || BCIndex == -1){
				throw new DbfImportException("dbf文件缺少需要的列！");
			}
			
			Object[] rowValues;
			// 一条条取出path文件中记录
			while ((rowValues = reader.nextRecord()) != null) {
				String KMDC=rowValues[KCDMIndex].toString().trim();						//科目代码
				String KMMC=rowValues[KCMCIndex].toString().trim();						//科目名称
				String JC=rowValues[JCIndex].toString().trim();						//教材名称
				String BZ=rowValues[BZIndex].toString().trim();						//编者
				String CBS=rowValues[CBSIndex].toString().trim();						//出版社
				String BC=rowValues[BCIndex].toString().trim();						//版次
				
				DbfPaperInfo p=new DbfPaperInfo(KMDC,KMMC,JC,BZ,CBS,BC);
				ls.add(p);
			}
		} catch (DbfImportException e) {
			throw e;
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ls;
	}
	
}
