package com.api.shusheng.web;

import com.alibaba.fastjson.JSONObject;
import com.sun.jersey.core.header.FormDataContentDisposition;
//import com.sun.jersey.multipart.FormDataBodyPart;
//import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.FormDataParam;
import weaver.general.BaseBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/shusheng/upload")
public class ShushengUploadFileActionApi {

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Path("/savedoc")
    public void saveOfd(@FormDataParam("file") InputStream fileInputStream,
                          @FormDataParam("file") FormDataContentDisposition dataContentDisposition
                          ) {
        BaseBean bb = new BaseBean();
        Map<String,Object> apidatas = new HashMap<String,Object>();
        String imageFileLocation = "";
        try{
//            String requestid  = request.getParameter("requestid");
//            bb.writeLog("savedocrequestid:"+requestid);
            if(fileInputStream.available()>0){
                apidatas.put("fileInputStream",fileInputStream.available());
            }
            apidatas.put("getFileName",dataContentDisposition.getFileName());

            //拼接文件目录
            imageFileLocation = File.separator+"data" + File.separator+"dajc" +File.separator+"fj"+File.separator+ dataContentDisposition.getFileName();

            File image = writeToFile(fileInputStream, imageFileLocation);

            apidatas.put("image",image.getPath()+File.separator+image.getName());

            apidatas.put("api_status",true);
        }catch (Exception e){
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg","exception:"+e.getMessage());
        }
        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));
    }


    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Path("/savedoc4")
    public Response saveOfd4(@FormDataParam("file") InputStream fileInputStream,
                            @FormDataParam("file") FormDataContentDisposition dataContentDisposition
    ) {
        BaseBean bb = new BaseBean();
        Map<String,Object> apidatas = new HashMap<String,Object>();
        String imageFileLocation = "";
        try{
//            String requestid  = request.getParameter("requestid");
            if(fileInputStream.available()>0){
                apidatas.put("fileInputStream",fileInputStream.available());
            }
            apidatas.put("getFileName",dataContentDisposition.getFileName());

            //拼接文件目录
            imageFileLocation = File.separator+"data" + File.separator+"dajc" +File.separator+"fj"+File.separator+ dataContentDisposition.getFileName();

            File image = writeToFile(fileInputStream, imageFileLocation);

            apidatas.put("image",image.getPath()+File.separator+image.getName());

            apidatas.put("api_status",true);
        }catch (Exception e){
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg","exception:"+e.getMessage());
        }
        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));

        return Response.status(200).entity("File uploaded at " + imageFileLocation).build();
    }

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Path("/savedoc3")
    public Response saveOfd3(@Context HttpServletRequest request,@Context HttpServletResponse response,
                            @FormDataParam("file") InputStream fileInputStream,
                            @FormDataParam("file") FormDataContentDisposition dataContentDisposition
    ) {
        BaseBean bb = new BaseBean();
        Map<String,Object> apidatas = new HashMap<String,Object>();
        String imageFileLocation = "";
        try{
            String requestid  = request.getParameter("requestid");
            bb.writeLog("requestid:"+requestid);
            if(fileInputStream.available()>0){
                apidatas.put("fileInputStream",fileInputStream.available());
            }
            apidatas.put("getFileName",dataContentDisposition.getFileName());
            //拼接文件目录
            imageFileLocation = File.separator+"data" + File.separator+"dajc" +File.separator+"fj"+File.separator+ dataContentDisposition.getFileName();

            File image = writeToFile(fileInputStream, imageFileLocation);

            apidatas.put("image",image.getPath()+File.separator+image.getName());

            apidatas.put("api_status",true);
        }catch (Exception e){
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg","exception:"+e.getMessage());
        }
        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));

        return Response.status(200).entity("File uploaded at " + imageFileLocation).build();

    }

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Path("/savedoc5")
    public void savedoc5(@Context HttpServletRequest request,@Context HttpServletResponse response,
                             @FormDataParam("file") InputStream fileInputStream,
                             @FormDataParam("file") FormDataContentDisposition dataContentDisposition
    ) {
        BaseBean bb = new BaseBean();
        Map<String,Object> apidatas = new HashMap<String,Object>();
        String imageFileLocation = "";
        try{
            String requestid  = request.getParameter("requestid");
            bb.writeLog("requestid:"+requestid);
            if(fileInputStream.available()>0){
                apidatas.put("fileInputStream",fileInputStream.available());
            }
            apidatas.put("getFileName",dataContentDisposition.getFileName());
            //拼接文件目录
            imageFileLocation = File.separator+"data" + File.separator+"dajc" +File.separator+"fj"+File.separator+ dataContentDisposition.getFileName();

            File image = writeToFile(fileInputStream, imageFileLocation);

            apidatas.put("image",image.getPath()+File.separator+image.getName());

            apidatas.put("api_status",true);
        }catch (Exception e){
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg","exception:"+e.getMessage());
        }
        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));
    }


//    @POST
//    @Consumes(MediaType.MULTIPART_FORM_DATA)
//    @Path("/savedoc2")
//    public Response saveOfd2(FormDataMultiPart form) {
//        BaseBean bb = new BaseBean();
//        Map<String,Object> apidatas = new HashMap<String,Object>();
//        try{
////            String requestid  = request.getParameter("requestid");
//            FormDataBodyPart formDataBodyPart = form.getField("file");
//
//            String name = formDataBodyPart.getName();
//
//            InputStream in = formDataBodyPart.getValueAs(InputStream.class);
//            if(in.available() > 0){
//                apidatas.put("InputStream",in.available());
//            }
//
//            FormDataContentDisposition formDataContentDisposition = formDataBodyPart.getFormDataContentDisposition();
//
//            String name2 = formDataContentDisposition.getName();
//            apidatas.put("name",name);
//            apidatas.put("name2",name2);
//
//            apidatas.put("api_status",true);
//        }catch (Exception e){
//            apidatas.put("api_status",false);
//            apidatas.put("api_errormsg","exception:"+e.getMessage());
//        }
//        bb.writeLog("apidatas:"+ JSONObject.toJSONString(apidatas));
//        return Response.status(200).entity("File uploaded at " + JSONObject.toJSONString(apidatas)).build();
//
//    }

    /***
     *
     * @param is
     * @param uploadedFileLocation
     * @return
     */
    public static File writeToFile(InputStream is, String uploadedFileLocation) {
        File file = new File(uploadedFileLocation);
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            byte buffer[] = new byte[4 * 1024];
            while ((is.read(buffer)) != -1) {
                os.write(buffer);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(uploadedFileLocation+"文件大小"+file.length());
        if (file.length()<5) {
            file.delete();
            return null;
        }
        return file;
    }


    @GET
    @Path("/test")
    @Produces({MediaType.TEXT_PLAIN})
    public String Test(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String,Object> apidatas = new HashMap<String,Object>();
        apidatas.put("api_status","Test");
        return JSONObject.toJSONString(apidatas);
    }

//
//    @POST
//    @Consumes(MediaType.MULTIPART_FORM_DATA)
//    @Path("/docuUpload")
//    public String uploadDocuments(
//            @FormDataParam("file") List<FormDataBodyPart> upload){
//
//        Map<String,Object> apidatas = new HashMap<String,Object>();
//        try{
//            apidatas.put("requestid","11223");
//            apidatas.put("api_status",true);
//        }catch (Exception e){
//            apidatas.put("api_status",false);
//            apidatas.put("api_errormsg","exception:"+e.getMessage());
//        }
//        return JSONObject.toJSONString(apidatas);
//    }



}
