package cn.report.excel.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.multipart.MultipartFile;
import cn.report.excel.easyexcel.api.EasyExcel;
import cn.report.excel.easyexcel.custom.NoModelListener;
import cn.report.excel.easyexcel.custom.ObjectListener;
import cn.util.core.IsKit;

/**
 * 读取工具类
 */
public class ReadExcelUtil {

	/**
	 * 返回Map型的实体模型数据
	 * @param fileName 文件名
	 * @param obj  对象反射类 xxx.Class
	 * @param sheetNo sheet编号
	 * @param headNum 表头行数，1表示1行
	 * @return
	 */
	public static Map<String, List<Object>> getMap(String fileName, Class<?> obj, Integer sheetNo, Integer headNum)
	{
		Map<String, List<Object>> map = new HashMap<>();
		//new监听器
		ObjectListener objectListener = new ObjectListener();
		//读取操作
		EasyExcel.read(fileName, obj, objectListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		//获取读取的数据
		List<Object> objList = objectListener.getObjectList();
		//获取sheet名
		String objSheetName = objectListener.getSheetName();
		map.put(objSheetName,objList);
		return map;
	}

	//返回List型的实体模型数据
	public static List<Object> getList(String fileName,Class<?> obj,Integer sheetNo,Integer headNum){
		ObjectListener objectListener = new ObjectListener();
		EasyExcel.read(fileName, obj, objectListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Object> objList = objectListener.getObjectList();
		return objList;
	}

	//返回有实体模型的表头
	public static List<Object> getHeadList(String fileName,Class<?> obj,Integer sheetNo,Integer headNum)
	{
		ObjectListener objectListener = new ObjectListener();
		EasyExcel.read(fileName, obj, objectListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Object> objList = objectListener.getHeadList();
		return objList;
	}


	/**返回Map型的没有实体模型数据(带有列号的数据)
	 * map<列号，数值> 
	 * */
	public static Map<String,List<Map<Integer, String>>> getMapNoModel(String fileName,Integer sheetNo,Integer headNum)
	{
		Map<String,List<Map<Integer, String>>> map = new HashMap<>();
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(fileName, noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Map<Integer, String>> lists = noModelListener.getLists();
		String sheetName = noModelListener.getSheetName();
		map.put(sheetName,lists);
		return map;
	}

	/**返回List型的没有实体模型数据(带有列号的数据)
	 * map<列号，数值> 
	 */
	public static List<Map<Integer, String>> getListNoModel(String fileName,Integer sheetNo,Integer headNum)
	{
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(fileName, noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Map<Integer, String>> lists = noModelListener.getLists();
		return lists;
	}

	/**
	 * 返回List型的没有实体模型数据(带有字段的数据)map<字段名，数值> 
	 * @param fileName : 文件名
	 * @param sheetNo : sheet编号
	 * @param headNum : 表头行数，1表示表头占1行
	 * @param rowNum :  数据行号，索引从0开始，即0为第一行
	 */
	public static List<Map<String, Object>> getListNoModel(
			String fileName,Integer sheetNo,
			Integer headNum,Integer rowNum)
	{
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(fileName,noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		//数据map<列号，数值> 
		List<Map<Integer,String>> rowListData = noModelListener.getLists();
		 //表头(String fileName,int sheetNo,Integer headNum,Integer rowNum)
        List<String> headList = getHeadListNoModel(fileName,sheetNo,headNum,rowNum);
             
        List<Map<String, Object>> resultList = new ArrayList<>();
        if(IsKit.isNullOrEmpty(rowListData)) {
        	 return resultList;
        }
        Integer rowSize = rowListData.size();
		//循环一次，处理一行数据
		for(Integer i=0; i<rowSize; ++i) 
		{
			Map<Integer, String> rowMap = rowListData.get(i);
			Map<String, Object> tempMap = new HashMap<>();
			for(Map.Entry<Integer, String> entry : rowMap.entrySet()){
				tempMap.put(headList.get(entry.getKey()), entry.getValue());
			}
			resultList.add(tempMap);
		}
       return resultList;
	}
	public static List<Map<String, Object>> getListNoModel(
			MultipartFile mFile,int sheetNo,
			Integer headNum,Integer rowNum) throws IOException
	{
		InputStream inputStream = mFile.getInputStream();
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(inputStream,noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		//数据map<列号，数值> 
		List<Map<Integer,String>> rowListData = noModelListener.getLists();
		 //表头(String fileName,int sheetNo,Integer headNum,Integer rowNum)
        List<String> headList = getHeadListNoModel(mFile,sheetNo,headNum,rowNum);
             
        List<Map<String, Object>> resultList = new ArrayList<>();
        if(IsKit.isNullOrEmpty(rowListData)) {
        	 return resultList;
        }
        Integer rowSize = rowListData.size();
		//循环一次，处理一行数据
		for(Integer i=0; i<rowSize; ++i) 
		{
			Map<Integer, String> rowMap = rowListData.get(i);
			Map<String, Object> tempMap = new HashMap<>();
			for(Map.Entry<Integer, String> entry : rowMap.entrySet()){
				tempMap.put(headList.get(entry.getKey()), entry.getValue());
			}
			resultList.add(tempMap);
		}
       return resultList;
	}
	//返回List型的没有实体模型数据(仅有数据)
	public static List<List<Object>> getOnlyListNoModel(MultipartFile mFile,Integer sheetNo,Integer headNum) throws IOException
	{
		InputStream inputStream = mFile.getInputStream();
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(inputStream, noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Map<Integer, String>> lists = noModelListener.getLists();
		List<List<Object>> allData = new ArrayList<>();
		for (int i = 0; i < lists.size(); i++) {
			Map<Integer, String> map = lists.get(i);
			List<Object> rowData = new ArrayList<>();
			for (Map.Entry<Integer, String> entry : map.entrySet()) {
				rowData.add(entry.getValue());
			}
			allData.add(rowData);
		}
		return allData;
	}

	//返回List型的没有实体模型数据(仅有数据)
	public static List<List<Object>> getOnlyListNoModel(String fileName,Integer sheetNo,Integer headNum)
	{
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(fileName, noModelListener)
		.sheet(sheetNo)
		.headRowNumber(headNum)
		.doRead();
		List<Map<Integer, String>> lists = noModelListener.getLists();
		List<List<Object>> allData = new ArrayList<>();
		for (int i = 0; i < lists.size(); i++) {
			Map<Integer, String> map = lists.get(i);
			List<Object> rowData = new ArrayList<>();
			for (Map.Entry<Integer, String> entry : map.entrySet()) {
				rowData.add(entry.getValue());
			}
			allData.add(rowData);
		}
		return allData;
	}

	//返回没有实体模型的表头
	public static List<Map<Integer,String>> getHeadListNoModel(
			String fileName,Integer sheetNo,Integer headNum)
	{
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(fileName, noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Map<Integer, String>> lists = noModelListener.getHeadList();
		return lists;
	}
	
	public static List<Map<Integer,String>> getHeadListNoModel(
			MultipartFile mFile,Integer sheetNo,Integer headNum) throws IOException
	{
		InputStream inputStream = mFile.getInputStream();
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(inputStream,noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Map<Integer, String>> lists = noModelListener.getHeadList();
		return lists;
	}
	/**
	 * 返回没有实体模型的具体行的表头
	 * @param fileName : 文件名
	 * @param sheetNo : sheet编号
	 * @param headNum : 表头行数，1表示1行
	 * @param rowNum : 行号，索引从0开始，即0为第一行
	 * @return java.util.List<java.lang.String>
	 * @throws
	 */
	public static List<String> getHeadListNoModel(String fileName,int sheetNo,Integer headNum,Integer rowNum)
	{
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(fileName, noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Map<Integer, String>> headList = noModelListener.getHeadList();
		List<String> oneHead = new ArrayList<>();
		for (Integer i = 0; i < headList.size(); i++) {
			if(rowNum.equals(i))
			{
				Map<Integer, String> map = headList.get(i);
				for (Map.Entry<Integer, String> entry : map.entrySet()) {
					oneHead.add(entry.getValue());
				}
			}
		}
		return oneHead;
	}
	/**
	 * 返回没有实体模型的具体行的表头
	 * 功能同上，区别：从流数据中获取
	 * @throws IOException 
	 * */
	public static List<String> getHeadListNoModel(
			MultipartFile mFile,int sheetNo,Integer headNum,Integer rowNum) throws IOException
	{
		InputStream inputStream = mFile.getInputStream();
		NoModelListener noModelListener = new NoModelListener();
		EasyExcel.read(inputStream, noModelListener).sheet(sheetNo).headRowNumber(headNum).doRead();
		List<Map<Integer, String>> headList = noModelListener.getHeadList();
		List<String> oneHead = new ArrayList<>();
		for (Integer i = 0; i < headList.size(); i++) {
			if(rowNum.equals(i))
			{
				Map<Integer, String> map = headList.get(i);
				for (Map.Entry<Integer, String> entry : map.entrySet()) {
					oneHead.add(entry.getValue());
				}
			}
		}
		return oneHead;
	}
}
