package cloud.app.tms.transit.api;

import cloud.bos.dataproxy.DataUtil;
import cloud.bos.dataproxy.entity.UserEntity;
import cloud.bos.datasource.DataHelper;
import cloud.bos.datasource.search.Searcher;
import cloud.bos.entity.AData;
import cloud.bos.entity.AList;
import cn.hutool.core.date.DateTime;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;

/**
* 爬取中国银行汇率
*
* @author zhangbs
*/
@Service
public class RateApi {
public static void main(String[] args) {

List<TerstEntity> queryList = getExchangeRate2("EUR", "");
if(queryList.size()>0) {
TerstEntity terstEntity1 = queryList.get(0);
String middleRate = terstEntity1.getMiddleRate();
queryList.get(0).getMiddleRate();
System.out.println(queryList.get(0).getMiddleRate());
}
/*for (int i = 0; i < usd.size(); i++) {
usd.get(i).getMiddleRate();
System.out.println(usd.get(i).getMiddleRate());

}*/
System.out.println("汇总:" + queryList);
}
public static Double execute(UserEntity user ,String sourceCurency) {
AList currency = DataHelper.List(DataUtil.LoadTable("currency", user), new Searcher());
Double aDouble=0d;
for(AData list :currency.getList()) {
List<TerstEntity> queryList = getExchangeRate2(list.getKeyName(), "");
String a = null;
if (queryList.size() > 0) {
TerstEntity terstEntity1 = queryList.get(0);
String middleRate = terstEntity1.getMiddleRate();
queryList.get(0).getMiddleRate();
a = queryList.get(0).getMiddleRate();
}

System.out.println("长度:" + queryList.size());
System.out.println("汇总:" + queryList);
aDouble = Double.valueOf(a.toString());
AData aData = new AData();
if(aDouble!=0d){
aData.put("exchange_rate",aDouble/100);
}
aData.put("id",list.getInteger("id"));
DataHelper.Update(DataUtil.LoadTable("currency",user),aData);
}
return aDouble;
}


/**
* 获取当日传入币别汇率信息
*
* @param sourceCurrency 币别
* @param date 日期
* @return
*/
private static List getExchangeRate(String sourceCurrency, String date) {

/***判断入参lsDate是否为空,若为空则赋值为当前时间**/
String lsToday = StringUtils.isEmpty(date) ? new DateTime().toString("yyyy-MM-dd") : date;
List list = new ArrayList();
for (int page = 1; page <= 10; page++) {
/**抓取时间为lsToday,币别为sourceCurrency,页数为page的中国银行网页信息*/
String searchEnHtml = getSearchEnHtml(lsToday, sourceCurrency, String.valueOf(page));

/**开始解析html中的汇率列表信息**/
Map map = assembleObjByHtml(searchEnHtml, sourceCurrency, lsToday);
String flag = (String) map.get("flag");
String htmlPage = (String) map.get("page");
list.add (map.get("list"));

/**当flag为1执行成功时,或总页数等于循环查询到的页数时,则不需要再次进行查询**/
if ("1".equals(flag) || Integer.parseInt(htmlPage) < page) {
break;
}
}
return list;
}
/**
* 获取当日传入币别汇率信息
*
* @param sourceCurrency 币别
* @param date 日期
* @return
*/
private static List<TerstEntity> getExchangeRate2(String sourceCurrency, String date) {

/***判断入参lsDate是否为空,若为空则赋值为当前时间**/
String lsToday = StringUtils.isEmpty(date) ? new DateTime().toString("yyyy-MM-dd") : date;
List<TerstEntity> terstEntities = new ArrayList();
for (int page = 1; page <= 1; page++) {
/**抓取时间为lsToday,币别为sourceCurrency,页数为page的中国银行网页信息*/
String searchEnHtml = getSearchEnHtml(lsToday, sourceCurrency, String.valueOf(page));

/**开始解析html中的汇率列表信息**/
terstEntities = assembleTerByHtml(searchEnHtml, sourceCurrency, lsToday);


/**当flag为1执行成功时,或总页数等于循环查询到的页数时,则不需要再次进行查询**/

}
return terstEntities;
}


/**
* 获取整个网页的内容
*
* @param lsToday 传入当前时间或空
* @param lsSourceCurrency 币种
* @param liPage 当前查询页数
* @return
*/
private static String getSearchEnHtml(String lsToday, String lsSourceCurrency, String liPage) {

StringBuilder url = new StringBuilder("https://srh.bankofchina.com/search/whpj/searchen.jsp?");
url.append("erectDate=").append(lsToday);
url.append("&nothing=").append(lsToday);
url.append("&pjname=").append(lsSourceCurrency);
url.append("&page=").append(liPage);
System.out.println("拼接好的url:" + url);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpPost httpPost = new HttpPost(url.toString());
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
httpPost.setHeader("Accept", "Accept: text/plain, */*");
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3724.8 Safari/537.36");
httpPost.addHeader("x-amazon-user-agent", "AmazonJavascriptScratchpad/1.0 (Language=Javascript)");
httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
String html = "";
try {
response = httpClient.execute(httpPost);

/**判断响应状态为200,进行处理**/
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();
html = EntityUtils.toString(httpEntity, "utf-8");
} else {
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
HttpClientUtils.closeQuietly(response);
HttpClientUtils.closeQuietly(httpClient);
}

/***返回请求得到的页面**/
return html;
}


/**
* 根据取得的网页,解析html中的内容 先不做业务逻辑,全部查询
*
* @param html 要解析的html
* @param lsSourceCurrency 币种
* @param lsToday 日期
* @return
*/
private static Map assembleObjByHtml(String html, String lsSourceCurrency, String lsToday) {
/**存储数据**/
Map map = new HashMap(5);

/**使用Jsoup将html解析为Document对象**/
Document document = Jsoup.parse(html);

/**获取页面隐藏域中存放的当前页数**/
Elements pageItem = document.getElementsByAttributeValue("name", "page");
String pageItemValue = "";
pageItemValue = pageItem.select("input[name=page]").val();
map.put("page", pageItemValue);

/**获取页面的整个table信息,这个返回的页面基本上是返回多个table,下方需要细化处理**/
Elements tables = document.getElementsByTag("table");

/**设置存放汇率信息的table下标为-1(默认不存在)**/
int tableIndex = -1;

/**从table中循环获取,查找含有Currency Name字段的table**/
for (int i = 0; i < tables.size(); i++) {
Element element = tables.get(i);
String text = element.text();
/**找到含有汇率信息的table,给tableIndex赋值,跳出循环**/
if (text.indexOf("Currency Name") > -1) {
tableIndex = i;
break;
}
}
List<TerstEntity> list = new ArrayList();
/**如果找到汇率列表信息**/
if (tableIndex > -1) {
Element table = tables.get(tableIndex);
/**遍历该表格内的所有的<tr> <tr/>*/
Elements trs = table.select("tr");
for (int i = 1; i < trs.size(); ++i) {
TerstEntity terstEntity = new TerstEntity();
Element tr = trs.get(i);
/**将数据放入实体对象中*/
Elements tds = tr.select("td");
terstEntity.setCurrencyName(tds.get(0).text());
terstEntity.setBuyingRate(tds.get(1).text());
terstEntity.setCashBuyingRate(tds.get(2).text());
terstEntity.setSellingRate(tds.get(3).text());
terstEntity.setCashSellingRate(tds.get(4).text());
terstEntity.setMiddleRate(tds.get(5).text());
terstEntity.setPubTime(tds.get(6).text());
list.add(terstEntity);
}
map.put("list", list);
}else{
map.put("flag", "1");
}
return map;
}
/**
* 根据取得的网页,解析html中的内容 先不做业务逻辑,全部查询
*
* @param html 要解析的html
* @param lsSourceCurrency 币种
* @param lsToday 日期
* @return
*/
private static List<TerstEntity> assembleTerByHtml(String html, String lsSourceCurrency, String lsToday) {
/**存储数据**/
Map map = new HashMap(5);

/**使用Jsoup将html解析为Document对象**/
Document document = Jsoup.parse(html);

/**获取页面隐藏域中存放的当前页数**/
Elements pageItem = document.getElementsByAttributeValue("name", "page");
String pageItemValue = "";
pageItemValue = pageItem.select("input[name=page]").val();
map.put("page", pageItemValue);

/**获取页面的整个table信息,这个返回的页面基本上是返回多个table,下方需要细化处理**/
Elements tables = document.getElementsByTag("table");

/**设置存放汇率信息的table下标为-1(默认不存在)**/
int tableIndex = -1;

/**从table中循环获取,查找含有Currency Name字段的table**/
for (int i = 0; i < tables.size(); i++) {
Element element = tables.get(i);
String text = element.text();
/**找到含有汇率信息的table,给tableIndex赋值,跳出循环**/
if (text.indexOf("Currency Name") > -1) {
tableIndex = i;
break;
}
}
List<TerstEntity> list = new ArrayList();
/**如果找到汇率列表信息**/
if (tableIndex > -1) {
Element table = tables.get(tableIndex);
/**遍历该表格内的所有的<tr> <tr/>*/
Elements trs = table.select("tr");
for (int i = 1; i < trs.size(); ++i) {
TerstEntity terstEntity = new TerstEntity();
Element tr = trs.get(i);
/**将数据放入实体对象中*/
Elements tds = tr.select("td");
terstEntity.setCurrencyName(tds.get(0).text());
terstEntity.setBuyingRate(tds.get(1).text());
terstEntity.setCashBuyingRate(tds.get(2).text());
terstEntity.setSellingRate(tds.get(3).text());
terstEntity.setCashSellingRate(tds.get(4).text());
terstEntity.setMiddleRate(tds.get(5).text());
terstEntity.setPubTime(tds.get(6).text());
list.add(terstEntity);
}
map.put("list", list);
}else{
map.put("flag", "1");
}
return list;
}
/**
* 测试使用
*/
public static class TerstEntity {



private String currencyName;
public String getCurrencyName() {
return currencyName;
}

public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}



private String buyingRate;
public String getBuyingRate() {
return buyingRate;
}

public void setBuyingRate(String buyingRate) {
this.buyingRate = buyingRate;
}

private String cashBuyingRate;

public String getCashBuyingRate() {
return cashBuyingRate;
}

public void setCashBuyingRate(String cashBuyingRate) {
this.cashBuyingRate = cashBuyingRate;
}

private String sellingRate;

public String getSellingRate() {
return sellingRate;
}

public void setSellingRate(String sellingRate) {
this.sellingRate = sellingRate;
}

private String cashSellingRate;

public String getCashSellingRate() {
return cashSellingRate;
}

public void setCashSellingRate(String cashSellingRate) {
this.cashSellingRate = cashSellingRate;
}

private String middleRate;

public String getMiddleRate() {
return middleRate;
}

public void setMiddleRate(String middleRate) {
this.middleRate = middleRate;
}

private String PubTime;

public String getPubTime() {
return PubTime;
}

public void setPubTime(String pubTime) {
PubTime = pubTime;
}
public TerstEntity() {
}

@Override
public String toString() {
return "TerstEntity{" +
"currencyName='" + currencyName + '\'' +
", buyingRate='" + buyingRate + '\'' +
", cashBuyingRate='" + cashBuyingRate + '\'' +
", sellingRate='" + sellingRate + '\'' +
", cashSellingRate='" + cashSellingRate + '\'' +
", middleRate='" + middleRate + '\'' +
", PubTime='" + PubTime + '\'' +
'}';
}
}



}