apzl_leasing/src/com/tenwa/app/manage/util/ContractSignActionTwoUtil.java

456 lines
17 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Contract> createContract(Map<String, List<Map<String, String>>> parameter,String subjectId,String fileNme) throws Exception {
SdkClient client = this.getSdkClient(subjectId);
Contract draftContract = new Contract();
draftContract.setSubject(fileNme);
draftContract.setOrdinal(false);
List<Map<String, String>> list = parameter.get("PERSONAL");
if(list!=null && list.size()>0){
for (Map<String, String> 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<String, String> 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<Contract> sdkResponse = JSONUtils.toQysResponse(response,
Contract.class);
if (!sdkResponse.getCode().equals(0)) {
throw new Exception("创建合同草稿失败,失败原因:" + sdkResponse.getMessage());
}
return sdkResponse;
}
//添加本地文件
public SdkResponse<DocumentAddResult> 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<DocumentAddResult> sdkResponse = JSONUtils.toQysResponse(
response, DocumentAddResult.class);
if (!sdkResponse.getCode().equals(0)) {
throw new Exception("根据文件添加文档失败,失败原因:" + sdkResponse.getMessage());
}
return sdkResponse;
}
//发起合同
public SdkResponse send(List<Map<String, String>> 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<Stamper> stampers = new LinkedList<Stamper>();
// 获取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<String, String> 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)){
if(contractName.indexOf("狮桥") > -1){
personalStamper.setKeyword("乙方(承租人):");
personalStamper.setOffsetX(-0.05);
//personalStamper.setOffsetY(-0.02);
//申请人添加第二个签字位置
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);
}else{
personalStamper.setKeyword("承租人(签字/盖章):");
//personalStamper.setOffsetX(0.6);
personalStamper.setOffsetY(-0.02);
}
}else if("gt".equals(type)){
if(contractName.indexOf("狮桥") > -1){
personalStamper.setKeyword("乙方(承租人):");
personalStamper.setOffsetX(-0.05);
personalStamper.setOffsetY(-0.065);
//共同申请人添加第二个签字位置
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);
}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);
}else{
sealStamper.setKeyword("出租人2盖章");
sealStamper.setOffsetX(-0.08);
sealStamper.setOffsetY(-0.06);
}
stampers.add(sealStamper);
} else {
sealStamper.setKeyword("出租人1盖章");
sealStamper.setOffsetX(-0.08);
sealStamper.setOffsetY(-0.06);
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<Signatory> 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<Signatory> 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<Contract> 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<Contract> 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();
}
}
}