package com.qmth.qrzk.service;

import com.qmth.qrzk.dao.repositories.PaperAnalysisRepo;
import com.qmth.qrzk.dbf.XlsImportException;
import com.qmth.qrzk.repository.user.model.PaperAnalysis;
import com.qmth.qrzk.repository.user.model.PaperBatch;
import com.qmth.qrzk.repository.utils.General;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 *
 */
@Service
public class PaperAnalysisService {


    @Autowired
    private PaperAnalysisRepo paperAnalysisRepo;

    @Transactional
    public void upload(InputStream is, Integer batchId) throws XlsImportException {
        int curRowIndex = 0;
        int curCellIndex = 0;
        String curCellStr = null;
        try {
            //excel导入
            Workbook wb = new HSSFWorkbook(is);
            Sheet sheet = wb.getSheetAt(0);
            int rowIndex = 0;
            PaperBatch pb = new PaperBatch();
            pb.setId(batchId);
            List<PaperAnalysis> pas = new ArrayList();
            for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext(); ) {
                Row row = rit.next();
                curRowIndex = rowIndex;
                curCellIndex=0;
                if (rowIndex == 0) {
                    rowIndex++;
                    continue;
                }
                PaperAnalysis pa = new PaperAnalysis();
                pa.setPaperBatch(pb);
                pa.setCourseCode(General.getCellTextForShow((HSSFCell) row.getCell(0)));
                curCellIndex++;
                pa.setCourseName(General.getCellTextForShow((HSSFCell) row.getCell(1)));
                curCellIndex++;
                pa.setProvince(General.getCellTextForShow((HSSFCell) row.getCell(2)));
                curCellIndex++;
                pa.setCoverage(General.getCellTextForShow((HSSFCell) row.getCell(3)));
                curCellIndex++;
                pa.setScientificity(General.getCellTextForShow((HSSFCell) row.getCell(4)));
                curCellIndex++;
                pa.setFacilityValue(General.getCellTextForShow((HSSFCell) row.getCell(5)));
                curCellIndex++;
                pa.setCognitiveLevel(General.getCellTextForShow((HSSFCell) row.getCell(6)));
                curCellIndex++;
                pa.setQuestionType(General.getCellTextForShow((HSSFCell) row.getCell(7)));
                curCellIndex++;
                pa.setPaperNorm(General.getCellTextForShow((HSSFCell) row.getCell(8)));
                curCellIndex++;
                pa.setActualNumber(Integer.valueOf(General.getCellTextForShow((HSSFCell) row.getCell(9))));
                curCellIndex++;
                pa.setPassesNumber(Integer.valueOf(General.getCellTextForShow((HSSFCell) row.getCell(10))));
                curCellIndex++;
                pa.setPassRate(Float.valueOf(General.getCellTextForShow((HSSFCell) row.getCell(11))));
                curCellIndex++;
                pa.setAverage(Float.valueOf(General.getCellTextForShow((HSSFCell) row.getCell(12))));
                curCellIndex++;
                pa.setRemarks(General.getCellTextForShow((HSSFCell) row.getCell(13)));
                curCellIndex++;
                pas.add(pa);
                rowIndex++;
            }
            paperAnalysisRepo.save(pas);
        } catch (XlsImportException e) {
            throw e;
        } catch (Exception e) {
            throw new XlsImportException("解析Excel出现异常，单元格坐标" + General.toExcelCoordinate(curRowIndex, curCellIndex) , e);
        }
    }
}
	
