CacheController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.enums.OperationType;
  3. import com.izouma.nineth.exception.BusinessException;
  4. import com.izouma.nineth.service.CacheService;
  5. import com.izouma.nineth.service.CollectionService;
  6. import lombok.AllArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.springframework.security.access.prepost.PreAuthorize;
  10. import org.springframework.web.bind.annotation.PathVariable;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import java.lang.reflect.InvocationTargetException;
  15. import java.lang.reflect.Method;
  16. import java.util.Objects;
  17. @RestController
  18. @RequestMapping("/cache")
  19. @AllArgsConstructor
  20. @Slf4j
  21. public class CacheController {
  22. private final CacheService cacheService;
  23. private final CollectionService collectionService;
  24. @RequestMapping("/clear")
  25. @PreAuthorize("hasRole('ADMIN')")
  26. public void clear(String name, String stringParam, Long longParam) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  27. Method method;
  28. if (StringUtils.isNotEmpty(stringParam)) {
  29. method = CacheService.class.getMethod("clear" + StringUtils.capitalize((name)), String.class);
  30. method.invoke(cacheService, stringParam);
  31. } else if (longParam != null) {
  32. method = CacheService.class.getMethod("clear" + StringUtils.capitalize((name)), Long.class);
  33. method.invoke(cacheService, longParam);
  34. } else {
  35. method = CacheService.class.getMethod("clear" + StringUtils.capitalize((name)));
  36. method.invoke(cacheService);
  37. }
  38. }
  39. @PostMapping(value = "/operating/{collectionId}/{num}/{operationType}")
  40. public void operatingStockOrSale(@PathVariable Long collectionId, @PathVariable int num, @PathVariable OperationType operationType) {
  41. if (Objects.isNull(collectionId)) {
  42. throw new BusinessException("collectionId参数错误");
  43. }
  44. if (0 >= num) {
  45. throw new BusinessException("num参数错误");
  46. }
  47. if (Objects.isNull(operationType)) {
  48. throw new BusinessException("操作类型不可为空");
  49. }
  50. switch (operationType) {
  51. case INCREASE_STOCK:
  52. log.info(String.format("collectionId:[%S] 加库存:[%S]", collectionId, num));
  53. collectionService.increaseStock(collectionId, num);
  54. break;
  55. case DECREASE_STOCK:
  56. log.info(String.format("collectionId:[%S] 减库存:[%S]", collectionId, num));
  57. collectionService.decreaseStock(collectionId, num);
  58. break;
  59. case INCREASE_SALE:
  60. log.info(String.format("collectionId:[%S] 加销量:[%S]", collectionId, num));
  61. collectionService.increaseSale(collectionId, num);
  62. break;
  63. case DECREASE_SALE:
  64. log.info(String.format("collectionId:[%S] 减销量:[%S]", collectionId, num));
  65. collectionService.decreaseSale(collectionId, num);
  66. break;
  67. default:
  68. String err = String.format("暂不支持的操作类型:[%S]", operationType);
  69. log.info(err);
  70. throw new BusinessException(err);
  71. }
  72. }
  73. }