apzl_leasing/src_tenwa/com/tenwa/loan/action/LoanCashPoolManagerAction.java
2018-06-03 22:26:41 +08:00

463 lines
16 KiB
Java

package com.tenwa.loan.action;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.amarsoft.are.jbo.JBOTransaction;
import com.amarsoft.awe.util.ASResultSet;
import com.amarsoft.awe.util.SqlObject;
import com.amarsoft.awe.util.Transaction;
public class LoanCashPoolManagerAction {
/**
* 展示三个月的资金池
* @param tx
* @return
* @throws Exception
*/
public List<Map<String,String>> showCashPoolForThreeMonth(JBOTransaction tx) throws Exception{
Date curDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String showCurDate = sdf.format(curDate);
String showDate="";
Map<String,String> showDates = new HashMap<String,String>();
showDates.put("0week",showCurDate);
for(int i=1;i<=13;i++){
Date date = new Date();
date.setTime(curDate.getTime()+(7L*i*24*3600000));
showDate = sdf.format(date);
showDates.put(i+"week",showDate);
}
List<Map<String,String>> showWeeks = new ArrayList<Map<String,String>>();
Map<String,String> showWeek = new HashMap<String,String>();
showWeek.put("fundtype", "资金类型");
for(int i=0;i<showDates.size();i++){
showWeek.put("week"+i, showDates.get(i+"week"));
}
showWeeks.add(showWeek);
Map<String,Map<String,String>> fundtypes = new HashMap<String,Map<String,String>>();
for(int i=0;i<11;i++){
fundtypes.put("fund"+i, new HashMap<String,String>());
}
fundtypes.get("fund0").put("fundtype", "期初资金余额");
fundtypes.get("fund1").put("fundtype", "回笼租金");
fundtypes.get("fund2").put("fundtype", "业务收款");
fundtypes.get("fund3").put("fundtype", "理财产品到期");
fundtypes.get("fund4").put("fundtype", "新增融资");
fundtypes.get("fund5").put("fundtype", "流入合计");
fundtypes.get("fund6").put("fundtype", "业务放款");
fundtypes.get("fund7").put("fundtype", "偿还贷款");
fundtypes.get("fund8").put("fundtype", "购买理财");
fundtypes.get("fund9").put("fundtype", "流出合计");
fundtypes.get("fund10").put("fundtype", "期末银行余额");
Transaction Sqlca =null;
Sqlca = Transaction.createTransaction(tx);
ASResultSet rs = null;
String sql = "select MAX(INPUTDATE) as calcdate from loan_fund_balance where inputdate<='"+showCurDate+"'";
SqlObject asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String calcDate = "";
if (rs.next()){
calcDate=rs.getString("calcdate");
}
if(calcDate==null||calcDate.length()<=0){
return null;
}
fundtypes = calcWeek0 (fundtypes,calcDate,showCurDate,Sqlca);
fundtypes=calcWeekOther(fundtypes,showDates,Sqlca);
Sqlca.disConnect();
Sqlca=null;
for(int i=0;i<fundtypes.size();i++){
showWeeks.add(fundtypes.get("fund"+i));
}
return showWeeks;
}
/**
* 计算当天期初资金余额
* @param calcDate
* @param showCurDate
* @param Sqlca
* @return
* @throws Exception
*/
public String calcFundBalance (String calcDate,String showCurDate,Transaction Sqlca) throws Exception{
ASResultSet rs = null;
String sql="";
SqlObject asql=null;
String fundbalancein0 ="";
if(showCurDate.equals(calcDate)){
sql = "select SUM(OWN_ACCOUNT_BALANCE) as fundbalance from loan_fund_balance where inputdate='"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
if (rs.next()){
fundbalancein0 = rs.getString("fundbalance");
}
if(fundbalancein0==null||fundbalancein0.length()<=0){
fundbalancein0="0.00";
}
fundbalancein0 = new BigDecimal(String.valueOf(fundbalancein0)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}else{
sql = "select SUM(OWN_ACCOUNT_BALANCE) as fundbalance from loan_fund_balance where inputdate='"+calcDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String fundbalancein = "";
if (rs.next()){
fundbalancein = rs.getString("fundbalance");
}
if(fundbalancein==null||fundbalancein.length()<=0){
fundbalancein="0.00";
}
rs = null;
sql = "select SUM(rent) as rentbalance from lc_rent_plan where plan_date>='"+calcDate+"' and plan_date<'"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String rent="";
if (rs.next()){
rent=rs.getString("rentbalance");
}
if(rent==null||rent.length()<=0){
rent="0.00";
}
rs = null;
sql = "select SUM(plan_money) as fundbalance from lc_fund_plan where pay_type='pay_type_in' and plan_date>='"+calcDate+"' and plan_date<'"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String fundin="";
if (rs.next()){
fundin=rs.getString("fundbalance");
}
if(fundin==null||fundin.length()<=0){
fundin="0.00";
}
rs = null;
sql = "SELECT SUM(PRODUCT_AMOUNT+DUE_INTEREST) AS finproduct FROM LOAN_FIN_PRODUCT WHERE REDEMPTION_DATE >='"+calcDate+"' and REDEMPTION_DATE<'"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String finproductin="";
if (rs.next()){
finproductin=rs.getString("finproduct");
}
if(finproductin==null||finproductin.length()<=0){
finproductin="0.00";
}
rs = null;
sql = "SELECT SUM(LOAN_AMOUNT) AS loanmoney FROM LOAN_BILL_INFO WHERE LOAN_START_DATE>='"+calcDate+"' and LOAN_START_DATE<'"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String loanmoneyin="";
if (rs.next()){
loanmoneyin=rs.getString("loanmoney");
}
if(loanmoneyin==null||loanmoneyin.length()<=0){
loanmoneyin="0.00";
}
rs = null;
sql = "select SUM(plan_money) as fundbalance from lc_fund_plan where pay_type='pay_type_out' and plan_date>='"+calcDate+"' and plan_date<'"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String fundout="";
if (rs.next()){
fundout=rs.getString("fundbalance");
}
if(fundout==null||fundout.length()<=0){
fundout="0.00";
}
rs = null;
sql = "SELECT SUM(rent) AS loanmoney FROM LOAN_FUND_PLAN WHERE plan_date>='"+calcDate+"' and plan_date<'"+showCurDate+"'";;
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String loanmoneyout="";
if (rs.next()){
loanmoneyout=rs.getString("loanmoney");
}
if(loanmoneyout==null||loanmoneyout.length()<=0){
loanmoneyout="0.00";
}
rs = null;
sql = "SELECT SUM(PRODUCT_AMOUNT) AS finproduct FROM LOAN_FIN_PRODUCT WHERE BUY_DATE>='"+calcDate+"' and BUY_DATE<'"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String finproductout="";
if (rs.next()){
finproductout=rs.getString("finproduct");
}
if(finproductout==null||finproductout.length()<=0){
finproductout="0.00";
}
fundbalancein0 = String.valueOf(Double.parseDouble(fundbalancein)+Double.parseDouble(rent)+Double.parseDouble(fundin)+Double.parseDouble(finproductin)+Double.parseDouble(loanmoneyin)-Double.parseDouble(fundout)-Double.parseDouble(finproductout)-Double.parseDouble(loanmoneyout));
fundbalancein0 = new BigDecimal(String.valueOf(fundbalancein0)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}
return fundbalancein0;
}
/**
* 计算当天的资金流入和流出状况
* @param week0
* @param calcDate
* @param showCurDate
* @param Sqlca
* @return
* @throws Exception
*/
public Map<String,Map<String,String>> calcWeek0 (Map<String,Map<String,String>> fundtypes,String calcDate,String showCurDate,Transaction Sqlca) throws Exception{
ASResultSet rs = null;
String sql = "";
SqlObject asql = null;
String fundbalancein = calcFundBalance (calcDate,showCurDate,Sqlca);
fundbalancein = new BigDecimal(String.valueOf(fundbalancein)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
fundtypes.get("fund0").put("week0", fundbalancein);
sql = "select SUM(rent) as rentbalance from lc_rent_plan where plan_date='"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String rent0="";
if (rs.next()){
rent0=rs.getString("rentbalance");
}
if(rent0==null||rent0.length()<=0){
rent0="0.00";
}
fundtypes.get("fund1").put("week0", rent0);
rs = null;
sql = "select SUM(plan_money) as fundbalance from lc_fund_plan where pay_type='pay_type_in' and plan_date='"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String fundin0="";
if (rs.next()){
fundin0=rs.getString("fundbalance");
}
if(fundin0==null||fundin0.length()<=0){
fundin0="0.00";
}
fundtypes.get("fund2").put("week0", fundin0);
rs = null;
sql = "SELECT SUM(PRODUCT_AMOUNT+DUE_INTEREST) AS finproduct FROM LOAN_FIN_PRODUCT WHERE REDEMPTION_DATE='"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String finproductin0="";
if (rs.next()){
finproductin0=rs.getString("finproduct");
}
if(finproductin0==null||finproductin0.length()<=0){
finproductin0="0.00";
}
fundtypes.get("fund3").put("week0", finproductin0);
rs = null;
sql = "SELECT SUM(LOAN_AMOUNT) AS loanmoney FROM LOAN_BILL_INFO WHERE LOAN_START_DATE='"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String loanmoneyin0="";
if (rs.next()){
loanmoneyin0=rs.getString("loanmoney");
}
if(loanmoneyin0==null||loanmoneyin0.length()<=0){
loanmoneyin0="0.00";
}
fundtypes.get("fund4").put("week0", loanmoneyin0);
String intotal = String.valueOf(Double.parseDouble(rent0)+Double.parseDouble(fundin0)+Double.parseDouble(finproductin0)+Double.parseDouble(loanmoneyin0));
intotal = new BigDecimal(String.valueOf(intotal)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
fundtypes.get("fund5").put("week0", intotal);
rs = null;
sql = "select SUM(plan_money) as fundbalance from lc_fund_plan where pay_type='pay_type_out' and plan_date='"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String fundout0="";
if (rs.next()){
fundout0=rs.getString("fundbalance");
}
if(fundout0==null||fundout0.length()<=0){
fundout0="0.00";
}
fundtypes.get("fund6").put("week0", fundout0);
rs = null;
sql = "SELECT SUM(rent) AS loanmoney FROM LOAN_FUND_PLAN WHERE plan_date='"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String loanmoneyout0="";
if (rs.next()){
loanmoneyout0=rs.getString("loanmoney");
}
if(loanmoneyout0==null||loanmoneyout0.length()<=0){
loanmoneyout0="0.00";
}
fundtypes.get("fund7").put("week0", loanmoneyout0);
rs = null;
sql = "SELECT SUM(PRODUCT_AMOUNT) AS finproduct FROM LOAN_FIN_PRODUCT WHERE BUY_DATE='"+showCurDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String finproductout0="";
if (rs.next()){
finproductout0=rs.getString("finproduct");
}
if(finproductout0==null||finproductout0.length()<=0){
finproductout0="0.00";
}
fundtypes.get("fund8").put("week0", finproductout0);
String outtotal = String.valueOf(Double.parseDouble(fundout0)+Double.parseDouble(finproductout0)+Double.parseDouble(loanmoneyout0));
outtotal = new BigDecimal(String.valueOf(outtotal)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
fundtypes.get("fund9").put("week0", outtotal);
String inouttotal = String.valueOf(Double.parseDouble(fundtypes.get("fund0").get("week0"))+Double.parseDouble(intotal)-Double.parseDouble(outtotal));
inouttotal = new BigDecimal(String.valueOf(inouttotal)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
fundtypes.get("fund10").put("week0", inouttotal);
return fundtypes;
}
/**
* 计算除了当天之外的资金流入和流出状况
* @param weekResult
* @param showDates
* @param Sqlca
* @return
* @throws Exception
*/
public Map<String,Map<String,String>> calcWeekOther(Map<String,Map<String,String>> fundtypes,Map<String,String> showDates,Transaction Sqlca) throws Exception{
ASResultSet rs = null;
String sql="";
SqlObject asql=null;
for(int i=1;i<showDates.size();i++){
String startDate = showDates.get((i-1)+"week");
String endDate = showDates.get(i+"week");
String fundbalancein = fundtypes.get("fund10").get("week"+(i-1));
fundbalancein = new BigDecimal(String.valueOf(fundbalancein)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
fundtypes.get("fund0").put("week"+i, fundbalancein);
sql = "select SUM(rent) as rentbalance from lc_rent_plan where plan_date>'"+startDate+"' and plan_date<='"+endDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String rent="";
if (rs.next()){
rent=rs.getString("rentbalance");
}
if(rent==null||rent.length()<=0){
rent="0.00";
}
fundtypes.get("fund1").put("week"+i, rent);
rs = null;
sql = "select SUM(plan_money) as fundbalance from lc_fund_plan where pay_type='pay_type_in' and plan_date>'"+startDate+"' and plan_date<='"+endDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String fundin="";
if (rs.next()){
fundin=rs.getString("fundbalance");
}
if(fundin==null||fundin.length()<=0){
fundin="0.00";
}
fundtypes.get("fund2").put("week"+i, fundin);
rs = null;
sql = "SELECT SUM(PRODUCT_AMOUNT+DUE_INTEREST) AS finproduct FROM LOAN_FIN_PRODUCT WHERE REDEMPTION_DATE >'"+startDate+"' and REDEMPTION_DATE<='"+endDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String finproductin="";
if (rs.next()){
finproductin=rs.getString("finproduct");
}
if(finproductin==null||finproductin.length()<=0){
finproductin="0.00";
}
fundtypes.get("fund3").put("week"+i, finproductin);
rs = null;
sql = "SELECT SUM(LOAN_AMOUNT) AS loanmoney FROM LOAN_BILL_INFO WHERE LOAN_START_DATE>'"+startDate+"' and LOAN_START_DATE<='"+endDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String loanmoneyin="";
if (rs.next()){
loanmoneyin=rs.getString("loanmoney");
}
if(loanmoneyin==null||loanmoneyin.length()<=0){
loanmoneyin="0.00";
}
fundtypes.get("fund4").put("week"+i, loanmoneyin);
String intotal = String.valueOf(Double.parseDouble(rent)+Double.parseDouble(fundin)+Double.parseDouble(finproductin)+Double.parseDouble(loanmoneyin));
intotal = new BigDecimal(String.valueOf(intotal)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
fundtypes.get("fund5").put("week"+i, intotal);
rs = null;
sql = "select SUM(plan_money) as fundbalance from lc_fund_plan where pay_type='pay_type_out' and plan_date>'"+startDate+"' and plan_date<='"+endDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String fundout="";
if (rs.next()){
fundout=rs.getString("fundbalance");
}
if(fundout==null||fundout.length()<=0){
fundout="0.00";
}
fundtypes.get("fund6").put("week"+i, fundout);
rs = null;
sql = "SELECT SUM(rent) AS loanmoney FROM LOAN_FUND_PLAN WHERE plan_date>'"+startDate+"' and plan_date<='"+endDate+"'";;
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String loanmoneyout="";
if (rs.next()){
loanmoneyout=rs.getString("loanmoney");
}
if(loanmoneyout==null||loanmoneyout.length()<=0){
loanmoneyout="0.00";
}
fundtypes.get("fund7").put("week"+i, loanmoneyout);
rs = null;
sql = "SELECT SUM(PRODUCT_AMOUNT) AS finproduct FROM LOAN_FIN_PRODUCT WHERE BUY_DATE>'"+startDate+"' and BUY_DATE<='"+endDate+"'";
asql = new SqlObject(sql);
rs = Sqlca.getASResultSet(asql);
String finproductout="";
if (rs.next()){
finproductout=rs.getString("finproduct");
}
if(finproductout==null||finproductout.length()<=0){
finproductout="0.00";
}
fundtypes.get("fund8").put("week"+i, finproductout);
String outtotal = String.valueOf(Double.parseDouble(fundout)+Double.parseDouble(finproductout)+Double.parseDouble(loanmoneyout));
outtotal = new BigDecimal(String.valueOf(outtotal)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
fundtypes.get("fund9").put("week"+i, outtotal);
String inouttotal = String.valueOf(Double.parseDouble(fundbalancein)+Double.parseDouble(intotal)-Double.parseDouble(outtotal));
inouttotal = new BigDecimal(String.valueOf(inouttotal)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
fundtypes.get("fund10").put("week"+i, inouttotal);
}
return fundtypes;
}
}