По сути стандартный калькулятор, заполняет BalanceULDetail наработкой модуля NPAY.
Код:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ru.ttk.billing.npay;
import bitel.billing.common.TimeUtils;
import bitel.billing.server.npay.bean.ServiceObject;
import java.lang.ref.SoftReference;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import ru.bitel.bgbilling.common.BGException;
import ru.bitel.bgbilling.kernel.tariff.tree.server.TariffModuleTree;
import ru.bitel.bgbilling.kernel.tariff.tree.server.TariffModuleTreeSet;
import ru.bitel.bgbilling.kernel.tariff.tree.server.TariffModuleTreeSetDao;
import ru.bitel.bgbilling.modules.npay.tariff.server.CalculateModeTariffTreeNode;
import ru.bitel.bgbilling.modules.npay.tariff.server.NPayTariffContext;
import ru.bitel.bgbilling.modules.npay.tariff.server.NPayTariffRequest;
import ru.bitel.bgbilling.server.util.Setup;
import ru.bitel.common.Preferences;
import ru.bitel.common.Utils;
import ru.bitel.common.model.Period;
import ru.bitel.common.sql.ConnectionSet;
import ru.ttk.billing.TTKConst;
import ru.ttk.billing.balance.BalanceULDetail;
/**
*
* @author lda
*/
public class CalculatorUL{
private Calendar startMonth;
private Calendar endMonth;
private Calendar startCalc;
private Calendar endCalc;
private int daysInMonth;
private Calendar StartActiveStatus = null;
private Setup setup;
private ConnectionSet connectionSet;
private Connection con;
private Connection conSlave;
private String query_get_abn;
private int mid = TTKConst.MID_NPAY;
private String cids = "";
private NPayTariffContext context;
private static final Logger log = Logger.getLogger( CalculatorUL.class );
private HashMap<Integer,BalanceULDetail> HashMapBalance = new HashMap<Integer, BalanceULDetail>();
public CalculatorUL(Setup setup, ConnectionSet connectionSet) {
this.setup = setup;
this.connectionSet = connectionSet;
this.con = connectionSet.getConnection();
this.conSlave = connectionSet.getSlaveConnection();
}
public void setStartActiveStatus(Calendar StartActiveStatus) {
this.StartActiveStatus = StartActiveStatus;
}
public BalanceULDetail FillAmounts (int cid, Calendar date1, Calendar date2){
cids = String.valueOf(cid);
startCalc = TimeUtils.getStartMonth( TimeUtils.clear_HOUR_MIN_MIL_SEC(date1) );
endCalc = TimeUtils.getEndMonth( TimeUtils.clear_HOUR_MIN_MIL_SEC(date2) );
query_get_abn = "SELECT cid, sid, date1, date2, col, emid, eid " +
"FROM npay_service_object_" + mid + " " +
"WHERE (date1 IS NULL OR date1<=?) AND (date2 IS NULL OR date2>=?) ";
query_get_abn += " AND cid IN (" + cid + ")";
query_get_abn += " ORDER BY cid, sid";
startMonth = (Calendar)startCalc.clone();
while(TimeUtils.dateBeforeOrEq(startMonth, endCalc)){
endMonth = TimeUtils.getEndMonth((Calendar)startMonth.clone());
daysInMonth = startMonth.getActualMaximum( Calendar.DAY_OF_MONTH );
getAmounts();
startMonth.set(Calendar.DAY_OF_MONTH, 1);
startMonth.add(Calendar.MONTH, 1);
}
return HashMapBalance.get(cid);
}
public HashMap<Integer, BalanceULDetail> FillAmountsForDailyCheckTask(String cids, Calendar date1, Calendar date2){
this.cids = cids;
startCalc = TimeUtils.getStartMonth( TimeUtils.clear_HOUR_MIN_MIL_SEC(date1) );
endCalc = TimeUtils.getEndMonth( TimeUtils.clear_HOUR_MIN_MIL_SEC(date2) );
query_get_abn = "SELECT npay_service.cid, npay_service.sid, npay_service.date1, npay_service.date2, npay_service.col, npay_service.emid, npay_service.eid " +
"FROM npay_service_object_" + mid + " AS npay_service" +
" INNER JOIN contract_script " +
" ON (npay_service.cid=contract_script.cid AND contract_script.script_id='1') " +
" INNER JOIN contract "+
" ON (npay_service.cid=contract.id AND contract.status='0') "+
"WHERE (npay_service.date1 IS NULL OR npay_service.date1<=?) AND (npay_service.date2 IS NULL OR npay_service.date2>=?) ";
if( Utils.notBlankString( cids ) ){
query_get_abn += " AND npay_service.cid IN (" + cids + ")";
}
query_get_abn += " ORDER BY npay_service.cid, npay_service.sid";
startMonth = (Calendar)startCalc.clone();
while(TimeUtils.dateBeforeOrEq(startMonth, endCalc)){
endMonth = TimeUtils.getEndMonth((Calendar)startMonth.clone());
daysInMonth = startMonth.getActualMaximum( Calendar.DAY_OF_MONTH );
getAmounts();
startMonth.set(Calendar.DAY_OF_MONTH, 1);
startMonth.add(Calendar.MONTH, 1);
}
return HashMapBalance;
}
private void getAmounts(){
try {
TariffModuleTreeSetDao tariffModuleTreeSetDao = new TariffModuleTreeSetDao( con );
Preferences moduleSetup = setup.getModuleSetup( mid );
context = new NPayTariffContext( moduleSetup, con, mid, startMonth, endMonth, endMonth, cids, null );
if (StartActiveStatus != null){
context.setPrecalcOpenPeriod( new Period( TimeUtils.convertCalendarToDate( StartActiveStatus ), null ) );
}
PreparedStatement ps = conSlave.prepareStatement( query_get_abn );
ps.setDate( 1, TimeUtils.convertCalendarToSqlDate( endMonth ) );
ps.setDate( 2, TimeUtils.convertCalendarToSqlDate( startMonth ) );
ResultSet rs = ps.executeQuery();
while( rs.next() ){
ServiceObject serviceObject = new ServiceObject();
serviceObject.setContractId( rs.getInt( 1 ) );
serviceObject.setServiceId( rs.getInt( 2 ) );
serviceObject.setDate1( rs.getDate( 3 ) );
serviceObject.setDate2( rs.getDate( 4 ) );
serviceObject.setCol( rs.getInt( 5 ) );
serviceObject.setEntityMid( rs.getInt( 6 ) );
serviceObject.setEntityId( rs.getInt( 7 ) );
processServiceObject(serviceObject, tariffModuleTreeSetDao );
}
ps.close();
tariffModuleTreeSetDao.freeResources();
}catch( Exception e ){
e.printStackTrace();
}
}
private void processServiceObject(ServiceObject serviceObject, TariffModuleTreeSetDao tariffModuleTreeSetDao ){
final int cid = serviceObject.getContractId();
BalanceULDetail bd = HashMapBalance.get(cid);
if (bd == null){
bd = new BalanceULDetail(cid, startCalc, endCalc);
HashMapBalance.put(cid, bd);
}
final int sid = serviceObject.getServiceId();
try{
final Date serviceStartDate = serviceObject.getDate1();
final Date serviceEndDate = serviceObject.getDate2();
final int emid = serviceObject.getEntityMid();
final int eid = serviceObject.getEntityId();
final Calendar serviceStart = TimeUtils.convertDateToCalendar( serviceObject.getDate1() );
final Calendar serviceEnd = TimeUtils.convertDateToCalendar( serviceObject.getDate2() );
int col = serviceObject.getCol();
final Calendar serviceDate1 = TimeUtils.convertDateToCalendar( trimByMonthStart( serviceStartDate ) );
final Calendar serviceDate2 = TimeUtils.convertDateToCalendar( trimByMonthEnd( serviceEndDate ) );
TariffModuleTreeSet tts = getTreeSet( cid, emid, eid, tariffModuleTreeSetDao);
for( TariffModuleTreeSet.Entry tariffPeriod : tts.entries() ){
Calendar tariffFrom = tariffPeriod.getCalendarFrom();
Calendar tariffTo = tariffPeriod.getCalendarTo();
// log.info("tariffFrom="+TimeUtils.convertCalendarToDateString(tariffFrom));
// log.info("tariffTo="+TimeUtils.convertCalendarToDateString(tariffTo));
Calendar date1 = (Calendar)serviceDate1.clone();
Calendar date2 = (Calendar)serviceDate2.clone();
// если тариф начинает действовать после окончания периода услуги
if( tariffFrom != null && TimeUtils.dateBefore( date2, tariffFrom ) ){
continue;
}
// тариф начал работать позже начала периода услуги
if( tariffFrom != null && TimeUtils.dateBefore( date1, tariffFrom ) ){
date1 = tariffFrom;
}
// тариф кончил работать раньше окончания периода услаги
if( tariffTo != null && TimeUtils.dateBefore( tariffTo, date2 ) ){
if( TimeUtils.dateBeforeOrEq( date1, tariffTo ) ){
date2 = tariffTo;
}else{
continue;
}
}
NPayTariffRequest req = new NPayTariffRequest( mid, cid, sid );
req.setInfoEnable( true );
req.setConnectionSet( connectionSet );
req.setAccountingMonthDays( daysInMonth );
req.setTime( date1 );
req.setServiceCost( sid, Collections.singletonList( new NPayTariffRequest.NPayServiceCost( sid, serviceStart, serviceEnd, date1, date2, col ) ) );
final TariffModuleTree tree = tariffPeriod.getTree();
tree.execute( req, context );
if( req.isAccepted() ){
for( NPayTariffRequest.NPayServiceCost serviceCost : req.getServiceCostList() ){
if (req.getCalculateModeLog() == CalculateModeTariffTreeNode.MODE_DAY){
for (Map.Entry<Integer, BigDecimal> detail : req.getDayDetailLog().entrySet()) {
Calendar day = (Calendar)serviceCost.date1.clone();
day.set(Calendar.DAY_OF_MONTH, detail.getKey());
HashMapBalance.get(cid).addAmountNpay(day, sid, detail.getValue().doubleValue());
}
}else if (req.getCalculateModeLog() == CalculateModeTariffTreeNode.MODE_MONTH){
HashMapBalance.get(cid).addAmountNpay(startMonth, sid, serviceCost.cost.doubleValue());
}
}
}
}
}catch( Exception e ){
e.printStackTrace();
}
}
private Date trimByMonthEnd( Date date){
if( date == null || TimeUtils.dateBefore( endMonth.getTime(), date ) ){
date = ((Calendar)endMonth.clone()).getTime();
}
return date;
}
private Date trimByMonthStart( Date date1){
if( date1 == null || TimeUtils.dateBefore( date1, startMonth.getTime() ) ){
date1 = ((Calendar)startMonth.clone()).getTime();
}
return date1;
}
private Map<Integer, SoftReference<Map<Integer, Map<Integer, TariffModuleTreeSet>>>> treeSetMap = new HashMap<Integer, SoftReference<Map<Integer, Map<Integer, TariffModuleTreeSet>>>>( 1024 );
private TariffModuleTreeSet getTreeSet( int cid, int emid, int eid, TariffModuleTreeSetDao tariffModuleTreeSetDao)
throws BGException{
SoftReference<Map<Integer, Map<Integer, TariffModuleTreeSet>>> emidMapRef = treeSetMap.get( cid );
Map<Integer, Map<Integer, TariffModuleTreeSet>> emidMap = emidMapRef != null ? emidMapRef.get() : null;
if( emidMap == null ){
emidMap = new HashMap<Integer, Map<Integer, TariffModuleTreeSet>>( 8 );
treeSetMap.put( cid, new SoftReference<Map<Integer, Map<Integer, TariffModuleTreeSet>>>( emidMap ) );
}
Map<Integer, TariffModuleTreeSet> eidMap = emidMap.get( emid );
if( eidMap == null ){
emidMap.put( emid, eidMap = new HashMap<Integer, TariffModuleTreeSet>( 8 ) );
}
TariffModuleTreeSet result = eidMap.get( eid );
if( result == null ){
result = tariffModuleTreeSetDao.getRealtimeTariffTreeSet( cid, startMonth.getTime(), "npay", mid, emid, eid );
}
return result;
}
}