package com.yn.bftl.thirdparty.modules.chinaums.controller.member;

import com.yn.bftl.common.modules.account.dto.AccountPaymentsDTO;
import com.yn.bftl.common.modules.account.dto.WithdrawalsDTO;
import com.yn.bftl.common.modules.account.vo.AccountPaymentsVO;
import com.yn.bftl.common.modules.account.vo.BalanceAcctsVO;
import com.yn.bftl.common.modules.account.vo.BalanceTransactionsPageVO;
import com.yn.bftl.common.modules.account.vo.WithdrawalsVO;
import com.yn.bftl.common.modules.order.dto.SubaccountRefundDTO;
import com.yn.bftl.common.modules.order.dto.WalletRefundDTO;
import com.yn.bftl.common.modules.order.vo.RefundVO;
import com.yn.bftl.common.modules.order.vo.SubaccountRefundVO;
import com.yn.bftl.common.modules.subaccount.entity.ChinaumsPayment;
import com.yn.bftl.common.modules.subaccount.entity.ChinaumsRefund;
import com.yn.bftl.thirdparty.modules.business.repository.ChinaumsPaymentRepository;
import com.yn.bftl.thirdparty.modules.business.repository.ChinaumsRefundRepository;
import com.yn.bftl.thirdparty.modules.chinaums.dto.WalletCreateByCompanyDTO;
import com.yn.bftl.thirdparty.modules.chinaums.dto.WalletOrderPayDTO;
import com.yn.bftl.thirdparty.modules.chinaums.service.WalletService;
import com.yn.bftl.thirdparty.modules.chinaums.vo.ChinaumsRechargeAccountVO;
import com.yn.bftl.thirdparty.modules.chinaums.vo.ElectronicReceiptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 电子账簿控制层
 *
 * @author huabiao
 * @create 2022/12/5  15:19
 **/
@Api(value = "WalletMemberController", tags = "用户端-电子账簿")
@RestController
@RequestMapping("/member/wallet")
@Validated
public class WalletMemberController {

    @Resource
    private WalletService walletService;
    @Resource
    private ChinaumsRefundRepository chinaumsRefundRepository;
    @Resource
    private ChinaumsPaymentRepository chinaumsPaymentRepository;

    @ApiOperation("电子账簿查询（电子账簿 ID）")
    @GetMapping({"/balance_accts_qry_by_acctid/{balanceAcctId}", "/balance_accts_qry_by_acctid"})
    public BalanceAcctsVO balanceAcctsQryByAcctid(@PathVariable(required = false) String balanceAcctId) {
        return walletService.balanceAcctsQryByAcctId(balanceAcctId);
    }

    @ApiOperation("电子账簿流水查询")
    @GetMapping("/balance_transactions")
    public BalanceTransactionsPageVO balanceTransactions(String balanceAcctId, String startAt, String endAt) {
        return walletService.balanceTransactions(balanceAcctId, startAt, endAt);
    }

    @ApiOperation(value = "电子账簿-获取充值账户")
    @RequestMapping(value = "/recharge_account", method = RequestMethod.GET)
    public ChinaumsRechargeAccountVO getWalletRechargeAccount() {
        return walletService.getWalletRechargeAccount();
    }

    @ApiOperation("电子账簿-消费(转账)")
    @PostMapping("/payments")
    public AccountPaymentsVO payments(@Validated @RequestBody AccountPaymentsDTO accountPaymentsDTO) {
        return walletService.payments(accountPaymentsDTO);
    }

    @ApiOperation("消费订单查询（平台订单号）")
    @GetMapping("/payments_by_out_order_no/{outOrderNo}")
    public AccountPaymentsVO paymentsByOutOrderNo(@PathVariable String outOrderNo) {
        return walletService.paymentsByOutOrderNo(outOrderNo);
    }

    @ApiOperation("提现创建")
    @PostMapping("/withdrawals")
    public WithdrawalsVO withdrawals(@Validated @RequestBody WithdrawalsDTO withdrawalsDTO) {
        return walletService.withdrawals(withdrawalsDTO);
    }

    @ApiOperation("提现订单查询（平台订单号）")
    @GetMapping("/withdrawals_by_out_order_no/{outOrderNo}")
    public WithdrawalsVO withdrawalsByOutOrderNo(@PathVariable String outOrderNo) {
        return walletService.withdrawalsByOutOrderNo(outOrderNo);
    }

    @ApiOperation(value = "电子账簿-订单支付")
    @RequestMapping(value = "/order_pay", method = RequestMethod.POST)
    public String orderPay(@Valid @RequestBody WalletOrderPayDTO walletOrderPayDTO) {
        return walletService.orderPay(walletOrderPayDTO);
    }

    @ApiOperation(value = "电子账簿-退款")
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public RefundVO refund(@Valid @RequestBody WalletRefundDTO walletRefundDTO) {
        return walletService.refund(walletRefundDTO);
    }

    @ApiOperation("POS充值转账（经销商在POS充值后，由系统定时任务把钱转到经销商预存款账户）")
    @PostMapping("/pos_transfer")
    public void posTransfer() {
        walletService.posTransfer();
    }

    @ApiOperation("扫码充值转账（经销商在扫码充值后，由系统定时任务把钱转到经销商预存款账户）")
    @PostMapping("/scan_transfer")
    public void scanTransfer() {
        walletService.scanTransfer();
    }

    @ApiOperation(value = "电子账簿-分账退款")
    @RequestMapping(value = "/subaccount_refund", method = RequestMethod.POST)
    public SubaccountRefundVO subaccountRefund(@Valid @RequestBody SubaccountRefundDTO subaccountRefundDTO) {
        return walletService.subaccountRefund(subaccountRefundDTO);
    }

    @ApiOperation("电子回单")
    @GetMapping("/electronicReceipts/{tradeId}")
    public ElectronicReceiptVO electronicReceipts(@PathVariable String tradeId) {
        return walletService.electronicReceipts(tradeId);
    }

    @ApiOperation(value = "电子账簿-创建企业电子账本")
    @RequestMapping(value = "/create_by_company", method = RequestMethod.POST)
    public void createWalletByCompany(@Valid @RequestBody WalletCreateByCompanyDTO walletCreateByCompanyDTO) {
        walletService.createWalletByCompany(walletCreateByCompanyDTO);
    }

    @ApiOperation(value = "电子账簿-创建授信还款电子账簿")
    @GetMapping("/create_credit_refund")
    public void createWalletCreditRefund() {
        walletService.createWalletCreditRefund();
    }

    // TODO 临时代码，后续删除
    @ApiOperation(value = "银联退款单-处理")
    @GetMapping("/chinaums_refund/handle")
    public void handleChinaumsRefund() {
        // 获取所有银联退款单
        List<ChinaumsRefund> chinaumsRefundList = chinaumsRefundRepository.findAll();
        for (ChinaumsRefund chinaumsRefund : chinaumsRefundList) {
            // 通过原始系统单号获取银联支付单
            ChinaumsPayment chinaumsPayment = chinaumsPaymentRepository.findFirstByGuaranteePaymentId(chinaumsRefund.getOrigTradeId());
            if (chinaumsPayment != null) {
                chinaumsRefund.setSubaccountType(chinaumsPayment.getSubaccountType());
                chinaumsRefundRepository.save(chinaumsRefund);
            }
        }
    }
}
