package com.kym.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.kym.common.R;
import com.kym.common.annotation.SysLog;
import com.kym.entity.queryParams.*;
import com.kym.entity.queryParams.WalletDetailQueryParam;
import com.kym.service.*;
import com.kym.service.wechat.WxPayService;
import jakarta.validation.Valid;
import java.util.Map;
import org.springframework.web.bind.annotation.*;

/**
 * 财务
 *
 * @author skyline
 * @since 2025-02-24
 */
@RestController
@RequestMapping("/finance")
public class FinanceController {

    private final SplitRecordService splitRecordService;
    private final StationAccountService stationAccountService;
    private final WithdrawnRecordService withdrawnRecordService;
    private final SettlementService settlementService;
    private final WxPayService wxPayService;
    private final RefundLogService refundLogService;
    private final PlatformAccountService platformAccountService;
    private final PlatformRevenueRecordService platformRevenueRecordService;
    private final WalletDetailService walletDetailService;

    public FinanceController(SplitRecordService splitRecordService, StationAccountService stationAccountService,
                             WithdrawnRecordService withdrawnRecordService, SettlementService settlementService,
                             WxPayService wxPayService, RefundLogService refundLogService,
                             PlatformAccountService platformAccountService, PlatformRevenueRecordService platformRevenueRecordService,
                             WalletDetailService walletDetailService) {
        this.splitRecordService = splitRecordService;
        this.stationAccountService = stationAccountService;
        this.withdrawnRecordService = withdrawnRecordService;
        this.settlementService = settlementService;
        this.wxPayService = wxPayService;
        this.refundLogService = refundLogService;
        this.platformAccountService = platformAccountService;
        this.platformRevenueRecordService = platformRevenueRecordService;
        this.walletDetailService = walletDetailService;
    }

    /**
     * 站点账户列表
     *
     * @param params
     * @return
     */
    @PostMapping("/stationAccounts")
    public R<?> stationAccounts(@RequestBody StationQueryParam params) {
        return R.success(stationAccountService.listStationAccounts(params));
    }

    /**
     * 分账记录列表
     *
     * @param params
     * @return
     */
    @PostMapping("/splitRecords")
    public R<?> splitRecords(@RequestBody SplitRecordQueryParams params) {
        return R.success(splitRecordService.listSplitRecords(params));
    }

    /**
     * 申请提现
     *
     * @param params
     * @return
     */
    @SaCheckPermission("withdrawnRecord.modify")
    @PostMapping("/applyWithdrawn")
    public R<?> applyWithdrawn(@RequestBody WithdrawnQueryParam params) {
        stationAccountService.applyWithdrawn(params);
        return R.success();
    }

    /**
     * 提现记录列表
     *
     * @param params
     * @return
     */
    @PostMapping("/withdrawnRecords")
    public R<?> withdrawnRecords(@RequestBody WithdrawnQueryParam params) {
        return R.success(withdrawnRecordService.listWithdrawnRecords(params));
    }

    /**
     * 提现记录审核
     *
     * @param params
     * @return
     */
    @SaCheckPermission("withdrawnRecord.modify")
    @PostMapping("/reviewWithdrawn")
    public R<?> reviewWithdrawn(@RequestBody WithdrawnQueryParam params) {
        withdrawnRecordService.reviewWithdrawn(params);
        return R.success();
    }

    /**
     * 提现记录打款确认
     *
     * @param params
     * @return
     */
    @SaCheckPermission("withdrawnRecord.modify")
    @PostMapping("/confirmWithdrawnPayment")
    public R<?> confirmWithdrawnPayment(@RequestBody WithdrawnQueryParam params) {
        withdrawnRecordService.confirmWithdrawnPayment(params);
        return R.success();
    }


    /**
     * 处理用户退款
     *
     * @param refundLogId
     * @return
     */
    @SaCheckPermission("refundLog.modify")
    @SysLog("处理用户微信退款")
    @GetMapping("/customWxRefund/{refundLogId}")
    R<?> customWxRefund(@PathVariable("refundLogId") long refundLogId) {
        wxPayService.wxRefund(refundLogId);
        return R.success();
    }

    /**
     * 批量处理用户退款
     */
    @SaCheckPermission("refundLog.modify")
    @SysLog("批量处理用户微信退款")
    @PostMapping("/batchWxRefund")
    R<?> batchWxRefund(@RequestBody java.util.List<Long> refundLogIds) {
        wxPayService.batchWxRefund(refundLogIds);
        return R.success();
    }

    /**
     * 退款记录列表
     *
     * @param params
     * @return
     */
    @SysLog("退款记录列表")
    @GetMapping("/listRefundLog")
    public R<?> listRefundLog(@ModelAttribute CommonQueryParam params) {
        return R.success(refundLogService.listRefundLog(params));
    }


    /**
     * 处理用户退款
     *
     * @param param
     * @return
     */
    @SaCheckPermission("refundLog.modify")
    @SysLog("用户退款申请")
    @PostMapping("/applyRefund")
    @ResponseBody
    R<?> wxAppRefund(@Valid @RequestBody RefundParam param) {
        wxPayService.applyWxRefund(param.getUserId(), param.getReason());
        return R.success();
    }

    /**
     * 结算记录列表
     */
    @PostMapping("/settlementRecords")
    public R<?> settlementRecords(@RequestBody SettlementQueryParam params) {
        return R.success(settlementService.listSettlementRecords(params));
    }

    /**
     * 手动触发结算（运维用）
     */
    @SaCheckPermission("settlement.modify")
    @SysLog("手动触发月度结算")
    @PostMapping("/triggerSettlement")
    public R<?> triggerSettlement() {
        settlementService.executeMonthlySettlement();
        return R.success();
    }

    /**
     * 确认结算 — 将待结算金额转入站点可提现余额
     */
    @SaCheckPermission("settlement.modify")
    @SysLog("确认结算")
    @PostMapping("/confirmSettlement")
    public R<?> confirmSettlement(@RequestBody Map<String, Long> params) {
        settlementService.confirmSettlement(params.get("id"));
        return R.success();
    }

    /**
     * 平台账户信息
     */
    @PostMapping("/platformAccount")
    public R<?> platformAccount() {
        return R.success(platformAccountService.getPlatformAccount());
    }

    /**
     * 平台收入记录列表
     */
    @PostMapping("/platformRevenueRecords")
    public R<?> platformRevenueRecords(@RequestBody CommonQueryParam params) {
        return R.success(platformRevenueRecordService.listRevenueRecords(params));
    }

    /**
     * 用户资金流水列表
     */
    @GetMapping("/walletDetails")
    public R<?> walletDetails(@ModelAttribute WalletDetailQueryParam params) {
        return R.success(walletDetailService.listAdminWalletDetail(params));
    }

    @SysLog("提现记录详情")
    @GetMapping("/withdrawnRecord/{id}")
    public R<?> withdrawnRecordDetail(@PathVariable String id) {
        return R.success(withdrawnRecordService.detail(id));
    }

    @SysLog("退款详情")
    @GetMapping("/refundLog/detail/{outRefundNo}")
    public R<?> refundLogDetail(@PathVariable String outRefundNo) {
        return R.success(refundLogService.detail(outRefundNo));
    }

    @SaCheckPermission("offlineRecharge")
    @SysLog("线下充值")
    @PostMapping("/offlineRecharge")
    public R<?> offlineRecharge(@RequestBody OfflineRechargeParam param) {
        wxPayService.offlineRecharge(param.getUserId(), param.getAmount(),
                param.getGrantsAmount(), param.getRemark());
        return R.success();
    }

}
