package com.ssj.service.ai.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.ssj.bean.kmt.kmt.domain.KmtQuestion;
import com.ssj.bean.kmt.kmt.domain.KmtQuestionKnow;
import com.ssj.bean.kmt.kmt.domain.KmtQuestionTempExam;
import com.ssj.service.ai.model.KmtQuestionModel;



public class ExcelUtils {
	
	public static List<KmtQuestionTempExam> runExamExcelUtils(String path){
		List<List<String>>  data=importExecl(path);
		List<KmtQuestionTempExam> kmtQuestionModels=new ArrayList<KmtQuestionTempExam>();

		for (List<String> list : data) {
			
			KmtQuestionTempExam qu=new KmtQuestionTempExam();
			qu.setCourseName(list.get(0).toString().trim());
			qu.setQuestion(list.get(1).toString().trim());
			qu.setOptionContent(StringUtils.isNotEmpty(list.get(2).toString().trim())?list.get(2).toString().trim():null);
			qu.setDegree(StringUtils.isNotEmpty(list.get(3).toString().trim())?list.get(3).toString().trim():null);
			qu.setSource(StringUtils.isNotEmpty(list.get(4).toString().trim())?list.get(4).toString().trim():null);
			qu.setExplainText(StringUtils.isNotEmpty(list.get(5).toString().trim())?list.get(5).toString().trim():null);
			qu.setId(StringUtils.isNotEmpty(list.get(6).toString().trim())?list.get(6).toString().trim():null);
			qu.setOptionAnswer(StringUtils.isNotEmpty(list.get(7).toString().trim())?list.get(7).toString().trim():null);
			qu.setCreateTime(new Date());

			kmtQuestionModels.add(qu);
		}
		return kmtQuestionModels;
	}
	
	
	
	
	public static List<KmtQuestionModel> runExcelUtils(String path){
		List<List<String>>  data=importExecl(path);
		List<KmtQuestionModel> kmtQuestionModels=new ArrayList<KmtQuestionModel>();

		KmtQuestionModel kmtQuestionModel=null;
		for (List<String> list : data) {
			kmtQuestionModel=new KmtQuestionModel();
			
			KmtQuestion qu=new KmtQuestion();
			qu.setCourseName(list.get(0).toString().trim());
			qu.setId(list.get(2).toString().trim());
			qu.setParentId(StringUtils.isNotEmpty(list.get(3).toString().trim())?list.get(3).toString().trim():null);
			qu.setQuestion(list.get(8).toString().trim());
			qu.setOptionContent(StringUtils.isNotEmpty(list.get(9).toString().trim())?list.get(9).toString().trim():null);
			qu.setOptionAnswer(StringUtils.isNotEmpty(list.get(10).toString().trim())?list.get(10).toString().trim():null);
			qu.setOptionType(StringUtils.isNotEmpty(list.get(11).toString().trim())?list.get(11).toString().trim():null);
			qu.setEasy(StringUtils.isNotEmpty(list.get(12).toString().trim())?Integer.parseInt(list.get(12).toString().trim()):1); 
			qu.setSkill(StringUtils.isNotEmpty(list.get(13).toString().trim())?list.get(13).toString().trim():null);
			if("1".equals(list.get(1).toString().trim())){
				qu.setType("5");
			}else{
				qu.setType("3");
			}
			qu.setCreateTime(new Date());

			KmtQuestionKnow kmtQuestionKnow=new KmtQuestionKnow();
			kmtQuestionKnow.setCourseName(list.get(0).toString().trim());
			kmtQuestionKnow.setHowDetailsId(list.get(6).toString().trim());
			kmtQuestionKnow.setHowId(list.get(5).toString().trim());
			kmtQuestionKnow.setIsLoad(1);
			kmtQuestionKnow.setNum(0);
			kmtQuestionKnow.setTags(StringUtils.isNotEmpty(list.get(4).toString().trim())?list.get(4).toString().trim():null);
			kmtQuestionKnow.setHowDetailsKdId(StringUtils.isNotEmpty(list.get(7).toString().trim())?list.get(7).toString().trim():null);
			
			List<KmtQuestionKnow> kmtQuestionKnows=new ArrayList<KmtQuestionKnow>();
			kmtQuestionKnows.add(kmtQuestionKnow);
			
			kmtQuestionModel.setKmtQuestion(qu);
			kmtQuestionModel.setKmtQuestionKnows(kmtQuestionKnows);

			kmtQuestionModels.add(kmtQuestionModel);
		}
		return kmtQuestionModels;
	}
	
