package com.tenwa.makeContract.impl; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import java.util.Map; import java.util.UUID; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import jbo.app.tenwa.calc.LC_FUND_INCOME; import jbo.app.tenwa.customer.CUSTOMER_ACCOUNT; import jbo.app.tenwa.doc.LB_DOC_CONTRACT_LIST; import jbo.com.tenwa.entity.comm.flow.FLOW_WORK_FLAG; import jbo.com.tenwa.lease.comm.LB_CONTRACT_INFO; import jbo.com.tenwa.lease.comm.LB_CONTRACT_STATUS; import jbo.com.tenwa.lease.comm.LB_EQUIPMENT_CAR; import jbo.com.tenwa.lease.comm.LB_PROJECT_INFO; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.BizObjectManager; import com.amarsoft.are.jbo.JBOException; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.jbo.JBOTransaction; import com.amarsoft.awe.Configure; import com.amarsoft.awe.util.ASResultSet; import com.amarsoft.awe.util.SqlObject; import com.amarsoft.awe.util.Transaction; import com.tenwa.doc.util.BASE64; import com.tenwa.makeContract.McontractService; import com.tenwa.makeContract.ReturnMessage; import com.tenwa.makeContract.util.MakeOneCodeAndContractModel; import com.tenwa.makeContract.util.McCalcConditionCopyService; import com.tenwa.util.SerialNumberUtil; public class McontractServiceImpl implements McontractService{ MakeOneCodeAndContractModel modc = new MakeOneCodeAndContractModel(); Configure CurConfig = Configure.getInstance(); @Override public ReturnMessage Mcontract(String projectNo,String carNumber, String carNo,String Mstatus){ ReturnMessage rMage = new ReturnMessage(); if(projectNo==null||projectNo==""||projectNo.length()<=0){ rMage.setContractStatus("error"); rMage.setMale("参数错误:申请编号不能为空"); }else if (carNumber==null||carNumber==""||carNumber.length()<=0){ rMage.setContractStatus("error"); rMage.setMale("参数错误:车架号不能为空"); }else if(carNo==null||carNo==""||carNo.length()<=0){ rMage.setContractStatus("error"); rMage.setMale("参数错误:发动机号不能为空"); }else if(Mstatus==null||Mstatus==""||Mstatus.length()<=0){ rMage.setContractStatus("error"); rMage.setMale("参数错误:制作标识不能为空"); }else if("NEW".equalsIgnoreCase(Mstatus)){ String carColor = "以车辆登记证登记颜色为准"; return makeContract(projectNo,carNumber,carNo,carColor); }else if("UPDATE".equalsIgnoreCase(Mstatus)){ return changeContract(projectNo,carNumber,carNo); }else{ rMage.setContractStatus("error"); rMage.setMale("参数错误:制作标识参数有误!!"); } return rMage; } public ReturnMessage makeContract(String projectNo,String carNumber, String carNo,String carColor){ McCalcConditionCopyService mcs = new McCalcConditionCopyService(); ReturnMessage rMage = new ReturnMessage(); MakeOneCodeAndContractModel modc = new MakeOneCodeAndContractModel(); String tempid="";//模板id String sTemplateParam="{'CurUserId':'8006U00000003','CurOrgId':'8006003'";//生成word需要的参数 JBOTransaction tx = null; try { tx =JBOFactory.createJBOTransaction(); BizObjectManager lpiManage = JBOFactory.getBizObjectManager(LB_PROJECT_INFO.CLASS_NAME,tx); BizObjectManager caManage = JBOFactory.getBizObjectManager(CUSTOMER_ACCOUNT.CLASS_NAME,tx); BizObjectManager lciManage = JBOFactory.getBizObjectManager(LB_CONTRACT_INFO.CLASS_NAME,tx); BizObject lpi = lpiManage.createQuery("PROJECT_NO=:projectNo ").setParameter("projectNo", projectNo).getSingleResult(false); if(lpi==null){ rMage.setContractStatus("error"); rMage.setMale("系统中无此申请编号,无法合同制作!!!"); return rMage; } /* BizObject ca = caManage.createQuery("PROJECT_ID=:projectId").setParameter("projectId", lpi.getAttribute("ID").getString()).getSingleResult(false); if(ca==null){ rMage.setContractStatus("error"); rMage.setMale("该申请编号在系统中没有查到对应扣款卡信息。请联系管理员!"); return rMage; }else if(!"Y".equals(ca.getAttribute("SIGN_STATUS").getString())){ rMage.setContractStatus("error"); rMage.setMale("该申请号没有完成签约验证。请提醒客户查看手机短信,首先完成扣款卡的签约验证!"); return rMage; }*/ BizObject lci = lciManage.createQuery("PROJECT_ID=:projectId").setParameter("projectId", lpi.getAttribute("ID").getString()).getSingleResult(false); if(lci!=null){ rMage.setContractStatus("success"); rMage.setMale("该申请已合同制作,无法重新制作!!!"); Map fileBase = modc.fileBase(lci.getAttribute("ID").getString()); if(fileBase.size()>0){ rMage.setFileName(fileBase.get("fileName")); rMage.setOutputFile(fileBase.get("outputFile")); }else{ rMage.setMale("没有找到文件,请联系管理员!!!"); } return rMage; } //生成合同编号 String area_code="0"; String contractno=""; String dept=""; dept+="A"; if(lpi.getAttribute("LEAS_FORM").toString().equals("01")){//直租 dept+="Z"; tempid="ed20493a9234495f9ba9a88a040e3a26"; }else{//回租 dept+="H"; tempid="7cd0bccfc26b4a71aeb159cf36d7bc4f"; } contractno=SerialNumberUtil.getContractSerialNaumber(dept,area_code, tx);//生产合同编号 String contractId = java.util.UUID.randomUUID().toString().replaceAll("-", "");//生成合同ID Transaction Sqlca =null; Sqlca = Transaction.createTransaction(tx); String projectId = lpi.getAttribute("ID").getString(); String SQL="{call proc_contract_Making('"+projectId+"','"+contractId+"','"+contractno+"','"+carNumber+"','"+carColor+"','"+carNo+"')} "; SqlObject asql = new SqlObject(SQL); ASResultSet rs = null; try { rs = Sqlca.getASResultSet(asql); mcs.tempToContractOne(projectId, projectNo, contractno, contractId, tx); } catch (Exception e) { e.printStackTrace(); } finally{ Sqlca.disConnect(); Sqlca=null; }; String diskPath=CurConfig.getConfigure("OneDimensionalCode"); //根据合同编号生成一维码图片 // JBOTransaction onetx =JBOFactory.createJBOTransaction(); String path = modc.MOneDimensionalCode(contractno, contractId, projectId,diskPath,tx); //生成word文档 String wordPath=CurConfig.getConfigure("FileSavePath"); sTemplateParam=sTemplateParam+",'CONTRACT_ID':'"+contractId+"','PROJ_ID':'"+projectId+"','CONTRACT_NO':'"+contractno+"','fileSavePath':'"+wordPath+"','carcolor':'"+carColor+"','carNumber':'"+carNumber+"','carNo':'"+carNo+"'}"; modc.createBqWord(sTemplateParam, tempid, tx); //word转pdf // onetx.commit(); // JBOTransaction wordtx =JBOFactory.createJBOTransaction(); String wordTopdf = modc.wordTopdf(projectId, contractId, tx); //合同盖章 String compnySignIng = modc.compnySignIng(contractId, tx); //将一维码添加到pdf中 // wordtx.commit(); // JBOTransaction pdftx =JBOFactory.createJBOTransaction(); String outPdfFile = CurConfig.getConfigure("BQcontract"); String newPath = modc.pdfAndMark(outPdfFile, path, contractId, tx); // pdftx.commit(); String massage = outputFile(newPath); if("error".equals(massage)){ rMage.setContractStatus("error"); rMage.setMale("pdf文件不存在!!!"); tx.rollback(); return rMage; } rMage.setContractStatus("success"); rMage.setFileName(newPath.substring(newPath.lastIndexOf("/")+1)); rMage.setOutputFile(massage); // String upload = BASE64.encodeImgageToBase64(file); // System.out.println(upload); // return "{'contractStatus':'success','fileName':'"+newPath.substring(newPath.indexOf("/")+1)+"','outputFile':'"+upload+"','male':'申请编号为:"+projectNo+"已合同制作,请勿重复制作'}"; tx.commit(); return rMage; } catch (Exception e) { try { tx.rollback(); } catch (JBOException e1) { e1.printStackTrace(); } e.printStackTrace(); System.out.println(e.toString()); rMage.setContractStatus("error"); rMage.setMale("合同制作失败,请重新发起合同制作!!!"); // return "{'contractStatus':'error','fileName':'','outputFile':'','male':'"+e.toString()+"'}"; return rMage; } } public ReturnMessage changeContract(String projectNo,String carNumber, String carNo){ ReturnMessage rMage = new ReturnMessage(); String tempid="";//模板id String sTemplateParam="{'CurUserId':'8006U00000003','CurOrgId':'8006003'";//生成word需要的参数 JBOTransaction tx = null; try { tx =JBOFactory.createJBOTransaction(); BizObjectManager lpiManage = JBOFactory.getBizObjectManager(LB_PROJECT_INFO.CLASS_NAME,tx); BizObjectManager lecManage = JBOFactory.getBizObjectManager(LB_EQUIPMENT_CAR.CLASS_NAME,tx); BizObjectManager lciManage = JBOFactory.getBizObjectManager(LB_CONTRACT_INFO.CLASS_NAME,tx); BizObject lpi = lpiManage.createQuery("PROJECT_NO=:projectNo ").setParameter("projectNo", projectNo).getSingleResult(false); if(lpi==null){ rMage.setContractStatus("error"); rMage.setMale("系统中无此申请编号,无法合同变更!!!"); return rMage; } String projectId = lpi.getAttribute("ID").getString(); BizObject lci = lciManage.createQuery("PROJECT_ID=:projectId").setParameter("projectId", lpi.getAttribute("ID").getString()).getSingleResult(false); if(lci==null){ rMage.setContractStatus("error"); rMage.setMale("该申请号没有生产合同,请先合同制作!!!"); return rMage; }else if(!"21".equals(lci.getAttribute("CONTRACT_STATUS").getString())){ BizObjectManager lcsManage = JBOFactory.getBizObjectManager(LB_CONTRACT_STATUS.CLASS_NAME,tx); BizObject lcs = lcsManage.createQuery(" STATUS_CODE=:statuscode ").setParameter("statuscode", lci.getAttribute("CONTRACT_STATUS").getString()).getSingleResult(false); if(lcs==null){ rMage.setContractStatus("error"); rMage.setMale("合同状态有误,请联系管理员!!!"); }else{ rMage.setContractStatus("error"); rMage.setMale("合同状态为:"+lcs.getAttribute("STATUS_NAME").getString()+",不能合同变更"); } return rMage; } String contractId = lci.getAttribute("ID").getString(); String contractno = lci.getAttribute("contract_no").getString(); //校验合同是否在付款申请中 BizObjectManager fwfManage = JBOFactory.getBizObjectManager(FLOW_WORK_FLAG.CLASS_NAME,tx); BizObject fwf = fwfManage.createQuery(" mutex_key=:cotnractId and flow_name='FundPaymentCarFlow' ").setParameter("cotnractId", contractId).getSingleResult(false); if(fwf!=null){ rMage.setContractStatus("error"); rMage.setMale("该合同在付款申请中,不能合同变更!!!"); return rMage; } //校验合同是否已经通过付款申请 BizObjectManager lfiManage = JBOFactory.getBizObjectManager(LC_FUND_INCOME.CLASS_NAME,tx); List lfis = lfiManage.createQuery(" contract_id=:contractid ").setParameter("contractid", contractId).getResultList(false); if(lfis.size()>0){ rMage.setContractStatus("error"); rMage.setMale("该合同已经通过付款申请,不能合同变更!!!"); return rMage; } //校验参数数据是否变更 BizObject lec = lecManage.createQuery(" PROJECT_ID=:projectId and CONTRACT_ID=:contractId").setParameter("projectId", projectId).setParameter("contractId", lci.getAttribute("ID").getString()).getSingleResult(true); if(lec==null){ rMage.setContractStatus("error"); rMage.setMale("没有查到租赁物信息,请联系管理员!!!"); return rMage; }else if(carNumber.equals(lec.getAttribute("FRAME_NUMBER").getString())&&carNo.equals(lec.getAttribute("ENGINE_NUMBER").getString())){ rMage.setContractStatus("error"); rMage.setMale("没有可变的数据,请检查数据!!!"); Map fileBase = modc.fileBase(lci.getAttribute("ID").getString()); if(fileBase.size()>0){ rMage.setFileName(fileBase.get("fileName")); rMage.setOutputFile(fileBase.get("outputFile")); } return rMage; } lec.setAttributeValue("FRAME_NUMBER", carNumber); lec.setAttributeValue("ENGINE_NUMBER", carNo); lecManage.saveObject(lec); //获取模板编号 if(lpi.getAttribute("LEAS_FORM").toString().equals("01")){//直租 tempid="ed20493a9234495f9ba9a88a040e3a26"; }else{//回租 tempid="7cd0bccfc26b4a71aeb159cf36d7bc4f"; } //获取模板路径 String wordPath=CurConfig.getConfigure("FileSavePath"); sTemplateParam=sTemplateParam+",'CONTRACT_ID':'"+contractId+"','PROJ_ID':'"+projectId+"','CONTRACT_NO':'"+contractno+"','fileSavePath':'"+wordPath+"','carcolor':'"+lec.getAttribute("CAR_COLOUR").getString()+"','carNumber':'"+carNumber+"','carNo':'"+carNo+"'}"; //删除LB_DOC_CONTRACT_LIST以前的数据 modc.BqChangecontract(contractId, tx); //重新生成word文档 String createBqWord = modc.createBqWord(sTemplateParam, tempid, tx); //将word转pdf,并返回生成的路径 String wordTopdf = modc.wordTopdf(projectId, contractId, tx); //合同盖章 String compnySignIng = modc.compnySignIng(contractId, tx); //将一维码添加到pdf中 String outPdfFile = CurConfig.getConfigure("BQcontract"); String newPath = modc.pdfAndMark(outPdfFile, "", contractId, tx); rMage.setContractStatus("success"); Map fileBase = modc.fileBase(contractId); if(fileBase.size()>0){ rMage.setFileName(fileBase.get("fileName")); rMage.setOutputFile(fileBase.get("outputFile")); rMage.setMale("合同变更成功"); }else{ rMage.setMale("合同文件没有找到,请联系管理员!!!"); } tx.commit(); return rMage; /*String filePath = filePath(lci.getAttribute("ID").getString()); String massage = outputFile(filePath); if("error".equals(massage)){ rMage.setContractStatus("success"); rMage.setMale("pdf文件不存在,请联系管理员!!!"); return rMage; } rMage.setContractStatus("error"); rMage.setMale("该申请已合同制作,无法重新制作!!!"); rMage.setFileName(filePath.substring(filePath.lastIndexOf("/")+1)); rMage.setOutputFile(massage); return rMage;*/ } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return rMage; } public String outputFile(String newPath){ //将文件转为String类型 File file = new File(newPath); if (!file.exists()) { // throw new RuntimeException("要读取的文件不存在"); return "error"; } return BASE64.encodeImgageToBase64(file); } /*public String filePath(String contractId) throws Exception{ BizObjectManager contBom = JBOFactory.getBizObjectManager(LB_DOC_CONTRACT_LIST.CLASS_NAME); BizObject cont = contBom.createQuery(" contract_id=:contractid").setParameter("contractid", contractId).getSingleResult(false); if(cont==null){ return ""; } return cont.getAttribute("fullpath").getString(); }*/ /*public String upload(DataHandler handler, String fileName) { if (fileName != null && !"".equals(fileName)) { File file = new File(fileName); if (handler != null) { InputStream is = null; FileOutputStream fos = null; try { is = handler.getInputStream(); fos = new FileOutputStream(file); byte[] buff = new byte[1024 * 8]; int len = 0; while ((len = is.read(buff)) > 0) { fos.write(buff, 0, len); } } catch (FileNotFoundException e) { return "fileNotFound"; } catch (Exception e) { return "upload File failure"; } finally { try { if (fos != null) { fos.flush(); fos.close(); } if (is != null) { is.close(); } } catch (Exception e) { e.printStackTrace(); } } // return "file absolute path:" + file.getAbsolutePath(); return file.getAbsolutePath(); } else { return "handler is null"; } } else { return "fileName is null"; } }*/ }