package com.muchinfo.mtp3century;

import com.baomidou.mybatisplus.annotation.TableName;
import com.muchinfo.mtp3common.utils.EncryptionUtils;
import com.muchinfo.mtp3interface.mapper.IRoleuserMapper;
import com.muchinfo.mtp3pojos.entity.Roleuser;
import com.muchinfo.mtp3pojos.entity.Taaccount;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shiro.codec.Base64;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

@SpringBootTest
class Mtp3CenturyApplicationTests {

    public static final String KEY_ALGORITHM = "RSA";

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    @Resource
    private IRoleuserMapper iRoleuserMapper;

    //@Test
    public void getGeneratorID() throws SQLException {
        SqlSession session = sqlSessionFactory.openSession();
        ResultSet resultSet = session.getConnection().createStatement()
                .executeQuery("SELECT SEQ_" + Taaccount.class.getAnnotation(TableName.class).value() + ".NEXTVAL AS id FROM dual");
        if (resultSet.next()) { // 移动游标到第一行
            Long id = resultSet.getLong("id"); // 获取id
            System.out.println(id.toString()); // 输出id
        } else {
            throw new SQLException("No results returned from the query.");
        }
    }

    //@Test
    void contextLoads() {
        String json = "a4c33e57ed77661f1f71c36ffdc4a658aab2c789336996577c7540ba74d0d51b6df220ba7812766cd20d9f31b33f4cb28d73c03689877c4c015551354f66b4998874d77a86b80b79e5bbf7d94e14b5f5c34d7168e3c00def5a5ec3b44860788c7e821a6a62622c77070c1910a5ea141b68dee0d9f0911417921d0d613e9fb2b17e5d8ab750233df1ace6b2d0287aca81d220236c465c4bf53e686e1589062f230b460ac12c1df92a42d6ce996e89c207";
        String key = "50Dv8Csrfl4IWRnx";
        System.out.println("key message ="+ Arrays.toString(key.getBytes()) +"  key lenth :"+key.getBytes().length);
        String string = EncryptionUtils.decryptForAES(json, key);
        System.out.println("解密结果"+string);
    }



    //@Test
    void Rsatest() {
        try {
            String string = decryptByPrivateKey("MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL68qZkKchVojsojVKpjYuEVerAb\n" +
                    "2v4XXfkzj8RoIsrO1laiTOXrtKMKOYKCyTlqakNr+E/G6BoAmjQpk9FoMapWrnhwubf52i3xrUdT\n" +
                    "2seo8JLky9BFLuCTxx7+3y4djjT8rEuiQ6lcqbMzWtzcE2iHt6UCdEgwIzdby+FdmSF3AgMBAAEC\n" +
                    "gYAzyv8ys4dvEMlVUmq3Ke27uR1rnOTGgoneFYZAixaHg8roE4JWHL8cVrZvHhyOHrtkO3leEGEO\n" +
                    "ToORDok5H9X8NykuaDAOTjdrGCIky5xkREny5pTDnYsn+LIEd3EiFnmNQVFgcJ7mUvbKAXROoQzZ\n" +
                    "O5BqouSsW5brd6FXW1LvwQJBAOLEpyANEZCVl81s8Yx23kVsbpZfD38HZIqCssrlfGp/riRUFE6x\n" +
                    "l8KmElryUKISn8H35IhY4QBQ9um7feyRkQkCQQDXUvrlyQoY27pXYzEK3msTj1ECEpy4XLArpfAf\n" +
                    "o++ehRCsDsS7yXA600enPC6z8KPTP1igGBOTb2jq8jGosj5/AkB1FpMwom7SV9tk736dR3cnI4DK\n" +
                    "VdUbgwav2iJVTz75gmm5SkyeAWbZ04Qgv4NQ3ArvTX8Lw9CRqDyFnRC9CMB5AkEAkF3OYmocHvaT\n" +
                    "RSv4IbVmTUL1qVIZSXK7m0gyasRbUAkfWHy+Yrl4SbiGfhXXqkVZUbVTEmOSHHPCAGRJ7zUyuQJA\n" +
                    "YQWS/0qacIB46IWd7yA92PymPLM+hGyM2wFe9hnMdQK4zMpDbUxRwjxHQZ8vi57gjJ8HTbcAfibX\n" +
                    "t845/ieHqA==", "BIllWOykUXUXBpy6L5rT+Z63qKuqHEexrp06jRQ3HIVyVhR9gnB25i2boHhiGITD7o+Rik64QuA0lxrSP5lY2upt2lIK+G58p8VIf/Ek8RGJSJGG9miWVjBTDl3LEL+6u8aqiXcreW/pHkZj1EuIisLB2WQ3PC6k95gpDRACoIY=");
            //密文
            System.out.println("aes key = "+string);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String decryptByPrivateKey(String privateKeyString, String text) {
        try {
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decode(privateKeyString));
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] result = cipher.doFinal(Base64.decode(text));
            return new String(result);
        } catch (Exception e) {
            return null;
        }
    }

    //@Test
   /* @Transactional(rollbackFor = Exception.class)*/
    public void trantest() throws Exception{

        Roleuser ur = new Roleuser();
        ur.setRoleid(12L);
        ur.setManagerid(139L);
        ur.setRoletype(3);
        iRoleuserMapper.insert(ur);

       /* int i = 1/0;*/
    }
}
