PayMethodConfigController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.ruoyi.app.pay;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.ruoyi.app.pay.dto.PayMethodConfigItemDto;
  4. import com.ruoyi.app.pay.dto.PayMethodConfigSaveDto;
  5. import com.ruoyi.common.annotation.Log;
  6. import com.ruoyi.common.core.controller.BaseController;
  7. import com.ruoyi.common.core.domain.AjaxResult;
  8. import com.ruoyi.common.enums.BusinessType;
  9. import com.ruoyi.common.exception.ServiceException;
  10. import com.ruoyi.common.utils.MessageUtils;
  11. import com.ruoyi.common.utils.SecurityUtils;
  12. import com.ruoyi.system.domain.PaymentMethodConfig;
  13. import com.ruoyi.system.mapper.PaymentMethodConfigMapper;
  14. import com.ruoyi.system.service.PaymentMethodGateService;
  15. import org.springframework.security.access.prepost.PreAuthorize;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import org.springframework.web.bind.annotation.GetMapping;
  18. import org.springframework.web.bind.annotation.PutMapping;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * 平台支付方式设置接口(031)。
  29. * 维护方式×维度开关矩阵;可用性判定本身在 {@link PaymentMethodGateService},
  30. * 本接口只做配置读写——保存后闸门直查即生效,无需任何缓存失效动作。
  31. */
  32. @RestController
  33. @RequestMapping("/system/payMethodConfig")
  34. public class PayMethodConfigController extends BaseController {
  35. private final PaymentMethodConfigMapper configMapper;
  36. public PayMethodConfigController(PaymentMethodConfigMapper configMapper) {
  37. this.configMapper = configMapper;
  38. }
  39. /**
  40. * 查询开关全矩阵:固定按维度×组顺序返回,缺行按"开"补齐,
  41. * 平台未做任何设置时前端看到的就是全开(与系统现状一致)。
  42. */
  43. @PreAuthorize("@ss.hasPermi('pay:method:config')")
  44. @GetMapping("/list")
  45. public AjaxResult list() {
  46. List<PaymentMethodConfig> rows = configMapper.selectList(null);
  47. Map<String, PaymentMethodConfig> existing = new HashMap<>();
  48. for (PaymentMethodConfig row : rows) {
  49. existing.put(row.getMethodCode() + "@" + row.getScope(), row);
  50. }
  51. List<PaymentMethodConfig> matrix = new ArrayList<>();
  52. for (PaymentMethodGateService.GateScope scope : PaymentMethodGateService.GateScope.values()) {
  53. for (String group : PaymentMethodGateService.groupsOf(scope)) {
  54. PaymentMethodConfig row = existing.get(group + "@" + scope.name());
  55. if (row == null) {
  56. // 缺行=开的语义直接以补齐行返回,避免前端各自理解空行
  57. row = new PaymentMethodConfig();
  58. row.setMethodCode(group);
  59. row.setScope(scope.name());
  60. row.setEnabled(1);
  61. }
  62. matrix.add(row);
  63. }
  64. }
  65. return success(matrix);
  66. }
  67. /**
  68. * 批量保存开关:逐项校验组×维度合法后 upsert。
  69. * "开"也落行(记录操作人),缺行=开的语义只用于读取兜底。
  70. */
  71. @PreAuthorize("@ss.hasPermi('pay:method:config')")
  72. @Log(title = "支付方式设置", businessType = BusinessType.UPDATE)
  73. @Transactional(rollbackFor = Exception.class)
  74. @PutMapping
  75. public AjaxResult save(@RequestBody PayMethodConfigSaveDto dto) {
  76. if (dto == null || dto.getItems() == null || dto.getItems().isEmpty()) {
  77. throw new ServiceException(MessageUtils.message("pay.method.config.invalid"));
  78. }
  79. for (PayMethodConfigItemDto item : dto.getItems()) {
  80. // 组必须属于该维度的可管控集合(同时校验组代码与维度取值合法)
  81. if (item == null || item.getEnabled() == null
  82. || (item.getEnabled() != 0 && item.getEnabled() != 1)
  83. || !isValidScope(item.getScope())
  84. || !PaymentMethodGateService.groupsOf(scopeOf(item.getScope())).contains(item.getMethodCode())) {
  85. throw new ServiceException(MessageUtils.message("pay.method.config.invalid"));
  86. }
  87. }
  88. Date now = new Date();
  89. Long adminId = SecurityUtils.getUserId();
  90. for (PayMethodConfigItemDto item : dto.getItems()) {
  91. PaymentMethodConfig existing = configMapper.selectOne(
  92. new QueryWrapper<PaymentMethodConfig>()
  93. .eq("method_code", item.getMethodCode())
  94. .eq("scope", item.getScope()));
  95. if (existing == null) {
  96. PaymentMethodConfig row = new PaymentMethodConfig();
  97. row.setMethodCode(item.getMethodCode());
  98. row.setScope(item.getScope());
  99. row.setEnabled(item.getEnabled());
  100. row.setUpdateBy(adminId);
  101. row.setCreateTime(now);
  102. row.setUpdateTime(now);
  103. configMapper.insert(row);
  104. } else {
  105. existing.setEnabled(item.getEnabled());
  106. existing.setUpdateBy(adminId);
  107. existing.setUpdateTime(now);
  108. configMapper.updateById(existing);
  109. }
  110. }
  111. return success();
  112. }
  113. private boolean isValidScope(String scope) {
  114. try {
  115. scopeOf(scope);
  116. return true;
  117. } catch (IllegalArgumentException exception) {
  118. return false;
  119. }
  120. }
  121. private PaymentMethodGateService.GateScope scopeOf(String scope) {
  122. return PaymentMethodGateService.GateScope.valueOf(scope);
  123. }
  124. }