
package com.tmerclub.cloud.supplier.controller.supplier;


import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.supplier.dto.AllinpaySupplierWithdrawCashDTO;
import com.tmerclub.cloud.supplier.dto.SupplierWithdrawCashConfigDto;
import com.tmerclub.cloud.supplier.dto.SupplierWithdrawCashDTO;
import com.tmerclub.cloud.supplier.service.SupplierWithdrawCashService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 提现申请信息
 *
 * @author tmerclub
 * @date 2021-06-10 10:28:13
 */
@RestController("supplierSupplierWithdrawCashController")
@RequestMapping("/s/supplier_withdraw_cash")
@Tag(name = "supplier-提现申请信息")
public class SupplierWithdrawCashController {

    @Autowired
    private SupplierWithdrawCashService supplierWithdrawCashService;

    @GetMapping("/getWithdrawCash")
    public ServerResponseEntity<SupplierWithdrawCashConfigDto> getWithdrawCashConfig() {
        return ServerResponseEntity.success(supplierWithdrawCashService.getConfig());
    }

    @PostMapping("/apply")
    @Operation(summary = "供应商提交申请提现", description = "供应商提交申请提现")
    public ServerResponseEntity<Void> apply(@RequestBody SupplierWithdrawCashDTO supplierWithdrawCashDTO) {
        supplierWithdrawCashService.apply(supplierWithdrawCashDTO, AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success();
    }

    @PostMapping("/allinpay_apply")
    @Operation(summary = "供应商提交通联申请提现", description = "通联独有")
    public ServerResponseEntity<String> allinpayApply(@RequestBody AllinpaySupplierWithdrawCashDTO allinpaySupplierWithdrawCashDTO) {
        if (Objects.isNull(allinpaySupplierWithdrawCashDTO.getAmount()) || Objects.isNull(allinpaySupplierWithdrawCashDTO.getBankCardNo())) {
            throw new LuckException("提现金额和银行卡号不能为空");
        }
        String bizOrderNo = supplierWithdrawCashService.allinpayApply(allinpaySupplierWithdrawCashDTO, AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success(bizOrderNo);
    }

    @Operation(summary = "提现确认支付", description = "通联独有")
    @PostMapping("/confirm_withdraw_pay")
    public ServerResponseEntity<Void> confirmWithdrawPay(@RequestBody AllinpaySupplierWithdrawCashDTO allinpaySupplierWithdrawCashDTO) {
        if (Objects.isNull(allinpaySupplierWithdrawCashDTO.getBizOrderNo()) || Objects.isNull(allinpaySupplierWithdrawCashDTO.getVerificationCode())) {
            throw new LuckException("短信验证码和订单号不能为空");
        }
        supplierWithdrawCashService.confirmWithdrawPay(allinpaySupplierWithdrawCashDTO, AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success();
    }

    @GetMapping("/check_draw_cash_amount")
    @Operation(summary = "检查提现金额")
    public ServerResponseEntity<Boolean> checkDrawCashAmount(Long amount) {
        supplierWithdrawCashService.checkDrawCashAmount(amount);
        return ServerResponseEntity.success(true);
    }

    @Operation(summary = "重新发送提现支付验证码", description = "通联独有")
    @PostMapping("/resendPaySms")
    public ServerResponseEntity<Void> resendPaySms(@RequestBody AllinpaySupplierWithdrawCashDTO allinpaySupplierWithdrawCashDTO) {
        if (Objects.isNull(allinpaySupplierWithdrawCashDTO.getBizOrderNo())) {
            throw new LuckException("提现订单号不能为空");
        }
        supplierWithdrawCashService.resendPaySms(allinpaySupplierWithdrawCashDTO.getBizOrderNo(), AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success();
    }
}
