package com.ruoyi.app.pay; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ruoyi.app.pay.dto.PayMethodConfigItemDto; import com.ruoyi.app.pay.dto.PayMethodConfigSaveDto; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.system.domain.PaymentMethodConfig; import com.ruoyi.system.mapper.PaymentMethodConfigMapper; import com.ruoyi.system.service.PaymentMethodGateService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 平台支付方式设置接口(031)。 * 维护方式×维度开关矩阵;可用性判定本身在 {@link PaymentMethodGateService}, * 本接口只做配置读写——保存后闸门直查即生效,无需任何缓存失效动作。 */ @RestController @RequestMapping("/system/payMethodConfig") public class PayMethodConfigController extends BaseController { private final PaymentMethodConfigMapper configMapper; public PayMethodConfigController(PaymentMethodConfigMapper configMapper) { this.configMapper = configMapper; } /** * 查询开关全矩阵:固定按维度×组顺序返回,缺行按"开"补齐, * 平台未做任何设置时前端看到的就是全开(与系统现状一致)。 */ @PreAuthorize("@ss.hasPermi('pay:method:config')") @GetMapping("/list") public AjaxResult list() { List rows = configMapper.selectList(null); Map existing = new HashMap<>(); for (PaymentMethodConfig row : rows) { existing.put(row.getMethodCode() + "@" + row.getScope(), row); } List matrix = new ArrayList<>(); for (PaymentMethodGateService.GateScope scope : PaymentMethodGateService.GateScope.values()) { for (String group : PaymentMethodGateService.groupsOf(scope)) { PaymentMethodConfig row = existing.get(group + "@" + scope.name()); if (row == null) { // 缺行=开的语义直接以补齐行返回,避免前端各自理解空行 row = new PaymentMethodConfig(); row.setMethodCode(group); row.setScope(scope.name()); row.setEnabled(1); } matrix.add(row); } } return success(matrix); } /** * 批量保存开关:逐项校验组×维度合法后 upsert。 * "开"也落行(记录操作人),缺行=开的语义只用于读取兜底。 */ @PreAuthorize("@ss.hasPermi('pay:method:config')") @Log(title = "支付方式设置", businessType = BusinessType.UPDATE) @Transactional(rollbackFor = Exception.class) @PutMapping public AjaxResult save(@RequestBody PayMethodConfigSaveDto dto) { if (dto == null || dto.getItems() == null || dto.getItems().isEmpty()) { throw new ServiceException(MessageUtils.message("pay.method.config.invalid")); } for (PayMethodConfigItemDto item : dto.getItems()) { // 组必须属于该维度的可管控集合(同时校验组代码与维度取值合法) if (item == null || item.getEnabled() == null || (item.getEnabled() != 0 && item.getEnabled() != 1) || !isValidScope(item.getScope()) || !PaymentMethodGateService.groupsOf(scopeOf(item.getScope())).contains(item.getMethodCode())) { throw new ServiceException(MessageUtils.message("pay.method.config.invalid")); } } Date now = new Date(); Long adminId = SecurityUtils.getUserId(); for (PayMethodConfigItemDto item : dto.getItems()) { PaymentMethodConfig existing = configMapper.selectOne( new QueryWrapper() .eq("method_code", item.getMethodCode()) .eq("scope", item.getScope())); if (existing == null) { PaymentMethodConfig row = new PaymentMethodConfig(); row.setMethodCode(item.getMethodCode()); row.setScope(item.getScope()); row.setEnabled(item.getEnabled()); row.setUpdateBy(adminId); row.setCreateTime(now); row.setUpdateTime(now); configMapper.insert(row); } else { existing.setEnabled(item.getEnabled()); existing.setUpdateBy(adminId); existing.setUpdateTime(now); configMapper.updateById(existing); } } return success(); } private boolean isValidScope(String scope) { try { scopeOf(scope); return true; } catch (IllegalArgumentException exception) { return false; } } private PaymentMethodGateService.GateScope scopeOf(String scope) { return PaymentMethodGateService.GateScope.valueOf(scope); } }