diff --git a/src/main/java/au/com/royalpay/payment/manage/dev/core/MerchantLocationService.java b/src/main/java/au/com/royalpay/payment/manage/dev/core/MerchantLocationService.java index 61b31104f..56482064a 100644 --- a/src/main/java/au/com/royalpay/payment/manage/dev/core/MerchantLocationService.java +++ b/src/main/java/au/com/royalpay/payment/manage/dev/core/MerchantLocationService.java @@ -8,6 +8,8 @@ public interface MerchantLocationService { List listMerchantsLocations(PartnerQuery query); + void initClientLocations() throws InterruptedException; + JSONObject getMerchantLocationByMoniker(String clientMoniker); void updateMerchantLocation(JSONObject manager, String clientMoniker, JSONObject geoData); diff --git a/src/main/java/au/com/royalpay/payment/manage/dev/core/impl/MerchantLocationServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/dev/core/impl/MerchantLocationServiceImpl.java index d51cf3041..24ca4aad1 100644 --- a/src/main/java/au/com/royalpay/payment/manage/dev/core/impl/MerchantLocationServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/dev/core/impl/MerchantLocationServiceImpl.java @@ -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()); + private Set failureSet = new ConcurrentSet<>(); + + @Resource + private JdbcTemplate jdbcTemplate; + + @Override + public void initClientLocations() throws InterruptedException { + List> 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 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); diff --git a/src/main/java/au/com/royalpay/payment/manage/dev/web/ClientLocationsController.java b/src/main/java/au/com/royalpay/payment/manage/dev/web/ClientLocationsController.java index 390fbc040..ec2c600b0 100644 --- a/src/main/java/au/com/royalpay/payment/manage/dev/web/ClientLocationsController.java +++ b/src/main/java/au/com/royalpay/payment/manage/dev/web/ClientLocationsController.java @@ -22,4 +22,9 @@ public class ClientLocationsController { public List getClientsLocations(PartnerQuery query) { return merchantLocationService.listMerchantsLocations(query); } + + @RequestMapping(value = "/init") + public void initClientLocations() throws InterruptedException { + merchantLocationService.initClientLocations(); + } }