package cn.ysk.cashier.utils;

import cn.hutool.core.util.IdUtil;
import cn.ysk.cashier.dto.shop.ExportTableStsDataDto;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.ysk.cashier.utils.FileUtil.SYS_TEM_DIR;

/**
 * @author yijiegong
 */
public class FileUtils {
    /**
     * 该方法用于导出台桌数据统计，不通用
     */
    public static void downloadTableDataStsToExcel(List<ExportTableStsDataDto> dataList, List<String> keyList, HttpServletResponse response) throws IOException {
//        String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();

        for (ExportTableStsDataDto exportTableStsDataDto : dataList) {

            Sheet sheet = workbook.createSheet(exportTableStsDataDto.getDateStr());

            for (List<Integer> mergeDatum : exportTableStsDataDto.getMergeCells()) {
                sheet.addMergedRegion(new CellRangeAddress(mergeDatum.get(0), mergeDatum.get(1), mergeDatum.get(2), mergeDatum.get(3)));
            }

            Row row0 = sheet.createRow(0);
            for (int i = 0; i < keyList.size(); i++) {
                Cell cell = row0.createCell(i);
                cell.setCellValue(keyList.get(i));
            }
            for (int i = 0; i < exportTableStsDataDto.getData().size(); i++) {
                Map<String, Object> map = exportTableStsDataDto.getData().get(i);
                Row row = sheet.createRow(i + 1);
                for (int j = 0; j < keyList.size(); j++) {
                    Cell cell = row.createCell(j);
                    String value = map.get(keyList.get(j)) == null ? "" : map.get(keyList.get(j)).toString();
                    cell.setCellValue(value);

                    if (j == 10 && !"0".equals(value)) {
                        setCellBackground(cell, IndexedColors.YELLOW, workbook);
                    }

                    if (j == 11 && !"0.00".equals(value)) {
                        setCellBackground(cell, IndexedColors.YELLOW, workbook);
                    }
                }
            }
        }

        // response为HttpServletResponse对象
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");

        try (ServletOutputStream out = response.getOutputStream()) {
//
//            workbook.write(outputStream);
            workbook.write(out);
            out.flush();
        } catch (IOException e) {
            // 更详细的错误处理
            e.printStackTrace();
            // 可以考虑返回一个错误响应给客户端
        }
    }

    private static void setCellBackground(Cell cell, IndexedColors color, Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(color.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell.setCellStyle(style);
    }
}
