package com.qmth.boot.test.tools.excel.read;

import com.qmth.boot.test.tools.excel.model.ExcelTestEntity;
import com.qmth.boot.test.tools.excel.model.GenderEnum;
import com.qmth.boot.tools.excel.ExcelReader;
import com.qmth.boot.tools.excel.enums.ExcelType;
import com.qmth.boot.tools.excel.filter.SheetFilter;
import com.qmth.boot.tools.excel.listener.ObjectCollector;
import com.qmth.boot.tools.models.ByteArray;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class ExcelReadTest {

    @Test
    public void testObjectRead() throws Exception {
        ObjectCollector<ExcelTestEntity> listener = new ObjectCollector<>();
        ExcelReader.create(ExcelType.XLSX, ByteArray.fromResource("test.xlsx").toInputStream(), 0)
                .readObject(ExcelTestEntity.class, listener);
        List<ExcelTestEntity> list = listener.getObjectList();
        Assert.assertEquals(2, list.size());
        Assert.assertEquals(list.get(1).getId(), Long.valueOf(3));
        Assert.assertEquals(list.get(1).getName(), "李四");
        Assert.assertEquals(list.get(1).getScore(), Double.valueOf(100.6));
        Assert.assertFalse(list.get(1).isEnable());
        Assert.assertEquals(list.get(1).getGender(), GenderEnum.FEMALE);
        Assert.assertEquals(list.get(1).getTime(), DateUtils.parseDate("2023-11-30", "yyyy-MM-dd"));

        listener = new ObjectCollector<>();
        ExcelReader
                .create(ExcelType.XLSX, ByteArray.fromResource("test.xlsx").toInputStream(), SheetFilter.forIndex(1), 0)
                .readObject(ExcelTestEntity.class, listener);
        list = listener.getObjectList();
        Assert.assertEquals(2, list.size());
        Assert.assertEquals(list.get(1).getId(), Long.valueOf(3));
        Assert.assertEquals(list.get(1).getName(), "李四");
        Assert.assertEquals(list.get(1).getScore(), Double.valueOf(100.6));
        Assert.assertFalse(list.get(1).isEnable());
        Assert.assertEquals(list.get(1).getGender(), GenderEnum.FEMALE);
        Assert.assertEquals(list.get(1).getTime(), DateUtils.parseDate("2023-11-01", "yyyy-MM-dd"));
    }

    @Test
    public void testDataArrayRead() throws Exception {
        List<String[]> list = ExcelReader
                .create(ExcelType.XLS, ByteArray.fromResource("test.xls").toInputStream(), SheetFilter.first(), 1)
                .getDataArrayList();
        Assert.assertEquals(4, list.size());
        Assert.assertEquals(list.get(0)[0], "1");
        Assert.assertEquals(list.get(0)[2], "0.56");
        Assert.assertEquals(list.get(2)[1], "李四");
        Assert.assertEquals(list.get(2)[3], "否");
        Assert.assertNull(list.get(3)[4]);
        Assert.assertEquals(list.get(3)[5], "2023/01/01");

        list = ExcelReader.create(ExcelType.XLS, ByteArray.fromResource("test.xls").toInputStream(),
                SheetFilter.forName("Sheet2"), 0).getDataArrayList();
        Assert.assertEquals(3, list.size());
        Assert.assertEquals(list.get(0)[3], "男");
        Assert.assertEquals(list.get(0)[1], "0.56");
        Assert.assertEquals(list.get(1)[1], "100.6");
        Assert.assertEquals(list.get(2)[2], "是");
    }

}
