/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.base.controller;

import com.tofly.base.api.entity.Ftpfiles;
import com.tofly.base.service.FtpfilesService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.io.IOUtil;
import com.tofly.common.oauth.auth.innerCall;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公共基础附件信息表
 *
 * @author wangjian
 * @date Mon Feb 17 00:00:00 CST 2020
 */
@RestController
@AllArgsConstructor
@RequestMapping("/ftpfiles")
//@Api(tags="公共基础附件信息表接口")
public class FtpfilesController {

    private final  FtpfilesService ftpfilesService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param ftpfiles 公共基础附件信息表
    * @return
    */
//    @GetMapping("/page" )
//    @ApiOperation(value = "分页查询")
//    public ResultRespone getFtpfilesPage(Page page, @ModelAttribute Ftpfiles ftpfiles) {
//      return ResultRespone.success(ftpfilesService.page(page, Wrappers.query(ftpfiles)));
//    }
//
//
//    /**
//     * 通过id查询公共基础附件信息表
//     * @param id id
//     * @return ResultRespone
//     */
//    @GetMapping("/{id}" )
//    @ApiOperation(value = "通过ID查询")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
//    })
//    public ResultRespone getById(@PathVariable("id" ) Long id) {
//      return ResultRespone.success(ftpfilesService.getById(id));
//    }
//
//    /**
//     * 新增公共基础附件信息表
//     * @param ftpfiles 公共基础附件信息表
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="新增公共基础附件信息表" )
//    @ApiOperation(value = "新增公共基础附件信息表")
//    @PostMapping
//    public ResultRespone save(@RequestBody Ftpfiles ftpfiles) {
//      return ResultRespone.success(ftpfilesService.save(ftpfiles));
//    }
//
//    /**
//     * 修改公共基础附件信息表
//     * @param ftpfiles 公共基础附件信息表
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="修改公共基础附件信息表" )
//    @ApiOperation(value = "修改公共基础附件信息表")
//    @PutMapping
//    public ResultRespone updateById(@RequestBody Ftpfiles ftpfiles) {
//      return ResultRespone.success(ftpfilesService.updateById(ftpfiles));
//    }
//
//    /**
//     * 通过id删除公共基础附件信息表
//     * @param id id
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="通过id删除公共基础附件信息表" )
//    @ApiOperation(value = "通过ID删除公共基础附件信息表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
//    })
//    @DeleteMapping("/{id}" )
//    public ResultRespone removeById(@PathVariable Long id) {
//      return ResultRespone.success(ftpfilesService.removeById(id));
//    }

    @innerCall
    @PostMapping("/saveBatch")
    public ResultRespone saveBatch(@RequestBody List<Ftpfiles> listFtpFiles){
        return ResultRespone.success(ftpfilesService.customSaveBatch(listFtpFiles));
    }

    @GetMapping("/queryFiles")
    @ApiOperation(tags = {"文件FTP相关操作"},value = "通过ID批量查询公共附件信息")
    public ResultRespone queryFiles(@ApiParam(value = "字符串型文件id，多个以;隔开", name = "ids",required = true) String ids){
        List<Long> list=Arrays.stream(ids.split(";")).map(Long::parseLong).collect(Collectors.toList());
        return ResultRespone.success(ftpfilesService.listByIds(list));
    }

    @GetMapping("/download")
    @ApiOperation(tags = {"文件FTP相关操作"},value = "通过ID批量下载附件信息")
    @SneakyThrows
    public void download(@ApiParam(value = "字符串型文件id，多个以;隔开", name = "ids",required = true) String ids,
                         HttpServletResponse response){
        List<Long> list=Arrays.stream(ids.split(";")).map(Long::parseLong).collect(Collectors.toList());
        Collection<Ftpfiles> ftpfiles = ftpfilesService.listByIds(list);
        byte[] data =ftpfilesService.getDataFiles(ftpfiles);
        Ftpfiles first = ftpfiles.stream().findFirst().get();
        String fileName= first.getFilePath().split("/")[2];
        response.reset();
        response.setHeader("Content-Disposition", String.format("attachment; filename=%s.zip", URLEncoder.encode(fileName, "utf-8")));
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtil.write(response.getOutputStream(), Boolean.TRUE, data);

    }

}
