178 lines
9.3 KiB
Java
178 lines
9.3 KiB
Java
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<Map<String, String>> cashes =DataOperatorUtil.getDataBySql(tx, cashSql, null);
|
||
List<String > netList = new ArrayList<String>();
|
||
List<String > dateList = new ArrayList<String>();
|
||
for(Map<String, String> 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<BigDecimal> newNetList=new ArrayList<BigDecimal>();
|
||
List<String> newDateList=new ArrayList<String>();
|
||
for(int i=0;i<dateList.size();i++){
|
||
String monthFistDay=dateList.get(i).substring(0, dateList.get(i).length()-2)+"01";
|
||
if(monthFistDay.equals(upperListDate)){
|
||
newNetList.set(newNetList.size()-1, newNetList.get(newNetList.size()-1).add(new BigDecimal(netList.get(i))));
|
||
}else{
|
||
newNetList.add(new BigDecimal(netList.get(i)));
|
||
newDateList.add(monthFistDay);
|
||
}
|
||
upperListDate=monthFistDay;
|
||
}
|
||
List<BigDecimal> inflowPour=new ArrayList<BigDecimal>();
|
||
String plandate="";
|
||
for(int i=0;i<newDateList.size();i++){
|
||
if(i==0){
|
||
inflowPour.add(newNetList.get(i));
|
||
}else{
|
||
int month=DateUtils.getBetweenMonths(plandate, newDateList.get(i),"yyyy/MM/dd");
|
||
if(month>1){
|
||
for(int y=1;y<month;y++){
|
||
inflowPour.add(BigDecimal.ZERO);
|
||
}
|
||
}
|
||
inflowPour.add(newNetList.get(i));
|
||
}
|
||
plandate=newDateList.get(i);
|
||
}
|
||
irr=IRRCalculateUtil.getIRR(inflowPour, BigDecimal.ONE).multiply(new BigDecimal(100)).toString();
|
||
}else{
|
||
irr = IrrTools.getIrrNew(netList,dateList, tcb.getCalType());
|
||
}
|
||
return irr;
|
||
}
|
||
|
||
private void delete(TabCalBean tcb,ConditionBean cb) throws Exception{
|
||
BizObjectManager bm=JBOFactory.getBizObjectManager(tcb.getContractCashTb(), tx);
|
||
String sql = " delete from O where flowunid='" + tcb.getDocId() + "' and "+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'";
|
||
bm.createQuery(sql).executeUpdate();
|
||
}
|
||
|
||
private void add(ConditionBean cb,TabCalBean tcb) throws Exception{
|
||
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, " ;
|
||
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 " ;
|
||
sql +=" where fundplan.flowunid = '"+ tcb.getDocId()+"' and fundplan."+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'";
|
||
}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, " ;
|
||
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<String,Map<String,String>> productCashInIRRList = ProductParamUtil.getProductComponentType(productId, "PRD0315");
|
||
Set<Entry<String,Map<String,String>>> entry = productCashInIRRList.entrySet();
|
||
for(Entry<String,Map<String,String>> e : entry){
|
||
Map<String,String> 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(sql);
|
||
Sqlca.executeSQL(asql);
|
||
}
|
||
|
||
|
||
|
||
}
|