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

import cn.hutool.core.bean.BeanUtil;
import com.yn.bftl.common.common.exception.YnceError;
import com.yn.bftl.common.modules.company.entity.ChinaumsAuthInfo;
import com.yn.bftl.common.modules.company.entity.ChinaumsBankCard;
import com.yn.bftl.common.modules.company.enums.ApplicationStatus;
import com.yn.bftl.common.modules.company.enums.ChinaumsBankCardStatus;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.modules.chinaums.dto.BankCardAmountValidateDTO;
import com.yn.bftl.thirdparty.modules.chinaums.dto.byx.ByxChinaumsAcctBindBankCardDTO;
import com.yn.bftl.thirdparty.modules.chinaums.dto.chinaums.ChinaumsAddBankCardDTO;
import com.yn.bftl.thirdparty.modules.chinaums.enums.OprtTypeEnum;
import com.yn.bftl.thirdparty.modules.chinaums.repository.ChinaumsAuthInfoRepository;
import com.yn.bftl.thirdparty.modules.chinaums.repository.ChinaumsBankCardRepository;
import com.yn.bftl.thirdparty.modules.chinaums.service.BankCardService;
import com.yn.bftl.thirdparty.modules.chinaums.vo.ChinaumsBankCardDetailVO;
import com.yn.bftl.thirdparty.modules.chinaums.vo.ChinaumsBankCardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 银联银行卡控制层
 *
 * @author huabiao
 * @create 2023/2/15  14:57
 **/
@Api(value = "BankCardMemberController", tags = "用户端-银联银行卡")
@RestController
@RequestMapping("/member/chinaums/bank_card")
public class BankCardMemberController {

    @Resource
    private BankCardService bankCardService;
    @Resource
    private ChinaumsAuthInfoRepository chinaumsAuthInfoRepository;
    @Resource
    private ChinaumsBankCardRepository chinaumsBankCardRepository;

    @ApiOperation(value = "银联银行卡-列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "status", value = "银行卡状态", required = true, dataType = "ChinaumsBankCardStatus", dataTypeClass = ChinaumsBankCardStatus.class),
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<ChinaumsBankCardVO> bankCardList(ChinaumsBankCardStatus status) {
        return bankCardService.bankCardList(status);
    }

    @ApiOperation(value = "银联银行卡-绑定")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addBankCard(@Valid @RequestBody ChinaumsAddBankCardDTO chinaumsAddBankCardDTO) {
        if (chinaumsAddBankCardDTO.getBankAcctType().equals("1")) {
            // 校验手机号
            if (StringUtils.isBlank(chinaumsAddBankCardDTO.getMobileNumber())) {
                throw new YnceErrorException(YnceError.YNCE_218021);
            }
            // 校验验证码
            if (StringUtils.isBlank(chinaumsAddBankCardDTO.getSmsCode())) {
                throw new YnceErrorException(YnceError.YNCE_218022);
            }
        }
        bankCardService.addBankCard(chinaumsAddBankCardDTO);
    }

    @ApiOperation(value = "银联银行卡-删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "银行卡ID", required = true, dataType = "Long", dataTypeClass = Long.class),
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Boolean deleteBankCard(@NotNull Long id) {
        return bankCardService.deleteBankCard(id);
    }

    @ApiOperation(value = "银联银行卡-打款金额验证")
    @RequestMapping(value = "/amount_validate", method = RequestMethod.POST)
    public Boolean amountValidate(@Valid @RequestBody BankCardAmountValidateDTO bankCardAmountValidateDTO) {
        return bankCardService.amountValidate(bankCardAmountValidateDTO);
    }

    @ApiOperation(value = "银联银行卡-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "银行卡ID", required = true, dataType = "Long", dataTypeClass = Long.class),
    })
    @RequestMapping(value = "/one", method = RequestMethod.GET)
    public ChinaumsBankCardDetailVO getBankCardDetail(@NotNull Long id) {
        return bankCardService.getBankCardDetail(id);
    }

    @ApiOperation(value = "银联银行卡-处理")
    @RequestMapping(value = "/handle", method = RequestMethod.POST)
    public void handleChinaumsBankCard() {
        // 获取所有银联认证信息
        List<ChinaumsAuthInfo> chinaumsAuthInfoList = chinaumsAuthInfoRepository.findAll();
        for (ChinaumsAuthInfo chinaumsAuthInfo : chinaumsAuthInfoList) {
            // 审核通过的需要将银行卡信息保存到银联认证信息中
            if (chinaumsAuthInfo.getApplicationStatus().equals(ApplicationStatus.REGISTER_SUCCESS)) {
                ChinaumsBankCard chinaumsBankCard = new ChinaumsBankCard();
                chinaumsBankCard.setBankCode(chinaumsAuthInfo.getSettleAcctBankCode());
                chinaumsBankCard.setBankBranchCode(chinaumsAuthInfo.getSettleAcctBankBranchCode());
                chinaumsBankCard.setBankAcctNo(chinaumsAuthInfo.getSettleAcctBankAcctNo());
                if (StringUtils.isNotBlank(chinaumsAuthInfo.getCusApplicationId())) {
                    chinaumsBankCard.setMobileNumber(chinaumsAuthInfo.getMobileNumber());
                } else {
                    chinaumsBankCard.setMobileNumber(chinaumsAuthInfo.getLegalPersonMobileNumber());
                }
                chinaumsBankCard.setChinaumsAuthInfo(chinaumsAuthInfo);
                chinaumsBankCard.setOutRequestNo(chinaumsAuthInfo.getOutRequestNo());
                chinaumsBankCard.setBankAcctType(chinaumsAuthInfo.getSettleAcctType());
                chinaumsBankCard.setBankName(chinaumsAuthInfo.getSettleAcctBankName());
                chinaumsBankCard.setSettleAcctId(chinaumsAuthInfo.getSettleAcctId());
                chinaumsBankCard.setStatus(ChinaumsBankCardStatus.SUCCEEDED);
                chinaumsBankCardRepository.save(chinaumsBankCard);
            }
        }
    }
}
