
package com.crm.ucard.service.impl;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.pojo.table.ucard.SysUcardConfigTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.service.RedisService;
import com.crm.rely.backend.util.RSAUtil;
import com.crm.ucard.configuration.CryptoKeyHolder;
import com.crm.ucard.dao.repository.SysUcardConfigRepository;
import com.crm.ucard.service.SysUcardConfigService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUcardConfigServiceImpl implements SysUcardConfigService {

    @Autowired
    private SysUcardConfigRepository sysUcardConfigRepository;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CryptoKeyHolder cryptoKeyHolder;

    @Override
    public SysUcardConfigTable getByCode(String code) throws Exception {
        SysUcardConfigTable sysConfigTable = redisService.getEntity(code, SysUcardConfigTable.class);

        if (sysConfigTable == null) {
            sysConfigTable = sysUcardConfigRepository.getByCode(code);
            if (sysConfigTable == null) {
                throw ServiceException.exception(Constants.INFO_NOT_FOUND);
            } else {
                redisService.save(sysConfigTable.getCode(), sysConfigTable);
            }

        }
        String decrypt = RSAUtil.decryptByPrivateKey(sysConfigTable.getValue(), cryptoKeyHolder.getPrivateKey());
        sysConfigTable.setValue(decrypt);
        return sysConfigTable;
    }

    @SneakyThrows
    public static void main(String[] args) {
        String s = "{\n" +
                "\"apiUrl\":\"https://sandbox-api-merchant.wasabicard.com\",\n" +
                "\"apiKey\":\"f9ddaac1-bec6-42d8-8cc9-876d41b4ca1f-089a2225-d5e2-4733-af8c-d8f45536a26f\",\n" +
                "\"privateKey\":\"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIiyenvvlFBKCgVqEt01D3A+8aEYE5tCm4O+pVP2Flci5Enp/rWiXpTeEf7nwKVbUDwc0DX9fnV81hWas6p4mAGvm5QUTBoZsF1wqXp/zc4Zd0sijCUEwJEWBJ9bToC30KBraUGBOasgfOXmEWk3BZ/jGK7q0JHhYPguZH9mZv2lAgMBAAECgYBxqm885R+fj6E7lVNwSzG2NTC4souDG+SyrPFOmp9C/4lyiLc6k3QwN2QhYNYI6aATDCZ57nqb+R0PQ99HzhUef4fLyCYLAhybmeMD7Y7EN3o+2cRNLFsKz4hw9GtqmDnT4ra8dcLk6oqjSabDPPPENw9E72cDEzqddAc/JQj6NQJBANeZa6Rx5LGYtVLhf7i8c7La4kbtuU3uQ/nbq0YaIxl421MUMGnUZU46dn9PjiDTSy7s8AKq4jZB23NlqeqjmT8CQQCiUAVAZuYOsVQZyQX6l+aA1MyK5d0Drd/nsfMWa2Ts0zI5akVx1p16W9AVPPpQQohKMZSMpWUiSfwXuDoccSwbAkEAya1ZVQ25SSw5Sdto921JqWA5VPIhajXc3ohPMhdXCAfW2mNDWmv1i/UCTdskb65XYQEBDu29B9YZK1xJUxVQTwJAZtuk6FIM+kSf0hNVCZ/XP88aq+7M53b5TD+1l4CJHZcc3cudpG1nsFGchMj2q4PuEG5o/9QUSe1upq1ljf6d/wJBAK2RodHBq+IrsUHDZzE1jFFn+jKWQZGz/WEKpeFnWFcaCNjtBR/SK3vZ7U1fyoUSIWK7LEdey8qVlheG78/OhtY=\",\n" +
                "\"publicKey\":\"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCIsnp775RQSgoFahLdNQ9wPvGhGBObQpuDvqVT9hZXIuRJ6f61ol6U3hH+58ClW1A8HNA1/X51fNYVmrOqeJgBr5uUFEwaGbBdcKl6f83OGXdLIowlBMCRFgSfW06At9Cga2lBgTmrIHzl5hFpNwWf4xiu6tCR4WD4LmR/Zmb9pQIDAQAB\",\n" +
                "\"merchantTag\":\"CWG\",\n" +
                "\"wsbPublicKey\":\"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyyPUFA1Zu2GFS2DdPL6xP5bGmPG9dFMSXpDy3GykW/lL4VR24UsjKs5h6g8OfSOuOze3yC4jWE/IBW5/L9xM9kGvFQK/S1zr44hMnDn+7S5MSi+jnZDRIrCRQJTqkwfpy39h2cQ4xlHKfbEjkkeo5ZQ0VFgRHYPr7b10l+3j8gwIDAQAB\",\n" +
                "\"regionUrl\":\"/merchant/core/mcb/common/region\",\n" +
                "\"cityUrl\":\"/merchant/core/mcb/common/city\",\n" +
                "\"cityV2Url\":\"/merchant/core/mcb/common/v2/city\",\n" +
                "\"mobileAreaUrl\":\"/merchant/core/mcb/common/mobileAreaCode\",\n" +
                "\"fileUploadUrl\":\"/merchant/core/mcb/common/file/upload\",\n" +
                "\"workSubmitUrl\":\"/merchant/core/mcb/work/submit\",\n" +
                "\"workListUrl\":\"/merchant/core/mcb/work/list\",\n" +
                "\"accountInfoUrl\":\"/merchant/core/mcb/account/info\",\n" +
                "\"walletDepositUrl\":\"/merchant/core/mcb/account/walletDeposit\",\n" +
                "\"walletDepositTransactionUrl\":\"/merchant/core/mcb/account/walletDepositTransaction\",\n" +
                "\"cardTypesUrl\":\"/merchant/core/mcb/card/v2/cardTypes\",\n" +
                "\"openCardUrl\":\"/merchant/core/mcb/card/openCard\",\n" +
                "\"activeCardUrl\":\"/merchant/core/mcb/card/physicalCard/activeCard\",\n" +
                "\"cardInfoUrl\":\"/merchant/core/mcb/card/info\",\n" +
                "\"cardSensitiveUrl\":\"/merchant/core/mcb/card/sensitive\",\n" +
                "\"cardBalanceInfoUrl\":\"/merchant/core/mcb/card/balanceInfo\",\n" +
                "\"cardFreezeV2Url\":\"/merchant/core/mcb/card/v2/freeze\",\n" +
                "\"cardUnfreezeV2Url\":\"/merchant/core/mcb/card/v2/unfreeze\",\n" +
                "\"cardDepositUrl\":\"/merchant/core/mcb/card/deposit\",\n" +
                "\"updatePinUrl\":\"/merchant/core/mcb/card/physicalCard/updatePin\",\n" +
                "\"cardTransactionUrl\":\"/merchant/core/mcb/card/transaction\",\n" +
                "\"cardAuthTransactionUrl\":\"/merchant/core/mcb/card/authTransaction\",\n" +
                "\"third3dsTransactionUrl\":\"/merchant/core/mcb/card/third3dsTransaction\",\n" +
                "\"holderOccupationsUrl\":\"/merchant/core/mcb/card/holder/occupations\",\n" +
                "\"holderCreateV2Url\":\"/merchant/core/mcb/card/holder/v2/create\",\n" +
                "\"holderUpdateV2Url\":\"/merchant/core/mcb/card/holder/v2/update\",\n" +
                "\"holderQueryUrl\":\"/merchant/core/mcb/card/holder/query\",\n" +
                "\"cardWithdrawUrl\":\"/merchant/core/mcb/card/withdraw\",\n" +
                "\"availableCurrenciesUrl\":\"/merchant/core/mcb/gt/template/availableCurrencies\",\n" +
                "\"availableFieldParamsUrl\":\"/merchant/core/mcb/gt/template/availableFieldParams\",\n" +
                "\"statesUrl\":\"/merchant/core/mcb/gt/template/states\",\n" +
                "\"availableBankCitiesUrl\":\"/merchant/core/mcb/gt/template/availableBankCities\",\n" +
                "\"exchangeRateUrl\":\"/merchant/core/mcb/gt/transaction/exchangeRate\",\n" +
                "\"transactionTransferUrl\":\"/merchant/core/mcb/gt/transaction/transfer\",\n" +
                "\"transactionListUrl\":\"/merchant/core/mcb/gt/transaction/list\",\n" +
                "\"transactionDetailUrl\":\"/merchant/core/mcb/gt/transaction/detail\",\n" +
                "\"submitRfiUrl\":\"/merchant/core/mcb/gt/transaction/submitRfi\"\n" +
                "}";
        String p = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIyrDNkrcE8DjgtGf6mGeOdyPSzkPwe5CKb57kZfDdH2apEtKsIYxYwogpwSzZpfg6UKFZc9fu8eDbASXiFylVkCAwEAAQ==";
        System.out.println(RSAUtil.encryptByPublicKey(s, p));
    }


}
