package jnpf.util.attendance;

import com.alibaba.nacos.common.utils.CollectionUtils;
import jnpf.model.attendance.model.WebExportNewDetailsModel;
import jnpf.model.attendance.model.WebExportNewModel;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

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

public class UnionQualityExcelUtil {
    /**
     * 生成sheet
     *
     * @return
     */
    public static void generateSheet(Workbook workbook, List<WebExportNewModel> exportNewModelList) {
        Sheet sheet = workbook.createSheet("考勤月度统计报表");
        sheet.setDisplayGridlines(true);
        sheet.getPrintSetup().setLandscape(true);
        CellStyle headStyle = workbook.createCellStyle();
        Font headFont = workbook.createFont();
        headStyle = setCellStyle(headStyle, headFont, (short) 12, true, true, HorizontalAlignment.CENTER);
        //创建表头
        String[] heads = {"用户名", "名称", "应出勤天数", "实际出勤天数", "有效出勤天数", "应出勤工时", "实际出勤工时", "有效出勤工时", "计薪假抵扣时长",
                "不计薪假扣时长", "请假次数", "请假时长", "未抵扣请假时长", "加班次数", "加班时长", "外勤打卡次数", "缺勤次数", "缺卡次数", "补卡次数", "迟到次数"
                , "迟到累计分钟", "早退次数", "早退累计分钟"};
        Row headRow = sheet.createRow(0);
        headRow.setHeightInPoints(25);
        for (int cn = 0; cn < heads.length; cn++) {
            Cell cell = headRow.createCell(cn);
            cell.setCellValue(heads[cn]);
            cell.setCellStyle(headStyle);
        }
        Font contentFont = workbook.createFont();
        CellStyle contentStyle = workbook.createCellStyle();
        contentStyle = setCellStyle(contentStyle, contentFont, (short) 10, true, true, HorizontalAlignment.CENTER);
        CellStyle contentDetailsStyle = workbook.createCellStyle();
        contentDetailsStyle = setCellStyle(contentDetailsStyle, contentFont, (short) 9, false, true, HorizontalAlignment.CENTER);
        if (CollectionUtils.isNotEmpty(exportNewModelList)) {
            //外层用户循环
            for (int index = 0; index < exportNewModelList.size(); index++) {
                WebExportNewModel newModel = exportNewModelList.get(index);
                String[] a = createString(newModel);
                int temp = sheet.getLastRowNum() + 1;
                Row row = sheet.createRow(temp);
                row.setHeightInPoints(25);
                for (int i = 0; i < a.length; i++) {
                    Cell cell = row.createCell(i);
                    cell.setCellValue(a[i]);
                    cell.setCellStyle(contentStyle);
                }
                if (CollectionUtils.isNotEmpty(newModel.getDetails())) {
                    for (int detailIndex = 0; detailIndex < newModel.getDetails().size(); detailIndex++) {
                        String[] details = createString1(newModel.getDetails().get(detailIndex));
                        int detailsTemp = sheet.getLastRowNum() + 1;
                        Row detailsRow = sheet.createRow(detailsTemp);
                        detailsRow.setHeightInPoints(25);
                        for (int i = 0; i < details.length; i++) {
                            Cell cell = detailsRow.createCell(i);
                            cell.setCellValue(details[i]);
                            cell.setCellStyle(contentDetailsStyle);
                        }
                    }
                    sheet.addMergedRegion(new CellRangeAddress(temp, temp + newModel.getDetails().size(), 0, 0));
                }
            }
        }
        sheet.setColumnWidth(0, 15 * 256);
        sheet.setColumnWidth(1, 15 * 256);
        sheet.setColumnWidth(2, 15 * 256);
        sheet.setColumnWidth(3, 17 * 256);
        sheet.setColumnWidth(4, 17 * 256);
        sheet.setColumnWidth(5, 17 * 256);
        sheet.setColumnWidth(6, 17 * 256);
        sheet.setColumnWidth(7, 17 * 256);
        sheet.setColumnWidth(8, 18 * 256);
        sheet.setColumnWidth(9, 18 * 256);
        sheet.setColumnWidth(10, 18 * 256);
        sheet.setColumnWidth(11, 18 * 256);
        sheet.setColumnWidth(12, 18 * 256);
        sheet.setColumnWidth(13, 17 * 256);
        sheet.setColumnWidth(14, 17 * 256);
        sheet.setColumnWidth(15, 17 * 256);
        sheet.setColumnWidth(16, 17 * 256);
        sheet.setColumnWidth(17, 17 * 256);
        sheet.setColumnWidth(18, 17 * 256);
        sheet.setColumnWidth(19, 17 * 256);
        sheet.setColumnWidth(20, 17 * 256);
        sheet.setColumnWidth(21, 17 * 256);
        sheet.setColumnWidth(22, 17 * 256);
        sheet.setColumnWidth(23, 17 * 256);
    }

