package io.renren.modules.dataSet.upload;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import io.renren.common.exception.RRException;
import io.renren.common.utils.MinIoUtils;
import io.renren.common.utils.R;
import io.renren.modules.dataSet.enumeration.DataSetType;
import io.renren.modules.sys.entity.dataset.DataSet;
import io.renren.modules.sys.service.CategoryService;
import io.renren.modules.sys.service.DataSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * web读写案例
 *
 * @author Jiaju Zhuang
 **/
@RestController
@RequestMapping("/dataset/staticup")
public class UpLoadController {


    /**
     * 文件上传
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link }
     * <p>
     * 2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link }
     * <p>
     * 3. 直接读即可
     */
    @Autowired
    private UpLoadService upLoadService;

    @Autowired
    private DataSetService dataSetService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("upload")
    @ResponseBody
    public R uploadExcel(MultipartFile file) throws IOException {
//        EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener(uploadDAO)).sheet().doRead();

        Map map = upLoadService.upload(file);

//        System.out.println(map);
        return R.ok().put("data", map);

    }

    @RequestMapping("uploadFile")
    public R uploadFile(@RequestPart("file") MultipartFile file, @RequestParam("dataSet") String dataSetJson) throws Exception {
        MinIoUtils.uploadMultipartFile(file, DataSetType.FILE_DATASET.getBucketName(), file.getOriginalFilename());
        DataSet dataSet = new Gson().fromJson(dataSetJson, DataSet.class);
        dataSet.setCreateTime(new Date());
        dataSet.setCategoryId(categoryService.getByName(DataSetType.FILE_DATASET.getClassificationName()));
        dataSetService.save(dataSet);
        return R.ok();
    }

    @PostMapping("/uploadDir")
    public R uploadDir(@RequestPart("files") MultipartFile[] files, @RequestParam("dataSet") String dataSetJson) throws Exception {
        DataSet dataSet = new Gson().fromJson(dataSetJson, DataSet.class);
        dataSet.setCreateTime(new Date());
        dataSet.setCategoryId(categoryService.getByName(DataSetType.DIR_DATASET.getClassificationName()));
        for (MultipartFile file : files) {
            MinIoUtils.uploadMultipartFile(file, DataSetType.DIR_DATASET.getBucketName(), file.getOriginalFilename());
        }
        dataSetService.save(dataSet);
        return R.ok();
    }


    @RequestMapping("uploadcsv")
    public R uploadCSV(MultipartFile file) throws Exception {
        System.out.println(file);
        Map map = upLoadService.uploadCSV(file);
//        System.out.println(map);
        return R.ok().put("data", map);
    }

    @RequestMapping("getstapre")
    public R getstaticprecontent(@RequestBody Map map) {

        final List getstaticprecontent = upLoadService.getstaticprecontent(map);
//        System.out.println(datas);

//        subeqpdemoService.getPrecontent(subeqpments);
        return R.ok().put("data", getstaticprecontent);
    }
}
