JAVA_将JSONObejct对象格式化字符串由接口传给第三方

单个对象,SerializerFeature.WriteMapNullValue)

String mainFields = "";
        // 表头
        JSONObject mainFieldsJson = new JSONObject();
        mainFieldsJson.put("djId", String.valueOf(info.getId())); // 单据ID
        mainFieldsJson.put("bostypeId", EasOAConstant.BosType_CasPaymentBill); // 付款单bosType
        mainFieldsJson.put("FName", "付款单"); // 名称
        // --------基本信息------------------
        mainFieldsJson.put("FCompanyID", info.getCompany() == null ? null : info.getCompany().getId().toString());    //公司
        mainFieldsJson.put("FNUMBER", info.getNumber() == null ? null : info.getNumber());                            //单据编号
        mainFieldsJson.put("FBizDate", info.getBizDate() == null ? null : info.getBizDate().toString());                //业务日期
        mainFieldsJson.put("FAgentPayCompany", info.getAgentPayCompany() == null ? null : info.getAgentPayCompany().getName());    //代理付款公司
        CostCenterOrgUnitInfo costCenter = info.getCostCenter();
//        mainFieldsJson.put("FCostCenterId", costCenter == null ? null :"("+costCenter.getNumber()+")"+costCenter.getName());            //成本中心
        mainFieldsJson.put("FCostCenterId", costCenter == null ? null :costCenter.getId().toString());            //成本中心

        AccountViewInfo payerViewInfo = info.getPayerAccount();
        AccountBankInfo acctBank = info.getPayerAccountBank();
        AccountViewInfo acctView = info.getPayerAccount();
        CurrencyInfo currency = info.getCurrency();
        BigDecimal balance = RecPayHelper.getTotalBalance(null, info.getCompany(), acctView, acctBank, currency);

        mainFieldsJson.put("FBalance", balance.subtract(info.getActPayAmt()));    //即时余额,列表界面需要减去付款单金额
        mainFieldsJson.put("FFeeTypeID", info.getFeeType() == null ? null : info.getFeeType().getName());    //收付类别
        mainFieldsJson.put("FPayBillTypeID", info.getPayBillType() == null ? null : info.getPayBillType().getName());//付款类型
        mainFieldsJson.put("FActualPayAmount", info.getActPayAmt() == null ? new BigDecimal("0.00") : info.getActPayAmt());//付款金额
        mainFieldsJson.put("FCurrencyId", info.getCurrency() == null ?null: info.getCurrency().getName());//付款币别
        mainFieldsJson.put("FExchangeRate", info.getExchangeRate() == null ?new BigDecimal("1.00") : info.getExchangeRate());//汇率
        mainFieldsJson.put("FActPayLocAmt", info.getActPayLocAmt() == null ?new BigDecimal("0.00") : info.getActPayLocAmt());//实付本位币金额合计
        mainFieldsJson.put("FPayerAccountBankID", info.getPayerAccountBank() == null ?null : info.getPayerAccountBank().getBankAccountNumber());//付款账户
        mainFieldsJson.put("FPayerAccountID", payerViewInfo == null ?null : "("+payerViewInfo.getNumber()+")"+payerViewInfo.getName());//----付款科目
        mainFieldsJson.put("FPayerBankID", info.getPayerBank() == null ?null : info.getPayerBank().getName());//付款银行
        mainFieldsJson.put("FBizTypeID", info.getBizType() == null ? null : info.getBizType().getName());//业务种类
        mainFieldsJson.put("FSettlementTypeID", info.getSettlementType() == null ?null : info.getSettlementType().getName());//结算方式
        mainFieldsJson.put("FSettlementNumber", info.getSettlementNumber() == null ?null : info.getSettlementNumber());//结算号
        
        mainFieldsJson.put("FOutBgItemID", info.getOutBgItemName() == null ?null : info.getOutBgItemName());//流出预算项目
        mainFieldsJson.put("FConceit", info.getConceit() == null? new BigDecimal("0.00") : info.getConceit());//打回意见
        mainFieldsJson.put("FDescription", info.getDescription() == null ?null : info.getDescription());//摘要
        AdminOrgUnitInfo depart = info.getAdminOrgUnit();