    /**
     * 设置单元格样式
     *
     * @param style
     * @param font
     * @param fontSize
     * @param bold
     * @param isBoder
     * @param horizontalAlignment
     * @return
     */
    public static CellStyle setCellStyle(CellStyle style, Font font, short fontSize, Boolean bold, Boolean isBoder, HorizontalAlignment horizontalAlignment) {
        short stringFormat = (short) BuiltinFormats.getBuiltinFormat("TEXT");
        // 将字体大小设置为9px
        font.setFontHeightInPoints(fontSize);
        // 将字体设置为加粗
        font.setBold(bold);
        //字体样式
        font.setFontName("宋体");
        //字体大小
        style.setFont(font);
        // 设置单元格内容是否自动换行
        style.setWrapText(true);
        if (isBoder) {
            //下边框
            style.setBorderBottom(BorderStyle.THIN);
            //左边框
            style.setBorderLeft(BorderStyle.THIN);
            //上边框
            style.setBorderTop(BorderStyle.THIN);
            //右边框
            style.setBorderRight(BorderStyle.THIN);
        }
        //水平居中
        style.setAlignment(horizontalAlignment);
        //上下居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置自动换行
        style.setWrapText(true);
        style.setDataFormat(stringFormat);
        return style;
    }

    private static String[] createString(WebExportNewModel exportDTO) {
        String[] a = {exportDTO.getUserName(), exportDTO.getName(), exportDTO.getShouldAttendDays().toString(), compareNumber(exportDTO.getActualAttendDays()),
                compareNumber(exportDTO.getEffectiveAttendDays()), compareNumber(exportDTO.getShouldAttendHours()), compareNumber(exportDTO.getActualAttendHours()),
                compareNumber(exportDTO.getEffectiveAttendHours()), compareNumber(exportDTO.getPaidLeaveHours()), compareNumber(exportDTO.getUnpaidLeaveHours()),
                exportDTO.getLeaveTimes().toString(), compareNumber(exportDTO.getLeaveHours()), compareNumber(exportDTO.getUnDeductedLeaveHours()),
                exportDTO.getOvertimeTimes().toString(), compareNumber(exportDTO.getOvertimeHours()), exportDTO.getOutworkClockTimes().toString(), exportDTO.getAbsenceTimes().toString(),
                exportDTO.getAbsenceCardTimes().toString(), exportDTO.getMakeUpCardTimes().toString(), exportDTO.getLateTimes().toString(), exportDTO.getLateAccumulatedMinutes().toString(),
                exportDTO.getEarlyLeaveTimes().toString(), exportDTO.getEarlyLeaveAccumulatedMinutes().toString()
        };
        return a;
    }

    private static String[] createString1(WebExportNewDetailsModel exportDTO) {
        String[] a = {exportDTO.getUserName(), exportDTO.getName(), exportDTO.getShouldAttendDays().toString(), compareNumber(exportDTO.getActualAttendDays()),
                compareNumber(exportDTO.getEffectiveAttendDays()), compareNumber(exportDTO.getShouldAttendHours()), compareNumber(exportDTO.getActualAttendHours()),
                compareNumber(exportDTO.getEffectiveAttendHours()), compareNumber(exportDTO.getPaidLeaveHours()), compareNumber(exportDTO.getUnpaidLeaveHours()),
                exportDTO.getLeaveTimes().toString(), compareNumber(exportDTO.getLeaveHours()), compareNumber(exportDTO.getUnDeductedLeaveHours()),
                exportDTO.getOvertimeTimes().toString(), compareNumber(exportDTO.getOvertimeHours()), exportDTO.getOutworkClockTimes().toString(), exportDTO.getAbsenceTimes().toString(),
                exportDTO.getAbsenceCardTimes().toString(), exportDTO.getMakeUpCardTimes().toString(), exportDTO.getLateTimes().toString(), exportDTO.getLateAccumulatedMinutes().toString(),
                exportDTO.getEarlyLeaveTimes().toString(), exportDTO.getEarlyLeaveAccumulatedMinutes().toString()
        };
        return a;
    }

    public static String compareNumber(BigDecimal number) {
        if (!"".equals(number) && number != null) {
            if (new BigDecimal(number.intValue()).compareTo(number) == 0) {
                //整数
                return String.valueOf(number.intValue());
            } else {
                //小数
                return String.valueOf(number.setScale(2, RoundingMode.HALF_UP));
            }
        }
        return "";
    }
}
