很伤心,if I were a boy

package com.winchampion.credit.yq;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.winchampion.credit.core2.common.utils.GsonUtil;
import com.winchampion.credit.core2.repository.bean.news.NegNew;
import com.winchampion.credit.core2.repository.bean.news.YQNegnewsJson;
import com.winchampion.credit.http.HttpHelper;
import com.winchampion.credit.http.ResponseContent;


@Service
public class PubSentimentServiceImpl implements PubSentimentService {
// 1.获取公司名录,从上海和江苏的两个数据库中获取
// 2.按照公司名称去除重复,把List放到HashSet
// 3.按公司名录删除表中已存在的公司名称数据t_news表中的数据
// 4.调用接口(gatway),把返回的数据添加到表中(service-dao)

private static Logger logger = LoggerFactory.getLogger(PubSentimentServiceImpl.class);

private String charset = "utf-8";

/**添加企业名称接口URL*/
@Value("${comAddkeywordUrl}")
private String comAddkeywordUrl;
/** 企业具体负面新闻内容接口 URL */
@Value("${comNegnewsUrl}")
private String comNegnewsUrl;
/** 行业具体负面新闻内容URL */
@Value("${indNegnewsUrl}")
private String indNegnewsUrl;
/**行业负面行业政策接口*/
@Value("${indNegpolicyUrl}")
private String indNegpolicyUrl;

private String baseGet(String url){
ResponseContent result=HttpHelper.getUrlRespContent(url,charset);
if(result!=null){
try {
logger.info("舆情推送返回值:"+result.getContent());
return result.getContent();
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
return null;
}


@Override
public String addCompayName(String compName) {
return  this.baseGet(comAddkeywordUrl+compName);
}

@Override
public List<NegNew> getComNegnews(String fromDate, String toDate,
String compName) {
List<NegNew> negnewsList=new ArrayList<NegNew>();
String resultJsonStr=this.baseGet(comNegnewsUrl+"from="+fromDate+"&to="+toDate+"&name="+compName);//如果遍历两个公司名。如果这个公司名在HashSet中,说明还没有查过,查,查后在HashSet中删除。如果不在,说明已经查过了,就不要再查,直接下一个。
try{
Gson gson = GsonUtil.gson;
if(null!=resultJsonStr&&!"".equals(resultJsonStr)){
// List<YQNegnewsJson> resultJson;
gson.toJson(resultJsonStr);
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(resultJsonStr);
JsonArray jsonArray = null;
if(element.isJsonArray()){
jsonArray = element.getAsJsonArray();
}
//System.out.println("abc");
Iterator it = jsonArray.iterator();
while(it.hasNext()){
JsonElement ele = (JsonElement)it.next();
YQNegnewsJson yqNegnewsJson = gson.fromJson(ele, YQNegnewsJson.class);
NegNew negnews = new NegNew();
negnews.setNewsId(yqNegnewsJson.getNews_id());
negnews.setTitle(yqNegnewsJson.getTitle());
negnews.setUrl(yqNegnewsJson.getUrl());
negnews.setPulishTime(yqNegnewsJson.getPulish_time());
negnews.setOperateTime(yqNegnewsJson.getLast_update_time());
negnews.setKeyword(compName);
negnews.setType("1");
negnewsList.add(negnews);
}
}
}catch(Exception e){
System.err.println(e.toString());
}
return negnewsList;

  

//把HashSet中数据循环,然后分别传参数,
@Override
public List<NegNew> getIndNegnews(String fromDate, String toDate,
String industryname) {
List<NegNew> negnewsList=new ArrayList<NegNew>();
String resultJsonStr=this.baseGet(indNegnewsUrl+"from="+fromDate+"&to="+toDate+"&name="+industryname);
try{
Gson gson = GsonUtil.gson;
/*JSONArray jsonArray = new JSONArray();
if(null!=resultJsonStr&&!"".equals(resultJsonStr)){
// YQNegnewsJson[] resultJson = gson.fromJson(resultJsonStr,(new YQNegnewsJson[]{}).getClass());
(List)JSONArray.
for(YQNegnewsJson yqNegnewsJson : resultJson){
NegNew negnews = new NegNew();
negnews.setNewsId(yqNegnewsJson.getNews_id());
negnews.setTitle(yqNegnewsJson.getTitle());
negnews.setUrl(yqNegnewsJson.getUrl());
negnews.setPulishTime(yqNegnewsJson.getPulish_time());
negnews.setOperateTime(yqNegnewsJson.getLast_update_time());//operateTime
negnews.setKeyword(industryname);
negnews.setType("2");
negnewsList.add(negnews);
}
}*/
}catch(Exception e){
System.err.println(e.toString());
}
return negnewsList;
}




@SuppressWarnings("unchecked")
@Override
public List<NegNew> getIndNegpolicy(String fromDate, String toDate,
String name) {
List<NegNew> negnewsList=new ArrayList<NegNew>();
String resultJsonStr=this.baseGet(comNegnewsUrl+"from="+fromDate+"&to="+toDate+"&name="+name);
try{
Gson gson = GsonUtil.gson;
if(null!=resultJsonStr&&!"".equals(resultJsonStr)){
List<YQNegnewsJson> resultJson = gson.fromJson(resultJsonStr,ArrayList.class);
Iterator it = resultJson.iterator();
while(it.hasNext()){
Map<String,Object> ele = (LinkedHashMap<String,Object>)it.next();
NegNew negnews = new NegNew();
negnews.setNewsId((String.valueOf(ele.get("news_id"))));
negnews.setTitle((String)ele.get("title"));
negnews.setUrl((String)ele.get("url"));
negnews.setPulishTime((String)ele.get("pulish_time"));
negnews.setOperateTime((String)ele.get("last_update_time"));
negnews.setKeyword(name);
negnews.setType("3");
negnewsList.add(negnews);
System.out.println(ele.get("url"));
}
}
}catch(Exception e){
System.err.println(e.toString());
}
return negnewsList;
}
}

你可能感兴趣的:(很伤心,if I were a boy)