package thyyxxk.webserver.service.utilities;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

@Service
public class CreateExcelFileService {
    private final WxUploadFileService wxUploadFileService;

    @Autowired
    public CreateExcelFileService(WxUploadFileService wxUploadFileService) {
        this.wxUploadFileService = wxUploadFileService;
    }

    private File createExcelXls(String sheetName, String[] titleRow) throws Exception {
        File file = File.createTempFile("supply", ".xls");
        HSSFWorkbook hWorkbook = new HSSFWorkbook();

        HSSFCellStyle hSSFCellStyle = hWorkbook.createCellStyle();
        hSSFCellStyle.setAlignment(HorizontalAlignment.LEFT);
        hSSFCellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
        hWorkbook.createSheet(sheetName);
        hWorkbook.getSheet(sheetName).createRow(0);
        HSSFRow row = hWorkbook.getSheet(sheetName).createRow(0);
        row.setHeight((short) 400);
        short j;
        for (j = 0; j < titleRow.length; j = (short) (j + 1)) {
            HSSFCell cell = row.createCell(j, CellType.BLANK);
            cell.setCellValue(titleRow[j]);
            cell.setCellStyle(hSSFCellStyle);
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        hWorkbook.write(fileOutputStream);
        fileOutputStream.close();
        return file;
    }

    private void writeToExcelXls(String touserFrom, String sheetName, File file,
                                        List<Map<String, String>> mapList) throws Exception {
        HSSFWorkbook hWorkbook = new HSSFWorkbook(Files.newInputStream(file.toPath()));
        FileOutputStream fileOutputStream;
        HSSFSheet sheet = hWorkbook.getSheet(sheetName);
        int columnCount = sheet.getRow(0).getLastCellNum();
        HSSFRow titleRow = sheet.getRow(0);
        HSSFCellStyle hSSFCellStyle = hWorkbook.createCellStyle();
        hSSFCellStyle.setAlignment(HorizontalAlignment.LEFT);
        hSSFCellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
        if (titleRow != null) {
            for (int rowId = 0; rowId < mapList.size(); rowId++) {
                Map<String, String> map = mapList.get(rowId);
                HSSFRow newRow = sheet.createRow(rowId + 1);
                newRow.setHeight((short) 400);
                short columnIndex;
                for (columnIndex = 0; columnIndex < columnCount; columnIndex = (short) (columnIndex + 1)) {
                    String mapKey = titleRow.getCell(columnIndex).toString().trim();
                    HSSFCell cell = newRow.createCell(columnIndex);
                    cell.setCellStyle(hSSFCellStyle);
                    cell.setCellValue((map.get(mapKey) == null) ? null : map.get(mapKey));
                }
            }
        }

        fileOutputStream = new FileOutputStream(file);
        hWorkbook.write(fileOutputStream);
        fileOutputStream.close();
        wxUploadFileService.upload(touserFrom, file);
    }

    public void executeWriteExcel(String touserFrom, Map<String, List<Map<String, String>>> excelData) throws Exception {
        String sheetName = "物料申领";
        String[] title = {"申请人", "申请科室", "特殊情况", "库存编号", "名称", "规格", "数量", "单位"};
        File file = createExcelXls(sheetName, title);
        writeToExcelXls(touserFrom, sheetName, file, excelData.get(sheetName));
    }
}