//        mainFieldsJson.put("FAdminOrgUnitID", depart == null ?null : "("+depart.getNumber()+")"+depart.getName());//部门
        mainFieldsJson.put("FAdminOrgUnitID", depart == null ?null : depart.getId().toString());//部门
        mainFieldsJson.put("FPersonID", info.getPerson() == null ?null : info.getPerson().getName());//人员
        mainFieldsJson.put("FPayeeTypeID", info.getPayeeType() == null ?null : info.getPayeeType().getName());//(收款人类型)往来户类型
        mainFieldsJson.put("FPayeeName", info.getPayeeName() == null ? null : info.getPayeeName());//(收款人名称)往来户名称
        mainFieldsJson.put("FPayeeAccountBank", info.getPayeeAccountBank() == null ?null: info.getPayeeAccountBank());//收款账号
        mainFieldsJson.put("FPayeeBank", info.getPayeeBank() == null ?null: info.getPayeeBank());//收款银行
        AccountViewInfo oppAccountViewInfo = info.getOppAccount();
        mainFieldsJson.put("FOppAccountID", oppAccountViewInfo == null ?null : "("+oppAccountViewInfo.getNumber()+")"+oppAccountViewInfo.getName());//对方科目
        mainFieldsJson.put("FProjectID", info.getProject() == null ?null : info.getProject().getName());//项目
        mainFieldsJson.put("FProjectManagerID", info.getProjectManager() == null ? null : info.getProjectManager().getName());//项目经理
        mainFieldsJson.put("FAccessoryAmt", info.getAccessoryAmt());//附件// 附件
        JSONArray attachFileArray = new JSONArray();
        try {
            String querySQL = " select b.FID ID, b.FNAME_l2 NAME, b.FREMOTEPATH REMOTEPATH " + " from T_BAS_BoAttchAsso a " 
            + " left join T_BAS_Attachment b on a.FATTACHMENTID = b.FID " + " where fASSOBUSOBJTYPE = '" + EasOAConstant.BosType_CasPaymentBill + "' " 
            + " and fboid = '" + info.getId().toString() + "' " + " order by b.FCREATETIME desc";
            IRowSet rowSet = SQLExecutorFactory.getRemoteInstance(querySQL).executeSQL();
            if (rowSet != null && rowSet.size() > 0) {
                while (rowSet.next()) {
                    String attachmentID = rowSet.getString("ID");
                    String attachmentName = rowSet.getString("NAME");
                    String attachmentPath = rowSet.getString("REMOTEPATH");
                    String fileName = null;
                    if (StringUtils.isNotBlank(attachmentPath)) {
                        fileName = attachmentName + attachmentPath.substring(attachmentPath.indexOf("."));
                    }
                    File attachFile = new FileGetter(AttachmentFactory.getRemoteInstance(), AttachmentFtpFacadeFactory.getRemoteInstance()).downloadAttachment4edit(String.valueOf(attachmentID));
                    attachFileArray.add(fileName + "|" + Base64Util.encodeBase64File(attachFile.getPath()));
                }
                mainFieldsJson.put("fj_easfj", attachFileArray); // 附件
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            logger.error("组装附件信息异常:"+e1.getMessage());
        }
        mainFields = JSONObject.toJSONString(mainFieldsJson,SerializerFeature.WriteMapNullValue);
        return mainFields;

多个对象

 

    private String getDetailFields(PaymentBillInfo info){
        JSONArray detailFieldsArray = new JSONArray();
        JSONArray detailArray1 = new JSONArray();
        String detailFields = "";
        PaymentBillEntryCollection entries = info.getEntries();
        //付款单分录
        if (!EmptyUtil.isEmpty(entries)) {
            for (int i = 0; i < entries.size(); i++) {
                JSONObject entryJson = new JSONObject();  
                PaymentBillEntryInfo entryInfo = entries.get(i);
                entryJson.put("FID",entryInfo.getId()==null?null:entryInfo.getId().toString());                                                //分录id
                entryJson.put("FSEQ",entryInfo.getSeq());                                                                                    //分录序号
                entryJson.put("FPayMentBillID",entryInfo.getPaymentBill().getId().toString());                                                //单据头
                entryJson.put("FActualAmt",entryInfo.getActualAmt()==null? new BigDecimal("0.00") : entryInfo.getActualAmt());                //实付金额
                entryJson.put("FActualLocAmt",entryInfo.getActualLocAmt()==null? new BigDecimal("0.00") : entryInfo.getActualLocAmt());        //实收(付)本位币金额
                
                CostCenterOrgUnitInfo costCenter = entryInfo.getCostCenter();
                ExpenseTypeInfo expenseType = entryInfo.getExpenseType();
                AccountViewInfo oppAccount = entryInfo.getOppAccount();
                FundFlowItemInfo fundFlowItem = entryInfo.getFundFlowItem();
                String outBgItem = null;
                CostBgItemInfo costBgItem = (CostBgItemInfo)entryInfo.get("costBgItem");
                
                entryJson.put("FCostCenterID",costCenter==null? null : "("+costCenter.getNumber()+")"+costCenter.getName());                    //成本中心
                entryJson.put("FExpenseTypeID",expenseType==null? null : "("+expenseType.getNumber()+")"+expenseType.getName());                //费用类型
                entryJson.put("FOppAccountID",oppAccount==null? null : "("+oppAccount.getNumber()+")"+oppAccount.getName());                    //对方科目
                if (!EmptyUtil.isEmpty(entryInfo.getOutBgItemNumber()) && !EmptyUtil.isEmpty(entryInfo.getOutBgItemName())) {
                    outBgItem = "("+entryInfo.getOutBgItemNumber()+")"+entryInfo.getOutBgItemName();
                }
                entryJson.put("FOutBgItemID",outBgItem);                                                                                    //流出预算项目
                entryJson.put("FFundFlowItemID",fundFlowItem==null? null : "("+fundFlowItem.getNumber()+")"+fundFlowItem.getName());            //资金流量项目
                entryJson.put("FRemark",entryInfo.getRemark());                                                                                //备注
                entryJson.put("CFCostBgItemID",costBgItem==null? null : "("+costBgItem.getNumber()+")"+costBgItem.getName());                    //资金预算项目
                entryJson.put("CFCostBgAccount",entryInfo.get("costbgaccount")==null? new BigDecimal("0.00") : entryInfo.getBigDecimal("costbgaccount"));        //资金预算数
                entryJson.put("CFCostBgUse",entryInfo.get("costBguse")==null? new BigDecimal("0.00") : entryInfo.getBigDecimal("costBguse"));        //资金可用预算数
                detailArray1.add(entryJson);
            }
        }
        detailFieldsArray.add(detailArray1);
        detailFields = detailFieldsArray.toJSONString();
        return detailFields;
    }

 

posted @ 2023-08-24 09:33  凉了记忆  阅读(42)  评论(0编辑  收藏  举报