package com.pqs9900.controller.business;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.njcn.enums.LogTypeEnum;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.njcn.mapper.data.GridTopMapper;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.data.IImportGirdTopService;

@Controller
@RequestMapping("/importGridTop")
public class importGridTopController {
    GridTopMapper gtm;

    @Resource
    IImportGirdTopService igridTop;

    @Autowired
    UserLogDetailService userLogDetailService;
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(importGridTopController.class);

    //获取所有的拓扑图信息
    @RequestMapping(value = "getGirdTop", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getGirdTop(HttpServletRequest request, @Param(value = "vollevel") String vollevel) {
        HttpResult result = new HttpResult();
        try {
            List<List> gridTop = igridTop.getGirdTop(vollevel);
            if (gridTop.isEmpty()) {
                result.setCode(500);
                result.setMessage("没有获取到拓扑图信息列表");
                logger.error("获取拓扑图信息失败");
            } else {
                result.setCode(200);
                result.setBody(gridTop);
                result.setMessage("获取拓扑图信息列表成功");
                logger.info("获取拓扑图信息成功");
            }
        } catch (Exception e) {
            logger.error("获取拓扑图异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取拓扑图失败");
        }
        return result;
    }

    //将excle表中的数据取出来
    @RequestMapping(value = "/importGirdTop", method = RequestMethod.POST)
    @ResponseBody
    public int upload(@RequestParam(value = "file", required = false) MultipartFile file, @Param(value = "volLevel") Integer volLevel, HttpServletRequest request, HttpServletResponse response) {
        int result = 0;
        /**
         * ---------------------------------------------------------------------------------------------------
         * xuyang 2020.08.27
         * 修改：对上传文件做判断
         * 1.文件类型 2.文件大小
         */
        //获取文件名
        String fileName = file.getOriginalFilename();
        //文件大小(此大小为kb)
        Long fileSize  = file.getSize()/1024;
        //文件大小判断
        if (fileSize > 3072){
            result = 2;
            return result;
        }
        //文件类型判断
        if (!fileName.matches("^.+\\.(?i)(xls)$")) {
            result = 3;
            return result;
        }
        /**
         * over
         * ---------------------------------------------------------------------------------------------------
         */
        try {
            result = igridTop.readExcelFile(file, volLevel);
            userLogDetailService.getLog("导入拓扑数据", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
        } catch (Exception e) {
            logger.error("数据读取异常，异常是" + e.toString());
            userLogDetailService.getLog("导入拓扑数据", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(),0);
            result = 0;
        }

        return result;
    }
}
