package com.dayou.report.html;

import com.dayou.report.calculation.cityPrice.annotation.TableHead;
import org.apache.poi.ss.formula.functions.T;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

import static com.dayou.report.html.TableHTML.*;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2023/9/15
 * created with IntelliJ IDEA.
 */
@Component
public class DataToHtmlConvertor {

    public String toHtml(List<? extends DataToHtmlBO> convertibles, String tableNumber){
        Document table = Jsoup.parse(TABLE);
        Elements select = table.select(".tableHead");
        List<Field> fields = Arrays.asList( convertibles.get(0).getClass().getDeclaredFields());
        fields.stream().forEach(f->{
            String name = f.getAnnotation(TableHead.class).name();
            select.append(TD_HEAD.replace("content",name));
        });
        Elements body = table.select(".tbody");
        convertibles.stream().forEach(x->{
            body.append(x.dataToHtml());
        });
        String html = table.body().html();
        html = html.replace("tableNumber",tableNumber);
        return html;
    }
}
