package com.tenwa.lease.app.quartzmession; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import jbo.com.tenwa.lease.comm.CONTRACT_RENT_MONTH; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import com.amarsoft.app.awe.config.InitDBType; import com.amarsoft.are.ARE; import com.amarsoft.are.jbo.JBOException; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.log.Log; import com.amarsoft.are.util.StringFunction; import com.amarsoft.awe.util.ASResultSet; import com.amarsoft.awe.util.SqlObject; import com.amarsoft.awe.util.Transaction; import com.amarsoft.dict.als.cache.CacheLoaderFactory; import com.base.util.QuartzUtil; import com.tenwa.leasing.service.voucher.CreateVoucherService; import com.tenwa.leasing.serviceImpl.voucher.CreateVoucherServiceImpl; import com.tenwa.reckon.constant.Scale; import com.tenwa.reckon.util.Conn; import com.tenwa.reckon.util.DateTools; import com.tenwa.reckon.util.DateUtils; public class SplitInts implements Job{ /* * (non-Javadoc) * @see org.quartz.Job#execute(org.quartz.JobExecutionContext) * 月末利息拆分、手续费均摊。 */ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { Transaction Sqlca =null; String startime = StringFunction.getTodayNow(); // 未计提或发生变更的合同重新计提 try { Sqlca = Transaction.createTransaction(CacheLoaderFactory.getDataSource()); String sql="select ci.contract_id,ci.payment_number,cc.period_type,cc.start_date,cc.handling_charge_money, " +" cc.income_interval_month,cc.clean_lease_money,crm.amount from lc_calc_condition_status ci " + " left join lc_calc_condition cc on cc.payment_number=ci.payment_number " + " left join (select count(1) amount,crm.payment_number " + " from CONTRACT_RENT_MONTH crm group by crm.payment_number) crm " + " on crm.payment_number=ci.payment_number where ci.plan_status > 30 and ci.plan_status < 100"; SqlObject asql = new SqlObject(sql); ASResultSet rs = null; rs = Sqlca.getASResultSet(asql); //初始化计提表 truncateContractRentMonth(); while(rs.next()){ String payment_number=rs.getStringValue("payment_number"); String period_type=rs.getStringValue("period_type"); String contract_id=rs.getStringValue("contract_id"); String handling_charge_money=rs.getStringValue("handling_charge_money"); String clean_lease_money=rs.getStringValue("clean_lease_money"); String income_number_year=rs.getStringValue("INCOME_INTERVAL_MONTH"); String start_date=rs.getStringValue("start_date"); if(start_date==null||"".equals(start_date)){ continue; } List dateList=new ArrayList(); List interestlist=new ArrayList(); List planidList=new ArrayList(); List handMoneyList=new ArrayList(); List AllRemainCorpus=new ArrayList(); AllRemainCorpus.add(clean_lease_money); Conn conn =new Conn(Sqlca); String sql1="select * from lc_rent_plan cfrp where cfrp.payment_number=? order by cfrp.plan_list"; List> rentPlans = conn.executeQuery(sql1, payment_number); for(int i=0;i rentplan=rentPlans.get(i); dateList.add(rentplan.get("plan_date")); interestlist.add(rentplan.get("interest")); planidList.add(rentplan.get("id")); if(i> contractRentMonth=null; //if("period_type_0".equals(period_type)){ contractRentMonth=splitInterest0(contract_id,payment_number, start_date, dateList, interestlist,handMoneyList, planidList); /*}else{ contractRentMonth=splitInterest1(contract_id, start_date,income_number_year ,dateList, interestlist,handMoneyList, planidList); }*/ //插入数据并生成凭证 insertContractRentMonth(contractRentMonth,Sqlca); } rs.close(); Sqlca.commit(); QuartzUtil.insertLog(startime,"com.tenwa.lease.app.quartzmession.SplitInts", "success", "成功"); } catch (Exception e) { QuartzUtil.insertLog(startime,"com.tenwa.lease.app.quartzmession.SplitInts", "success", "失败"); logger.error(e.getMessage()); e.printStackTrace(); }finally{ if(Sqlca!=null){ try { Sqlca.disConnect(); } catch (JBOException e) { e.printStackTrace(); } Sqlca = null; } } } private static Log logger=ARE.getLog(); //期末付款利息拆分 public List> splitInterest0(String contract_id,String payment_number,String startDate,List dateList,List interestlist,List handMoneyList,List planidList){ try { List> contractRentMonth=new ArrayList>(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); for(int i=0;i map=new HashMap(); BigDecimal monthInterest=BigDecimal.ZERO; BigDecimal monthhandMoney=BigDecimal.ZERO; int beginday=start.get(Calendar.DAY_OF_MONTH); int monthEndDay=Integer.parseInt(DateUtils.getLastDayOfMonth(start.get(Calendar.YEAR)+"", start.get(Calendar.MONTH)+1+"")); int up=monthEndDay-beginday; if(down != 0){ monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); } map.put("rent_list", i+1+""); map.put("interest", monthInterest+""); map.put("fee", monthhandMoney+""); map.put("payment_number", payment_number); map.put("contract_id", contract_id); map.put("plan_id", planid); map.put("start_date", sdf.format(start.getTime())); start.set(Calendar.DAY_OF_MONTH, monthEndDay); map.put("end_date", sdf.format(start.getTime())); map.put("accrued_date", start.get(Calendar.MONTH)+1+""); map.put("create_date", DateTools.getSystemDate()); contractRentMonth.add(map); start.set(Calendar.DAY_OF_MONTH, 1); start.set(Calendar.MONTH, start.get(Calendar.MONTH)+1); }else{//前部分利息已拆分 start.set(Calendar.DAY_OF_MONTH, 1); start.set(Calendar.MONTH, start.get(Calendar.MONTH)+1); } //每期利息按月拆分 for(int j=0;j map=new HashMap(); BigDecimal monthInterest=BigDecimal.ZERO; BigDecimal monthhandMoney=BigDecimal.ZERO; int endday=end.get(Calendar.DAY_OF_MONTH); int monthEndDay=Integer.parseInt(DateUtils.getLastDayOfMonth(start.get(Calendar.YEAR)+"", start.get(Calendar.MONTH)+1+"")); //当期最后一个月份时,下半段利息为次期一部分 if(j> splitInterest1(String contractId,String startDate,String incomeNumberyear,List dateList,List interestlist,List handMoneyList,List planidList){ try { List> contractRentMonth=new ArrayList>(); int number=Integer.parseInt(incomeNumberyear.substring(7)); SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); for(int i=0;i map=new HashMap(); BigDecimal monthInterest=BigDecimal.ZERO; BigDecimal monthhandMoney=BigDecimal.ZERO; int beginday=start.get(Calendar.DAY_OF_MONTH); int monthEndDay=Integer.parseInt(DateUtils.getLastDayOfMonth(start.get(Calendar.YEAR)+"", start.get(Calendar.MONTH)+1+"")); int up=monthEndDay-beginday; monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); map.put("rent_list", i+1+""); map.put("interest", monthInterest+""); map.put("fee", monthhandMoney+""); map.put("contract_id", contractId); map.put("plan_id", planid); map.put("start_date", sdf.format(start.getTime())); start.set(Calendar.DAY_OF_MONTH, monthEndDay); map.put("end_date", sdf.format(start.getTime())); map.put("accrued_date", start.get(Calendar.MONTH)+1+""); map.put("create_date", DateTools.getSystemDate()); contractRentMonth.add(map); start.set(Calendar.DAY_OF_MONTH, 1); start.set(Calendar.MONTH, start.get(Calendar.MONTH)+1); } //每期利息按月拆分 for(int j=0;j map=new HashMap(); BigDecimal monthInterest=BigDecimal.ZERO; BigDecimal monthhandMoney=BigDecimal.ZERO; int endday=end.get(Calendar.DAY_OF_MONTH); int monthEndDay=Integer.parseInt(DateUtils.getLastDayOfMonth(end.get(Calendar.YEAR)+"", end.get(Calendar.MONTH)+1+"")); if(j>0){ int up=monthEndDay; monthInterest=interest.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP); monthhandMoney=handMoney.multiply(new BigDecimal(up)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP); //如果是最后一期最后一月 if(i==dateList.size()-1){ int up2=endday; monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up2)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up2)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); monthEndDay=endday; } }else{//当期第一个月份时,下半段利息为次期一部分 //如果是最后一期最后一月 if(i==dateList.size()-1){ int up3=endday; monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up3)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up3)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); monthEndDay=endday; }else{ int up1=endday; int up2=monthEndDay-endday; Calendar sd=Calendar.getInstance(); sd.setTime(sdf.parse(startDate)); sd.add(Calendar.MONTH, number); Calendar ed=Calendar.getInstance(); ed.setTime(sdf.parse(endDate)); ed.add(Calendar.MONTH, number); long down2=DateTools.getDateDiff(sdf.format(ed.getTime()), sdf.format(sd.getTime())); BigDecimal interest2=new BigDecimal(interestlist.get(i+1));//次期利息 BigDecimal handMoney2=handMoneyList.get(i+1);//次期手续费 if(down2<=0){ monthInterest=monthInterest.add(interest2); monthhandMoney=monthhandMoney.add(handMoney2); }else{ monthInterest=monthInterest.add(interest2.multiply(new BigDecimal(up2)).divide(new BigDecimal(down2),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); monthhandMoney=monthhandMoney.add(handMoney2.multiply(new BigDecimal(up2)).divide(new BigDecimal(down2),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); } monthInterest=monthInterest.add(interest.multiply(new BigDecimal(up1)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); monthhandMoney=monthhandMoney.add(handMoney.multiply(new BigDecimal(up1)).divide(new BigDecimal(down),Scale.INTEREST_SCALE,BigDecimal.ROUND_HALF_UP)); } } map.put("rent_list", i+1+""); map.put("interest", monthInterest+""); map.put("fee", monthhandMoney+""); map.put("contract_id", contractId); map.put("plan_id", planid); end.set(Calendar.DAY_OF_MONTH, 1); map.put("start_date", sdf.format(end.getTime())); end.set(Calendar.DAY_OF_MONTH, monthEndDay); map.put("end_date", sdf.format(end.getTime())); map.put("accrued_date", start.get(Calendar.MONTH)+1+""); map.put("create_date", DateTools.getSystemDate()); contractRentMonth.add(map); end.set(Calendar.MONTH, end.get(Calendar.MONTH)+1); } } return contractRentMonth; } catch (Exception e) { logger.error("利息拆分出错!!"); e.printStackTrace(); } return null; } /** * 手续费按期拆分 * @param handMoney * @param remainCorpusList * @return */ public List gethandMoneyList(String handMoney,List remainCorpusList){ List handMoneyList=new ArrayList(); BigDecimal hmoney=new BigDecimal(handMoney); BigDecimal money=BigDecimal.ZERO; BigDecimal all=BigDecimal.ZERO; for(int i=0;i gethandMoneyList(String handMoney,List dateList,String startDate,String period_type,String incomeNumberyear){ List handMoneyList=new ArrayList(); try { BigDecimal hmoney=new BigDecimal(handMoney); int number=Integer.parseInt(incomeNumberyear); if("period_type_1".equals(period_type)){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); dateList.remove(0); Calendar enddate=Calendar.getInstance(); enddate.setTime(sdf.parse(dateList.get(dateList.size()-1))); enddate.add(Calendar.MONTH, number); dateList.add(sdf.format(enddate.getTime())); } BigDecimal money=BigDecimal.ZERO; BigDecimal all=BigDecimal.ZERO; all=new BigDecimal(DateTools.getDateDiff(dateList.get(dateList.size()-1), startDate)); for(int i=0;i> contractRentMonth,Transaction tx) throws Exception{ Conn conn =new Conn(tx); for(Map map:contractRentMonth){ String sql1="insert into contract_rent_month( id "; String sql2=" values ( replace(UUID(),'-','') "; if("ORACLE".equals(InitDBType.DBTYPE)){ sql2=" values ( sys_guid() "; } Set keys = map.keySet(); for(String key :keys){ sql1+=","+key; sql2+=",'"+map.get(key)+"'"; } String sql=sql1+") "+sql2+") "; conn.executeUpdate(sql); //若为当月,则生成凭证 SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); Calendar current=Calendar.getInstance(); current.setTime(sdf.parse(DateTools.getSystemDate())); Calendar plan=Calendar.getInstance(); plan.setTime(sdf.parse(map.get("end_date"))); if(plan.get(Calendar.YEAR)==current.get(Calendar.YEAR)&&plan.get(Calendar.MONTH)==current.get(Calendar.MONTH)){ //map.get("interest");为当月利息,fee为当月手续费,contract_id; // TODO 在此生成凭证 Transaction sqlca1 = Transaction.createTransaction(CacheLoaderFactory.getDataSource()); /*String interest = map.get("interest");//利息 String payment_number = map.get("payment_number");//合同id String handling_fee = map.get("fee");//手续费 String account_date = map.get("end_date");//生成日期 CreateVoucherService c = new CreateVoucherServiceImpl();*/ try{ /* c.createVoucher_rentmonth(payment_number, handling_fee, interest,account_date, null, sqlca1); */ }catch(Exception e){ e.printStackTrace(); sqlca1.rollback(); }finally{ sqlca1.disConnect(); } } } } //初始化计提表 public void truncateContractRentMonth() throws Exception{ JBOFactory.createBizObjectQuery(CONTRACT_RENT_MONTH.CLASS_NAME, "delete from O").executeUpdate(); } public static void main(String[] args) { String contractid="11213"; String startDate="2017/4/11"; List dateList=new ArrayList(); List interestlist=new ArrayList(); List planidList=new ArrayList(); List handMoneyList=new ArrayList(); dateList.add("2017/5/11"); dateList.add("2017/6/11"); dateList.add("2017/7/11"); interestlist.add("10000"); interestlist.add("20000"); interestlist.add("30000"); handMoneyList.add(new BigDecimal("2000")); handMoneyList.add(new BigDecimal("3000")); handMoneyList.add(new BigDecimal("4000")); planidList.add("1111111"); planidList.add("1111111"); planidList.add("1111111"); /* System.out.println(new SplitInts().splitInterest0(contractid,startDate,dateList,interestlist,handMoneyList,planidList)); */ } }