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 SpecialtyPersonOutputExcelBuilder extends AbstractProjectOutputExcelBuilder {

    public Workbook build(ExportData data) {
        Workbook workbook = createWorkbook();
        Sheet sheet = createSheet(workbook, "专业内人员计取", "专业内人员计取");
        setColumnWidths(sheet, 8, 20, 14, 16, 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, 9);

        Row infoRowOne = sheet.createRow(rowIndex++);
        writeLabelValue(infoRowOne, 0, 1, "项目名称", data.getProjectName(), infoStyle);
        writeLabelValue(infoRowOne, 4, 5, "规划内容", data.getPlanningContent(), infoStyle);

        Row infoRowTwo = sheet.createRow(rowIndex++);
        writeLabelValue(infoRowTwo, 0, 1, "专业", data.getSpecialtyName(), infoStyle);
        writeLabelValue(infoRowTwo, 4, 5, "年度", data.getYear(), infoStyle);

        rowIndex = writeRow(sheet, rowIndex, headerStyle, (Object[]) new String[]{
                "序号", "角色", "员工姓名", "角色比例", "人员比例",
                "一季度", "二季度", "三季度", "四季度", "年度合计"
        });

        int serialNo = 1;
        for (PersonQuarterRow rowData : data.getRows()) {
            Row row = sheet.createRow(rowIndex++);
            setText(row, 0, serialNo++, cellStyle);
            setText(row, 1, rowData.getRoleName(), cellStyle);
            setText(row, 2, rowData.getEmployeeName(), cellStyle);
            setText(row, 3, percentText(rowData.getRoleRatio()), cellStyle);
            setText(row, 4, percentText(rowData.getPersonRatio()), cellStyle);
            setText(row, 5, text(rowData.getQuarterOneAmount()), cellStyle);
            setText(row, 6, text(rowData.getQuarterTwoAmount()), cellStyle);
            setText(row, 7, text(rowData.getQuarterThreeAmount()), cellStyle);
            setText(row, 8, text(rowData.getQuarterFourAmount()), cellStyle);
            setText(row, 9, text(rowData.getYearTotalAmount()), cellStyle);
        }
        return workbook;
    }

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

    @Data
    public static class PersonQuarterRow {
        private String roleName;
        private String employeeName;
        private BigDecimal roleRatio;
        private BigDecimal personRatio;
        private BigDecimal quarterOneAmount;
        private BigDecimal quarterTwoAmount;
        private BigDecimal quarterThreeAmount;
        private BigDecimal quarterFourAmount;
        private BigDecimal yearTotalAmount;
    }

}
