package com.yn.bftl.thirdparty.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.yn.bftl.thirdparty.common.dto.BaseSecretDTO;
import com.yn.bftl.thirdparty.common.service.ByxTradeWayService;
import com.yn.bftl.thirdparty.common.util.CryptoUtils;
import com.yn.bftl.thirdparty.modules.chinaums.util.ByxChinaumsUtil;
import com.yn.bftl.thirdparty.modules.secret.service.RandomKeyService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class ByxTradeWayServiceImpl implements ByxTradeWayService {

    @Resource
    private RandomKeyService randomKeyService;

    @Resource
    private ByxChinaumsUtil byxChinaumsUtil;


    public void setTradeWayInPassword(BaseSecretDTO param) {
        Map<String, String> tradeWayFields = new HashMap<>();
        // 支付密码验证，实际应用中，由密码控件加密支付密码
        param.setTradeWayCode("b_pass");
        tradeWayFields.put("encryptType", "2");
        tradeWayFields.put("encryptPwd", param.getEncryptPwd());
        tradeWayFields.put("plugRandomKey", param.getPlugRandomKey());
        param.setTradeWayFeilds(JSON.toJSONString(tradeWayFields));
    }

    public void setTradeWayInCert(BaseSecretDTO param, String walletId) {
        Map<String, String> tradeWayFields = new HashMap<>();
            // 交易证书签名
        param.setTradeWayCode("b_certificate");
        String plugRandKey = randomKeyService.getOneKey();
        String signSource = walletId + "," + plugRandKey;
        tradeWayFields.put("plugRandomKey", plugRandKey);
        tradeWayFields.put("certSign", CryptoUtils.sign("SHA256withRSA", signSource, byxChinaumsUtil.getTransactionPrivateKey(), "UTF-8"));
        param.setTradeWayFeilds(JSON.toJSONString(tradeWayFields));
    }

    @Override
    public void setCert(BaseSecretDTO param, String walletId) {
        // 交易证书签名
        String plugRandKey = randomKeyService.getOneKey();
        String signSource = walletId + "," + plugRandKey;
        param.setPlugRandomKey(plugRandKey);
        param.setEncryptType("2");
        param.setCertSign(CryptoUtils.sign("SHA256withRSA", signSource, byxChinaumsUtil.getTransactionPrivateKey(), "UTF-8"));
    }
}
