package com.qmth.qrzk.usedbatch.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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 com.qmth.qrzk.batch.model.BatchGJCourse;
import com.qmth.qrzk.dao.repositories.BatchGJCourseRepo;
import com.qmth.qrzk.dao.repositories.BatchRepo;
import com.qmth.qrzk.repository.common.base.BaseDao;
import com.qmth.qrzk.repository.service.query.DbfPaperInfoQueryParams;
import com.qmth.qrzk.repository.user.model.DbfPaperInfo;
import com.qmth.qrzk.repository.user.model.PaperBatch;

@Service
public class UsedBatchService {
	@Autowired private BatchGJCourseRepo gjRepo;
	@Autowired private BatchRepo batchRepo;
	@Autowired private BaseDao dao;
	
	/**
	 * 导入启用批次下的共卷课程
	 * @param is
	 * @param batchId
	 * @throws IOException
	 */
	@Transactional
	public String uploadGJCourses(InputStream is,Integer batchId) throws IOException{
		DbfPaperInfoQueryParams params = new DbfPaperInfoQueryParams();
		params.setPaperBatchId(batchId);
		List<DbfPaperInfo> dbfPaperInfos = dao.getList("queryDbfPaperInfo", params);
		Set<String> batchCourseCodes = new HashSet<String>();
		for(DbfPaperInfo dbfPaperInfo : dbfPaperInfos){
			batchCourseCodes.add(dbfPaperInfo.getCourseCode());
		}
		
		//excel导入
		Workbook wb = new HSSFWorkbook(is);  
		Sheet sheet = wb.getSheetAt(0);
		int rowIndex = 0;
		List<BatchGJCourse> gjCourses = new ArrayList<BatchGJCourse>();
		boolean existsInBatch = true;
		String notExistsCourseCode = "";
		
		PaperBatch batch = batchRepo.getOne(batchId);
		for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext() && existsInBatch;) {  
            Row row = rit.next();
            if(rowIndex != 0){
            	BatchGJCourse gjCourse = new BatchGJCourse();
            	gjCourse.setBatchId(batchId);
            	gjCourse.setBatchTitle(batch.getTitle());
	            
	            for (int cellIndex = 0; cellIndex < 4; cellIndex++) {  
	            	Cell cell = row.getCell(cellIndex);
	            	
		            String strCell = "";
		            if(cell != null){
			            switch (cell.getCellType()){
			            case HSSFCell.CELL_TYPE_NUMERIC:strCell = String.valueOf(cell.getNumericCellValue());
			            break;
			            case HSSFCell.CELL_TYPE_STRING:strCell = cell.getStringCellValue();
			            break;
			            case HSSFCell.CELL_TYPE_BOOLEAN:strCell = String.valueOf(cell.getBooleanCellValue());
			            break;
			            default:strCell = "";
			            break;
			            }
		            }
		            if(cellIndex == 0){
		            	if(!batchCourseCodes.contains(strCell)){
		            		existsInBatch = false;
		            		notExistsCourseCode = strCell;
		            	}
		            	gjCourse.setCourseCode(strCell);
		        	}
		            else if(cellIndex == 1){
		            	gjCourse.setCourseName(strCell);
		            }
		            else if(cellIndex == 2){
		            	if(!batchCourseCodes.contains(strCell)){
		            		existsInBatch = false;
		            		notExistsCourseCode = strCell;
		            	}
		            	gjCourse.setbCourseCode(strCell);
		            }
		            else if(cellIndex == 3){
		            	gjCourse.setbCourseName(strCell);
		            }
	            }
	            
	            gjCourses.add(gjCourse);
            }
            rowIndex++;
        }
		
		if(!existsInBatch){
			return "课程代码("+notExistsCourseCode+")在批次中不存在！第" + rowIndex + "行。";
		}else{
			//导入前清空该批次下所有课程
			gjRepo.deleteFromBatch(batchId);
			gjRepo.save(gjCourses);
			return null;
		}
	}
	
}
