package com.product.admin.service;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.common.base.Joiner;
|
import com.product.core.dao.BaseDao;
|
import com.product.core.entity.DataTableEntity;
|
import com.product.core.entity.FieldMetaEntity;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.core.entity.SQLEntity;
|
import com.product.core.exception.BaseException;
|
|
import java.io.IOException;
|
import java.util.*;
|
|
import com.product.core.spring.context.SpringMVCContextHolder;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpStatus;
|
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.util.EntityUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import com.product.core.service.support.AbstractBaseService;
|
import com.product.util.BaseUtil;
|
import com.product.admin.config.CmnConst;
|
import com.product.admin.config.SystemCode;
|
|
/**
|
* Copyright © 2020 LX-BASE
|
*
|
* @Title: LX-BASE-SERVER
|
* @Project: base-server-admin
|
* @Date: 2020年11月20日
|
* @Author: Mr.Xu
|
* @Description:公共汇率
|
*/
|
@Component
|
public class SystemExchangeRateService extends AbstractBaseService {
|
|
@Autowired
|
private BaseDao dao;
|
|
|
/**
|
* 更新所有汇率
|
*
|
* @return
|
* @throws BaseException
|
*/
|
public boolean updateCurrencyRate() throws BaseException {
|
List<String> currencyCodes = getCurrencyCodes();
|
return dao.add(getCurrencyRates(currencyCodes));
|
}
|
|
/**
|
* 获取所有启用货币code
|
*
|
* @throws BaseException
|
*/
|
public List<String> getCurrencyCodes() throws BaseException {
|
SQLEntity sqlEntity = new SQLEntity();
|
sqlEntity.setTableNames(CmnConst.PRODUCT_COMMON_CURRENCY);
|
sqlEntity.setFilter("is_used=1");
|
DataTableEntity dt = dao.listTable(sqlEntity);
|
if (!BaseUtil.dataTableIsEmpty(dt)) {
|
List<String> currencyCodes = new ArrayList<>();
|
for (int i = 0; i < dt.getRows(); i++) {
|
currencyCodes.add(dt.getString(i, "currency_code"));
|
}
|
return currencyCodes;
|
}
|
return Collections.emptyList();
|
}
|
|
/**
|
* 获取指定货币汇率
|
*
|
* @param originalCurrency 原货币
|
* @param foreignCurrencys 兑换后货币 集合多个用逗号分隔
|
* @return
|
* @throws BaseException
|
*/
|
public DataTableEntity getAssignCurrencyRate(String originalCurrency, String foreignCurrencys)
|
throws BaseException {
|
Map<String, Object> map = new HashMap<>();
|
map.put("base", originalCurrency);
|
map.put("symbols", foreignCurrencys);
|
String url = BaseUtil.urlParamsJoin(CmnConst.RATE_API_URL, map);
|
if (BaseUtil.strIsNull(url)) {
|
return null;
|
}
|
JSONObject sendRequest = sendRequest(url, null, 0, 0);
|
String errorKey = "error";
|
String saveRateKey = "getAssignCurrencyRate(String originalCurrency, String foreignCurrencys)";
|
if (sendRequest == null || sendRequest.isEmpty()) {
|
throw new BaseException(SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getValue(),
|
SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getText() , this.getClass(),
|
saveRateKey);
|
}
|
if (!BaseUtil.strIsNull(sendRequest.getString(errorKey))) {
|
throw new BaseException(SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getValue(),
|
SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getText() + "," + sendRequest.getString(errorKey), this.getClass(),
|
saveRateKey);
|
}
|
return paramsJoinToDataTable(sendRequest);
|
}
|
|
/**
|
* 获取汇率
|
*
|
* @param currencyCodes 汇率code 集合
|
* @return
|
* @throws BaseException
|
*/
|
public DataTableEntity getCurrencyRates(List<String> currencyCodes) throws BaseException {
|
|
if (currencyCodes == null) {
|
return null;
|
}
|
DataTableEntity result = null;
|
for (int i = 0; i < currencyCodes.size(); i++) {
|
Map<String, Object> map = new HashMap<>();
|
List<String> currencyCodes2 = new ArrayList<>();
|
currencyCodes2.addAll(currencyCodes);
|
currencyCodes2.remove(currencyCodes2.get(i));
|
map.put("base", currencyCodes.get(i));
|
map.put("symbols", Joiner.on(",").join(currencyCodes2));
|
String url = BaseUtil.urlParamsJoin(CmnConst.RATE_API_URL, map);
|
if (BaseUtil.strIsNull(url)) {
|
continue;
|
}
|
JSONObject sendRequest = sendRequest(url, null, 0, 0);
|
String error = null;
|
if (sendRequest != null) {
|
sendRequest.getString("error");
|
}
|
if (sendRequest == null || !BaseUtil.strIsNull(error)) {
|
throw new BaseException(SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getValue(),
|
SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getText() + "," + error, this.getClass(),
|
"getCurrencyRates(List<String> currencyCodes) throws BaseException");
|
}
|
sendRequest.put("url", url);
|
DataTableEntity saveRate = paramsJoinToDataTable(sendRequest);
|
if (result == null) {
|
result = saveRate;
|
} else {
|
result = BaseUtil.dataTableMerge(result, saveRate);
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 第三方参数拼接成dataTable
|
*
|
* @param json
|
* @return
|
*/
|
|
public DataTableEntity paramsJoinToDataTable(JSONArray array) throws BaseException {
|
if (array == null) { // 第三方接口返回信息为空
|
throw new BaseException(SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getValue(),
|
SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getText(), this.getClass(), "saveRate");
|
}
|
DataTableEntity result = null;
|
for (int i = 0; i < array.size(); i++) {
|
DataTableEntity saveRate = paramsJoinToDataTable(array.getJSONObject(i));
|
if (result == null) {
|
result = saveRate;
|
} else {
|
result = BaseUtil.dataTableMerge(result, saveRate);
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 第三方参数拼接成dataTable
|
*
|
* @param json
|
* @return
|
*/
|
|
public DataTableEntity paramsJoinToDataTable(JSONObject json) throws BaseException {
|
if (json == null || json.getJSONObject("rates") == null) { // 第三方接口返回信息为空
|
throw new BaseException(SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getValue(),
|
SystemCode.SYSTEM_GAIN_CURRENCY_RATE_PARAM_EMPTY_FIAL.getText(), this.getClass(), "saveRate");
|
}
|
DataTableEntity dt = new DataTableEntity();
|
String base = json.getString("base");
|
|
String date = json.getString("date");
|
JSONObject resultJson = json.getJSONObject("rates");
|
for (String currencyCode : resultJson.keySet()) {
|
FieldSetEntity fs = new FieldSetEntity();
|
FieldMetaEntity metaEntity = new FieldMetaEntity();
|
metaEntity.setTableName(new Object[]{CmnConst.PRODUCT_COMMON_FOREIGN_EXCHANGE_RATE});
|
fs.setMeta(metaEntity);
|
fs.setValue("foreign_currency_uuid", currencyCode);
|
fs.setValue("exchanged_utc_datetime", date);
|
fs.setValue("exchange_time_zone", 0);
|
fs.setValue("original_currency_uuid", base);
|
fs.setValue("exchange_rate", resultJson.getString(currencyCode));
|
BaseUtil.createCreatorAndCreationTime(null, fs);
|
fs.setValue(CmnConst.CREATED_BY, "1");
|
dt.addFieldSetEntity(fs);
|
}
|
return dt;
|
}
|
|
/**
|
* 发送http 请求获取数据
|
*
|
* @param url
|
* @param token
|
* @param socketTimeout
|
* @param connectTimeout
|
* @return
|
*/
|
private JSONObject sendRequest(String url, String token, int socketTimeout, int connectTimeout) {
|
if (token == null) {
|
token = "";
|
}
|
HttpGet request = new HttpGet(url);
|
if (socketTimeout <= 0) {
|
socketTimeout = 6000;
|
}
|
if (connectTimeout <= 0) {
|
connectTimeout = 6000;
|
}
|
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
|
.setConnectTimeout(connectTimeout).build();
|
request.setConfig(requestConfig);
|
request.setHeader("Authorization", token);
|
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
CloseableHttpResponse response = client.execute(request);
|
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
HttpEntity entity = response.getEntity();
|
String strResult = EntityUtils.toString(entity, "utf-8");
|
return JSON.parseObject(strResult);
|
}
|
} catch (IOException e) {
|
throw new BaseException(e);
|
} finally {
|
request.releaseConnection();
|
}
|
return null;
|
}
|
|
}
|