package com.jeesharp.business.modules.pay.paypufaconfig.controller;

import com.jeesharp.business.modules.pay.paypufaconfig.application.PayPufaConfigApplicationService;
import com.jeesharp.business.modules.pay.paypufaconfig.dto.PayPufaConfigSaveDTO;
import com.jeesharp.business.modules.pay.paypufaconfig.entity.PayPufaConfig;
import com.jeesharp.business.modules.pay.paypufaconfig.vo.PayPufaConfigInfoVO;
import com.jeesharp.business.modules.pay.paypufaconfig.vo.PayPufaConfigListVO;
import com.jeesharp.business.modules.pay.paypufaconfig.vo.PayPufaConfigQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 浦发渠道配置管理端 REST 接口。
 *
 * @author jeesharp
 * @since 2026-05-25
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/pay/payPufaConfig/v1")
@Tag(name = "浦发配置")
@AllArgsConstructor
public class PayPufaConfigController extends BaseController {

    private final PayPufaConfigApplicationService payPufaConfigApplicationService;

    @GetMapping("info")
    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payPufaConfig:info')")
    public AjaxResult<PayPufaConfigInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(payPufaConfigApplicationService.get(id));
    }

    @GetMapping("active")
    @Operation(summary = "获取当前生效配置")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payPufaConfig:info')")
    public AjaxResult<PayPufaConfigInfoVO> active(
            @RequestParam(value = "tenantId", required = false) String tenantId,
            @RequestParam(value = "channelConfigId", required = false) String channelConfigId,
            @RequestParam(value = "payChannelConfigCode", required = false) String payChannelConfigCode) {
        return AjaxResult.okData(
                payPufaConfigApplicationService.resolveActive(tenantId, channelConfigId, payChannelConfigCode));
    }

    @PostMapping("list")
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payPufaConfig:list')")
    public AjaxResult<PageRespVO<PayPufaConfigListVO>> list(@RequestBody PayPufaConfigQueryVO query) {
        return AjaxResult.okData(payPufaConfigApplicationService.findPage(new Page<>(query), query));
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payPufaConfig:save')")
    public AjaxResult<Void> save(@RequestBody @Valid PayPufaConfigSaveDTO dto) {
        payPufaConfigApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @GetMapping("delete")
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payPufaConfig:delete')")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        payPufaConfigApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }
}
