package com.engine.htsc.langchaoPDF.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.langchaoPDF.service.UploadFileService;
import com.engine.htsc.langchaoPDF.service.impl.UploadFileServiceImpl;
import sun.misc.BASE64Decoder;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class LangChaoPDFAction {
    private UploadFileService getService() {
        return ServiceUtil.getService(UploadFileServiceImpl.class);
    }

    @POST
    @Path("/pdf")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String, Object> upLoadPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> data = new HashMap<>();
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);
            String lcbh = Util.null2String(param.get("lcbh"));
            String workFlowNum = URLDecoder.decode(lcbh.substring(0,lcbh.length()-12),"UTF-8") + lcbh.substring(lcbh.length()-12,lcbh.length());
            JAVATools.SetCmdwritelog("workFlowNum", workFlowNum);
            String ngr = Util.null2String(param.get("ngr"));
            JAVATools.SetCmdwritelog("ngr", ngr);
            String fileType = "0";
            String fileName = workFlowNum + System.currentTimeMillis() + ".png";
            String pdf = Util.null2String(param.get("pdf"));
            JAVATools.SetCmdwritelog("pdf", pdf);
            byte[] png = new BASE64Decoder().decodeBuffer(pdf);
            //环境生成文件again
            InputStream inputstream = new ByteArrayInputStream(png);
            int index;
            byte[] bytes = new byte[1024];
            String path = "/app/ecology/filesystem/langchaoyingxinangTemp/" + workFlowNum;
            File dir = new File(path);
            if(dir.exists()){
                dir.delete();
            }
            dir.setWritable(true, false);
            dir.mkdirs();
            Runtime.getRuntime().exec("chmod -R 777 " + path);
            path += "/" + fileName;
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            }

            FileOutputStream uploadFile = new FileOutputStream(file);
            while ((index = inputstream.read(bytes)) != -1) {
                uploadFile.write(bytes, 0, index);
                uploadFile.flush();
            }
            inputstream.close();
            uploadFile.close();
            Runtime.getRuntime().exec("chmod -R 777 " + path);
            //环境生成文件end

            //通过ngr查出loginid
            RecordSet rs = new RecordSet();
            String loginid = "";
            String sql = " select loginid from hrmresource where id = " + ngr;
            rs.execute(sql);
            JAVATools.SetCmdwritelog("查询" + sql);
            JAVATools.SetCmdwritelog("查询结果" + rs.toString());
            if (rs.next()) {
                loginid = Util.null2String(rs.getString("loginid"));
                JAVATools.SetCmdwritelog("loginid", loginid);
            }
            String result = getService().uploadFile(workFlowNum, png, fileType, loginid, fileName);
            JAVATools.SetCmdwritelog("浪潮OCR识别上传接口返回", result);
            data.put("code", 200);
            data.put("message", "接口调用成功");
            data.put("result", result);
        } catch (Exception e) {
            e.printStackTrace();
            data.put("code", -1);
            data.put("message", "接口调用失败");
            data.put("api_errormsg", "catch exception : " + e.getMessage());
            JAVATools.SetCmdwritelog("isPostion catch exception");
        }
        return data;
    }
}
