package com.tenwa.reckon.executor; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import jbo.app.tenwa.calc.LC_FUND_PLAN; import jbo.sys.CODE_LIBRARY; import com.amarsoft.app.util.ProductParamUtil; import com.amarsoft.app.util.StringUtil; 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.amarsoft.dict.als.cache.CodeCache; import com.amarsoft.dict.als.object.Item; import com.tenwa.comm.exception.BusinessException; import com.tenwa.comm.util.jboutil.DataOperatorUtil; import com.tenwa.reckon.bean.ConditionBean; import com.tenwa.reckon.bean.FundPlanBean; import com.tenwa.reckon.bean.FundRentPlanBean; import com.tenwa.reckon.bean.TabCalBean; import com.tenwa.reckon.util.UUIDUtil; public class FundFundPlanExecutor { public void run(ConditionBean cb , TabCalBean tcb,FundRentPlanBean rentPlan,JBOTransaction tx) throws Exception{ cb.setDocId(tcb.getDocId()); List fundPlanBeans=new ArrayList(); if(Double.parseDouble(cb.getCautionMoney()) > 0 ){ fundPlanBeans = splitFundFundPlan(rentPlan, cb, "", "feetype17","feetype16"); }else{ cb.setDeductionLastPlanDate(cb.getLastPlanDate());//不生成保证金抵扣及退还计划 } fundPlanBeans = this.create(tcb,fundPlanBeans,tx); this.delete(tcb,tx); this.add(fundPlanBeans, tcb,tx); } //创建资金计划 private List create(TabCalBean tcb,List fundPlanBeans,JBOTransaction tx) throws Exception{ //获取产品租赁物类型配置 Map> map=ProductParamUtil.getProductComponentType(tcb.getProductId(), "PRD0315"); //手动添加租赁本金 Map cleanLeaseMoney = new HashMap(); cleanLeaseMoney.put("CostType01", "pay_type_out"); cleanLeaseMoney.put("CostType02", "PaymentTime1"); map.put("CLEAN_LEASE_MONEY", cleanLeaseMoney); String custid=""; if("quoted_price".equals(tcb.getCalType())){ custid=tcb.getDocId(); }else if(tcb.getUserId()==null||tcb.getUserId().equals("")){ } else{ String sql2="select customer_id from "+tcb.getUserId()+" where flowunid='"+tcb.getDocId()+"'"; List> list=DataOperatorUtil.getDataBySql(tx, sql2, null); if(list.size()>0){ custid=list.get(0).get("customer_id"); } } BizObject con=tcb.getCondition(); ConditionBean cb=tcb.getCb(); //String sql="select itemno from O where codeno='FeeType' and relativecode=:code"; Item[] items = CodeCache.getItems("FeeType"); for(Map.Entry> entry:map.entrySet()){ Map param=entry.getValue(); String key = entry.getKey(); if(entry.getKey().indexOf("feetype")>=0){ continue; } //取值 BigDecimal temp =new BigDecimal(con.getAttribute(key).getDouble()); Item item = null; for(Item it : items) { if(key.equals(it.getRelativeCode())) { item = it; break; } } //BizObject item=JBOFactory.createBizObjectQuery(CODE_LIBRARY.CLASS_NAME, sql).setParameter("code", entry.getKey()).getSingleResult(false); if (item!=null && temp.compareTo(BigDecimal.ZERO)>0) { FundPlanBean ffcp = new FundPlanBean(); if("onHire_process".equals(tcb.getCalType())){ BizObjectManager bomLFP = JBOFactory.getBizObjectManager(LC_FUND_PLAN.CLASS_NAME,tx); BizObject boLFP = bomLFP.createQuery("select id from O where contract_id='"+tcb.getCb().getContractId()+"' and fee_type='"+item.getItemNo()+"'") .getSingleResult(true); ffcp.setId(boLFP.getAttribute("id").getString()); bomLFP.deleteObject(boLFP); } ffcp.setFeeType(item.getItemNo());//费用类型 ffcp.setPlanList("1"); ffcp.setSettleMethod("payfund6"); ffcp.setPayType(StringUtil.nullToString(param.get("CostType01")).length()>0?param.get("CostType01"):"pay_type_in"); if("NOMINAL_PRICE".equals(entry.getKey())){ param.put("CostType02", "PaymentTime7"); } if("feetype5".equals(item.getItemNo()) && "".equals(StringUtil.nullToString(param.get("CostType02")))) { continue; } ffcp.setPlanDate(this.getPlanDate(StringUtil.nullToString(param.get("CostType02")), cb)); ffcp.setPlanMoney(temp.toString()); if(StringUtil.nullToString(param.get("CostType03")).equals("cust")){ ffcp.setPayCust(custid); } fundPlanBeans.add(ffcp); if(StringUtil.nullToString(param.get("FINA")).equals("Y")){//需要判断是否融资 if(con.getAttribute(entry.getKey()).getString().equals("N")) { FundPlanBean ffcp2 = new FundPlanBean(); ffcp2.setFeeType(item.getItemNo());//费用类型 ffcp2.setPlanList("1"); ffcp2.setSettleMethod("payfund6"); ffcp2.setPayType(ffcp.getPayType().equals("pay_type_in")?"pay_type_out":"pay_type_in"); ffcp2.setPlanDate(this.getPlanDate(StringUtil.nullToString(param.get("CostType02")), cb)); ffcp2.setPlanMoney(temp.toString()); if(StringUtil.nullToString(param.get("CostType03")).equals("cust")){ ffcp2.setPayCust(custid); } fundPlanBeans.add(ffcp2); } } } } return fundPlanBeans; } //删除资金计划 private void delete(TabCalBean tcb,JBOTransaction tx) throws Exception{ String sql = " delete from O where flowunid='" + tcb.getDocId() + "' and "+tcb.getPlanCName()+"='"+tcb.getPlanCValue()+"'"; BizObjectManager bm=JBOFactory.getBizObjectManager(tcb.getFundFundPlan_tb(), tx); bm.createQuery(sql).executeUpdate(); } //保存资金计划到表 private void add(List fp,TabCalBean tcb,JBOTransaction tx) throws Exception{ com.amarsoft.awe.util.Transaction tran = com.amarsoft.awe.util.Transaction.createTransaction(tx); Connection conn = tran.getConnection(tran); String Sql = "INSERT INTO " + tcb.getFundFundPlan_tb().substring(tcb.getFundFundPlan_tb().lastIndexOf(".") + 1) + " (" + "id, flowunid, " + tcb.getPlanCName() + ", plan_list, " + "plan_date, FEE_TYPE, PLAN_MONEY, PAY_OBJ, SETTLE_METHOD, PAY_TYPE) " + " VALUES (?,?,?,?,?,?,?,?,?,?)"; PreparedStatement ps = null; try { ps = conn.prepareStatement(Sql); for(FundPlanBean fund : fp) { if(fund.getPlanDate() == null){ continue; } ps.setString(1, UUIDUtil.getUUID()); ps.setString(2, tcb.getDocId()); ps.setString(3, tcb.getPlanCValue()); ps.setString(4, fund.getPlanList()); ps.setString(5, fund.getPlanDate()); ps.setString(6, fund.getFeeType()); ps.setString(7, fund.getPlanMoney()); ps.setString(8, fund.getPayObj()); ps.setString(9, fund.getSettleMethod()); ps.setString(10, fund.getPayType()); ps.addBatch(); } ps.executeBatch(); } catch(Exception e) { e.printStackTrace(); throw new BusinessException("insert fund_plan error"); } finally { if(ps != null) ps.close(); } /*BizObjectManager bm=JBOFactory.getBizObjectManager(tcb.getFundFundPlan_tb(), tx); for(FundPlanBean fund : fp){ if(fund.getPlanDate() == null){ continue; } BizObject fundPlan=bm.newObject(); fundPlan.setAttributeValue("flowunid", tcb.getDocId()); fundPlan.setAttributeValue("project_id",tcb.getDocId()); fundPlan.setAttributeValue("plan_list", fund.getPlanList()); fundPlan.setAttributeValue("plan_date", fund.getPlanDate()); fundPlan.setAttributeValue(tcb.getPlanCName(),tcb.getPlanCValue()); if("onHire_process".equals(tcb.getCalType())){ fundPlan.setAttributeValue("id", fund.getId()); fundPlan.setAttributeValue("project_id",tcb.getCb().getProjId()); //fundPlan.setAttributeValue("project_plan_number",tcb.getProjectPlanNumber()); fundPlan.setAttributeValue("contract_id",tcb.getCb().getContractId()); //fundPlan.setAttributeValue("contract_plan_number",tcb.getContractPlanNumber()); } fundPlan.setAttributeValue("FEE_TYPE", fund.getFeeType()); fundPlan.setAttributeValue("PLAN_MONEY", fund.getPlanMoney()); fundPlan.setAttributeValue("PAY_OBJ", fund.getPayObj()); fundPlan.setAttributeValue("SETTLE_METHOD",fund.getSettleMethod()); fundPlan.setAttributeValue("PAY_TYPE", fund.getPayType()); //联合方出资比例 //BigDecimal unionRatio=new BigDecimal(tcb.getCb().getUnioRatio()); //fundPlan.setAttributeValue("plan_money_union",new BigDecimal(fundPlan.getAttribute("PLAN_MONEY").getString()).multiply(unionRatio).divide(new BigDecimal(100), 2,BigDecimal.ROUND_HALF_UP).toString()); //fundPlan.setAttributeValue("plan_money_company", new BigDecimal(fundPlan.getAttribute("PLAN_MONEY").getString()).subtract(new BigDecimal(fundPlan.getAttribute("plan_money_union").getString())).toString()); bm.saveObject(fundPlan); }*/ } //生成抵扣保证金 保证金退还 资金计划 private static List splitFundFundPlan(FundRentPlanBean planBean,ConditionBean cb,String custID,String remainType,String reduceType) throws Exception{ List planDates = planBean.getPlanDateList(); List rentPlans = planBean.getRentList(); List newFundPlans = new ArrayList(); if(Double.parseDouble(cb.getCautionMoneyRemain()) > 0){//退还保证金 FundPlanBean newFundPlan=new FundPlanBean(); newFundPlan.setFeeType(remainType); newFundPlan.setPlanList("1"); newFundPlan.setSettleMethod("payfund6"); newFundPlan.setPayType("pay_type_out"); newFundPlan.setPlanMoney(cb.getCautionMoneyRemain()); newFundPlan.setPlanDate(cb.getEndDate()); newFundPlan.setPayCust(custID); newFundPlans.add(newFundPlan); } if(Double.parseDouble(cb.getCautionDeductionMoney()) > 0){//抵扣保证金 BigDecimal planMoney=new BigDecimal(cb.getCautionDeductionMoney()); int planList=0; for(int i = planDates.size()-1 ; i >= 0 ; i--){ planList++; BigDecimal rent=new BigDecimal(rentPlans.get(i)); if(rent.compareTo(BigDecimal.ZERO)<=0){ continue; } if(planMoney.compareTo(BigDecimal.ZERO)<=0){ cb.setDeductionLastPlanDate(planDates.get(i-1)); break; } if(planMoney.compareTo(rent)>=0){ planMoney=planMoney.subtract(rent); FundPlanBean newFundPlan=new FundPlanBean(); newFundPlan.setFeeType(reduceType); newFundPlan.setPlanList(planList+""); newFundPlan.setSettleMethod("payfund6"); newFundPlan.setPayType("pay_type_out"); newFundPlan.setPlanMoney(rent.toString()); newFundPlan.setPlanDate(planDates.get(i)); newFundPlan.setPayCust(custID); newFundPlans.add(newFundPlan); }else{ FundPlanBean newFundPlan=new FundPlanBean(); newFundPlan.setFeeType(reduceType); newFundPlan.setPlanList(planList+""); newFundPlan.setSettleMethod("payfund6"); newFundPlan.setPayType("pay_type_out"); newFundPlan.setPlanMoney(planMoney.toString()); newFundPlan.setPlanDate(planDates.get(i)); newFundPlan.setPayCust(custID); newFundPlans.add(newFundPlan); cb.setDeductionLastPlanDate(planDates.get(i)); break; } } }else{ cb.setDeductionLastPlanDate(cb.getLastPlanDate()); } return newFundPlans; } public String getPlanDate(String type,ConditionBean cb){ String planDate=""; switch(type){ case "PaymentTime1":planDate=cb.getStartDate();break;//起租日 case "PaymentTime2":planDate=cb.getLeaseAmtDate();break;//付款日 case "PaymentTime3":planDate="";break;//期末保证金 case "PaymentTime4":planDate=cb.getLastPlanDate();break;//末期租金还款日 case "PaymentTime5":planDate=cb.getDeductionLastPlanDate();break;//抵扣后末期租金还款日 case "PaymentTime6":planDate=cb.getEndDate();break;//按租赁期限末期 case "PaymentTime7":planDate=cb.getEndPlanDate();break;//按租金计划末期 default: planDate=cb.getLeaseAmtDate(); } return planDate; } }