	 /**
     * excel表的导入功能
     * @作者：我
     * @param filePath  excel表的储存路径
     * @return List<List<String>> 行(列(,,,,,))
     */
    public static List<List<String>> importExecl(String filePath){
        List<List<String>> data=new ArrayList<List<String>>();
        File file=new File(filePath);
        //判断文件是否存在或者后缀名是否正确,否则返回为null
        if(!file.exists()||!(isExcel2003(filePath) || isExcel2007(filePath))){
            return null;
        }
        //判断文件的版本2003或者2007版
        InputStream is = null;
        //得到Excel工作簿对象
        Workbook wb = null;
        boolean isExcel2003 = true;
        if(isExcel2007(filePath)){
            isExcel2003 = false;
        }
        try {
            is =new FileInputStream(filePath);
            if(isExcel2003){
                System.out.println("本表是2003版的excel表");
                wb = new HSSFWorkbook(is);
            }else{
                System.out.println("本表是2007版的excel表");
                wb=new XSSFWorkbook(is);
            }
            //得到Excel工作表对象
            //得到第一个shell
            Sheet sheet = wb.getSheetAt(0);
            //得到所有行数
            int totalRows=sheet.getPhysicalNumberOfRows();
            // 得到Excel的列数
            int totalCells=0;
            if (totalRows >= 1 && sheet.getRow(0) != null){ 
                totalCells = sheet.getRow(0).getPhysicalNumberOfCells(); 
            }
            //循环行
            for(int rowNum=1;rowNum<totalRows;rowNum++){
                Row row = sheet.getRow(rowNum);
                if(row==null){
                    continue;
                }
                //存放本行中的所列的数据
                List<String> rowLst = new ArrayList<String>();
                //循环列
                for(int column=0;column<=totalCells;column++){
                    Cell cell = row.getCell(column);
                    String cellValue = "";
                    if(null!=cell){
                        //以下是判断数据的类型
                        switch (cell.getCellType()) {
                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                // 处理日期格式、时间格式 
                                SimpleDateFormat sdf = null; 
                                if (cell.getCellStyle().getDataFormat() == HSSFDataFormat 
                                        .getBuiltinFormat("h:mm")) { 
                                    sdf = new SimpleDateFormat("HH:mm"); 
                                } else {// 日期 
                                    sdf = new SimpleDateFormat("yyyy-MM-dd"); 
                                } 
                                java.util.Date date =cell.getDateCellValue();
                                cellValue = sdf.format(date); 
                            } else if (cell.getCellStyle().getDataFormat() == 58) { 
                                // 处理自定义日期格式：m月d日(通过判断单元格的格式id解决，id的值是58) 
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
                                double value = cell.getNumericCellValue(); 
                                java.util.Date date =org.apache.poi.ss.usermodel.DateUtil 
                                        .getJavaDate(value); 
                                cellValue = sdf.format(date); 
                            } else {
                                //村数字格式
                                double value = cell.getNumericCellValue(); 
                                CellStyle style = cell.getCellStyle(); 
                                DecimalFormat format = new DecimalFormat(); 
                                String temp = style.getDataFormatString(); 
                                // 单元格设置成常规 
                                if ("General".equals(temp)) { 
                                    format.applyPattern("#"); 
                                } 
                                cellValue = format.format(value); 
                            } 
                            break;
                        case HSSFCell.CELL_TYPE_STRING: // 字符串 
                            cellValue = cell.getStringCellValue(); 
                            break; 
       
                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean 
                            cellValue = cell.getBooleanCellValue() + ""; 
                            break; 
       
                        case HSSFCell.CELL_TYPE_FORMULA: // 公式 
                            cellValue = cell.getCellFormula() + ""; 
                            break; 
       
                        case HSSFCell.CELL_TYPE_BLANK: // 空值 
                            cellValue = ""; 
                            break; 
       
                        case HSSFCell.CELL_TYPE_ERROR: // 故障 
                            cellValue = "非法字符"; 
                            break;
                        default:
                            cellValue = "未知类型"; 
                            break;
                        }
                    }
                    rowLst.add(cellValue);
                }
                data.add(rowLst);
            }
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
    /**
     * 
     * @描述：是否是2003的excel，返回true是2003
     * 
     * @作者：我
     * @param filePath是文件名
     * @参数：@return
     * 
     * @返回值：boolean
     */ 
   
    public static boolean isExcel2003(String filePath) 
    { 
        return filePath.matches("^.+\\.(?i)(xls)$"); 
    } 
   
    /**
     * 
     * @描述：是否是2007的excel，返回true是2007
     * @作者：我
     * @param filePath是文件名
     * @参数：@return
     * 
     * @返回值：boolean
     */ 
   
    public static boolean isExcel2007(String filePath) 
    { 
        return filePath.matches("^.+\\.(?i)(xlsx)$"); 
    }
	
    

    
    
	
	
	

}
