package cn.iocoder.lyzsys.module.tjt.service.report.builder;

import lombok.Data;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
public class ProjectQuarterOutputExcelBuilder extends AbstractProjectOutputExcelBuilder {

    public Workbook build(ExportData data) {
        Workbook workbook = createWorkbook();
        Sheet sheet = createSheet(workbook, "年度季度计取", "年度季度计取");
        setColumnWidths(sheet, 8, 24, 12, 22, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12);

        CellStyle titleStyle = createTitleStyle(workbook);
        CellStyle infoStyle = createInfoStyle(workbook);
        CellStyle headerStyle = createHeaderStyle(workbook);
        CellStyle cellStyle = createCellStyle(workbook);

        int rowIndex = 0;
        rowIndex = writeMergedTitleRow(sheet, rowIndex, "项目考核产值年度/季度计取表", titleStyle, 15);

        Row infoRow = sheet.createRow(rowIndex++);
        writeLabelValue(infoRow, 0, 1, "项目名称", data.getProjectName(), infoStyle);
        writeLabelValue(infoRow, 4, 5, "年度", data.getYear(), infoStyle);

        rowIndex = writeRow(sheet, rowIndex, headerStyle, (Object[]) new String[]{
                "序号", "项目名称", "归属类型", "规划内容", "往年已发放比例", "本年度发放比例",
                "未发放比例", "一季度", "二季度", "三季度", "四季度", "本年度小计",
                "项目经理/项目负责人", "专业所合计", "专业分配说明", "考核产值"
        });

        int serialNo = 1;
        for (QuarterRow rowData : data.getRows()) {
            Row row = sheet.createRow(rowIndex++);
            setText(row, 0, serialNo++, cellStyle);
            setText(row, 1, data.getProjectName(), cellStyle);
            setText(row, 2, rowData.getOwnershipType(), cellStyle);
            setText(row, 3, rowData.getPlanningContent(), cellStyle);
            setText(row, 4, percentText(rowData.getHistoricalIssuedRatio()), cellStyle);
            setText(row, 5, percentText(rowData.getCurrentYearRatio()), cellStyle);
            setText(row, 6, percentText(rowData.getPendingRatio()), cellStyle);
            setText(row, 7, text(rowData.getQuarterOneAmount()), cellStyle);
            setText(row, 8, text(rowData.getQuarterTwoAmount()), cellStyle);
            setText(row, 9, text(rowData.getQuarterThreeAmount()), cellStyle);
            setText(row, 10, text(rowData.getQuarterFourAmount()), cellStyle);
            setText(row, 11, text(rowData.getYearTotalAmount()), cellStyle);
            setText(row, 12, text(rowData.getProjectLeadAmount()), cellStyle);
            setText(row, 13, text(rowData.getOfficeAmount()), cellStyle);
            setText(row, 14, rowData.getSpecialtySummaryText(), cellStyle);
            setText(row, 15, text(rowData.getAssessmentOutputValue()), cellStyle);
        }
        return workbook;
    }

    @Data
    public static class ExportData {
        private String projectName;
        private Integer year;
        private List<QuarterRow> rows;
    }

    @Data
    public static class QuarterRow {
        private String ownershipType;
        private String planningContent;
        private BigDecimal historicalIssuedRatio;
        private BigDecimal currentYearRatio;
        private BigDecimal pendingRatio;
        private BigDecimal quarterOneAmount;
        private BigDecimal quarterTwoAmount;
        private BigDecimal quarterThreeAmount;
        private BigDecimal quarterFourAmount;
        private BigDecimal yearTotalAmount;
        private BigDecimal projectLeadAmount;
        private BigDecimal officeAmount;
        private String specialtySummaryText;
        private BigDecimal assessmentOutputValue;
    }

}
