package com.tenwa.app.manage.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.ResourceBundle; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import com.qiyuesuo.sdk.v2.SdkClient; import com.qiyuesuo.sdk.v2.bean.Action; import com.qiyuesuo.sdk.v2.bean.Contract; import com.qiyuesuo.sdk.v2.bean.Signatory; import com.qiyuesuo.sdk.v2.bean.Stamper; import com.qiyuesuo.sdk.v2.bean.User; import com.qiyuesuo.sdk.v2.http.StreamFile; import com.qiyuesuo.sdk.v2.json.JSONUtils; import com.qiyuesuo.sdk.v2.param.SignParam; import com.qiyuesuo.sdk.v2.request.ContractDetailRequest; import com.qiyuesuo.sdk.v2.request.ContractDraftRequest; import com.qiyuesuo.sdk.v2.request.ContractNoticeRequest; import com.qiyuesuo.sdk.v2.request.ContractSendRequest; import com.qiyuesuo.sdk.v2.request.ContractSignCompanyRequest; import com.qiyuesuo.sdk.v2.request.DocumentAddByFileRequest; import com.qiyuesuo.sdk.v2.request.DocumentDownloadRequest; import com.qiyuesuo.sdk.v2.response.DocumentAddResult; import com.qiyuesuo.sdk.v2.response.SdkResponse; import com.qiyuesuo.sdk.v2.utils.IOUtils; import com.tenwa.util.MultiSubjectUtil; public class ContractSignActionTwoUtil { private static final ResourceBundle resourceBunlde = ResourceBundle.getBundle("qiyuesuo"); //深圳公司参数 private static final String sZ_Url = resourceBunlde.getString("SZ_Url"); private static final String sZ_Key = resourceBunlde.getString("SZ_Key"); private static final String sZ_Secret = resourceBunlde.getString("SZ_Secret"); private static final String sZ_OfficialSealId = resourceBunlde.getString("SZ_OfficialSealId"); private static final String sZ_CompanyName = resourceBunlde.getString("SZ_CompanyName"); //天津公司参数 private static final String tJ_Url = resourceBunlde.getString("TJ_Url"); private static final String tJ_Key = resourceBunlde.getString("TJ_Key"); private static final String tJ_Secret = resourceBunlde.getString("TJ_Secret"); private static final String tJ_OfficialSealId = resourceBunlde.getString("TJ_OfficialSealId"); private static final String tJ_CompanyName = resourceBunlde.getString("TJ_CompanyName"); //子公司参数(辉煌) private static final String hH_CompanyName = resourceBunlde.getString("HH_CompanyName"); private static final String hH_OfficialSealId = resourceBunlde.getString("HH_OfficialSealId"); public static void main(String[] args) { System.out.println(sZ_Url); System.out.println(sZ_CompanyName); } private SdkClient getSdkClient(String subjectId){ if(MultiSubjectUtil.SZSUBJECTID.equals(subjectId)){ return new SdkClient(sZ_Url, sZ_Key, sZ_Secret); }else if (MultiSubjectUtil.TJSUBJECTID.equals(subjectId)){ return new SdkClient(sZ_Url, sZ_Key, sZ_Secret);//应客户要求,现在都是以深圳主体发起 //return new SdkClient(tJ_Url, tJ_Key, tJ_Secret); } return null; } private String getComPanyName(String subjectId){ if(MultiSubjectUtil.SZSUBJECTID.equals(subjectId)){ return sZ_CompanyName; }else if (MultiSubjectUtil.TJSUBJECTID.equals(subjectId)){ return tJ_CompanyName; } return null; } //创建合同 public SdkResponse createContract(Map>> parameter,String subjectId,String fileNme) throws Exception { SdkClient client = this.getSdkClient(subjectId); Contract draftContract = new Contract(); draftContract.setSubject(fileNme); draftContract.setOrdinal(false); List> list = parameter.get("PERSONAL"); if(list!=null && list.size()>0){ for (Map map : list) { Signatory persoanlSignatory = new Signatory(); persoanlSignatory.setTenantType("PERSONAL"); persoanlSignatory.setTenantName(map.get("name")); persoanlSignatory.setReceiver(new User(map.get("type"), map.get("mobile"), "MOBILE")); draftContract.addSignatory(persoanlSignatory); } } list = parameter.get("COMPANY"); if(list!=null && list.size()>0){ for (Map map : list) { Signatory platformSignatory = new Signatory(); platformSignatory.setTenantType("COMPANY"); Action sealAction = new Action(); sealAction.setType("COMPANY"); if ("1".equals(map.get("type"))) { if(MultiSubjectUtil.SZSUBJECTID.equals(subjectId)){ platformSignatory.setTenantName(sZ_CompanyName); sealAction.setName(sZ_CompanyName); sealAction.setSealId(new Long(sZ_OfficialSealId)); }else if (MultiSubjectUtil.TJSUBJECTID.equals(subjectId)){ platformSignatory.setTenantName(tJ_CompanyName); sealAction.setName(tJ_CompanyName); sealAction.setSealId(new Long(tJ_OfficialSealId)); } } else { platformSignatory.setTenantName(hH_CompanyName); sealAction.setName(hH_CompanyName); sealAction.setSealId(new Long(hH_OfficialSealId) ); } platformSignatory.setReceiver(new User(map.get("mobile"), "MOBILE"));//子公司必须设置接收方,不管是否是子公司,都设置了一个 platformSignatory.addAction(sealAction); draftContract.addSignatory(platformSignatory); } } draftContract.setSend(false); String response = null; try { response = client.service(new ContractDraftRequest(draftContract)); } catch (Exception e) { throw new Exception("创建合同草稿请求服务/器失败,失败原因:" + e.getMessage()); } SdkResponse sdkResponse = JSONUtils.toQysResponse(response, Contract.class); if (!sdkResponse.getCode().equals(0)) { throw new Exception("创建合同草稿失败,失败原因:" + sdkResponse.getMessage()); } return sdkResponse; } //添加本地文件 public SdkResponse addDocumentByFile(String filePath,String subjectId,Long contractId)throws Exception { SdkClient client = this.getSdkClient(subjectId); // 进入本地文件 StreamFile file = new StreamFile(new FileInputStream(new File(filePath))); String response = null; try { // PDF为本地文件的类型,请修改为对应的本地文件类型 response = client.service(new DocumentAddByFileRequest(contractId,file, "pdf", "由文件创建文档")); } catch (Exception e) { throw new Exception("根据文件添加文档请求服务器失败,失败原因:" + e.getMessage()); } SdkResponse sdkResponse = JSONUtils.toQysResponse( response, DocumentAddResult.class); if (!sdkResponse.getCode().equals(0)) { throw new Exception("根据文件添加文档失败,失败原因:" + sdkResponse.getMessage()); } return sdkResponse; } //发起合同 public SdkResponse send(List> personList,String subjectId, Long contractId,Long documentId) throws Exception { SdkClient client = this.getSdkClient(subjectId); Contract draft = getContract(subjectId, contractId).getResult(); String contractName = draft.getSubject(); List stampers = new LinkedList(); // 获取SignatoryId与ActionId,用于指定签署位置,公司签署位置需要指定ActionId,个人签署位置需要指定SignatoryId Long platformSignatoryId = null; Long personalSignatoryId = null; Long companySealActionId = null; int dbcont = 0; for (Signatory signatory : draft.getSignatories()) { // 获取个人签署方 if (signatory.getTenantType().equals("PERSONAL")) { personalSignatoryId = signatory.getId(); String typeName = signatory.getTenantName(); //User user = signatory.getReceiver(); String type = ""; if(personList != null && personList.size()>0){ for (Map person : personList) { if(person.get("name").equals(typeName)){ type = person.get("type"); break; } } } Stamper personalStamper = new Stamper(); personalStamper.setPage(0); personalStamper.setType("PERSONAL"); personalStamper.setSignatoryId(personalSignatoryId); personalStamper.setDocumentId(documentId); if("sq".equals(type)){ //联合租赁和狮桥的一样 personalStamper.setKeyword("乙方(承租人):"); personalStamper.setOffsetX(-0.05); //personalStamper.setOffsetY(-0.02); if(contractName.indexOf("狮桥") > -1){ //申请人添加第二个签字位置 Stamper personalStamper2 = new Stamper(); personalStamper2.setPage(0); personalStamper2.setType("PERSONAL"); personalStamper2.setSignatoryId(personalSignatoryId); personalStamper2.setDocumentId(documentId); personalStamper2.setKeyword("(本人签字并按手印/公司公章)"); personalStamper2.setOffsetX(-0.43); //personalStamper2.setOffsetY(-0.02); stampers.add(personalStamper2); //申请人添加第个签字位置 Stamper personalStamper3 = new Stamper(); personalStamper3.setPage(0); personalStamper3.setType("PERSONAL"); personalStamper3.setSignatoryId(personalSignatoryId); personalStamper3.setDocumentId(documentId); personalStamper3.setKeyword("承租人:"); personalStamper3.setOffsetX(-0.01); personalStamper3.setOffsetY(-0.006); stampers.add(personalStamper3); //添加一个签署时间 Stamper personalStamper4 = new Stamper(); personalStamper4.setPage(0); personalStamper4.setType("TIMESTAMP"); personalStamper4.setSignatoryId(personalSignatoryId); personalStamper4.setDocumentId(documentId); personalStamper4.setKeyword("租赁物已于"); personalStamper4.setOffsetX(0.025); personalStamper4.setOffsetY(-0.0025); stampers.add(personalStamper4); //添加二个签署时间 Stamper personalStamper5 = new Stamper(); personalStamper5.setPage(0); personalStamper5.setType("TIMESTAMP"); personalStamper5.setSignatoryId(personalSignatoryId); personalStamper5.setDocumentId(documentId); personalStamper5.setKeyword("(共同承租人)(若有)"); personalStamper5.setOffsetX(-0.3); personalStamper5.setOffsetY(-0.05); stampers.add(personalStamper5); } }else if("gt".equals(type)){ if(contractName.indexOf("狮桥") > -1){ personalStamper.setKeyword("乙方(承租人):"); personalStamper.setOffsetX(-0.05); personalStamper.setOffsetY(-0.075); //共同申请人添加第二个签字位置 Stamper personalStamper2 = new Stamper(); personalStamper2.setPage(0); personalStamper2.setType("PERSONAL"); personalStamper2.setSignatoryId(personalSignatoryId); personalStamper2.setDocumentId(documentId); personalStamper2.setKeyword("(共同承租人)(若有)"); personalStamper2.setOffsetX(-0.35); //personalStamper2.setOffsetY(-0.02); stampers.add(personalStamper2); //共同申请人添加第三个签字位置 Stamper personalStamper3 = new Stamper(); personalStamper3.setPage(0); personalStamper3.setType("PERSONAL"); personalStamper3.setSignatoryId(personalSignatoryId); personalStamper3.setDocumentId(documentId); personalStamper3.setKeyword("承租人:"); personalStamper3.setOffsetX(-0.01); personalStamper3.setOffsetY(-0.066); stampers.add(personalStamper3); }else{ personalStamper.setKeyword("共同申请人(签字/盖章)"); //personalStamper.setOffsetX(0.6); personalStamper.setOffsetY(-0.02); } }else if("db".equals(type)){ if(dbcont==0){ if(contractName.indexOf("狮桥") > -1){ personalStamper.setKeyword("丙方 1(保证人):"); personalStamper.setOffsetX(-0.05); //personalStamper.setOffsetY(-0.02); }else{ personalStamper.setKeyword("出租人2(盖章)"); personalStamper.setOffsetX(0.01); personalStamper.setOffsetY(-0.12); } }else if(dbcont==1){ if(contractName.indexOf("狮桥") > -1){ personalStamper.setKeyword("丙方 2(保证人):"); personalStamper.setOffsetX(-0.05); //personalStamper.setOffsetY(-0.02); }else{ personalStamper.setKeyword("承租人(签字/盖章):"); personalStamper.setOffsetX(-0.01); personalStamper.setOffsetY(-0.12); } }else if (dbcont==2){ if(contractName.indexOf("狮桥") > -1){ personalStamper.setKeyword("丙方 3(保证人):"); personalStamper.setOffsetX(-0.05); //personalStamper.setOffsetY(-0.02); } } dbcont++; } stampers.add(personalStamper); } // 获取平台方SignatoryId,以及对应的公章签署ActionId和法人章签署ActionId if (signatory.getTenantType().equals("COMPANY")) { for (Action action : signatory.getActions()) { platformSignatoryId = signatory.getId(); companySealActionId = action.getId(); Stamper sealStamper = new Stamper(); sealStamper.setPage(0); sealStamper.setType("COMPANY"); sealStamper.setActionId(companySealActionId); sealStamper.setSignatoryId(platformSignatoryId); sealStamper.setDocumentId(documentId); if (action.getName().equals(sZ_CompanyName)||action.getName().equals(tJ_CompanyName)) { if(contractName.indexOf("狮桥") > -1){ sealStamper.setKeyword("甲方(出租人):(公章) "); sealStamper.setOffsetX(-0.13); sealStamper.setOffsetY(-0.1); //添加第二个盖章位置 Stamper sealStamper2 = new Stamper(); sealStamper2.setPage(0); sealStamper2.setType("COMPANY"); sealStamper2.setActionId(companySealActionId); sealStamper2.setSignatoryId(platformSignatoryId); sealStamper2.setDocumentId(documentId); sealStamper2.setKeyword("出租人(公章)"); sealStamper2.setOffsetX(-0.13); sealStamper2.setOffsetY(-0.04); stampers.add(sealStamper2); //添加一个公司盖章时间 Stamper sealStamper3 = new Stamper(); sealStamper3.setPage(0); sealStamper3.setType("TIMESTAMP"); sealStamper3.setActionId(companySealActionId); sealStamper3.setSignatoryId(platformSignatoryId); sealStamper3.setDocumentId(documentId); sealStamper3.setKeyword("签署日期:"); //sealStamper3.setOffsetX(-0.13); sealStamper3.setOffsetY(-0.006); stampers.add(sealStamper3); }else{ sealStamper.setKeyword("甲方(出租人1):(公章) "); sealStamper.setOffsetX(-0.12); sealStamper.setOffsetY(-0.04); //添加第二个盖章位置 Stamper sealStamper2 = new Stamper(); sealStamper2.setPage(0); sealStamper2.setType("COMPANY"); sealStamper2.setActionId(companySealActionId); sealStamper2.setSignatoryId(platformSignatoryId); sealStamper2.setDocumentId(documentId); sealStamper2.setKeyword("本合同一式三份,交抵押登记机关一份"); sealStamper2.setOffsetX(-0.18); sealStamper2.setOffsetY(-0.10); stampers.add(sealStamper2); } stampers.add(sealStamper); } else { sealStamper.setKeyword("甲方(出租人2):(公章)"); sealStamper.setOffsetX(-0.12); sealStamper.setOffsetY(-0.13); stampers.add(sealStamper); } } } } String response = null; try { response = client.service(new ContractSendRequest(draft.getId(),stampers)); } catch (Exception e) { throw new Exception("发起合同请求服务器失败,失败原因:" + e.getMessage()); } SdkResponse sdkResponse = JSONUtils.toQysResponse(response); if (!sdkResponse.getCode().equals(0)) { throw new Exception("发起合同失败,失败原因:" + sdkResponse.getMessage()); } return sdkResponse; } //公司盖章 public SdkResponse companySealSign(String subjectId,Long contractId) throws Exception { String response = null; try { SdkClient client = this.getSdkClient(subjectId); Contract contract = this.getContract(subjectId,contractId).getResult(); List signatories = contract.getSignatories(); if(signatories!=null && signatories.size()>0){ for (Signatory signatory : signatories) { String tenantType = signatory.getTenantType(); String status = signatory.getStatus(); if(tenantType!=null && "COMPANY".equals(tenantType) && status!=null && "SIGNING".equals(status)){ SignParam param = new SignParam(); param.setContractId(contractId); String company = signatory.getTenantName(); System.out.println("盖章公司名称:"+company); param.setTenantName(company); if(sZ_CompanyName.equals(company)){ param.setSealId(new Long(sZ_OfficialSealId)); }else if(tJ_CompanyName.equals(company)){ param.setSealId(new Long(tJ_OfficialSealId)); }else if(hH_CompanyName.equals(company)){ param.setSealId(new Long(hH_OfficialSealId)); } response = client.service(new ContractSignCompanyRequest(param)); } } } } catch (Exception e) { throw new Exception("公章签署请求服务器失败,失败原因:" + e.getMessage()); } SdkResponse sdkResponse = JSONUtils.toQysResponse(response); if (!sdkResponse.getCode().equals(0)) { throw new Exception("公章签署失败,失败原因:" + sdkResponse.getMessage()); } return sdkResponse; } public String getContractStatus(String subjectId, Long contractId) throws Exception{ Contract contract = this.getContract(subjectId,contractId).getResult(); boolean stu = true; StringBuffer sb = new StringBuffer(); List signatories = contract.getSignatories(); if(signatories!=null && signatories.size()>0){ for (Signatory signatory : signatories) { String tenantType = signatory.getTenantType(); String status = signatory.getStatus(); if(tenantType!=null && "PERSONAL".equals(tenantType)){ if(stu && status != null && (!status.equals("SIGNED")) ){ stu = false ; } String statusName = ""; switch (status) { case "DRAFT": statusName="草稿"; break; case "RECALLED": statusName="已撤回"; break; case "SIGNING": statusName="签署中"; break; case "REJECTED": statusName="已退回"; break; case "SIGNED": statusName="已完成"; break; case "EXPIRED": statusName="已过期"; break; case "FILLING": statusName="拟定中"; break; case "WAITING": statusName="待签署"; break; case "INVALIDING": statusName="作废中"; break; case "INVALIDED": statusName="已作废"; break; default: statusName="状态未匹配"; break; } sb.append(signatory.getTenantName()).append(":").append(statusName).append(";"); } } } if(stu){ return "success"; }else{ return sb.substring(0, sb.length()-1); } } public SdkResponse getContract(String subjectId, Long contractId) throws Exception { SdkClient client = this.getSdkClient(subjectId); ContractDetailRequest request = new ContractDetailRequest(contractId); String response; try { response = client.service(request); } catch (Exception e) { throw new Exception("合同详情查询,失败原因:" + e.getMessage()); } SdkResponse sdkResponse = JSONUtils.toQysResponse(response, Contract.class); if (!sdkResponse.getCode().equals(0)) { throw new Exception("合同详情查询,失败原因:" + sdkResponse.getMessage()); } return sdkResponse; } //合同下载 public String contractDownload(String subjectId, Long documentId, String filePath) throws Exception { SdkClient client = this.getSdkClient(subjectId); DocumentDownloadRequest request = new DocumentDownloadRequest(documentId); FileOutputStream fos = null; try { fos = new FileOutputStream(filePath); client.download(request, fos); IOUtils.safeClose(fos); } catch (FileNotFoundException e) { throw new Exception("合同文件下载,失败原因:" + e.getMessage()); }finally{ fos.close(); } return "合同下载成功!"; } public String messageSend(String subjectId,Long signContractId){ SdkClient client = this.getSdkClient(subjectId); ContractNoticeRequest request = new ContractNoticeRequest(signContractId); String response = client.service(request); SdkResponse responseObj = JSONUtils.toQysResponse(response); if(responseObj.getCode() == 0) { return "success"; } else { System.out.println("短信重发失败:"+responseObj.getCode()+":"+responseObj.getMessage()); return responseObj.getMessage(); } } }