package com.tenwa.loan.calc; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import jbo.app.tenwa.calc.LC_STANDARD_INTEREST; import jbo.loan.LOAN_BILL_INFO; import jbo.loan.LOAN_CORPUS_PLAN; import jbo.loan.LOAN_CORPUS_PLAN_HIS; import jbo.loan.LOAN_FUND_PLAN; import jbo.loan.LOAN_FUND_PLAN_HIS; import jbo.loan.LOAN_INTEREST_PLAN; import jbo.loan.LOAN_INTEREST_PLAN_HIS; import jbo.loan.LOAN_RATE_ADJUST; import com.amarsoft.app.awe.config.InitDBType; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.BizObjectManager; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.jbo.JBOTransaction; import com.tenwa.comm.util.jboutil.DataOperatorUtil; public class FinancingCalculateServiceImpl { public void submitTransRate(String billId, String adjustId, String docId) throws Exception{ String hisType = "his_rate_change"; //调息 JBOTransaction tx=JBOFactory.createJBOTransaction(); try{ BizObject billInfo =JBOFactory.createBizObjectQuery(LOAN_BILL_INFO.CLASS_NAME,"id=:id").setParameter("id", billId).getSingleResult(false); //央行基准利率调整记录 BizObject adjustInfo = JBOFactory.createBizObjectQuery(LC_STANDARD_INTEREST.CLASS_NAME,"id=:id").setParameter("id", adjustId).getSingleResult(false); if(billInfo==null || adjustInfo==null) return; //获得本次调息的新年利率和调息生效日 Map adjustMap = FundCalculateHelper.getTransRateAndDate(billInfo, adjustInfo); String rateNew = adjustMap.get("yearRate"); String adjustDate = adjustMap.get("startDate");//调息之后利率的执行日期 String rateOld = billInfo.getAttribute("rate").getString(); String lastDate = billInfo.getAttribute("Loan_Start_Date").getString(); //上次结息日 //查询本次调息的上次结算日 String sql = "select max(settlement_date) SETTLEMENT_DATE from loan_interest_plan where bill_id='"+billId+"' and settlement_date<'"+adjustDate+"'"; List> rows =DataOperatorUtil.getDataBySql(tx, sql, null); if(rows!=null && rows.size()>0 && rows.get(0).get("SETTLEMENT_DATE")!=null){ lastDate = rows.get(0).get("SETTLEMENT_DATE").toString(); } //查询本次调息的原利率 sql = "select RATE_OLD,ADJUST_ID from loan_rate_adjust where bill_id='"+billId+"' and mod_reason='"+hisType+"' and status_='rate_adjust' order by adjust_date desc"; rows = DataOperatorUtil.getDataBySql(tx, sql, null); if(rows!=null && rows.size()>0 && rows.get(0).get("RATE_OLD")!=null){ rateOld = rows.get(0).get("RATE_OLD").toString(); for(int i=0; i oldCorpusPlans = billInfo.getLoanCorpusPlans(); Set oldInterestPlans = billInfo.getLoanInterestPlans(); Set oldFundPlans = billInfo.getLoanFundPlans(); List corpusPlanList = new ArrayList(oldCorpusPlans);*/ String hisBefore="his_status_before"; String hisAfter="his_status_after"; Map otherPropMap = new HashMap(); otherPropMap.put("doc_id", docId); otherPropMap.put("mod_reason", modReason); otherPropMap.put("mod_status", hisBefore); Map fromCondition=new HashMap(); fromCondition.put("bill_id", billInfo.getAttribute("id").getString()); DataOperatorUtil.copyJBOSet(LOAN_CORPUS_PLAN.CLASS_NAME, fromCondition, LOAN_CORPUS_PLAN_HIS.CLASS_NAME, null, otherPropMap, null, tx); DataOperatorUtil.copyJBOSet(LOAN_INTEREST_PLAN.CLASS_NAME, fromCondition, LOAN_INTEREST_PLAN_HIS.CLASS_NAME, null, otherPropMap, null, tx); DataOperatorUtil.copyJBOSet(LOAN_FUND_PLAN.CLASS_NAME, fromCondition, LOAN_FUND_PLAN_HIS.CLASS_NAME, null, otherPropMap, null, tx); List corpusPlanList=JBOFactory.createBizObjectQuery(LOAN_CORPUS_PLAN.CLASS_NAME,"BILL_ID=:billid order by plan_date").setParameter("billid", billInfo.getAttribute("id").getString()).getResultList(false); //保存利息计划,上次调息之后的利息计划 this.saveLoanInterestPlan(billInfo, corpusPlanList, lastDate,tx); //保存租金计划 /*String planDate = lastDate; if(lastDate.equals(billInfo.getLoanStartDate())){ this.saveLoanFundPlan(billInfo, lastDate); }else if(interestPlanList.size()>0){ planDate = interestPlanList.get(0).getPlanDate();//bug:planDate之前lastDate之后的计划不会更新 this.saveLoanFundPlan(billInfo, planDate); }else{ this.saveLoanFundPlan(billInfo, lastDate); }*/ this.saveLoanFundPlan(billInfo, lastDate,tx); //上面saveLoanFundPlan是jdbc处理故不需updateFlush() //this.tableService.updateFlush(); //保存历史表数据:后 //Set corpusPlans = billInfo.getLoanCorpusPlans(); //Set interestPlans = billInfo.getLoanInterestPlans(); //Set fundPlans = billInfo.getLoanFundPlans(); otherPropMap.put("mod_status", hisAfter); DataOperatorUtil.copyJBOSet(LOAN_CORPUS_PLAN.CLASS_NAME, fromCondition, LOAN_CORPUS_PLAN_HIS.CLASS_NAME, null, otherPropMap, null, tx); DataOperatorUtil.copyJBOSet(LOAN_INTEREST_PLAN.CLASS_NAME, fromCondition, LOAN_INTEREST_PLAN_HIS.CLASS_NAME, null, otherPropMap, null, tx); DataOperatorUtil.copyJBOSet(LOAN_FUND_PLAN.CLASS_NAME, fromCondition, LOAN_FUND_PLAN_HIS.CLASS_NAME, null, otherPropMap, null, tx); } @SuppressWarnings("unchecked") public List saveLoanInterestPlan(BizObject billInfo, List corpusPlanList, String lastDate,JBOTransaction tx) throws Exception{ //先把利息会发生变更的记录删掉, BizObjectManager bm=JBOFactory.getBizObjectManager(LOAN_INTEREST_PLAN.CLASS_NAME, tx); List interestPlanList = new ArrayList(); List rateInfoList = null; int startList = 1; //开始期项 bm.createQuery("DELETE from O where bill_id=:billid and (settlement_Date>'"+lastDate+"' or settlement_Date<='"+billInfo.getAttribute("loan_start_date").getString()+"')") .setParameter("billid", billInfo.getAttribute("id").getString()).executeUpdate(); String sql = "select count(*) NUM from loan_interest_plan where bill_id='"+billInfo.getAttribute("id").getString()+"' "; List> rows= DataOperatorUtil.getDataBySql(tx, sql, null); if(rows!=null && rows.size()>0 && rows.get(0).get("NUM")!=null){ startList = Integer.parseInt(rows.get(0).get("NUM").toString()); } startList = startList + 1;//需要重新计算利息的期项 if("ratetype01".equals(billInfo.getAttribute("Rate_Type").getString())){//浮动利率 //查询放款日之后的央行基准利率变更记录,不查询上次结算日之后的记录是由于借据的调息生效日可能是基准日之后的一段时间 //央行基准利率调整记录 rateInfoList=JBOFactory.createBizObjectQuery(LC_STANDARD_INTEREST.CLASS_NAME,"select * from O where start_Date>:startdate order by start_Date").setParameter("startdate", billInfo.getAttribute("loan_start_date").getString()).getResultList(false); } interestPlanList = FundCalculateHelper.calculateInterestPlan(billInfo, corpusPlanList, lastDate, startList, rateInfoList,tx); for(BizObject plan:interestPlanList){ BizObject bo=bm.newObject(); DataOperatorUtil.coptyJBOPropertyNoKey(plan, bo); bm.saveObject(bo); } return interestPlanList; } public BizObject saveCalculateFundPlan(BizObject info,Map param) throws Exception { JBOTransaction tx=JBOFactory.createJBOTransaction(); String lastDate =param.get("lastDate"); //上次结息日,默认放款日 String operType =param.get("operType"); //保存借款借据信息/ if("add".equals(operType)){//新增时才测算本金、利息,修改不计算 //保存本金计划 List corpusList = this.saveLoanCorpusPlan(info); //保存利息计划 List interestPlanList = this.saveLoanInterestPlan1(info, corpusList,lastDate); //保存租金计划 String planDate = lastDate; if(lastDate.equals(info.getAttribute("loan_start_date").getString())){ this.saveLoanFundPlan(info, lastDate,tx); }else if(interestPlanList.size()>0){ planDate = interestPlanList.get(0).getAttribute("plan_date").getString(); this.saveLoanFundPlan(info, planDate,tx); }else{ this.saveLoanFundPlan(info, lastDate,tx); } } tx.commit(); return info; } public void saveLoanFundPlan(BizObject billInfo, String planDate,JBOTransaction tx) throws Exception{ BizObjectManager bm=JBOFactory.getBizObjectManager(LOAN_FUND_PLAN.CLASS_NAME,tx); String sql = ""; String billid=billInfo.getAttribute("id").getString(); if(InitDBType.DBTYPE.equals("MYSQL")){ //删除指定借据上次结息日之后的租金计划,查询loan_fund_rent_income的最大日期是为了排除上次结息日与本次结息日之间的本金核销 sql="select ifnull(MAX(hire_date),'"+planDate+"') A from loan_fund_rent_income where bill_id='"+billid+"'"; }else if(InitDBType.DBTYPE.equals("ORACLE")){ //删除指定借据上次结息日之后的租金计划,查询loan_fund_rent_income的最大日期是为了排除上次结息日与本次结息日之间的本金核销 sql="select NVL(MAX(hire_date),'"+planDate+"') A from loan_fund_rent_income where bill_id='"+billid+"'"; } List> l=DataOperatorUtil.getDataBySql(tx, sql, null); bm.createQuery("delete from O where bill_id=:billid and (plan_date>'"+l.get(0).get("A")+"' or plan_date<:startdate)").setParameter("billid", billid).setParameter("startdate", billInfo.getAttribute("loan_start_date").getString()).executeUpdate(); if(InitDBType.DBTYPE.equals("MYSQL")){ //根据指定借据的本金、利息计划拼接上次结息日之后的租金计划 sql = "select tab.* from (" + "select BILL_ID,PLAN_DATE,MAX(SETTLEMENT_DATE) SETTLEMENT_DATE,SUM(RENT) RENT,SUM(CORPUS) CORPUS,SUM(INTEREST) INTEREST,CURRENCY from " + "(select bill_id,plan_date,null as settlement_date,ifnull(corpus,0) as rent,ifnull(corpus,0) as corpus,0 as interest,currency from loan_corpus_plan where bill_id='"+billid+"' " + "union all " + "select bill_id,plan_date,settlement_date,ifnull(interest,0) as rent,0 as corpus,ifnull(interest,0) as interest,currency from loan_interest_plan where bill_id='"+billid+"' " + ") tmp group by bill_id,plan_date,currency " + ") tab where plan_date>(select ifnull(MAX(hire_date),'"+planDate+"') a from loan_fund_rent_income where bill_id='"+billid+"') order by plan_date"; }else if(InitDBType.DBTYPE.equals("ORACLE")){ //根据指定借据的本金、利息计划拼接上次结息日之后的租金计划 sql = "select tab.* from (" + "select BILL_ID,PLAN_DATE,MAX(SETTLEMENT_DATE) SETTLEMENT_DATE,SUM(RENT) RENT,SUM(CORPUS) CORPUS,SUM(INTEREST) INTEREST,CURRENCY from " + "(select bill_id,plan_date,null as settlement_date,NVL(corpus,0) as rent,NVL(corpus,0) as corpus,0 as interest,currency from loan_corpus_plan where bill_id='"+billid+"' " + "union all " + "select bill_id,plan_date,settlement_date,NVL(interest,0) as rent,0 as corpus,NVL(interest,0) as interest,currency from loan_interest_plan where bill_id='"+billid+"' " + ") tmp group by bill_id,plan_date,currency " + ") tab where plan_date>(select NVL(MAX(hire_date),'"+planDate+"') a from loan_fund_rent_income where bill_id='"+billid+"') order by plan_date"; } List> list=DataOperatorUtil.getDataBySql(tx, sql, null); for(int i=0;i saveLoanCorpusPlan(BizObject billInfo) throws Exception{ BizObjectManager bm=JBOFactory.getBizObjectManager(LOAN_CORPUS_PLAN.CLASS_NAME); bm.createQuery("delete from O where bill_id=:billid").setParameter("billid", billInfo.getAttribute("id").getString()).executeUpdate(); List corpusPlanList = FundCalculateHelper.calculateCorpusPlan(billInfo); for(BizObject bo:corpusPlanList){ BizObject corpus=bm.newObject(); DataOperatorUtil.coptyJBOPropertyNoKey(bo, corpus); bm.saveObject(bo); } return corpusPlanList; } @SuppressWarnings("unchecked") public List saveLoanInterestPlan1(BizObject billInfo, List corpusPlanList, String lastDate) throws Exception{ BizObjectManager bm=JBOFactory.getBizObjectManager(LOAN_INTEREST_PLAN.CLASS_NAME); List interestPlanList = new ArrayList(); List rateInfoList = null; int startList = 1; //开始期项 bm.createQuery("delete from O where bill_id=:billid and (settlement_Date>:startdate or settlement_Date<=:enddate)") .setParameter("billid", billInfo.getAttribute("bill_id").getString()) .setParameter("startdate", lastDate) .setParameter("enddate",billInfo.getAttribute("loan_start_date").getString() ).executeUpdate(); List list=bm.createQuery("select * from O where bill_id=:billid").setParameter("billid",billInfo.getAttribute("bill_id").getString()).getResultList(false); if(list.size()>0){ startList=list.size(); } if("ratetype01".equals(billInfo.getAttribute("RATE_TYPE").getString())){//浮动利率 //查询放款日之后的央行基准利率变更记录,不查询上次结算日之后的记录是由于借据的调息生效日可能是基准日之后的一段时间 //央行基准利率调整记录 rateInfoList=JBOFactory.createBizObjectQuery(LC_STANDARD_INTEREST.CLASS_NAME,"select * from O where start_date>:startdate order by start_date").setParameter("startdate", billInfo.getAttribute("loan_start_date").getString()).getResultList(false); } interestPlanList = FundCalculateHelper.calculateInterestPlan1(billInfo, corpusPlanList, lastDate, startList, rateInfoList); for(BizObject interest:interestPlanList){ BizObject bo=bm.newObject(); DataOperatorUtil.coptyJBOPropertyNoKey(interest, bo); bm.saveObject(bo); } return interestPlanList; } }