/**
 * Copyright (C), 2018-2019, XXX有限公司
 * FileName: UploadAndDownController
 * Author:   pwb
 * Date:     2019/7/15 13:50
 * Description: 文件传输接口
 * History:
 * <author>          <time>          <version>          <desc>
 * frank           修改时间           1.0.0              ycypApp
 */
package com.yx.face.controller;


import com.alibaba.excel.EasyExcel;
import com.yx.face.boot.core.BaseController;
import com.yx.face.boot.core.Constants;
import com.yx.face.boot.restful.RestResponse;
import com.yx.face.boot.restful.RestResult;
import com.yx.face.boot.uitls.ExcelListenerUtils;
import com.yx.face.boot.uitls.FileUtils;
import com.yx.face.model.excel.UploadExcel;
import com.yx.face.model.excel.UserWhitelistExcel;
import com.yx.face.service.FaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * 〈文件传输接口〉
 *
 * @author pwb
 * @create 2019/7/15
 * @since 1.0.0
 */

@Slf4j
@Api(tags = "文件传输接口")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@RequestMapping(value = "/upload")
public class UploadController extends BaseController {

    private final FaceService faceService;

    //上传图片
    @ApiOperation(value = "单图片上传接口", notes = "单图片上传接口")
    @PostMapping(value = "/picture")
    public RestResult<String> uploadImage(@RequestParam MultipartFile file) throws IOException {
        String fogImg = faceService.addFaceDeviceLogImg(file.getInputStream(), getUserId());
        return RestResponse.ok(fogImg);
    }

    @ApiOperation("导出")
    @PostMapping("uploadExcel")
    public void uploadExcel(@RequestParam("file") MultipartFile excelFile) {
        ExcelListenerUtils listener = new ExcelListenerUtils();  //ExcelListener
        try {
            EasyExcel.read(excelFile.getInputStream(), UploadExcel.class, listener).sheet(0).doReadSync();  //ExcelModel 上面创建的实体类
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Object> datas = listener.getDatas(); //把拿到的数据处理到集合里去(根据所需自行修改)
        faceService.uploadExcel(datas);
    }
}
