package com.tenwa.reckon.executor; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import com.amarsoft.app.als.sys.tools.Tools; import com.amarsoft.app.awe.config.InitDBType; import com.amarsoft.app.util.ProductParamUtil; import com.amarsoft.are.jbo.BizObjectManager; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.jbo.JBOTransaction; import com.amarsoft.awe.util.SqlObject; import com.amarsoft.awe.util.Transaction; import com.tenwa.comm.util.jboutil.DataOperatorUtil; import com.tenwa.reckon.bean.ConditionBean; import com.tenwa.reckon.bean.TabCalBean; import com.tenwa.reckon.util.DateUtils; import com.tenwa.reckon.util.IRRCalculateUtil; import com.tenwa.reckon.util.IrrTools; public class CashFlowExecutor { private JBOTransaction tx; public CashFlowExecutor(JBOTransaction tx){ this.tx=tx; } public String run(ConditionBean cb , TabCalBean tcb) throws Exception{ this.delete(tcb, cb); this.add(cb, tcb); String irr = this.getIrr(cb,tcb); cb.setIrr(irr); return irr; } private String getIrr(ConditionBean cb ,TabCalBean tcb) throws Exception{ String cashSql = "select NET_FLOW,PLAN_DATE from "+Tools.getTable(tcb.getContractCashTb())+" where flowunid ='"+tcb.getDocId()+"' and "+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"' order by plan_date"; List> cashes =DataOperatorUtil.getDataBySql(tx, cashSql, null); List netList = new ArrayList(); List dateList = new ArrayList(); for(Map cash :cashes){ netList.add(cash.get("NET_FLOW")); dateList.add(cash.get("PLAN_DATE")); } String irr=""; if("STAGE_IRR".equals(cb.getIrrType())){//按期IRR BigDecimal issueRate=IRRCalculateUtil.getIRR2(netList); irr=issueRate.multiply( new BigDecimal(1200/cb.getIncomeIntervalMonth())).setScale(6,BigDecimal.ROUND_HALF_UP).toString(); }else if("MONTH_IRR".equals(cb.getIrrType())){//按月IRR String upperListDate=""; List newNetList=new ArrayList(); List newDateList=new ArrayList(); for(int i=0;i inflowPour=new ArrayList(); String plandate=""; for(int i=0;i1){ for(int y=1;y> productRevenues = tcb.getProductRevenues(); String GPSDifference = "0";//获取产品中的GPS差额配置 Map GPSMap = productRevenues == null ? null : productRevenues.get( "GPSDifference" ); if( GPSMap != null && GPSMap.size() > 0){ GPSDifference = GPSMap.get("GPSDifference"); if( GPSDifference == null || GPSDifference.isEmpty() ){ GPSDifference = "0"; } } /* * 根据客户(张文竹)要求,将个人意外险差额去掉 String personalInsuranceDifference = "0" ;//获取产品中的个人意外险差额配置 Map pidMap = productRevenues == null ? null : productRevenues.get( "personalInsuranceDifference" ); if( pidMap != null && pidMap.size() > 0){ personalInsuranceDifference = pidMap.get("personalInsuranceDifference"); if( personalInsuranceDifference == null || personalInsuranceDifference.isEmpty() ){ personalInsuranceDifference = "0"; } }*/ String sql = ""; if("MYSQL".equals(InitDBType.DBTYPE)){ sql += "INSERT INTO " +Tools.getTable(tcb.getContractCashTb())+ " (id," + tcb.getPlanCName() + ""; sql += ",plan_date"; sql += ",fund_in"; sql += ",fund_in_details"; sql += ",fund_out"; sql += ",fund_out_details"; sql += ",net_flow"; sql += ",flowunid";//create_date,creator_ "+DateUtil.getSystemDate()+"' create_date ,'"+SecurityUtil.getPrincipal().getId()+"' creator_" sql += " )"; sql +=" select replace(uuid(),'-','') id,'"+tcb.getPlanCValue()+"' "+tcb.getPlanCName()+",t.plan_date,sum(t.flowin) fundin,ifnull(group_concat(if(t.flowindetail='',null,t.flowindetail)),'-') fundindetails, "; sql +=" sum(t.flowout) fundout ,ifnull(group_concat(if(t.flowoutdetail='',null,t.flowoutdetail)),'-')fundoutdetails,sum(t.cleanfow)netflow ,'"+tcb.getDocId()+"' flowunid " ; sql +=" from (" ; sql +=" select cfrp.plan_date,cfrp.rent flowin,concat('第',cfrp.plan_list,'期租金:',format(cfrp.rent,2)) flowindetail,0 flowout,'' flowoutdetail,cfrp.rent-0 cleanfow " ; sql +=" from "+Tools.getTable(tcb.getRentPlan_tb()) +" cfrp" ; sql +=" where cfrp.flowunid = '"+tcb.getDocId()+"' and cfrp."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'" ; sql +=" union all " ; sql +=" select fundplan.plan_date,if(fundplan.pay_type='pay_type_in',fundplan.plan_money,0)flowin, "; sql +=" if(fundplan.pay_type='pay_type_in',concat(tdd.itemname,':',format(fundplan.plan_money,2)),''), " ; sql +=" if(fundplan.pay_type='pay_type_out',fundplan.plan_money,0)flowout, " ; if("business_product".equals(tcb.getProductType())) { sql +=" if(fundplan.pay_type='pay_type_out',concat(if(tdd.itemno = 'feetype10','设备款',tdd.itemname),':',format(fundplan.plan_money,2)),'')flowoutdetail, " ; } else { sql +=" if(fundplan.pay_type='pay_type_out',concat(tdd.itemname,':',format(fundplan.plan_money,2)),'')flowoutdetail, " ; } sql +=" if(fundplan.pay_type='pay_type_in',fundplan.plan_money,-fundplan.plan_money) cleanfow " ; sql +=" from "+Tools.getTable(tcb.getFundFundPlan_tb())+" fundplan " ; sql +=" left join code_library tdd on fundplan.fee_type = tdd.itemno and tdd.codeno='FeeType' " ; sql +=" where fundplan.flowunid = '"+ tcb.getDocId()+"' and fundplan."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"' and fundplan.fee_type in ('feetype2','feetype1','feetype10','feetype16','feetype17','feetype33','feetype24') "; //根据客户(张文竹)不管是否灵活产品,配置的GPS差额和个人意外险差额添加到IRR计算 /* if( !"0".equals(personalInsuranceDifference) ){ sql +=" union all select plan_date,'"+personalInsuranceDifference+"' flowin,'个人意外险差额:"+personalInsuranceDifference+"' flowindetail,'' flowout,'' flowoutdetil,'"+personalInsuranceDifference+"' cleanfow from lc_fund_plan_temp where flowunid = '"+tcb.getDocId()+"' and fee_type='feetype10' "; } */ if( !"0".equals(GPSDifference) ){ sql += "union all select plan_date,'"+GPSDifference+"' flowin,'GPS差额:"+GPSDifference+"' flowindetail,'' flowout,'' flowoutdetil,'"+GPSDifference+"' cleanfow from lc_fund_plan_temp where flowunid = '"+tcb.getDocId()+"' and fee_type='feetype10' "; } }else if("ORACLE".equals(InitDBType.DBTYPE)){ sql += "INSERT INTO " +Tools.getTable(tcb.getContractCashTb())+ " (id," + tcb.getPlanCName() + ""; sql += ",plan_date"; sql += ",fund_in"; sql += ",fund_in_details"; sql += ",fund_out"; sql += ",fund_out_details"; sql += ",net_flow"; sql += ",flowunid";//create_date,creator_ "+DateUtil.getSystemDate()+"' create_date ,'"+SecurityUtil.getPrincipal().getId()+"' creator_" sql += " )"; sql +=" select sys_guid() id,'"+tcb.getPlanCValue()+"' "+tcb.getPlanCName()+",t.plan_date,sum(t.flowin) fundin,nvl(wmsys.wm_concat(case when t.flowindetail='' then null else t.flowindetail end ),'-') fundindetails, "; sql +=" sum(t.flowout) fundout ,nvl(wmsys.wm_concat(case when t.flowoutdetail='' then null else t.flowoutdetail end),'-') fundoutdetails,sum(t.cleanfow)netflow ,'"+tcb.getDocId()+"' flowunid " ; sql +=" from (" ; sql +=" select cfrp.plan_date,cfrp.rent flowin,'第'||cfrp.plan_list||'期租金:'||to_char(cfrp.rent,'FM9,999,999,999,999,999,999,999,990.00') flowindetail,0 flowout,'' flowoutdetail,cfrp.rent-0 cleanfow " ; sql +=" from "+Tools.getTable(tcb.getRentPlan_tb()) +" cfrp" ; sql +=" where cfrp.flowunid = '"+tcb.getDocId()+"' and cfrp."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'" ; sql +=" union all " ; sql +=" select fundplan.plan_date,case when fundplan.pay_type='pay_type_in'then fundplan.plan_money else 0 end flowin, "; sql +=" case when fundplan.pay_type='pay_type_in'then concat(concat(tdd.itemname,':'),to_char(fundplan.plan_money,'FM9,999,999,999,999,999,999,999,990.00')) else ''end, " ; sql +=" case when fundplan.pay_type='pay_type_out'then fundplan.plan_money else 0 end flowout, " ; if("business_product".equals(tcb.getProductType())) { sql +=" case when fundplan.pay_type='pay_type_out' then (case then tdd.itemno = 'feetype10' then '设备款' else tdd.itemname end) ||':'||to_char(fundplan.plan_money,'FM9,999,999,999,999,999,999,999,990.00') else ''end flowoutdetail, " ; } else { sql +=" case when fundplan.pay_type='pay_type_out' then tdd.itemname||':'||to_char(fundplan.plan_money,'FM9,999,999,999,999,999,999,999,990.00') else ''end flowoutdetail, " ; } sql +=" case when fundplan.pay_type='pay_type_in' then fundplan.plan_money else -fundplan.plan_money end cleanfow " ; sql +=" from "+Tools.getTable(tcb.getFundFundPlan_tb())+" fundplan " ; sql +=" left join code_library tdd on fundplan.fee_type = tdd.itemno " ; sql +=" where fundplan.flowunid = '"+ tcb.getDocId()+"' and fundplan."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'"; } String productId = tcb.getProductId(); if(productId != null){ Map> productCashInIRRList = ProductParamUtil.getProductComponentType(productId, "PRD0315"); Set>> entry = productCashInIRRList.entrySet(); for(Entry> e : entry){ Map parameMap = e.getValue(); boolean flag = false; if("N".equals(parameMap.get("CostType10"))){ flag = true; } if(flag){ sql +=" and tdd.relativecode<>'"+e.getKey()+"' "; } } } //sql +=" and fundplan."+tcb.getContOrProjCName()+" = '"+tcb.getContOrProjCValue()+"'" ; sql +=" )t group by t.plan_date "; Transaction Sqlca =null; Sqlca = Transaction.createTransaction(tx); SqlObject asql = new SqlObject(""); asql.setOriginalSql(sql); Sqlca.executeSQL(asql); String depositStyle = ""; Map> productCost = ProductParamUtil.getProductComponentType(productId, "PRD0315"); if( productCost != null && productCost.size() > 0 ){ Map cautionMoney = productCost.get("CAUTION_MONEY"); if( cautionMoney != null && cautionMoney.size() > 0 ){ depositStyle = cautionMoney.get("DepositStyle"); } } String lcftSql = "select ID,PLAN_DATE,FUND_IN,FUND_OUT from lc_cash_flow_temp where flowunid ='"+tcb.getDocId()+"' and "+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"' order by plan_date desc"; List> lcftDate =DataOperatorUtil.getDataBySql(tx, lcftSql, null); BigDecimal outMoney = null ;//保证金 BigDecimal inMoney = null ;//每期租金 String updateSql = "" ; if( "caution_money_method01".equals( depositStyle ) ){//保证金抵扣 for (int i=0 ; i < lcftDate.size() ; i++ ) { if(i==0){ outMoney = new BigDecimal(cb.getCautionMoney() ); inMoney = new BigDecimal(lcftDate.get(i).get("FUND_IN") ); }else{ inMoney = new BigDecimal(lcftDate.get(i).get("FUND_IN") ); } if(outMoney.compareTo(inMoney)>=0){ updateSql = " update lc_cash_flow_temp set FUND_OUT='"+inMoney+"',FUND_OUT_DETAILS='抵扣保证金:"+inMoney+"',NET_FLOW='0.00' where id='"+lcftDate.get(i).get("ID")+"' "; asql.setOriginalSql(updateSql); Sqlca.executeSQL(asql); outMoney = outMoney.subtract(inMoney); }else{ updateSql = " update lc_cash_flow_temp set FUND_OUT='"+outMoney+"',FUND_OUT_DETAILS='抵扣保证金:"+outMoney+"',NET_FLOW='"+inMoney.subtract(outMoney)+"' where id='"+lcftDate.get(i).get("ID")+"' "; asql.setOriginalSql(updateSql); Sqlca.executeSQL(asql); break; } } }else if ("caution_money_method02".equals( depositStyle )){//保证金退回 outMoney = new BigDecimal(cb.getCautionMoney() ); inMoney = new BigDecimal(lcftDate.get(0).get("FUND_IN") ); updateSql = " update lc_cash_flow_temp set FUND_OUT='"+outMoney+"',FUND_OUT_DETAILS='抵扣保证金:"+outMoney+"',NET_FLOW='"+inMoney.subtract(outMoney)+"' where id='"+lcftDate.get(0).get("ID")+"' "; asql.setOriginalSql(updateSql); Sqlca.executeSQL(asql); } } }