package com.kym.admin.controller;

import com.kym.common.R;
import com.kym.common.annotation.SysLog;
import com.kym.entity.queryParams.*;
import com.kym.service.RefundLogService;
import com.kym.service.SplitRecordService;
import com.kym.service.StationAccountService;
import com.kym.service.WithdrawnRecordService;
import com.kym.service.wechat.WxPayService;
import jakarta.validation.Valid;
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 WxPayService wxPayService;

    private final RefundLogService refundLogService;

    public FinanceController(SplitRecordService splitRecordService, StationAccountService stationAccountService,
                             WithdrawnRecordService withdrawnRecordService, WxPayService wxPayService, RefundLogService refundLogService) {
        this.splitRecordService = splitRecordService;
        this.stationAccountService = stationAccountService;
        this.withdrawnRecordService = withdrawnRecordService;
        this.wxPayService = wxPayService;
        this.refundLogService = refundLogService;
    }

    /**
     * 站点账户列表
     *
     * @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
     */
    @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
     */
    @PostMapping("/reviewWithdrawn")
    public R<?> reviewWithdrawn(@RequestBody WithdrawnQueryParam params) {
        withdrawnRecordService.reviewWithdrawn(params);
        return R.success();
    }

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


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

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


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


}
