package com.liangjian.dataplatform.member.service.impl;

import com.liangjian.dataplatform.member.service.ISecretEncodeDecodeService;
import org.springframework.stereotype.Service;

import java.sql.*;

/**
 * @Author: 黄子奇
 * @Date: 2019/12/6
 */
@Service
public class SecretEncodeDecodeService implements ISecretEncodeDecodeService {

    private String url = "jdbc:sqlserver://192.168.50.32:1433;databaseName=Mall";
    private String username = "sa";
    private String password = "saljhy!@#";


    @Override
    public String encode(String str) {
        String secret = null;
        String encodeSql = String.format("select dbo.DesEncryptFixKey('123456','123456e10adc3949ba59abbe56e057f20f883e','%s')",str);
        try(Connection conn = DriverManager.getConnection(url,username,password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(encodeSql)) {

            rs.next();
            secret = rs.getString(1);
            return secret;
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public String decode(String secret) {
        String str = null;
        String encodeSql = String.format("select dbo.DesDecryptFixKey('123456','123456e10adc3949ba59abbe56e057f20f883e','%s')",secret);
        try(Connection conn = DriverManager.getConnection(url,username,password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(encodeSql)) {

            rs.next();
            str = rs.getString(1);
            return str;
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
}
