package com.saye.hospitalgd.commons.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author thuang
 * @version 1.0
 * @description: 退款统计报表Excel导出工具类
 * @date 2024/12/19 10:00
 */
public class RefundStatisticsExportXLSX {

    /**
     * 导出退款统计报表
     * @param dataList 统计数据列表
     * @param summaryData 汇总数据
     * @param filePath 文件路径
     * @throws IOException
     */
    public void exportRefundStatistics(List<HashMap<Object, Object>> dataList, 
                                     HashMap<Object, Object> summaryData, 
                                     String filePath) throws IOException {
        
        Workbook workbook = new XSSFWorkbook();
        
        // 创建样式
        CellStyle titleStyle = createTitleStyle(workbook);
        CellStyle headerStyle = createHeaderStyle(workbook);
        CellStyle dataStyle = createDataStyle(workbook);
        CellStyle summaryStyle = createSummaryStyle(workbook);
        
        // 创建汇总数据表
        createSummarySheet(workbook, summaryData, titleStyle, summaryStyle);
        
        // 创建详细数据表
        createDataSheet(workbook, dataList, titleStyle, headerStyle, dataStyle);
        
        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            workbook.write(fileOut);
        }
        
        workbook.close();
    }
    
    /**
     * 创建汇总数据表
     */
    private void createSummarySheet(Workbook workbook, HashMap<Object, Object> summaryData, 
                                  CellStyle titleStyle, CellStyle summaryStyle) {
        Sheet sheet = workbook.createSheet("汇总统计");
        
        // 标题
        Row titleRow = sheet.createRow(0);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue("退款数据统计汇总");
        titleCell.setCellStyle(titleStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        
        // 汇总数据
        int rowIndex = 2;
        String[] labels = {"退款总笔数", "退款总金额(元)", "平均退款金额(元)", "最大退款金额(元)", 
                          "最小退款金额(元)", "涉及患者数", "操作员数", "支付方式数"};
        String[] keys = {"total_refund_count", "total_refund_amount", "avg_refund_amount", 
                        "max_refund_amount", "min_refund_amount", "unique_patients", 
                        "unique_operators", "pay_type_count"};
        
        for (int i = 0; i < labels.length; i++) {
            Row row = sheet.createRow(rowIndex++);
            
            Cell labelCell = row.createCell(0);
            labelCell.setCellValue(labels[i]);
            labelCell.setCellStyle(summaryStyle);
            
            Cell valueCell = row.createCell(1);
            Object value = summaryData.get(keys[i]);
            if (value != null) {
                if (keys[i].contains("amount")) {
                    valueCell.setCellValue(Double.parseDouble(value.toString()));
                } else {
                    valueCell.setCellValue(value.toString());
                }
            } else {
                valueCell.setCellValue(0);
            }
            valueCell.setCellStyle(summaryStyle);
        }
        
        // 设置列宽
        sheet.setColumnWidth(0, 3000);
        sheet.setColumnWidth(1, 3000);
    }
    
    /**
     * 创建详细数据表
     */
    private void createDataSheet(Workbook workbook, List<HashMap<Object, Object>> dataList, 
                               CellStyle titleStyle, CellStyle headerStyle, CellStyle dataStyle) {
        Sheet sheet = workbook.createSheet("详细统计");
        
        // 标题
        Row titleRow = sheet.createRow(0);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue("退款数据详细统计");
        titleCell.setCellStyle(titleStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8));
        
        // 表头
        Row headerRow = sheet.createRow(2);
        String[] headers = {"统计维度", "维度值", "退款笔数", "退款总金额(元)", "平均退款金额(元)", 
                           "最大退款金额(元)", "最小退款金额(元)", "涉及患者数", "操作员数"};
        
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
        }
        
        // 数据行
        int rowIndex = 3;
        for (HashMap<Object, Object> data : dataList) {
            Row row = sheet.createRow(rowIndex++);
            
            String[] keys = {"dimension_name", "dimension_value", "refund_count", "total_refund_amount", 
                           "avg_refund_amount", "max_refund_amount", "min_refund_amount", 
                           "unique_patients", "unique_operators"};
            
            for (int i = 0; i < keys.length; i++) {
                Cell cell = row.createCell(i);
                Object value = data.get(keys[i]);
                
                if (value != null) {
                    if (keys[i].contains("amount")) {
                        cell.setCellValue(Double.parseDouble(value.toString()));
                    } else if (keys[i].contains("count") || keys[i].contains("patients") || keys[i].contains("operators")) {
                        cell.setCellValue(Integer.parseInt(value.toString()));
                    } else {
                        cell.setCellValue(value.toString());
                    }
                } else {
                    cell.setCellValue("");
                }
                cell.setCellStyle(dataStyle);
            }
        }
        
        // 设置列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.setColumnWidth(i, 2000);
        }
    }
    
    /**
     * 创建标题样式
     */
    private CellStyle createTitleStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 16);
        style.setFont(font);
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        return style;
    }
    
    /**
     * 创建表头样式
     */
    private CellStyle createHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        return style;
    }
    
    /**
     * 创建数据样式
     */
    private CellStyle createDataStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setBorderRight(CellStyle.BORDER_THIN);
        return style;
    }
    
    /**
     * 创建汇总样式
     */
    private CellStyle createSummaryStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        style.setAlignment(CellStyle.ALIGN_LEFT);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        return style;
    }
}
