package com.tzy.controller.pay;

import com.tzy.annotation.ApiLog;
import com.tzy.common.dto.InDto;
import com.tzy.common.dto.OutDTO;
import com.tzy.common.enums.BusinessType;
import com.tzy.pay.service.IPaymentBaseService;
import com.tzy.sportcard.api.domain.AppUserInfoDto;
import com.tzy.sportcard.api.service.MineApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 描述: 支付相关
 */
@Slf4j
@RestController
@RequestMapping("/api/payment")
public class PaymentController {

    @Autowired
    private MineApiService mineApiService;

    @Autowired
    private IPaymentBaseService paymentBaseService;

    /**
     * 用户绑定银行卡信息列表
     */
    @ApiLog(title = "用户绑定银行卡信息列表:支持免密支付标记", businessType = BusinessType.INSERT)
    @PostMapping( "/bindBankInfos")
    @ResponseBody
    public OutDTO bindBankInfos(@RequestBody InDto inDto) {
        checkUser(inDto);
        return paymentBaseService.bindBankInfos(inDto);
    }

    /**
     * 是否有绑定免密支付标记的银行卡
     * 如果有则返回绑定免密支付银行卡列表
     */
    @ApiLog(title = "是否有绑定免密支付标记的银行卡", businessType = BusinessType.SEARCH)
    @PostMapping( "/hasBindBankFreePay")
    @ResponseBody
    public OutDTO hasBindBankFreePay(@RequestBody InDto inDto) {
        checkUser(inDto);
        return paymentBaseService.hasBindBankFreePay(inDto);
    }
    /**
     * 用户绑定银行卡信息
     */
    @ApiLog(title = "用户绑定银行卡信息:支持免密支付标记", businessType = BusinessType.UPDATE)
    @PostMapping( "/v2/bindBank")
    @ResponseBody
    public OutDTO bindBankV2(@RequestBody InDto inDto) {
        checkUser(inDto);
        return paymentBaseService.bindBankV2(inDto);
    }
    /**
     * 用户绑定银行卡信息编辑
     */
    @ApiLog(title = "用户绑定银行卡信息编辑:支持免密支付标记", businessType = BusinessType.UPDATE)
    @PostMapping( "/updateBankFreePay")
    @ResponseBody
    public OutDTO updateBankFreePay(@RequestBody InDto inDto) {
        checkUser(inDto);
        return paymentBaseService.updateBankFreePay(inDto);
    }

    /**
     * 验证用户
     * @param inDto
     * @return
     */
    private void checkUser(InDto inDto) {
        if (null == inDto){
            throw new RuntimeException("参数不能为空");
        }
        OutDTO checkUser = mineApiService.checkUser(inDto);
        if (!checkUser.isSuccess()){
            throw new RuntimeException(checkUser.getMsg());
        }
        AppUserInfoDto appUserInfoDto = (AppUserInfoDto) checkUser.get("userInfo");
        inDto.put("appUserInfoDto", appUserInfoDto);
        inDto.setUserId(appUserInfoDto.getId());//重新设置userid
    }
}
