SystemDictService.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.yihu.wlyy.service.system;
  2. import com.yihu.wlyy.entity.dict.SystemDict;
  3. import com.yihu.wlyy.repository.dict.SystemDictDao;
  4. import com.yihu.wlyy.repository.dict.SystemDictListDao;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.apache.poi.util.StringUtil;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.redis.core.StringRedisTemplate;
  9. import org.springframework.stereotype.Service;
  10. import java.util.List;
  11. /**
  12. * Created by Administrator on 2016/8/13.
  13. */
  14. @Service
  15. public class SystemDictService {
  16. @Autowired
  17. private SystemDictDao systemDictDao;
  18. @Autowired
  19. private StringRedisTemplate redisTemplate;
  20. public List<SystemDict> getDictByDictName(String name) {
  21. return systemDictDao.findByDictName(name);
  22. }
  23. /**
  24. * 字典转译
  25. * @param dictName
  26. * @param code
  27. * @return
  28. */
  29. public String getDictValue(String dictName,String code){
  30. String re = "";
  31. try {
  32. if(!StringUtils.isEmpty(code))
  33. {
  34. //判断该字典redis是否存在
  35. String exit = redisTemplate.opsForValue().get("systemDict:"+dictName);
  36. if(!StringUtils.isEmpty(exit))
  37. {
  38. re = redisTemplate.opsForValue().get("systemDict:"+dictName+":"+code);
  39. }
  40. else{
  41. List<SystemDict> list = systemDictDao.findByDictName(dictName);
  42. if(list!=null && list.size()>0)
  43. {
  44. redisTemplate.opsForValue().set("systemDict:"+dictName,"1");
  45. for(SystemDict item:list)
  46. {
  47. redisTemplate.opsForValue().set("systemDict:"+dictName+":"+item.getCode(),item.getValue());
  48. if(code.equals(item.getCode()))
  49. {
  50. re = item.getValue();
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. ex.printStackTrace();
  60. re = systemDictDao.findByDictNameAndCode(dictName,code);
  61. }
  62. return re;
  63. }
  64. }