package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.WithdrawBo;
import org.dromara.main.domain.vo.WithdrawVo;
import org.dromara.main.service.IWithdrawService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后台提现管理Controller
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/withdraw")
public class WithdrawController extends BaseController {

    private final IWithdrawService withdrawService;

    @SaCheckPermission("system:withdraw:list")
    @GetMapping("/list")
    public TableDataInfo<WithdrawVo> list(WithdrawBo bo, PageQuery pageQuery) {
        return withdrawService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("system:withdraw:export")
    @Log(title = "提现管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WithdrawBo bo, HttpServletResponse response) {
        List<WithdrawVo> list = withdrawService.queryList(bo);
        ExcelUtil.exportExcel(list, "提现管理", WithdrawVo.class, response);
    }

    @SaCheckPermission("system:withdraw:query")
    @GetMapping("/{id}")
    public R<WithdrawVo> getInfo(@PathVariable Long id) {
        return R.ok(withdrawService.queryById(id));
    }

    @SaCheckPermission("system:withdraw:audit")
    @Log(title = "提现管理", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/pass/{id}")
    public R<Void> auditPass(@PathVariable Long id, @RequestParam(required = false) String remark) {
        withdrawService.auditAndTransfer(id, remark);
        return R.ok("审核通过，打款成功");
    }

    @SaCheckPermission("system:withdraw:audit")
    @Log(title = "提现管理", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/reject/{id}")
    public R<Void> auditReject(@PathVariable Long id, @RequestParam String remark) {
        withdrawService.auditReject(id, remark);
        return R.ok("审核拒绝成功");
    }
}
