package com.ruoyi.common.pay.yst.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.pay.yst.config.YstConfig;
import com.ruoyi.common.pay.yst.constants.YstApi;
import com.ruoyi.common.pay.yst.constants.YstTransCode;
import com.ruoyi.common.pay.yst.domain.BizParameter;
import com.ruoyi.common.pay.yst.domain.params.PersonRealNameAndBindCardApplyParam;
import com.ruoyi.common.pay.yst.service.YstAccountService;
import com.ruoyi.common.pay.yst.util.SM4Utils;
import com.ruoyi.common.pay.yst.util.YstClient;
import com.ruoyi.common.utils.SnowFlakeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wjh
 * 2024/8/1
 */
@Service
public class YstAccountServiceImpl implements YstAccountService {

    @Autowired
    private YstClient client;

    @Autowired
    private YstConfig config;

    /**
     * 个人会员实名及绑卡（申请）
     */
    @Override
    public void personRealNameAndBindCardApply(PersonRealNameAndBindCardApplyParam param) {
        try {

            // 组装参数
            BizParameter bizParameter = new BizParameter();
            // 发送请求
            bizParameter.addParam("reqTraceNum", String.valueOf(SnowFlakeUtil.newId()));
            bizParameter.addParam("signNum", param.getSignNum());
            bizParameter.addParam("memberRole", param.getMemberRole());
            bizParameter.addParam("cerType", param.getCertType());

            bizParameter.addParam("cerNum", SM4Utils.encryptEcb(config.getSecretKey(),param.getCertNum()));
            bizParameter.addParam("name",param.getName());
            bizParameter.addParam("acctNum",  SM4Utils.encryptEcb(config.getSecretKey(),param.getAcctNum()));

            bizParameter.addParam("phone",param.getPhone());
            bizParameter.addParam("bindType",param.getBindType());//6-通联通协议支付，7-收银宝快捷，8-银行卡四要素

            bizParameter.addParam("validDate",param.getValidDate());
            bizParameter.addParam("cvv2",param.getCvv2());
            bizParameter.addParam("bizOrderCode",String.valueOf(System.currentTimeMillis()));
            client.sendRequest(YstApi.TM, YstTransCode.INDIVIDUAL_MEMBER_REAL_NAME_AND_BIND_CARD_APPLY, bizParameter, String.class);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }

    }
}
