package com.winsun.utils;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.*;

public class ExcelDealUtils {
	
	
	@SuppressWarnings("unchecked")
	public static XSSFWorkbook getWorkBook(Map<String, Object> headData,List<Map<String,Object>> list) {
		String sheetName = headData.get("sheetName").toString();
		List<String> names = (List<String>)headData.get("names");
		List<String> columns =  (List<String>)headData.get("columns");
		
		
		XSSFWorkbook workbook = new XSSFWorkbook();
		//创建工作表
        XSSFSheet sheet = workbook.createSheet(sheetName);
        //添加表头
        final XSSFRow row = sheet.createRow(0);
        //设置列宽度
//        sheet.setColumnWidth(1, 6500);
//        sheet.setColumnWidth(2, 6500);

        //第一行字段名称
        names.forEach(column -> {
            XSSFCell cell = row.createCell(Math.max(row.getLastCellNum(), 0));
            cell.setCellValue(column);
        });
        //数据
        int rowIndex = row.getRowNum();
        for (Map<String, Object> maps: list) {
            final XSSFRow rowdata = sheet.createRow(++rowIndex);
            for (String column : columns) {
                XSSFCell cell = rowdata.createCell(Math.max(rowdata.getLastCellNum(), 0));
                if (maps.get(column) != null) {
                    cell.setCellValue(maps.get(column).toString());
                }
            }
        }
        
        return workbook;
	}
	
	public static Map<String, Object> dealHeadMap(Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<String> names = new ArrayList<>();
		List<String> columns = new ArrayList<>();
		
		for(Map.Entry<String, Object> theMap : map.entrySet()) {
			names.add(theMap.getKey());
			columns.add(theMap.getValue().toString());
		}
		
		resultMap.put("names", names);
		resultMap.put("columns", columns);
		
		return resultMap;
	}
	
	// 活动导出excel表头
	public static Map<String, Object> activityExcel(String sheetName){
		Map<String, Object> headMap = new LinkedHashMap<String, Object>();
		headMap.put("姓名", "name");
		headMap.put("账号", "account");
		headMap.put("所得积分", "recordIntegral");
		headMap.put("积分类型", "activityType");
		headMap.put("积分小类", "subclass");
		headMap.put("县分", "substName");
		headMap.put("学校", "schoolName");
		headMap.put("创建时间", "createTime");
	
		Map<String, Object> resultMap = dealHeadMap(headMap);
		resultMap.put("sheetName", sheetName);
		
		return resultMap;
	}
	// kpi管理导出excel表头
	public static Map<String, Object> kpiExcel(String sheetName){
		Map<String, Object> headMap = new LinkedHashMap<String, Object>();
		headMap.put("kpiId", "id");
		headMap.put("用户名", "sysuName");
		headMap.put("账号", "account");
		headMap.put("县分", "substName");
		headMap.put("学校", "schoolName");
		headMap.put("汇总任务量", "reportDaily");
		headMap.put("参与集中营销", "collectiveSales");
		headMap.put("组织周例会", "weeklyMeeting");
		headMap.put("定制扫楼营销方案", "organTraining");
		headMap.put("组织培训", "designPlan");
		headMap.put("KPI总值", "kpi");
		headMap.put("KPI月份", "month");

		Map<String, Object> resultMap = dealHeadMap(headMap);
		resultMap.put("sheetName", sheetName);

		return resultMap;
	}

	
	// 账单导出excel表头
	public static Map<String, Object> billExcel(String sheetName){
		Map<String, Object> headMap = new LinkedHashMap<String, Object>();
		headMap.put("月份", "month");
		headMap.put("项目类型", "billType");
		headMap.put("收支详情", "billDetail");
		headMap.put("县分", "substName");
		headMap.put("学校", "schoolName");
		headMap.put("校区", "campus");
		headMap.put("网点名称", "networkName");
		headMap.put("网点编码", "networkCode");
		headMap.put("上传时间", "createTime");
		headMap.put("不含税金额", "amount");
		headMap.put("含税金额", "taxAmount");
		headMap.put("税点", "taxPoint");
	
		Map<String, Object> resultMap = dealHeadMap(headMap);
		resultMap.put("sheetName", sheetName);
		
		return resultMap;
	}
}
