package io.renren.modules.admin.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.renren.common.utils.EchartstOptionsUtils;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.R;

/**
 * 
 *
 * @author huwhois
 * @email huwhois@gmail.com
 * @date 2020-07-22 15:28:24
 */
@RestController
@RequestMapping("admin/datatest")
public class DataTestController {

    
    /**
	 * 功能描述:excel导入member
	 *
	 * @author grm
	 * @since 2020年1月28日
	 * @param file 文件
	 * @return result
	 */
	@PostMapping("/importexcel")
	public R importMember(@RequestParam("file") MultipartFile file) {
		if (ExcelUtils.isExcel(file) == false) {
            return R.error("error,file is not excel!");
		}
		if (file.isEmpty()) {
			return  R.error("error,file is empty!");
        }
        EchartstOptionsUtils echartsOptions = new EchartstOptionsUtils();

		Workbook workbook = ExcelUtils.createWorkbook(file);
		if (workbook != null) {
			// 获取工作表
			Sheet sheet = workbook.getSheetAt(0);
			// 获取sheet中第一行行号
            int firstRowNum = sheet.getFirstRowNum();
            // 获取sheet中的第一行
            Row row1 = sheet.getRow(firstRowNum);
            // 获取sheet中的第二行
            Row row2 = sheet.getRow(firstRowNum+1);
            // 获取第二不为空的列是第几个。
            int	cellMaxNum = row2.getLastCellNum();
			// 获取sheet中最后一行行号
            int lastRowNum = sheet.getLastRowNum();
            //cellMaxNum = row.getPhysicalNumberOfCells();//是获取不为空的列个数。

            // title
            Cell cellTitle = row1.getCell(0);
            String titleText = ExcelUtils.getVal(cellTitle);
            echartsOptions.setTitleText(titleText);
            // // legend & series
            // List<String> legendData = new ArrayList<String>();
            // List<Map<String, Object>> series = new ArrayList<Map<String, Object>>();
            // for (int k = 0; k < cellMaxNum; k++) {
            //     Cell cellLegend = row1.getCell(k);
            //     String valueLegend = ExcelUtils.getVal(cellLegend);
            //     legendData.add(valueLegend);

            //     Map<String,Object> seriesData = new HashMap<>();
            //     List<String> yData = new ArrayList<String>();
            //     seriesData.put("data", yData);
            //     seriesData.put("name", valueLegend);
            //     seriesData.put("type", "line");
            //     seriesData.put("smooth", true);

            //     series.add(seriesData);
            // }

            // legend 
            List<String> legendData = new ArrayList<String>();
            Cell cellLegend = row2.getCell(0);
            String valueLegend = ExcelUtils.getVal(cellLegend);
            legendData.add(valueLegend);
            echartsOptions.setLegendData(legendData);
            // x 轴 
            List<String> xAxisData = new ArrayList<String>();
            echartsOptions.setXAxisData(xAxisData);  

            // series
            List<Map<String, Object>> series = new ArrayList<Map<String, Object>>();
            Map<String,Object> seriesData = new HashMap<>();
            seriesData.put("name", valueLegend);
            seriesData.put("type", "line");
            seriesData.put("smooth", true);

            List<String> yData = new ArrayList<String>();

            seriesData.put("data", yData);

            series.add(seriesData);
            // 循环插入数据
            for (int i = firstRowNum + 2; i <= lastRowNum; i++) { // 标题的下两行开始
				Row row = sheet.getRow(i);
				if (ExcelUtils.isEmptyRow(row) == false) {
                    Cell cell1 = row.getCell(0);
                    String cellValue1 = ExcelUtils.getVal(cell1);
                    xAxisData.add(cellValue1);
                    
                    Cell cell2 = row.getCell(1);
                    String cellValue2 = ExcelUtils.getVal(cell2);
                    yData.add(cellValue2);
				}
            }

            echartsOptions.setSeries(series);
        }
		// return R.ok("上传成功!").put("statistics", statistics).put("failList", failList);
		return R.ok("上传成功!").put("echartsOptions", echartsOptions);
    }
}
