| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package com.ruoyi.app.pay;
- import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
- import com.ruoyi.app.pay.dto.MerchantPayMethodSaveDto;
- import com.ruoyi.common.annotation.Anonymous;
- import com.ruoyi.common.core.controller.BaseController;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.exception.ServiceException;
- import com.ruoyi.common.utils.MessageUtils;
- import com.ruoyi.system.domain.InfoUser;
- import com.ruoyi.system.mapper.InfoUserMapper;
- import com.ruoyi.system.service.MerchantStoreAccessService;
- import com.ruoyi.system.service.PaymentMethodGateService;
- import com.ruoyi.system.utils.Auth;
- import com.ruoyi.system.utils.JwtUtil;
- 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.RequestHeader;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * 商家端支付方式选择接口(031,foodie-store 支付设置页)。
- * 选择在商家主账号级生效(连锁共享);未设置(NULL)=全部启用,存量商家行为零变化。
- * 子账号编辑写其主账号(022 体系,与银行信息语义一致)。
- */
- @RestController
- @RequestMapping("/system/merchantPayMethods")
- @Anonymous
- public class MerchantPayMethodController extends BaseController {
- private final PaymentMethodGateService gateService;
- private final InfoUserMapper userMapper;
- private final MerchantStoreAccessService merchantStoreAccessService;
- public MerchantPayMethodController(PaymentMethodGateService gateService,
- InfoUserMapper userMapper,
- MerchantStoreAccessService merchantStoreAccessService) {
- this.gateService = gateService;
- this.userMapper = userMapper;
- this.merchantStoreAccessService = merchantStoreAccessService;
- }
- /**
- * 设置页数据:available=平台商家维度开放的组(不带门店级就绪度,设置页只关心平台开关)
- * 追加现金项(031 US7:orderType 传 1 按自取/堂食口径返回含 CASH 的全集),
- * selected=商家当前选择(NULL 视为全部,返回空数组由前端展示为全选)。
- */
- @Auth
- @GetMapping
- public AjaxResult get(@RequestHeader String token) {
- Long ownerUserId = resolveOwner(token);
- InfoUser owner = userMapper.selectById(ownerUserId);
- List<PaymentMethodGateService.PayMethodOption> available =
- gateService.listAvailable(PaymentMethodGateService.GateScope.MERCHANT, null, null, 1L);
- List<String> selected = new ArrayList<>();
- String csv = owner == null ? null : owner.getPayMethods();
- if (csv != null && !csv.trim().isEmpty()) {
- for (String part : csv.split(",")) {
- if (!part.trim().isEmpty()) selected.add(part.trim());
- }
- }
- Map<String, Object> data = new HashMap<>();
- data.put("available", available);
- data.put("selected", selected);
- return success(data);
- }
- /**
- * 保存选择:空数组=清空回落"全部";写主账号行 pay_methods。
- * 只校验组代码合法,不与平台开关联动(平台后关不影响已存值,实时按闸门算)。
- */
- @Auth
- @PutMapping
- public AjaxResult save(@RequestHeader String token, @RequestBody MerchantPayMethodSaveDto dto) {
- Long ownerUserId = resolveOwner(token);
- List<String> groups = PaymentMethodGateService.groupsOf(PaymentMethodGateService.GateScope.MERCHANT);
- String csv = null;
- if (dto != null && dto.getMethodCodes() != null && !dto.getMethodCodes().isEmpty()) {
- for (String code : dto.getMethodCodes()) {
- if (code == null || !groups.contains(code.trim())) {
- throw new ServiceException(MessageUtils.message("pay.method.config.invalid"));
- }
- }
- csv = String.join(",", dto.getMethodCodes());
- }
- userMapper.update(null, new UpdateWrapper<InfoUser>()
- .eq("user_id", ownerUserId)
- .set("pay_methods", csv));
- return success();
- }
- /** token(主账号或子账号) → 主账号用户 ID。 */
- private Long resolveOwner(String token) {
- Long loginUserId = Long.valueOf(new JwtUtil().getusid(token));
- return merchantStoreAccessService.resolve(loginUserId).ownerUserId();
- }
- }
|