@Service("nRService")
public class NRServiceImpl implements NRWuhanService {
@Autowired
private SelfAddMapper selfAddMapper;
@Autowired
private DiyNewsresultWuhanMapper newsresultWuhanMapper;
@Autowired
private DiyNewsresultWuhanHotMapper diyNewsresultWuhanHotMapper;
@Autowired
private DiyUserKeywordMapper diyUserKeywordMapper;
/**
* 查询关系图谱
*/
@Override
public Map<String,Integer> countWordByAreaId(Map<String, Object> paramMap) {
int keywordId = Integer.parseInt(paramMap.get("keywordId").toString());
Map<String,Integer> countMap = new HashMap<String,Integer>();
DiyNewsresultWuhanExample example = new DiyNewsresultWuhanExample();
example.createCriteria().andKeywordidEqualTo(keywordId);
// 查询需要统计的数据
List<DiyNewsresultWuhan> dataList = newsresultWuhanMapper.selectByExample(example);
Map<String,Integer> areaMap = Constants.WUHAN_AREAS;
for(DiyNewsresultWuhan bean : dataList){
// 相同的文章数
int sameNum = bean.getSamenum();
// 分割关键词
String[] wordAry = bean.getRelatedwords().split(",");
// 遍历关键词,并统计
for(String word : wordAry){
// 如果关键词包含则不统计
if(areaMap.containsKey(word))break;
if(countMap.containsKey(word)){
int newCount = countMap.get(word)+sameNum+1;
countMap.put(word, newCount);
} else {
countMap.put(word, sameNum==0?1:sameNum);
}
}
}
countMap = sortMap(countMap,20);
return countMap;
}
/**
* 查询指定关键词的关系图谱
*/
@Override
public Map<String,Integer> countWordByKeywordId(Map<String,Object> paramMap){
int keywordId = Integer.parseInt(paramMap.get("keywordId").toString());
Map<String,Integer> countMap = new HashMap<String,Integer>();
DiyNewsresultWuhanHotExample example = new DiyNewsresultWuhanHotExample();
example.createCriteria().andKeywordidEqualTo(keywordId);
// 查询需要统计的数据
List<DiyNewsresultWuhanHot> dataList = diyNewsresultWuhanHotMapper.selectByExample(example);
Map<String,Integer> hotMap = Constants.HOT_CI;
for(DiyNewsresultWuhanHot bean : dataList){
// 相同的文章数
int sameNum = bean.getSamenum();
// 分割关键词
String[] wordAry = bean.getRelatedwords().split(",");
// 遍历关键词,并统计
for(String word : wordAry){
// 如果关键词包含在则不统计
if(hotMap.containsKey(word))break;
if(countMap.containsKey(word)){
int newCount = countMap.get(word)+sameNum+1;
countMap.put(word, newCount);
} else {
countMap.put(word, sameNum==0?1:sameNum);
}
}
}
countMap = sortMap(countMap,20);
return countMap;
}
private static Set<String> fmtStr(String str){
Set<String> wordSet = new HashSet<String>();
String[] strAry = str.split(",");
for(String s : strAry){
wordSet.add(s);
}
return wordSet;
}
/**
* 根据map排序,并倒叙取前几个
* @param oldMap 需要排序的map
* @param many 取的个数
* @return
*/
public static Map<String,Integer> sortMap(Map<String,Integer> oldMap,int many) {
ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<java.lang.String, Integer> arg0,
Entry<java.lang.String, Integer> arg1) {
return arg0.getValue() - arg1.getValue();
}
});
Map<String,Integer> newMap = new LinkedHashMap<String,Integer>();
int count = list.size()>many?many:list.size();
for (int i = count; i > 0; i--) {
newMap.put(list.get(list.size()-i).getKey(), list.get(list.size()-i).getValue());
}
return newMap;
}
/**
* 查询13区折线图
*/
@Override
public List<Map<String,Object>> countWordByAreaIdLine(Map<String, Object> paramMap) {
List<Map<String,Object>> dataMap = selfAddMapper.get13ByAreaIdLine(paramMap);
return dataMap;
}
/**
* 查询指定关键字的折线图
*/
@Override
public List<Map<String,Object>> countWordByKeywordIdLine(Map<String, Object> paramMap) {
List<Map<String,Object>> dataMap = selfAddMapper.getKeywordIdLine(paramMap);
return dataMap;
}
/**
* 保存用户输入的关键词
*/
@Override
public void addUserKeyword(DiyUserKeyword diyUserKeyword) {
diyUserKeywordMapper.insertSelective(diyUserKeyword);
}
/**
* 根据地区id获取文章
*/
@Override
public Page getArticleByAreaId(Map<String, Object> paramMap, Page p) {
Date beginDate = DateUtil.parse(paramMap.get("beginDate").toString(), "yyyy-MM-dd");
Date endDate = DateUtil.parse(paramMap.get("endDate").toString(), "yyyy-MM-dd");
int keywordId = Integer.parseInt(paramMap.get("keywordId").toString());
int offset = (p.getCurrentPage()-1)*p.getPageSize();
DiyNewsresultWuhanExample example = new DiyNewsresultWuhanExample();
example.createCriteria().andKeywordidEqualTo(keywordId).andPubtimeBetween(beginDate, endDate);
int count = newsresultWuhanMapper.countByExample(example);
example.setOrderByClause(" samenum desc, pubtime desc limit " + offset + "," + p.getPageSize());
List<DiyNewsresultWuhan> dataList = newsresultWuhanMapper.selectByExample(example);
p = PageUtil.getPage(p.getCurrentPage(), p.getPageSize(), count, dataList);
return p;
}
@Override
public Page getArticleByKeywordId(Map<String, Object> paramMap, Page p) {
int keywordId = Integer.parseInt(paramMap.get("keywordId").toString());
Date beginDate = DateUtil.parse(paramMap.get("beginDate").toString(), "yyyy-MM-dd");
Date endDate = DateUtil.parse(paramMap.get("endDate").toString(), "yyyy-MM-dd");
int offset = (p.getCurrentPage()-1)*p.getPageSize();
DiyNewsresultWuhanHotExample example = new DiyNewsresultWuhanHotExample();
example.createCriteria().andKeywordidEqualTo(keywordId).andPubtimeBetween(beginDate, endDate);
int count = diyNewsresultWuhanHotMapper.countByExample(example);
example.setOrderByClause(" samenum desc, pubtime desc limit " + offset + "," + p.getPageSize());
List<DiyNewsresultWuhanHot> dataList = diyNewsresultWuhanHotMapper.selectByExample(example);
p = PageUtil.getPage(p.getCurrentPage(), p.getPageSize(), count, dataList);
return p;
}
GeneraterMybatis所生成model查询的serviceimpl应用
最新推荐文章于 2024-09-08 12:31:21 发布