CommonController.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.ruoyi.demo.controller;
  2. import com.ruoyi.common.core.domain.R;
  3. import com.ruoyi.demo.constant.RedisContant;
  4. import com.ruoyi.demo.entity.AddrCategory;
  5. import com.ruoyi.demo.entity.bo.WdCount;
  6. import com.ruoyi.demo.entity.vo.AddrCategoryBody;
  7. import com.ruoyi.demo.entity.vo.TypeByBody;
  8. import com.ruoyi.demo.service.CommonService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.data.redis.core.RedisTemplate;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import java.util.List;
  15. import java.util.concurrent.TimeUnit;
  16. /**
  17. * 基础功能-下拉框
  18. */
  19. @RestController
  20. @RequestMapping("/common")
  21. public class CommonController {
  22. @Autowired
  23. CommonService commonService;
  24. @Autowired
  25. RedisTemplate redisTemplate;
  26. /**
  27. * 获取犇云分类下拉框
  28. * @return
  29. */
  30. @GetMapping("/tag")
  31. public R tag(){
  32. //1.查看redis中是否已存在
  33. List<TypeByBody> wdCount = (List<TypeByBody>) redisTemplate.boundValueOps(RedisContant.COMMON_TAG).get();
  34. if (wdCount != null) {
  35. return R.ok(wdCount);
  36. }
  37. //2.查询数据
  38. List<TypeByBody> tag = commonService.tag();
  39. //3.保存到redis中
  40. redisTemplate.boundValueOps(RedisContant.COMMON_TAG).set(tag);
  41. redisTemplate.expire(RedisContant.COMMON_TAG,RedisContant.COMMON_TAG_TIME, TimeUnit.MINUTES); //30分钟
  42. return R.ok(tag);
  43. }
  44. /**
  45. * 获取地区码下拉框
  46. * @return
  47. */
  48. @GetMapping("/addrCodeMap")
  49. public R addrCodeMap(){
  50. //1.查看redis中是否存在数据
  51. List<AddrCategoryBody> wdCount = (List<AddrCategoryBody>) redisTemplate.boundValueOps(RedisContant.COMMON_ADDR_CODE_MAP).get();
  52. if (wdCount != null) {
  53. return R.ok(wdCount);
  54. }
  55. //2.查询数据
  56. List<AddrCategoryBody> addrCategoryBodies = commonService.addrCodeMap();
  57. //3.存入redis
  58. redisTemplate.boundValueOps(RedisContant.COMMON_ADDR_CODE_MAP).set(wdCount);
  59. redisTemplate.expire(RedisContant.COMMON_ADDR_CODE_MAP,RedisContant.COMMON_ADDR_CODE_MAP_TIME, TimeUnit.MINUTES); //30分钟
  60. return R.ok(addrCategoryBodies);
  61. }
  62. /**
  63. * 分等级(省、市、区)查询地区
  64. * @param code 地区Code:level=province(不需要)、level=city(code=省码)、level=zone(code=市码)
  65. * @param level 搜索等级:province(查询全省)、city(查某个省的市)、zone(查某个市的区)
  66. * @return
  67. */
  68. @GetMapping("/addrCodeLevel")
  69. public R addrCodeLevel(String code,String level){
  70. //1.查看redis中是否存在数据
  71. List<AddrCategory> wdCount = (List<AddrCategory> ) redisTemplate.boundHashOps(RedisContant.COMMON_LEVEL).get(code+level);
  72. if (wdCount != null) {
  73. return R.ok(wdCount);
  74. }
  75. //2查询数据
  76. List<AddrCategory> addrCategories = commonService.addrCodeLevel(code,level);
  77. //3.存入redis
  78. redisTemplate.boundHashOps(RedisContant.COMMON_LEVEL).put(code+level,addrCategories);
  79. redisTemplate.expire(RedisContant.COMMON_LEVEL,RedisContant.COMMON_LEVEL_TIME, TimeUnit.MINUTES); //30分钟
  80. return R.ok(addrCategories);
  81. }
  82. }