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

import com.jeesharp.business.modules.pay.payalipayconfig.application.PayAlipayConfigApplicationService;
import com.jeesharp.business.modules.pay.payalipayconfig.dto.PayAlipayConfigSaveDTO;
import com.jeesharp.business.modules.pay.payalipayconfig.entity.PayAlipayConfig;
import com.jeesharp.business.modules.pay.payalipayconfig.vo.PayAlipayConfigInfoVO;
import com.jeesharp.business.modules.pay.payalipayconfig.vo.PayAlipayConfigListVO;
import com.jeesharp.business.modules.pay.payalipayconfig.vo.PayAlipayConfigQueryVO;
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/payAlipayConfig/v1")
@Tag(name = "支付宝配置")
@AllArgsConstructor
public class PayAlipayConfigController extends BaseController {

    private final PayAlipayConfigApplicationService payAlipayConfigApplicationService;

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

    @GetMapping("active")
    @Operation(summary = "获取当前生效配置")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payAlipayConfig:info')")
    public AjaxResult<PayAlipayConfigInfoVO> 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(
                payAlipayConfigApplicationService.resolveActive(tenantId, channelConfigId, payChannelConfigCode));
    }

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

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

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