ApplySignFileSample.java 11.4 KB
package com.example.demo.common;


import com.example.demo.domain.ReceivingObject;
import com.example.demo.domain.SignResult;
import com.example.demo.domain.SignatoryData;
import com.junziqian.api.JunziqianClient;
import com.junziqian.api.bean.Signatory;
import com.junziqian.api.common.DealType;
import com.junziqian.api.common.IdentityType;
import com.junziqian.api.common.SignLevel;
import com.junziqian.api.request.ApplySignFileRequest;
import com.junziqian.api.response.ApplySignResponse;
import org.ebaoquan.rop.thirdparty.com.alibaba.fastjson.JSONArray;
import org.ebaoquan.rop.thirdparty.com.alibaba.fastjson.JSONObject;
import org.ebaoquan.rop.thirdparty.com.google.common.collect.Sets;
import org.ebaoquan.rop.utils.spring.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by wlinguo on 2016-05-16.
 */

//public class ApplySignFileSample {
    public class ApplySignFileSample  {

    public SignResult Sample(MultipartFile file, ReceivingObject receivingObject ) throws IOException {

         String SERVICE_URL = "http://api.junziqian.com/services";

        // 请填入你的APPKey
         String APP_KEY = "dda683a619751e69";

        // 请填入你的APPSecret
         String APP_SECRET = "34a07facdda683a619751e69c4a887b8";

        JunziqianClient client = new JunziqianClient(SERVICE_URL, APP_KEY, APP_SECRET);
//        System.out.println("ahahahaha");
//        SignResult signResult = new SignResult();
//        signResult.setApplyNo("wahahahahahah");
//
//        return signResult;
        File convFile = new File(file.getOriginalFilename());
        convFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(convFile);
        fos.write(file.getBytes());
        fos.close();


      //  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(file.getInputStream(), "utf-8"));
//        String str;
//        while ((str = bufferedReader.readLine()) != null && !"".equals(str)) {
//            System.out.println(str);
//        }
//        bufferedReader.close();



//        CommonsMultipartFile cf= (CommonsMultipartFile)file;
//        DiskFileItem fi = (DiskFileItem)cf.getFileItem();
       // File files = (File)file;




        SignResult signResult = new SignResult();
        ApplySignFileRequest.Builder builder = new ApplySignFileRequest.Builder();
        String agreementName = receivingObject.getAgreementName();
        System.out.println(agreementName);
        String contractAmountStr = receivingObject.getAgreementAmount();
        Double contractAmount = Double.parseDouble(contractAmountStr);

        builder.withContractName(agreementName); // 合同名称,必填   *
        builder.withContractAmount(contractAmount); // 合同金额   *
        //1、本地文件方式
       // builder.withFile("/Users/xuwenhao/Downloads/借款协议2.pdf");

        //2、字节流方式
        //File file = new File("D:\\tmp\\买卖合同.pdf");
        builder.withFile(convFile.getName(), FileCopyUtils.copyToByteArray(convFile)); //合同文件

//      //上传签约附件,目前最大只支持上传3份附件
//    builder.withAttachFile1("D:\\tmp\\借款基础信息.xlsx");
//      builder.withAttachFile2("D:\\tmp\\bbbb.jpg");
//     builder.withAttachFile3("D:\\tmp\\shenqingshu.png");

        HashSet<Signatory> signatories = Sets.newHashSet();
        List<SignatoryData> signatoryDataList = receivingObject.getData();

        List<SignatoryData> signatoryPersonList = new ArrayList<>();

        List<SignatoryData> signatoryCompanyList = new ArrayList<>();

        for(int i=0;i<signatoryDataList.size();i++){
            if(signatoryDataList.get(i).getType().equals("person")){
                signatoryPersonList.add(signatoryDataList.get(i));
            }else if(signatoryDataList.get(i).getType().equals("company")){
                signatoryCompanyList.add(signatoryDataList.get(i));
            }
        }

        for(int j=0;j<signatoryPersonList.size();j++){
            String name = signatoryPersonList.get(j).getName();
            String idNo = signatoryPersonList.get(j).getIdNo();
            String mobile = signatoryPersonList.get(j).getMobile();
            Double page = Double.parseDouble(signatoryPersonList.get(j).getPage());
            Double offsetX = Double.parseDouble(signatoryPersonList.get(j).getOffsetX());
            Double offsetY = Double.parseDouble(signatoryPersonList.get(j).getOffsetY());

            Signatory signatory = new Signatory();
            signatory.setFullName(name); //姓名
            signatory.setSignatoryIdentityType(IdentityType.IDCARD); //证件类型
            signatory.setIdentityCard(idNo); //证件号码
            signatory.setMobile(mobile);

            JSONArray chapteJsonArray=new JSONArray();
            JSONObject pageJson=new JSONObject();
            pageJson.put("page", page);
            JSONArray chaptes=new JSONArray();
            pageJson.put("chaptes", chaptes);
            chapteJsonArray.add(pageJson);
            JSONObject chapte=new JSONObject();
            chapte.put("offsetX", offsetX);
            chapte.put("offsetY", offsetY);
            chaptes.add(chapte);

            signatory.withChapteJson(chapteJsonArray);
            signatory.setSignLevel(1); //0:标准图形章   1:公章或手写
            signatories.add(signatory);
        }

        for(int k=0;k<signatoryCompanyList.size();k++){
            Double page = Double.parseDouble(signatoryCompanyList.get(k).getPage());
            Double offsetX = Double.parseDouble(signatoryCompanyList.get(k).getOffsetX());
            Double offsetY = Double.parseDouble(signatoryCompanyList.get(k).getOffsetY());

            Signatory signatory = new Signatory();
            signatory.setFullName("七彩投资(苏州)有限公司"); //姓名
            signatory.setSignatoryIdentityType(IdentityType.BIZLIC); //证件类型
            signatory.setIdentityCard("913205083310094721");//证件号码(营业执照号或统一社会信用代码)
            signatory.setEmail("chrisniceboat@163.com");//企业账户注册邮箱
            signatory.setMobile("15062592832");//企业代表手机
            JSONArray chapteJsonArray3 = new JSONArray();
            JSONObject pageJson3 = new JSONObject();
            pageJson3.put("page", page);
            JSONArray chaptes3 = new JSONArray();
            pageJson3.put("chaptes", chaptes3);
            chapteJsonArray3.add(pageJson3);

            JSONObject chapte3 = new JSONObject();
            chapte3.put("offsetX", offsetX);
            chapte3.put("offsetY", offsetY);
            chaptes3.add(chapte3);
            signatory.withChapteJson(chapteJsonArray3);
            signatory.setSignLevel(1); //0:标准图形章   1:公章或手写
            signatories.add(signatory);

        }


//        /**
//         * 签约方1
//         */
//        Signatory signatory = new Signatory();
//        signatory.setFullName("徐文豪"); //姓名
//        signatory.setSignatoryIdentityType(IdentityType.IDCARD); //证件类型
//        signatory.setIdentityCard("320981199307111710"); //证件号码
//        signatory.setMobile("18896525239");
//
//        /*
//         * 设置签约方1的签章位置,签约位置平台自行设置
//         * 签字位置 (以文件页左上角(0.0,0.0)为基准,按百分比进行设置)offsetX,offsetY(x,y为比例,值范设置为0-1之间) page为页码,page从0开始计数,
//         * 每页为一个数组,每页不能超过10个签字位置
//         */
//        JSONArray chapteJsonArray=new JSONArray();
//        JSONObject pageJson=new JSONObject();
//        pageJson.put("page", 1);
//        JSONArray chaptes=new JSONArray();
//        pageJson.put("chaptes", chaptes);
//        chapteJsonArray.add(pageJson);
//        JSONObject chapte=new JSONObject();
//        chapte.put("offsetX", 0.20);
//        chapte.put("offsetY", 0.68);
//        chaptes.add(chapte);
////        chapte=new JSONObject();
////        chapte.put("offsetX", 0.52);
////        chapte.put("offsetY", 0.75);
////        chaptes.add(chapte);
//        signatory.withChapteJson(chapteJsonArray);
//		signatory.setSignLevel(1); //0:标准图形章   1:公章或手写
//        signatories.add(signatory);
//
//
//        /**
//         * 签约方3
//         */
//        signatory = new Signatory();
//        signatory.setFullName("史文斌"); //姓名
//        signatory.setSignatoryIdentityType(IdentityType.IDCARD); //证件类型
//        signatory.setIdentityCard("32058619850321881X"); //证件号码
//        signatory.setMobile("15162499345");
//        JSONArray chapteJsonArray2 = new JSONArray();
//        JSONObject pageJson2 = new JSONObject();
//        pageJson2.put("page", 1);
//        JSONArray chaptes2 = new JSONArray();
//        pageJson2.put("chaptes", chaptes2);
//        chapteJsonArray2.add(pageJson2);
//        JSONObject chapte2 = new JSONObject();
//        chapte2.put("offsetX", 0.20);
//        chapte2.put("offsetY", 0.73);
//        chaptes2.add(chapte2);
//        signatory.withChapteJson(chapteJsonArray2);
//        signatory.setSignLevel(1); //0:标准图形章   1:公章或手写
//
//        signatories.add(signatory);
//        /**
//         * 签约方2
//         */
//       signatory = new Signatory();
//       signatory.setFullName("七彩投资"); //姓名
//       signatory.setSignatoryIdentityType(IdentityType.BIZLIC); //证件类型
//       signatory.setIdentityCard("500903000035447");//证件号码(营业执照号或统一社会信用代码)
//       signatory.setEmail("qicaitouzi@www.bccto.me");//企业账户注册邮箱
//       signatory.setMobile("15062592832");//企业代表手机
//       JSONArray chapteJsonArray3 = new JSONArray();
//       JSONObject pageJson3 = new JSONObject();
//       pageJson3.put("page", 1);
//      JSONArray chaptes3 = new JSONArray();
//     pageJson3.put("chaptes", chaptes3);
//       chapteJsonArray3.add(pageJson3);
//
//      JSONObject chapte3 = new JSONObject();
//      chapte3.put("offsetX", 0.20);
//      chapte3.put("offsetY", 0.73);
//       chaptes3.add(chapte3);
//      signatory.withChapteJson(chapteJsonArray3);
//
//        signatory.setSignLevel(1); //0:标准图形章   1:公章或手写
//
//        signatories.add(signatory);
//
//
//

        builder.withSignatories(signatories); // 添加签约人

        builder.withSignLevel(SignLevel.GENERAL.getCode()); // 签字类型,这里选择标准图形章
        //builder.withRemark("这里是备注信息,不超过500个字符"); //备注
        //builder.withPreRecored("前执记录,会计录到日志中!");
         builder.withServerCa(1);//使用云证书,0 不使用,1 使用
        builder.withDealType(DealType.AUTH_SIGN);//自动签


        ApplySignResponse response = client.applySignFile(builder.build());
//        LogUtils.logResponse(response);
//        System.out.println(response.isSuccess());

        signResult.setSuccess(response.isSuccess());


        signResult.setApplyNo(response.getApplyNo());
       return signResult;
    }
}