package com.zcxk.rmcp.web.excel.download.adapter;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.zcxk.rmcp.web.excel.download.AbstractDownloadExcelAdapter;
import com.zcxk.rmcp.web.excel.model.InstallPlanInfoData;

import java.io.InputStream;

/**
 * @author linqingwei
 * @date 2021-07-30 10:55
 */
public class InstallPlanExcelFillAdapter extends AbstractDownloadExcelAdapter {
    @Override
    public AbstractDownloadExcelAdapter fillData() {
        data.setTemplatePath("excel/installPlanTemplate.xlsx");

        InputStream in = this.getClass().getClassLoader().getResourceAsStream(data.getTemplatePath());
        ExcelWriter excelWriter = EasyExcel.write(data.getFilePath()).withTemplate(in).build();

        //填充设备数据
        WriteSheet installPalnSheet = EasyExcel.writerSheet("安装计划模板").build();
        WriteSheet deviceTypeSheet = EasyExcel.writerSheet("设备型号").build();
        WriteSheet orgSheet = EasyExcel.writerSheet("公司").build();
        WriteSheet communitySheet = EasyExcel.writerSheet("小区").build();

        excelWriter.fill(data.getProductDataList(), deviceTypeSheet);
        excelWriter.fill(data.getOrgDataList(), orgSheet);
        excelWriter.fill(data.getCommunityDataList(), communitySheet);

        if (data.getInstallPlanInfoData() == null) {
            data.setInstallPlanInfoData(new InstallPlanInfoData());
        }

        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        excelWriter.fill(data.getInstallPlanInfoData(), fillConfig, installPalnSheet);
        excelWriter.fill(data.getInstallPlanExcelDataList(), fillConfig, installPalnSheet);

        // 千万别忘记关闭流
        excelWriter.finish();
        return this;
    }
}
