package com.lightinit.hsdatashow.controller.ms;

import com.lightinit.hsdatashow.common.ExcelUtils;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ResultStateCode;
import com.lightinit.hsdatashow.service.ms.IMsExcelIntService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "ms/excelInto")
public class MsExcelIntoController {

    @Autowired
    private IMsExcelIntService msExcelIntService ;

    @RequestMapping(value = "index.htm")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("ms/excelInto/index");
        return modelAndView;
    }
    @RequestMapping(value = "minsheng.htm")
    public ModelAndView minsheng() {
        ModelAndView modelAndView = new ModelAndView("ms/excelInto/minsheng");
        return modelAndView;
    }
    @RequestMapping(value = "huanjing.htm")
    public ModelAndView huanjing() {
        ModelAndView modelAndView = new ModelAndView("ms/excelInto/huanjing");
        return modelAndView;
    }

    //新增社会发展指数
    @RequestMapping("/addShfz.action")
    @ResponseBody
    public ResultState addShfz(MultipartFile file, HttpSession session) throws Exception {
        ResultState resultState = new ResultState();
        //判断文件是否为空
        if (file.isEmpty()) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("上传文件为空");
        }
        //判断文件大小
        long size = file.getSize();
        String name = file.getOriginalFilename();
        if (StringUtils.isBlank(name) || size == 0) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("上传文件为空");
        }
        //获取文件后缀
        String postfix = ExcelUtils.getPostfix(name);

        List<List<String>> lists = null;
        //读取文件内容
        if (StringUtils.equals("xlsx", postfix)) {
            lists = ExcelUtils.readXlsx(file);
        } else if (StringUtils.equals("xls", postfix)) {
            lists = ExcelUtils.readXls(file);
        }
        if (lists.size() > 0) {
            resultState = msExcelIntService.addShfz(lists) ;
        }
        return resultState;
    }


    //新增民生服务指数
    @RequestMapping("/addmsfw.action")
    @ResponseBody
    public ResultState addmsfw(MultipartFile file, HttpSession session) throws Exception {
        ResultState resultState = new ResultState();
        //判断文件是否为空
        if (file.isEmpty()) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("上传文件为空");
        }
        //判断文件大小
        long size = file.getSize();
        String name = file.getOriginalFilename();
        if (StringUtils.isBlank(name) || size == 0) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("上传文件为空");
        }
        //获取文件后缀
        String postfix = ExcelUtils.getPostfix(name);

        List<List<String>> lists = null;
        //读取文件内容
        if (StringUtils.equals("xlsx", postfix)) {
            lists = ExcelUtils.readXlsx(file);
        } else if (StringUtils.equals("xls", postfix)) {
            lists = ExcelUtils.readXls(file);
        }
        if (lists.size() > 0) {
            resultState = msExcelIntService.addmsfw(lists) ;
        }
        return resultState;
    }

    //新增生态环境指数
    @RequestMapping("/addsthj.action")
    @ResponseBody
    public ResultState addsthj(MultipartFile file, HttpSession session) throws Exception {
        ResultState resultState = new ResultState();
        //判断文件是否为空
        if (file.isEmpty()) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("上传文件为空");
        }
        //判断文件大小
        long size = file.getSize();
        String name = file.getOriginalFilename();
        if (StringUtils.isBlank(name) || size == 0) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("上传文件为空");
        }
        //获取文件后缀
        String postfix = ExcelUtils.getPostfix(name);

        List<List<String>> lists = null;
        //读取文件内容
        if (StringUtils.equals("xlsx", postfix)) {
            lists = ExcelUtils.readXlsx(file);
        } else if (StringUtils.equals("xls", postfix)) {
            lists = ExcelUtils.readXls(file);
        }
        if (lists.size() > 0) {
            resultState = msExcelIntService.addsthj(lists) ;
        }
        return resultState;
    }
}
