|  | @ -0,0 +1,161 @@
 | 
	
		
			
				|  |  | package com.yihu.wlyy.service.system;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.dict.HealthProblemDict;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.dict.Icd10Dict;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.dict.HealthProblemDictDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.dict.HpIcd10RelationDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.dict.Icd10DictDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.util.DateUtil;
 | 
	
		
			
				|  |  | import net.sf.json.JSONArray;
 | 
	
		
			
				|  |  | import net.sf.json.JSONObject;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.data.redis.core.StringRedisTemplate;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.BeanPropertyRowMapper;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.scheduling.annotation.Async;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.thymeleaf.util.DartUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.annotation.PostConstruct;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by chenweida on 2017/8/9.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class Icd10DictServcie {
 | 
	
		
			
				|  |  |     private final String keyIcd10 = "dict:idc:Icd10:";
 | 
	
		
			
				|  |  |     private final String keyHealthProblem = "dict:idc:HealthProblem:";
 | 
	
		
			
				|  |  |     private final String keyDict2healthProblem = "dict:idc:Icd10ToHealthProblem:";
 | 
	
		
			
				|  |  |     private final String keyHealthProblem2Dict = "dict:idc:HealthProblemToIcd10:";
 | 
	
		
			
				|  |  |     private final String ok = "dict:idc:Icd10:init";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private StringRedisTemplate redisTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据icd10的 code 获取name
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param Icd10Code
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String getIcd10Name(String Icd10Code) {
 | 
	
		
			
				|  |  |         return redisTemplate.opsForValue().get(keyIcd10 + Icd10Code);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据健康问题code获取name
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param HealthProblemCode
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String getHealthProblemName(String HealthProblemCode) {
 | 
	
		
			
				|  |  |         return redisTemplate.opsForValue().get(keyHealthProblem + HealthProblemCode);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据icd10的 code 获取全部的健康问题
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param Icd10Code
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String getHealthProblemsByIcd10Code(String Icd10Code) {
 | 
	
		
			
				|  |  |         return redisTemplate.opsForValue().get(keyDict2healthProblem + Icd10Code);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据健康问题的 code 获取全部的icd10
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param HealthProblemCode
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String getIcd10ByHealthProblemCode(String HealthProblemCode) {
 | 
	
		
			
				|  |  |         return redisTemplate.opsForValue().get(keyHealthProblem2Dict + HealthProblemCode);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  |     //==============================初始化数据到redis=============================
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 把icd10缓存到redis
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @PostConstruct
 | 
	
		
			
				|  |  |     public boolean cacheDict() {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if (redisTemplate.hasKey(ok)) {
 | 
	
		
			
				|  |  |             return true;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         List<HealthProblemDict> healthProblemDicts = getHealthProblemDicts();
 | 
	
		
			
				|  |  |         //缓存keyIcd10
 | 
	
		
			
				|  |  |         cacheIcd10(healthProblemDicts);
 | 
	
		
			
				|  |  |         //缓存keyHealthProblem
 | 
	
		
			
				|  |  |         cacheHealthProblem(healthProblemDicts);
 | 
	
		
			
				|  |  |         //缓存keyHealthProblem2Dict
 | 
	
		
			
				|  |  |         cacheHealthProblem2Dict(healthProblemDicts);
 | 
	
		
			
				|  |  |         //缓存keyDict2healthProblem
 | 
	
		
			
				|  |  |         cacheDict2healthProblem(healthProblemDicts);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         redisTemplate.opsForValue().set(ok, DateUtil.dateToStrLong(new Date()) + "初始化完成");
 | 
	
		
			
				|  |  |         return true;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     private void cacheIcd10(List<HealthProblemDict> healthProblemDicts) {
 | 
	
		
			
				|  |  |         healthProblemDicts.stream().forEach(one -> {
 | 
	
		
			
				|  |  |             String key = keyIcd10 + one.getCode();
 | 
	
		
			
				|  |  |             redisTemplate.opsForValue().set(key, one.getName());
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     private void cacheHealthProblem(List<HealthProblemDict> healthProblemDicts) {
 | 
	
		
			
				|  |  |         healthProblemDicts.stream().forEach(one -> {
 | 
	
		
			
				|  |  |             String key = keyHealthProblem + one.getHpcode();
 | 
	
		
			
				|  |  |             redisTemplate.opsForValue().set(key, one.getHpname());
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * key是 code   value是 hpcode  1对1
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     private void cacheDict2healthProblem(List<HealthProblemDict> healthProblemDicts) {
 | 
	
		
			
				|  |  |         healthProblemDicts.stream().forEach(one -> {
 | 
	
		
			
				|  |  |             String key = keyDict2healthProblem + one.getCode();
 | 
	
		
			
				|  |  |             JSONObject jsonObject = new JSONObject();
 | 
	
		
			
				|  |  |             jsonObject.put("key", one.getHpcode());
 | 
	
		
			
				|  |  |             jsonObject.put("value", one.getHpname());
 | 
	
		
			
				|  |  |             redisTemplate.opsForValue().set(key, jsonObject.toString());
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * key是 hpcode    value是 code  1对多
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     private void cacheHealthProblem2Dict(List<HealthProblemDict> healthProblemDicts) {
 | 
	
		
			
				|  |  |         Map<String, List<KeyValueModel>> saveMap = new HashMap<>();
 | 
	
		
			
				|  |  |         healthProblemDicts.stream().forEach(one -> {
 | 
	
		
			
				|  |  |             List<KeyValueModel> valueList = saveMap.get(one.getHpcode());
 | 
	
		
			
				|  |  |             if (valueList == null) {
 | 
	
		
			
				|  |  |                 valueList = new ArrayList<KeyValueModel>();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             valueList.add(new KeyValueModel(one.getCode(), one.getName()));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             saveMap.put(one.getHpcode(), valueList);
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         for (Map.Entry<String, List<KeyValueModel>> one : saveMap.entrySet()) {
 | 
	
		
			
				|  |  |             String key = keyHealthProblem2Dict + one.getKey();
 | 
	
		
			
				|  |  |             redisTemplate.opsForValue().set(key, JSONArray.fromObject(one.getValue()).toString());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private List<HealthProblemDict> getHealthProblemDicts() {
 | 
	
		
			
				|  |  |         String sql = "select i.*,p.code hpcode,p.`name` hpname from icd10_dict i,hp_icd10_relation hp,health_problem_dict p where i.id=hp.icd10_id and hp.hp_id=p.id";
 | 
	
		
			
				|  |  |         return jdbcTemplate.query(sql, new BeanPropertyRowMapper(HealthProblemDict.class));
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |