MerchantPayMethodController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.ruoyi.app.pay;
  2. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  3. import com.ruoyi.app.pay.dto.MerchantPayMethodSaveDto;
  4. import com.ruoyi.common.annotation.Anonymous;
  5. import com.ruoyi.common.core.controller.BaseController;
  6. import com.ruoyi.common.core.domain.AjaxResult;
  7. import com.ruoyi.common.exception.ServiceException;
  8. import com.ruoyi.common.utils.MessageUtils;
  9. import com.ruoyi.system.domain.InfoUser;
  10. import com.ruoyi.system.mapper.InfoUserMapper;
  11. import com.ruoyi.system.service.MerchantStoreAccessService;
  12. import com.ruoyi.system.service.PaymentMethodGateService;
  13. import com.ruoyi.system.utils.Auth;
  14. import com.ruoyi.system.utils.JwtUtil;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.PutMapping;
  17. import org.springframework.web.bind.annotation.RequestBody;
  18. import org.springframework.web.bind.annotation.RequestHeader;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * 商家端支付方式选择接口(031,foodie-store 支付设置页)。
  27. * 选择在商家主账号级生效(连锁共享);未设置(NULL)=全部启用,存量商家行为零变化。
  28. * 子账号编辑写其主账号(022 体系,与银行信息语义一致)。
  29. */
  30. @RestController
  31. @RequestMapping("/system/merchantPayMethods")
  32. @Anonymous
  33. public class MerchantPayMethodController extends BaseController {
  34. private final PaymentMethodGateService gateService;
  35. private final InfoUserMapper userMapper;
  36. private final MerchantStoreAccessService merchantStoreAccessService;
  37. public MerchantPayMethodController(PaymentMethodGateService gateService,
  38. InfoUserMapper userMapper,
  39. MerchantStoreAccessService merchantStoreAccessService) {
  40. this.gateService = gateService;
  41. this.userMapper = userMapper;
  42. this.merchantStoreAccessService = merchantStoreAccessService;
  43. }
  44. /**
  45. * 设置页数据:available=平台商家维度开放的组(不带门店级就绪度,设置页只关心平台开关)
  46. * 追加现金项(031 US7:orderType 传 1 按自取/堂食口径返回含 CASH 的全集),
  47. * selected=商家当前选择(NULL 视为全部,返回空数组由前端展示为全选)。
  48. */
  49. @Auth
  50. @GetMapping
  51. public AjaxResult get(@RequestHeader String token) {
  52. Long ownerUserId = resolveOwner(token);
  53. InfoUser owner = userMapper.selectById(ownerUserId);
  54. List<PaymentMethodGateService.PayMethodOption> available =
  55. gateService.listAvailable(PaymentMethodGateService.GateScope.MERCHANT, null, null, 1L);
  56. List<String> selected = new ArrayList<>();
  57. String csv = owner == null ? null : owner.getPayMethods();
  58. if (csv != null && !csv.trim().isEmpty()) {
  59. for (String part : csv.split(",")) {
  60. if (!part.trim().isEmpty()) selected.add(part.trim());
  61. }
  62. }
  63. Map<String, Object> data = new HashMap<>();
  64. data.put("available", available);
  65. data.put("selected", selected);
  66. return success(data);
  67. }
  68. /**
  69. * 保存选择:空数组=清空回落"全部";写主账号行 pay_methods。
  70. * 只校验组代码合法,不与平台开关联动(平台后关不影响已存值,实时按闸门算)。
  71. */
  72. @Auth
  73. @PutMapping
  74. public AjaxResult save(@RequestHeader String token, @RequestBody MerchantPayMethodSaveDto dto) {
  75. Long ownerUserId = resolveOwner(token);
  76. List<String> groups = PaymentMethodGateService.groupsOf(PaymentMethodGateService.GateScope.MERCHANT);
  77. String csv = null;
  78. if (dto != null && dto.getMethodCodes() != null && !dto.getMethodCodes().isEmpty()) {
  79. for (String code : dto.getMethodCodes()) {
  80. if (code == null || !groups.contains(code.trim())) {
  81. throw new ServiceException(MessageUtils.message("pay.method.config.invalid"));
  82. }
  83. }
  84. csv = String.join(",", dto.getMethodCodes());
  85. }
  86. userMapper.update(null, new UpdateWrapper<InfoUser>()
  87. .eq("user_id", ownerUserId)
  88. .set("pay_methods", csv));
  89. return success();
  90. }
  91. /** token(主账号或子账号) → 主账号用户 ID。 */
  92. private Long resolveOwner(String token) {
  93. Long loginUserId = Long.valueOf(new JwtUtil().getusid(token));
  94. return merchantStoreAccessService.resolve(loginUserId).ownerUserId();
  95. }
  96. }