AppVersionController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.AppVersion;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.exception.BusinessException;
  5. import com.izouma.nineth.repo.AppVersionRepo;
  6. import com.izouma.nineth.service.AppVersionService;
  7. import com.izouma.nineth.service.CacheService;
  8. import com.izouma.nineth.utils.ObjUtils;
  9. import com.izouma.nineth.utils.excel.ExcelUtils;
  10. import lombok.AllArgsConstructor;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.cache.annotation.Cacheable;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.security.access.prepost.PreAuthorize;
  15. import org.springframework.web.bind.annotation.*;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.IOException;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. @RestController
  22. @RequestMapping("/appVersion")
  23. @AllArgsConstructor
  24. public class AppVersionController extends BaseController {
  25. private AppVersionService appVersionService;
  26. private AppVersionRepo appVersionRepo;
  27. private CacheService cacheService;
  28. @PreAuthorize("hasRole('ADMIN')")
  29. @PostMapping("/save")
  30. public AppVersion save(@RequestBody AppVersion record) {
  31. record.setVersionNum(convertVersion(record.getVersion()));
  32. if (record.getId() != null) {
  33. AppVersion orig = appVersionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  34. ObjUtils.merge(orig, record);
  35. orig = appVersionRepo.save(orig);
  36. cacheService.clearCheckUpdate();
  37. return orig;
  38. }
  39. record = appVersionRepo.save(record);
  40. cacheService.clearCheckUpdate();
  41. return record;
  42. }
  43. private int convertVersion(String version) {
  44. String[] arr = version.split("\\.");
  45. StringBuilder str = new StringBuilder();
  46. for (int i = 0; i < arr.length; i++) {
  47. str.insert(0, String.format("%03d", Integer.parseInt(arr[arr.length - 1 - i])));
  48. }
  49. return Integer.parseInt(str.toString());
  50. }
  51. @PreAuthorize("hasRole('ADMIN')")
  52. @PostMapping("/all")
  53. public Page<AppVersion> all(@RequestBody PageQuery pageQuery) {
  54. return appVersionService.all(pageQuery);
  55. }
  56. @GetMapping("/get/{id}")
  57. public AppVersion get(@PathVariable Long id) {
  58. return appVersionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  59. }
  60. @PreAuthorize("hasRole('ADMIN')")
  61. @PostMapping("/del/{id}")
  62. public void del(@PathVariable Long id) {
  63. appVersionRepo.deleteById(id);
  64. }
  65. @GetMapping("/excel")
  66. @ResponseBody
  67. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  68. List<AppVersion> data = all(pageQuery).getContent();
  69. ExcelUtils.export(response, data);
  70. }
  71. @GetMapping("/checkIosReview")
  72. public AppVersion checkIosReview(@RequestParam String version) {
  73. return appVersionRepo.findByPlatformAndVersionAndChannelAndDelFalse("iOS", version, "default").orElseThrow(new BusinessException("版本不存在"));
  74. }
  75. @GetMapping("/checkAndroidReview")
  76. public AppVersion checkAndroidReview(@RequestParam String version) {
  77. return appVersionRepo.findByPlatformAndVersionAndChannelAndDelFalse("Android", version, "default").orElseThrow(new BusinessException("版本不存在"));
  78. }
  79. @GetMapping("/getVersion")
  80. public AppVersion checkReview(@RequestParam String platform, @RequestParam String version,
  81. @RequestParam(defaultValue = "default") String channel) {
  82. return appVersionRepo.findByPlatformAndVersionAndChannelAndDelFalse(platform, version, channel).orElseThrow(new BusinessException("版本不存在"));
  83. }
  84. @GetMapping("/checkUpdate")
  85. @Cacheable(value = "checkUpdate", key = "#platform+'@'+#version+'@'+#channel")
  86. public Map<String, Object> checkUpdate(@RequestParam String platform, @RequestParam String version,
  87. @RequestParam(required = false, defaultValue = "default") String channel) {
  88. Map<String, Object> map = new HashMap<>();
  89. AppVersion appVersion = appVersionRepo.findLatest(platform, channel)
  90. .orElseThrow(new BusinessException("版本不存在"));
  91. map.put("version", appVersion);
  92. if (convertVersion(version) < appVersion.getVersionNum()) {
  93. map.put("needUpdate", true);
  94. } else {
  95. map.put("needUpdate", false);
  96. }
  97. return map;
  98. }
  99. }