package ${domain};

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 ${domain}.frame.excel.DemoData;
import ${domain}.frame.excel.WExcel;
import ${domain}.frame.excel.exception.TemplateNotMatchException;

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;

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

    @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();
    }
}
