package com.th.demo.controller.develop;

import com.th.demo.mapping.develop.DevUpdownPathsMapper;
import com.th.demo.model.develop.DevUpdownPaths;
import com.th.demo.model.system.Type;
import com.th.demo.service.develop.CodeMachineService;
import com.th.demo.service.develop.UpDownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/DevelopUpDown")
public class UpDownController {

    @Autowired
    UpDownService upDownService;
    @Autowired
    DevUpdownPathsMapper devUpdownPathsMapper;

    @RequestMapping(value = "/uploadDemo.do", method = RequestMethod.POST)
    public String uploadDemo(HttpServletRequest request) {
        String result = "";
        try {
            result = upDownService.upload(request);
        } catch (Exception ex) {
            result = Type.FAIL + ex.getMessage();
            ex.printStackTrace();
        } finally {
            return result;
        }

    }

    @RequestMapping(value = "/query.do")
    public String query(int page, int rows) throws IOException {
        return upDownService.query(page, rows);
    }

    @RequestMapping(value = "/queryPaths.do")
    public String query(String id) throws IOException {
        return upDownService.queryPaths(id);
    }

    @RequestMapping(value = "/remove.do", method = RequestMethod.POST)
    public String remove(String id,
                         String userId) throws IOException {
        return upDownService.remove(id, userId);
    }

    @RequestMapping(value = "/removePaths.do", method = RequestMethod.POST)
    public String removePaths(String id,
                         String userId) throws IOException {
        return upDownService.removePaths(id, userId);
    }

    @RequestMapping(value = "/download.do")
    public String downloadLocal(String id, HttpServletResponse response) throws Exception {
        String result = "";
        DevUpdownPaths devUpdownPaths = devUpdownPathsMapper.selectByPrimaryKey(id);
        File file = new File(devUpdownPaths.getPaths());
        String fileName =  file.getName();
        InputStream inStream = new FileInputStream(devUpdownPaths.getPaths());// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; fileName=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result += Type.SUCCESS + "请选择下载";

    }

}
