@ -5,12 +5,29 @@ import au.com.royalpay.payment.manage.dev.core.MerchantLocationService;
import au.com.royalpay.payment.manage.mappers.system.ClientLocationsMapper ;
import au.com.royalpay.payment.manage.merchants.beans.PartnerQuery ;
import au.com.royalpay.payment.manage.merchants.core.ClientManager ;
import cn.yixblog.platform.http.HttpRequestGenerator ;
import cn.yixblog.platform.http.HttpRequestResult ;
import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;
import io.netty.util.internal.ConcurrentSet ;
import org.apache.commons.lang3.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.jdbc.core.JdbcTemplate ;
import org.springframework.stereotype.Service ;
import org.springframework.web.bind.annotation.RequestMethod ;
import org.springframework.web.util.UriComponentsBuilder ;
import javax.annotation.Resource ;
import java.io.IOException ;
import java.net.URISyntaxException ;
import java.util.Date ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import java.util.concurrent.LinkedBlockingQueue ;
import java.util.concurrent.ThreadPoolExecutor ;
import java.util.concurrent.TimeUnit ;
@Service
public class MerchantLocationServiceImpl implements MerchantLocationService {
@ -28,6 +45,76 @@ public class MerchantLocationServiceImpl implements MerchantLocationService {
return clientLocationsMapper . getAllMerchantsLocations ( params ) ;
}
private Logger logger = LoggerFactory . getLogger ( getClass ( ) ) ;
private static final String GOOGLE_MAPS_API = "https://maps.googleapis.com/maps/api/geocode/json" ;
// private static final String GOOGLE_API_KEY = "AIzaSyBbt7XV5PdXqB2aZeVFAZIzOTO0W6gn_YY";
private static final String GOOGLE_API_KEY = "AIzaSyDUu6qXRV-j24rSdbPOMfVdTN1-2OfC2o8" ;
private ThreadPoolExecutor pool = new ThreadPoolExecutor ( 5 , 100 , 5 , TimeUnit . SECONDS , new LinkedBlockingQueue < Runnable > ( ) ) ;
private Set < Integer > failureSet = new ConcurrentSet < > ( ) ;
@Resource
private JdbcTemplate jdbcTemplate ;
@Override
public void initClientLocations ( ) throws InterruptedException {
List < Map < String , Object > > clients = jdbcTemplate . queryForList ( "select client_id, CONCAT(IFNULL(address,''),' ',IFNULL(suburb,''),' ',IFNULL(state,''),' ',IFNULL(country,'')) as 'address' from royalpay_production.sys_clients where client_id not in (select client_id from royalpay_production.sys_clients_locations) AND client_moniker in ('CUEC','SEA8','SEA0','SEA2','SEA7','HUNT','HLSY','RIMO','TONY','SUCS','SUEM','SUSO','NEW2','NEW1','NEW3','SABO','TSRE','AQSY','JUSC','YMKP','ETAS','EASW','MEET','TDBR','DOUB','NOON','H2GR','LKST','ASON','JSMS','HIDA','DAFR','XANY','VEJE','LUTI','CNHJ','EHPL','YIFA','MRMA','NGMT','BJKC','FORU','CHED','GSAP','ASQI','OMPL','XAMC','HMJC','MGPL','6PHD','MTCS','AIND','KOTN','FDWK','NHKP','GFIT','GHCA','OGPL','RTCB','HOTT','MCRE','YBDW','WPX1','SFPT','WPXM','HOFT','AUID','SUBX','GBTK','PERA','TDCS','DUMP','QJF2','LBWD','HUGG','YINI','CALA','XHJJ','BLUS','SSSS','HEND')" ) ;
final int totalLen = clients . size ( ) ;
logger . info ( "total length:" + totalLen ) ;
for ( int i = 0 ; i < totalLen ; i + + ) {
Map < String , Object > client = clients . get ( i ) ;
final JSONObject clientObj = new JSONObject ( client ) ;
final int idx = i ;
pool . execute ( new Runnable ( ) {
@Override
public void run ( ) {
try {
processClient ( clientObj , idx , totalLen ) ;
} catch ( URISyntaxException e ) {
failureSet . add ( clientObj . getIntValue ( "client_id" ) ) ;
}
}
} ) ;
Thread . sleep ( 120 ) ;
}
pool . shutdown ( ) ;
pool . awaitTermination ( 5 , TimeUnit . HOURS ) ;
System . err . println ( "Failed clients:" + StringUtils . join ( failureSet , "," ) ) ;
}
private void processClient ( JSONObject client , int idx , int totalLen ) throws URISyntaxException {
String url = UriComponentsBuilder . fromHttpUrl ( GOOGLE_MAPS_API )
. queryParam ( "key" , GOOGLE_API_KEY )
. queryParam ( "address" , client . get ( "address" ) ) . toUriString ( ) ;
HttpRequestGenerator gen = new HttpRequestGenerator ( url , RequestMethod . GET ) ;
int clientId = client . getIntValue ( "client_id" ) ;
logger . info ( "start processing index " + idx + "/" + totalLen + ",client_id=" + clientId ) ;
HttpRequestResult result = gen . execute ( ) ;
if ( result . isSuccess ( ) ) {
try {
JSONObject address = result . getResponseContentJSONObj ( ) ;
System . err . println ( address ) ;
JSONArray array1 = address . getJSONArray ( "results" ) ;
JSONObject aarray2 = ( JSONObject ) array1 . get ( 0 ) ;
JSONObject location = aarray2 . getJSONObject ( "geometry" ) . getJSONObject ( "location" ) ;
String longitude = location . getString ( "lng" ) ;
String latitude = location . getString ( "lat" ) ;
String sql = "INSERT royalpay_production.sys_clients_locations values(replace(uuid(),'-','')," + clientId + ",'" + client . getString ( "address" ) + "','" + latitude + "','" + longitude + "','System Init',now())" ;
jdbcTemplate . update ( sql ) ;
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
failureSet . add ( clientId ) ;
}
} else {
System . err . print ( result . getStatusCode ( ) ) ;
failureSet . add ( clientId ) ;
}
logger . info ( "finished index " + idx + "/" + totalLen + ",client_id=" + clientId ) ;
}
@Override
public JSONObject getMerchantLocationByMoniker ( String clientMoniker ) {
return clientLocationsMapper . findTheLocationByMerchantCode ( clientMoniker ) ;