AppVersionController.java 4.2 KB

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