
package com.tmerclub.cloud.multishop.controller.multishop;


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.multishop.dto.AllinpayShopWithdrawCashDTO;
import com.tmerclub.cloud.multishop.dto.ShopWithdrawCashConfigDto;
import com.tmerclub.cloud.multishop.dto.ShopWithdrawCashDTO;
import com.tmerclub.cloud.multishop.service.ShopWithdrawCashService;
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
 */
@RestController("multishopShopWithdrawCashController")
@RequestMapping("/m/shopWithdrawApply")
@Tag(name = "multishop-商家提现申请信息")
public class ShopWithdrawCashController {

    @Autowired
    private ShopWithdrawCashService shopWithdrawCashService;

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

    @PostMapping("/apply/normal")
    @Operation(summary = "商家提交申请提现", description = "商家提交申请提现")
    public ServerResponseEntity<Void> apply(@RequestBody ShopWithdrawCashDTO shopWithdrawCashDTO) {
        shopWithdrawCashService.apply(shopWithdrawCashDTO, AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success();
    }

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

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

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

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

}
