@ -2,12 +2,10 @@ package au.com.royalpay.payment.manage.system.core.impl;
import au.com.royalpay.payment.manage.mappers.system.ClientAccountMapper ;
import au.com.royalpay.payment.manage.mappers.system.ClientFilesMapper ;
import au.com.royalpay.payment.manage.mappers.system.ClientRateMapper ;
import au.com.royalpay.payment.manage.mappers.system.ClientsContractMapper ;
import au.com.royalpay.payment.manage.merchants.core.ClientManager ;
import au.com.royalpay.payment.manage.system.core.ClientContractService ;
import au.com.royalpay.payment.tools.env.SysConfigManager ;
import au.com.royalpay.payment.tools.exceptions.BadRequestException ;
import au.com.royalpay.payment.tools.exceptions.NotFoundException ;
import au.com.royalpay.payment.tools.exceptions.ServerErrorException ;
@ -21,9 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import javax.annotation.Resource ;
@ -38,8 +34,6 @@ public class ClientContractServiceImpl implements ClientContractService {
@Resource
private ClientsContractMapper clientsContractMapper ;
@Resource
private ClientRateMapper clientRateMapper ;
@Resource
private SysConfigManager sysConfigManager ;
@Resource
private ClientAccountMapper clientAccountMapper ;
@ -51,11 +45,6 @@ public class ClientContractServiceImpl implements ClientContractService {
if ( client = = null ) {
throw new NotFoundException ( "merchant not found" ) ;
}
JSONObject expireInfo = getClientContractExpire ( client_id ) ;
JSONObject contract = clientsContractMapper . findByClientId ( client_id ) ;
Date now = new Date ( ) ;
if ( ( expireInfo . getBoolean ( "rate_waring" ) | | expireInfo . getBoolean ( "old_contract" ) )
& & ( contract = = null | | now . compareTo ( contract . getDate ( "expiry_date" ) ) > 0 ) ) {
try {
clientManager . getNewAggregateAgreeFile ( client . getString ( "client_moniker" ) , null , true ) ;
} catch ( Exception e ) {
@ -63,28 +52,15 @@ public class ClientContractServiceImpl implements ClientContractService {
throw new ServerErrorException ( "System error" ) ;
}
List < JSONObject > files = clientFilesMapper . findFileByClientAndType ( client_id , "source_agree_file" ) ;
saveContract ( client_id , now , channel ) ;
return files . get ( 0 ) ;
} else {
List < JSONObject > files = clientFilesMapper . findFileByClientAndType ( client_id , "source_agree_file" ) ;
if ( ! CollectionUtils . isEmpty ( files ) ) {
JSONObject file = files . get ( 0 ) ;
if ( expireInfo . getBoolean ( "rate_expire" ) ) {
contract . put ( "create_time" , now ) ;
contract . put ( "expiry_date" , DateUtils . addYears ( now , 1 ) ) ;
clientsContractMapper . update ( contract ) ;
}
return file ;
}
return null ;
}
}
private void saveContract ( int client_id , Date now , String channel ) {
@Override
public void saveContract ( int client_id , Date expire_date , String channel ) {
JSONObject newContract = new JSONObject ( ) ;
newContract . put ( "create_time" , now ) ;
newContract . put ( "expiry_date" , DateUtils . addYears ( now , 1 ) ) ;
newContract . put ( "create_time" , new Date ( ) ) ;
newContract . put ( "expiry_date" , DateUtils . addYears ( expire_date , 1 ) ) ;
newContract . put ( "sign_channel" , channel ) ;
newContract . put ( "client_id" , client_id ) ;
newContract . put ( "has_sign" , false ) ;
@ -98,40 +74,20 @@ public class ClientContractServiceImpl implements ClientContractService {
if ( client = = null ) {
throw new NotFoundException ( "merchant not found" ) ;
}
// JSONObject rateExpire = getClientContractExpire(client_id);
// boolean expire = rateExpire.getBoolean("rate_expire");
// boolean waring = rateExpire.getBoolean("rate_waring");
// boolean old_contract = rateExpire.getBoolean("old_contract");
JSONObject contract = clientsContractMapper . findByClientId ( client_id ) ;
if ( contract = = null ) {
throw new BadRequestException ( "generate contract first" ) ;
}
if ( contract . getBoolean ( "has_sign" ) ) {
if ( contract ! = null & & contract . getBoolean ( "has_sign" ) ) {
return ;
}
Date now = new Date ( ) ;
JSONObject account = clientAccountMapper . findById ( account_id ) ;
contract . put ( "has_sign" , 1 ) ;
contract . put ( "sign_account_id" , account_id ) ;
contract . put ( "sign_channel" , channel ) ;
contract . put ( "confirm_time" , now ) ;
contract . put ( "signatory" , account . getString ( "display_name" ) ) ;
clientsContractMapper . update ( contract ) ;
List < JSONObject > rateInfo = clientRateMapper . minExpiryTime ( client_id , null ) ;
rateInfo . forEach ( ( p ) - > {
p . remove ( "client_rate_id" ) ;
p . put ( "create_time" , now ) ;
p . put ( "active_time" , now ) ;
p . put ( "update_time" , now ) ;
p . put ( "manager_id" , "0" ) ;
p . put ( "expiry_time" , DateUtils . addYears ( now , 1 ) ) ;
p . put ( "manager_name" , "merchant_" + account_id + "_" + account . getString ( "display_name" ) ) ;
clientRateMapper . saveRate ( p ) ;
} ) ;
getOrGenerateSourceAgreement ( client_id , channel ) ;
clientManager . setClientTaxInSurcharge ( client . getString ( "client_moniker" ) , false ) ;
clientManager . setSkipContract ( client . getString ( "client_moniker" ) , true ) ;
}
@Override
@ -150,75 +106,25 @@ public class ClientContractServiceImpl implements ClientContractService {
throw new NotFoundException ( "merchant not found please check ID" ) ;
}
JSONObject sysconfig = sysConfigManager . getSysConfig ( ) ;
JSONObject defaultResult = new JSONObject ( ) ;
defaultResult . put ( "rate_expire" , false ) ;
defaultResult . put ( "rate_waring" , false ) ;
defaultResult . put ( "old_contract" , false ) ;
if ( sysconfig ! = null & & ! sysconfig . getBoolean ( "sys_new_contract_on" ) ) {
return defaultResult ;
}
if ( client . getBooleanValue ( "new_contract" ) ) {
return defaultResult ;
}
List < JSONObject > rateInfo = clientRateMapper . minExpiryTime ( client_id , null ) ;
JSONObject result = new JSONObject ( ) ;
if ( CollectionUtils . isEmpty ( rateInfo ) ) {
result . put ( "rate_expire" , true ) ;
result . put ( "rate_waring" , true ) ;
return result ;
}
result . put ( "rate_expire" , false ) ;
result . put ( "rate_waring" , false ) ;
result . put ( "old_contract" , false ) ;
Date now = new Date ( ) ;
JSONObject contract = clientsContractMapper . findByClientId ( client_id ) ;
if ( contract ! = null & & contract . getDate ( "expiry_date" ) . compareTo ( now ) > 0 & & contract . getBoolean ( "has_sign" ) ) {
result . put ( "alert" , false ) ;
if ( sysconfig ! = null & & ! sysconfig . getBoolean ( "sys_new_contract_on" ) ) {
return result ;
}
int waringDays = sysconfig . getIntValue ( "rate_warning_days" ) ;
Map < String , JSONObject > filterMap = new HashMap < > ( ) ;
rateInfo . forEach ( ( p ) - > {
String key = p . getString ( "rate_name" ) ;
JSONObject tmp = filterMap . get ( key ) ;
if ( tmp = = null ) {
filterMap . put ( key , p ) ;
} else {
if ( tmp . getDate ( "expiry_time" ) . compareTo ( p . getDate ( "expiry_time" ) ) < 0 ) {
filterMap . put ( key , p ) ;
}
}
} ) ;
for ( JSONObject p : filterMap . values ( ) ) {
if ( now . compareTo ( DateUtils . addDays ( p . getDate ( "expiry_time" ) , 1 ) ) > - 1 ) {
result . put ( "rate_expire" , true ) ;
}
if ( DateUtils . addDays ( now , waringDays ) . compareTo ( DateUtils . addDays ( p . getDate ( "expiry_time" ) , 1 ) ) > - 1 ) {
int expireDay = 0 ;
long expireSS = ( DateUtils . addDays ( p . getDate ( "expiry_time" ) , 1 ) . getTime ( ) - now . getTime ( ) ) ;
expireDay = ( int ) expireSS / ( 1000 * 60 * 60 * 24 ) ;
if ( expireSS % ( 1000 * 60 * 60 * 24 ) > 1 ) {
+ + expireDay ;
}
result . put ( "expire_days" , expireDay ) ;
result . put ( "rate_waring" , true ) ;
}
}
if ( ! result . getBoolean ( "rate_waring" ) & & ! result . getBoolean ( "rate_expire" ) ) {
List < JSONObject > files = clientFilesMapper . findFileByClientAndType ( client_id , "source_agree_file" ) ;
if ( CollectionUtils . isEmpty ( files ) ) {
result . put ( "old_contract" , true ) ;
result . put ( "rate_waring" , true ) ;
} else {
if ( ! CollectionUtils . isEmpty ( files ) ) {
JSONObject file = files . get ( 0 ) ;
if ( sysconfig . getDate ( "sys_new_contract_time" ) . compareTo ( file . getDate ( "last_update_date" ) ) > 0
| | ( contract ! = null & & ! contract . getBoolean ( "has_sign" ) ) ) {
result . put ( "old_contract" , true ) ;
result. put ( "rate_waring" , true ) ;
if ( sysconfig . getDate ( "sys_new_contract_time" ) . compareTo ( file . getDate ( "last_update_date" ) ) > 0 ) {
JSONObject contract = clientsContractMapper . findByClientId ( client_id ) ;
if ( contract = = null ) {
saveContract ( client_id , new Date ( ) , null ) ;
}
}
}
if ( client . getBooleanValue ( "tax_in_surcharge" ) ) {
result . put ( "alert" , true ) ;
return result ;
}
return result ;
}