package ${basePackage};

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import ${basePackage}.frame.excel.DemoData;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.AESUtil;
import ${basePackage}.frame.utils.Base64Util;
import ${basePackage}.frame.utils.IDgenerator;
import ${basePackage}.frame.utils.MD5Util;
import ${basePackage}.frame.utils.ProcessUtil;
import ${basePackage}.frame.utils.RSAUtil;
import ${basePackage}.frame.utils.ZipUtil;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * UtilTest -  - 测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class UtilTest {

    @Test
    public void testGeneratePwd() {
        System.out.print("[123456]加密=");
        System.out.println(MD5Util.generatePwd("123456"));
    }

    @Test
    public void testIDgenerator() {
        for (int i = 0; i < 10; i++) {
            long l = IDgenerator.nextId();
            System.out.println(l);
        }
    }

    @Test
    public void testAESUtil() {
        // 加密
        String data = "我有一个苹果";
        String secret = "1234567890123456";
        System.out.println("加密后的Base64密文是:" + AESUtil.encrypt2Base64(data.getBytes(), secret));

        // 解密
        String encrypt2Base64 = AESUtil.encrypt2Base64(data.getBytes(), secret);
        byte[] decrypt = AESUtil.decrypt(Base64Util.decode(encrypt2Base64), secret);
        System.out.println("解密后的明文是:" + new String(decrypt));
    }

    @Test
    public void testBase64Util() {
        String s = Base64Util.encodeToString(("我搜搜").getBytes());
        System.out.println(s);

        byte[] decode = Base64Util.decode(s);
        System.out.println(new String(decode));
    }

    @Test
    public void testMD5Util() {
        String encode = MD5Util.encode("123456");
        System.out.println(encode);
    }

    @Test
    public void testProcessUtil() {
        ProcessUtil.execExe("D:\\example.exe");
        ProcessUtil.execBat("D:\\example.bat");
    }

    @Test
    public void testRSAUtil() {
        {//创建秘钥对
            RSAUtil.createKey();
        }

        {//加解密

            //加密
            String encrypt = RSAUtil.encrypt2Base64("我有一个苹果".getBytes());
            System.out.println(encrypt);

            //解密
            String decrypt = RSAUtil.decrypt2String(encrypt);
            System.out.println(decrypt);
        }


        String sign = RSAUtil.sign2Base64("我有一个苹果".getBytes());
        System.out.println(sign);

        boolean b = RSAUtil.doCheck("我有一个苹果".getBytes(), sign);
        System.out.println(b);
    }

    @Test
    public void testZipUtil() {
        //解压
        ZipUtil.unZip(new File("E:\\AAA.zip"), new File("E:\\AAA"));
        //压缩
        ZipUtil.toZip(new File("E:\\AAA"), new File("E:\\AAA.zip"));
    }

    @Test
    public void testExcel() throws IOException, TemplateNotMatchException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("数据创建开始:" + dateFormat.format(new Date()));
        // 写
        List<DemoData> visitors = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            DemoData demoData = new DemoData();
            demoData.setStr1("我是标题Str1" + i);
            demoData.setStr2("我是标题Str2" + i);
            demoData.setStr3("我是标题Str3" + i);
            demoData.setStr4("我是标题Str4" + i);
            demoData.setSint((short) 1);
            demoData.setMint(1);
            demoData.setLint(1L);
            demoData.setDbn(0.01);
            demoData.setFln(0.01f);
            demoData.setDate(new Date());
            demoData.setValid(i % 5 == 0);
            visitors.add(demoData);
        }
        System.out.println("数据创建完成:" + dateFormat.format(new Date()));
        new WExcel<>(DemoData.class).addDatas(visitors, null).toFile(new File("D:\\xx.xlsx"));
        System.out.println("数据导出完成:" + dateFormat.format(new Date()));

        System.out.println();

        // 读
        System.out.println("数据导入开始:" + dateFormat.format(new Date()));
        WExcel<DemoData> wExcel = new WExcel<>(DemoData.class).read(new File("D:\\xx.xlsx"), (demoData, index) -> {
            if (index % 50 == 0) {
                return Arrays.asList("测试,第" + index + "条有错误");
            }
            return null;
        });
        System.out.println("数据导入完成:" + dateFormat.format(new Date()));
        System.out.println();
        System.out.println("错误导出开始:" + dateFormat.format(new Date()));
        wExcel.toFile(new File("D:\\xx-err.xlsx"));
        System.out.println("错误导出结束:" + dateFormat.format(new Date()));
        System.out.println();
    }
}
