| 
					
				 | 
			
			
				@ -0,0 +1,125 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				package com.yihu.ehr.resource.service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.ehr.query.BaseJpaService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.ehr.resource.dao.HotWordDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.ehr.resource.model.HotWord; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.apache.commons.collections.CollectionUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.apache.commons.lang.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.data.redis.core.RedisTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.data.redis.core.ZSetOperations; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.jdbc.core.JdbcTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.transaction.annotation.Transactional; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				@Service 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				@Transactional 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				public class ProfileSearchService extends BaseJpaService<HotWord,HotWordDao> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static final String PrefixRedis = "HOTWORDS_"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private RedisTemplate redisTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private JdbcTemplate jdbcTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public boolean saveOrUpdateHotWords(String searchText,int type){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        boolean b = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(StringUtils.isNotBlank(searchText)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<String> searchKeyWords = getSearchKeyWords(searchText); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            b = saveOrUpdateToRedis(searchKeyWords,type); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return b; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public List<String> getHotWords(int type,int recount){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<String> result = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        ZSetOperations zSetOperations = redisTemplate.opsForZSet(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Set<String> values = zSetOperations.reverseRangeByScore(PrefixRedis+type, 0, Double.MAX_VALUE); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(recount <= 0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(values.size() > 0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for(String value:values){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                result.add(value); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if(result.size() == recount){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }else{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //查看数据库是否有值,防止redis数据丢失 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from hot_word where type=" + type + " order by search_count desc"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if(CollectionUtils.isNotEmpty(list)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                //不为空,将数据同步到redis,并返回数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                for(Map<String,Object> map:list){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    zSetOperations.incrementScore(PrefixRedis+type, map.get("word"), Double.valueOf(map.get("search_count")+"")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if(result.size() < recount){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        result.add(map.get("word")+""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private List<String> getSearchKeyWords(String keys){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<String> list = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(StringUtils.isNotBlank(keys)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String[] keyArray = keys.split(" "); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for(String key:keyArray){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if(StringUtils.isNotBlank(key) && !"or".equalsIgnoreCase(key) && !"and".equalsIgnoreCase(key)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if(key.startsWith("\"") && key.endsWith("\"")){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        key = key.substring(1,key.length()-1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    }else if(key.startsWith("+") || key.startsWith("-")){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        key = key.substring(1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    list.add(key); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return list; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param keys 搜索词 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param type 1:病历(目前只有该分类,用于后期做扩展) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public boolean saveOrUpdateToRedis(List<String> keys,int type){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(CollectionUtils.isNotEmpty(keys)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            keys.forEach(key ->{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                ZSetOperations zSetOperations = redisTemplate.opsForZSet(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                zSetOperations.incrementScore(PrefixRedis+type, key, 1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public boolean synRedisHotWords(int type){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //1.获取redis中的热搜 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        ZSetOperations zSetOperations = redisTemplate.opsForZSet(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Set<ZSetOperations.TypedTuple<String>> values = zSetOperations.rangeByScoreWithScores(PrefixRedis+type, 0, Double.MAX_VALUE); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Date now = new Date(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(!values.isEmpty()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //2.清空mysql的热搜 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            jdbcTemplate.execute("delete from hot_word where type ="+type); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<HotWord> insertList = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for(ZSetOperations.TypedTuple value:values){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                HotWord hotWord = new HotWord(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                hotWord.setWord(value.getValue().toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                hotWord.setSearchCount((int)value.getScore().doubleValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                hotWord.setExpireTime(-1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                hotWord.setType(type); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                hotWord.setUpdateTime(now); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                insertList.add(hotWord); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            batchInsert(insertList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				} 
			 |