package com.unis.base.server.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.unis.base.common.vo.CcsbtzjsmxVO;
import com.unis.base.server.entity.CcsbtzjsmxDO;
import com.unis.base.server.service.CcsbtzjsmxService;
import com.unis.common.utils.CurrentUtil;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CcsbtzjsmxController
 * @Description 仓储设备投资建设明细
 * @Date 2024/1/2 10:49
 * @Created by rising123sun
 */
@RestController
@Api(value = "仓储设备投资建设明细", tags = "仓储设备投资建设明细")
@Slf4j
@RequestMapping("/ccsbtzjsmx")
public class CcsbtzjsmxController {
    @Resource
    private CcsbtzjsmxService ccsbtzjsmxService;

    // 查询当前企业仓储设备投资建设上报数据_生成时打开
    @PostMapping("/queryCcsbtzjsmx")
    @ApiOperation(value = "查询当前企业仓储设备投资建设上报数据_生成时打开", notes = "查询当前企业仓储设备投资建设上报数据_生成时打开")
    public ResponseWrapper queryCcsbtzjsmx(@RequestBody CcsbtzjsmxVO ccsbtzjsmxVO) {
        try {
            JSONArray jsonArray = ccsbtzjsmxService.queryCcsbtzjsmx(ccsbtzjsmxVO);
            return ResponseWrapper.success(jsonArray);
        } catch (Exception e) {
            log.error("查询当前企业仓储设备投资建设上报数据异常", e);
            return ResponseWrapper.error("查询当前企业仓储设备投资建设上报数据异常");
        }
    }

    // 上报仓储设备投资建设明细
    @PostMapping("/saveCcsbtzjsmx")
    @ApiOperation(value = "上报仓储设备投资建设明细", notes = "上报仓储设备投资建设明细")
    public ResponseWrapper saveCcsbtzjsmx(@RequestParam String orgCode,
                                          @RequestParam String sbnf,
                                          @RequestBody List<CcsbtzjsmxDO> ccsjList) {
        try {
            return ccsbtzjsmxService.saveCcsbtzjsmx(orgCode, sbnf, ccsjList);
        } catch (Exception e) {
            log.error("上报仓储设备投资建设明细异常", e);
            return ResponseWrapper.error("上报仓储设备投资建设明细异常");
        }
    }

    // 仓储设备投资建设汇总表管理端或购销公司查看
    @PostMapping("/queryCcsbtzjsmxForHz")
    @ApiOperation(value = "仓储设备投资建设汇总表管理端或购销公司查看", notes = "仓储设备投资建设汇总表管理端或购销公司查看")
    public ResponseWrapper queryCcsbtzjsmxForHz(@RequestBody CcsbtzjsmxVO ccsbtzjsmxVO) {
        try {
            JSONArray jsonArray = ccsbtzjsmxService.queryCcsbtzjsmxForHz(ccsbtzjsmxVO);
            return ResponseWrapper.success(jsonArray);
        } catch (Exception e) {
            log.error("仓储设备投资建设汇总表管理端或购销公司查看异常", e);
            return ResponseWrapper.error("仓储设备投资建设汇总表管理端或购销公司查看异常");
        }
    }

    // 仓储设备投资建设汇总表管理端或购销公司导出
    @PostMapping("/queryCcsbtzjsmxForHzExport")
    @ApiOperation(value = "仓储设备投资建设汇总表管理端或购销公司导出", notes = "仓储设备投资建设汇总表管理端或购销公司导出")
    public ResponseWrapper queryCcsbtzjsmxForHzExport(HttpServletResponse response,
                                                      @RequestBody CcsbtzjsmxVO ccsbtzjsmxVO) {
        try {
            JSONArray jsonArray = ccsbtzjsmxService.queryCcsbtzjsmxForHz(ccsbtzjsmxVO);
            //fileName导出文件名
            String fileName = CurrentUtil.getOrgName() + "仓储设备投资建设统计表.xls";
            //sheetName表格名
            String sheetName = CurrentUtil.getOrgName() + "仓储设备投资建设统计表";
            //headMap表头Map, key为属性名, value为展示名,根据实际情况修改
            // 设置响应类型和编码
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            Workbook workbook = new XSSFWorkbook();
            // 创建单个工作表并填充数据
            Sheet sheet = workbook.createSheet(sheetName);
            Font shFont = workbook.createFont();
            shFont.setFontName("宋体");
            shFont.setFontHeightInPoints((short) 18);

            Font font = workbook.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 14);

            // 第一行创建大标题行，合并前三列单元格，居中显示,宋体，加粗，字体大小18
            Row titleRow = sheet.createRow(0);
            Cell titleCell = titleRow.createCell(0);
            CellStyle titleStyle = workbook.createCellStyle();
            titleStyle.setAlignment(HorizontalAlignment.CENTER);
            titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            titleStyle.setFont(shFont);
            titleCell.setCellStyle(titleStyle);
            titleCell.setCellValue("仓储设备投资建设统计表");

            // 合并单元格
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 2);
            sheet.addMergedRegion(cellRangeAddress);

            // 设置合并单元格的边框
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet);

            // 第二行创建小标题行，合并前三列单元格，居中显示,宋体，字体大小14，靠右显示
            Row secondTitleRow = sheet.createRow(1);
            Cell secondTitleCell = secondTitleRow.createCell(0);
            CellStyle secondTitleStyle = workbook.createCellStyle();
            secondTitleStyle.setAlignment(HorizontalAlignment.RIGHT);
            secondTitleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            secondTitleStyle.setFont(font);
            secondTitleCell.setCellStyle(secondTitleStyle);
            secondTitleCell.setCellValue("单位：万元、个、把、台");

            // 合并单元格
            CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, 0, 2);
            sheet.addMergedRegion(cellRangeAddress1);

            // 设置合并单元格的边框
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress1, sheet);


            // 创建标题行
            // “”，购入数量，合计
            Row firstTitleRow = sheet.createRow(2);
            CellStyle headerStyle = workbook.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            headerStyle.setWrapText(true);
            headerStyle.setFont(font);
            headerStyle.setBorderBottom(BorderStyle.THIN); // 下边框
            headerStyle.setBorderLeft(BorderStyle.THIN); // 左边框
            headerStyle.setBorderTop(BorderStyle.THIN); // 上边框
            headerStyle.setBorderRight(BorderStyle.THIN); // 右边框

            Cell firstTitleCell1 = firstTitleRow.createCell(0);
            firstTitleCell1.setCellStyle(headerStyle);
            firstTitleCell1.setCellValue("");
            sheet.setColumnWidth(0, 32 * 256);
            Cell firstTitleCell2 = firstTitleRow.createCell(1);
            firstTitleCell2.setCellStyle(headerStyle);
            firstTitleCell2.setCellValue("购入数量");
            sheet.setColumnWidth(1, 32 * 256);
            Cell firstTitleCell3 = firstTitleRow.createCell(2);
            firstTitleCell3.setCellStyle(headerStyle);
            firstTitleCell3.setCellValue("合计");
            sheet.setColumnWidth(2, 32 * 256);

            // 插入数据
            int rowIndex = 3;
            // 创建单元格样式，并设置水平居中
            Font font1 = workbook.createFont();
            font1.setFontName("宋体");
            font1.setFontHeightInPoints((short) 11);
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle.setFont(font1);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);

            // 创建第一列单元格格式，左对齐
            CellStyle cellStyle1 = workbook.createCellStyle();
            cellStyle1.setAlignment(HorizontalAlignment.LEFT);
            cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle1.setFont(font1);
            cellStyle1.setBorderBottom(BorderStyle.THIN);
            cellStyle1.setBorderTop(BorderStyle.THIN);
            cellStyle1.setBorderRight(BorderStyle.THIN);
            cellStyle1.setBorderLeft(BorderStyle.THIN);


            // 遍历jsonArray数组，以及他的子数组，直到没有子数组为止，最终转换成集合
            List<JSONObject> list = new ArrayList<>();
            list = getJsonArrayData(jsonArray, list);
            for (JSONObject jsonObject : list) {
                // 创建行
                Row row = sheet.createRow(rowIndex);
                // 创建单元格
                Cell cell1 = row.createCell(0);
                cell1.setCellStyle(cellStyle1);
                cell1.setCellValue(jsonObject.getString("name"));
                sheet.setColumnWidth(0, 32 * 256);
                Cell cell2 = row.createCell(1);
                cell2.setCellStyle(cellStyle);
                cell2.setCellValue(jsonObject.getInteger("grsl"));
                sheet.setColumnWidth(1, 32 * 256);
                Cell cell3 = row.createCell(2);
                cell3.setCellStyle(cellStyle);
                cell3.setCellValue(jsonObject.getBigDecimal("hj").doubleValue());
                sheet.setColumnWidth(2, 32 * 256);
                rowIndex++;
            }

            // 将工作簿写入输出流
            workbook.write(response.getOutputStream());
            workbook.close();
            return ResponseWrapper.success("导出成功！");
        } catch (Exception e) {
            log.error("仓储设备投资建设汇总表管理端或购销公司导出异常", e);
            return ResponseWrapper.error("仓储设备投资建设汇总表管理端或购销公司导出异常");
        }
    }

    // 递归获取jsonArray中的数据，并转换成集合
    private List<JSONObject> getJsonArrayData(JSONArray jsonArray, List<JSONObject> list) {
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            list.add(jsonObject);
            JSONArray children = jsonObject.getJSONArray("children");
            if (children != null && children.size() > 0) {
                getJsonArrayData(children, list);
            }
        }
        return list;
    }

    // 仓储设备投资建设明细表企业端查看
    @PostMapping("/queryCcsbtzjsmxForMx")
    @ApiOperation(value = "仓储设备投资建设明细表企业端或区粮办查看", notes = "仓储设备投资建设明细表企业端或区粮办查看")
    public ResponseWrapper queryCcsbtzjsmxForMx(@RequestBody CcsbtzjsmxVO ccsbtzjsmxVO) {
        try {
            JSONObject jsonObject = ccsbtzjsmxService.queryCcsbtzjsmxForMx(ccsbtzjsmxVO);
            return ResponseWrapper.success(jsonObject);
        } catch (Exception e) {
            log.error("仓储设备投资建设明细表企业端查看异常", e);
            return ResponseWrapper.error("仓储设备投资建设明细表企业端查看异常");
        }
    }

    // 仓储设备投资建设明细表企业端导出
    @PostMapping("/queryCcsbtzjsmxForMxExport")
    @ApiOperation(value = "仓储设备投资建设明细表企业端或区粮办导出", notes = "仓储设备投资建设明细表企业端或区粮办导出")
    public ResponseWrapper queryCcsbtzjsmxForMxExport(HttpServletResponse response,
                                                      @RequestBody CcsbtzjsmxVO ccsbtzjsmxVO) {
        try {
            JSONObject jsonObject = ccsbtzjsmxService.queryCcsbtzjsmxForMx(ccsbtzjsmxVO);
            //fileName导出文件名
            String fileName = CurrentUtil.getOrgName() + "仓储设备投资建设明细表.xls";
            //sheetName表格名
            String sheetName = CurrentUtil.getOrgName() + "仓储设备投资建设明细表";
            //headMap表头Map, key为属性名, value为展示名,根据实际情况修改
            // 设置响应类型和编码
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            Workbook workbook = new XSSFWorkbook();
            // 创建单个工作表并填充数据
            Sheet sheet = workbook.createSheet(sheetName);
            Font shFont = workbook.createFont();
            shFont.setFontName("宋体");
            shFont.setFontHeightInPoints((short) 18);

            Font font = workbook.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 14);

            // 第一行创建大标题行，合并前八列单元格，居中显示,宋体，加粗，字体大小18
            Row titleRow = sheet.createRow(0);
            Cell titleCell = titleRow.createCell(0);
            CellStyle titleStyle = workbook.createCellStyle();
            titleStyle.setAlignment(HorizontalAlignment.CENTER);
            titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            titleStyle.setFont(shFont);
            titleCell.setCellStyle(titleStyle);
            titleCell.setCellValue("仓储设备投资建设明细表");

            // 合并单元格
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 7);
            sheet.addMergedRegion(cellRangeAddress);

            // 设置合并单元格的边框
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet);

            // 第二行创建小标题行，合并前八列单元格，居中显示,宋体，字体大小14，靠右显示
            Row secondTitleRow = sheet.createRow(1);
            Cell secondTitleCell = secondTitleRow.createCell(0);
            CellStyle secondTitleStyle = workbook.createCellStyle();
            secondTitleStyle.setAlignment(HorizontalAlignment.RIGHT);
            secondTitleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            secondTitleStyle.setFont(font);
            secondTitleCell.setCellStyle(secondTitleStyle);
            secondTitleCell.setCellValue("单位：万元、个、把、台");

            // 合并单元格
            CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, 0, 7);
            sheet.addMergedRegion(cellRangeAddress1);

            // 设置合并单元格的边框
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress1, sheet);


            // 创建标题行
            // 库区名称，设备类型，设备类别，设备名称，购入数量，金融合计，状态，确认时间
            Row firstTitleRow = sheet.createRow(2);
            CellStyle headerStyle = workbook.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            headerStyle.setWrapText(true);
            headerStyle.setFont(font);
            headerStyle.setBorderBottom(BorderStyle.THIN); // 下边框
            headerStyle.setBorderLeft(BorderStyle.THIN); // 左边框
            headerStyle.setBorderTop(BorderStyle.THIN); // 上边框
            headerStyle.setBorderRight(BorderStyle.THIN); // 右边框

            Cell firstTitleCell1 = firstTitleRow.createCell(0);
            firstTitleCell1.setCellStyle(headerStyle);
            firstTitleCell1.setCellValue("库区名称");
            sheet.setColumnWidth(0, 32 * 256);
            Cell firstTitleCell2 = firstTitleRow.createCell(1);
            firstTitleCell2.setCellStyle(headerStyle);
            firstTitleCell2.setCellValue("设备类型");
            sheet.setColumnWidth(1, 32 * 256);
            Cell firstTitleCell3 = firstTitleRow.createCell(2);
            firstTitleCell3.setCellStyle(headerStyle);
            firstTitleCell3.setCellValue("设备类别");
            sheet.setColumnWidth(2, 32 * 256);
            Cell firstTitleCell4 = firstTitleRow.createCell(3);
            firstTitleCell4.setCellStyle(headerStyle);
            firstTitleCell4.setCellValue("设备名称");
            sheet.setColumnWidth(3, 32 * 256);
            Cell firstTitleCell5 = firstTitleRow.createCell(4);
            firstTitleCell5.setCellStyle(headerStyle);
            firstTitleCell5.setCellValue("购入数量");
            sheet.setColumnWidth(4, 32 * 256);
            Cell firstTitleCell6 = firstTitleRow.createCell(5);
            firstTitleCell6.setCellStyle(headerStyle);
            firstTitleCell6.setCellValue("金融合计");
            sheet.setColumnWidth(5, 32 * 256);
            Cell firstTitleCell7 = firstTitleRow.createCell(6);
            firstTitleCell7.setCellStyle(headerStyle);
            firstTitleCell7.setCellValue("状态");
            sheet.setColumnWidth(6, 32 * 256);
            Cell firstTitleCell8 = firstTitleRow.createCell(7);
            firstTitleCell8.setCellStyle(headerStyle);
            firstTitleCell8.setCellValue("确认时间");
            sheet.setColumnWidth(7, 32 * 256);

            // 插入数据
            int rowIndex = 3;
            // 创建单元格样式，并设置水平居中
            Font font1 = workbook.createFont();
            font1.setFontName("宋体");
            font1.setFontHeightInPoints((short) 11);
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle.setFont(font1);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);


            JSONArray jsonArray = jsonObject.getJSONArray("data");
            for (Object o : jsonArray) {
                JSONObject jsonObject1 = (JSONObject) o;
                // 创建行
                Row row = sheet.createRow(rowIndex);
                // 创建单元格
                Cell cell1 = row.createCell(0);
                cell1.setCellStyle(cellStyle);
                cell1.setCellValue(jsonObject1.getString("kqmc"));
                sheet.setColumnWidth(0, 32 * 256);
                Cell cell2 = row.createCell(1);
                cell2.setCellStyle(cellStyle);
                cell2.setCellValue(jsonObject1.getString("sblx"));
                sheet.setColumnWidth(1, 32 * 256);
                Cell cell3 = row.createCell(2);
                cell3.setCellStyle(cellStyle);
                cell3.setCellValue(jsonObject1.getString("sblb"));
                sheet.setColumnWidth(2, 32 * 256);
                Cell cell4 = row.createCell(3);
                cell4.setCellStyle(cellStyle);
                cell4.setCellValue(jsonObject1.getString("sbmc"));
                sheet.setColumnWidth(3, 32 * 256);
                Cell cell5 = row.createCell(4);
                cell5.setCellStyle(cellStyle);
                cell5.setCellValue(jsonObject1.getInteger("grsl") == null ? "" : String.valueOf(jsonObject1.getInteger("grsl")));
                sheet.setColumnWidth(4, 32 * 256);
                Cell cell6 = row.createCell(5);
                cell6.setCellStyle(cellStyle);
                cell6.setCellValue(jsonObject1.getBigDecimal("jrhj") == null ? "" : String.valueOf(jsonObject1.getBigDecimal("jrhj")));
                sheet.setColumnWidth(5, 32 * 256);
                Cell cell7 = row.createCell(6);
                cell7.setCellStyle(cellStyle);
                cell7.setCellValue(jsonObject1.getString("zt"));
                sheet.setColumnWidth(6, 32 * 256);
                Cell cell8 = row.createCell(7);
                cell8.setCellStyle(cellStyle);
                cell8.setCellValue(jsonObject1.getString("qrsj"));
                sheet.setColumnWidth(7, 32 * 256);
                rowIndex++;
            }

            // 将工作簿写入输出流
            workbook.write(response.getOutputStream());
            workbook.close();
            return ResponseWrapper.success("导出成功！");

        } catch (Exception e) {
            log.error("仓储设备投资建设明细表企业端或区粮办导出异常", e);
            return ResponseWrapper.error("仓储设备投资建设明细表企业端或区粮办导出异常");
        }
    }

    // 仓储设备投资建设明细表管理端查看
    @PostMapping("/queryCcsbtzjsmxForMxGld")
    @ApiOperation(value = "仓储设备投资建设明细表管理端市区查看", notes = "仓储设备投资建设明细表管理端市区查看")
    public ResponseWrapper queryCcsbtzjsmxForMxGld(@RequestBody CcsbtzjsmxVO ccsbtzjsmxVO) {
        try {
            JSONObject jsonObject = ccsbtzjsmxService.queryCcsbtzjsmxForMxGld(ccsbtzjsmxVO);
            return ResponseWrapper.success(jsonObject);
        } catch (Exception e) {
            log.error("仓储设备投资建设明细表管理端查看异常", e);
            return ResponseWrapper.error("仓储设备投资建设明细表管理端查看异常");
        }
    }

    // 仓储设备投资建设明细表管理端导出
    @PostMapping("/queryCcsbtzjsmxForMxGldExport")
    @ApiOperation(value = "仓储设备投资建设明细表管理端市区导出", notes = "仓储设备投资建设明细表管理端市区导出")
    public ResponseWrapper queryCcsbtzjsmxForMxGldExport(HttpServletResponse response,
                                                         @RequestBody CcsbtzjsmxVO ccsbtzjsmxVO) {
        try {
            JSONObject jsonObject = ccsbtzjsmxService.queryCcsbtzjsmxForMxGld(ccsbtzjsmxVO);
            //fileName导出文件名
            String fileName = CurrentUtil.getOrgName() + "仓储设备投资建设明细表.xls";
            //sheetName表格名
            String sheetName = CurrentUtil.getOrgName() + "仓储设备投资建设明细表";
            //headMap表头Map, key为属性名, value为展示名,根据实际情况修改
            // 设置响应类型和编码
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            Workbook workbook = new XSSFWorkbook();
            // 创建单个工作表并填充数据
            Sheet sheet = workbook.createSheet(sheetName);
            Font shFont = workbook.createFont();
            shFont.setFontName("宋体");
            shFont.setFontHeightInPoints((short) 18);

            Font font = workbook.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 14);

            // 第一行创建大标题行，合并前九列单元格，居中显示,宋体，加粗，字体大小18
            Row titleRow = sheet.createRow(0);
            Cell titleCell = titleRow.createCell(0);
            CellStyle titleStyle = workbook.createCellStyle();
            titleStyle.setAlignment(HorizontalAlignment.CENTER);
            titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            titleStyle.setFont(shFont);
            titleCell.setCellStyle(titleStyle);
            titleCell.setCellValue("仓储设备投资建设明细表");

            // 合并单元格
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 8);
            sheet.addMergedRegion(cellRangeAddress);

            // 设置合并单元格的边框
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet);

            // 第二行创建小标题行，合并前九列单元格，居中显示,宋体，字体大小14，靠右显示
            Row secondTitleRow = sheet.createRow(1);
            Cell secondTitleCell = secondTitleRow.createCell(0);
            CellStyle secondTitleStyle = workbook.createCellStyle();
            secondTitleStyle.setAlignment(HorizontalAlignment.RIGHT);
            secondTitleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            secondTitleStyle.setFont(font);
            secondTitleCell.setCellStyle(secondTitleStyle);
            secondTitleCell.setCellValue("单位：万元、个、把、台");

            // 合并单元格
            CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, 0, 8);
            sheet.addMergedRegion(cellRangeAddress1);

            // 设置合并单元格的边框
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress1, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress1, sheet);


            // 创建标题行
            // 区域/集团，库区名称，设备类型，设备类别，设备名称，购入数量，金融合计，状态，确认时间
            Row firstTitleRow = sheet.createRow(2);
            CellStyle headerStyle = workbook.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            headerStyle.setWrapText(true);
            headerStyle.setFont(font);
            headerStyle.setBorderBottom(BorderStyle.THIN); // 下边框
            headerStyle.setBorderLeft(BorderStyle.THIN); // 左边框
            headerStyle.setBorderTop(BorderStyle.THIN); // 上边框
            headerStyle.setBorderRight(BorderStyle.THIN); // 右边框

            Cell firstTitleCell1 = firstTitleRow.createCell(0);
            firstTitleCell1.setCellStyle(headerStyle);
            firstTitleCell1.setCellValue("区域/集团");
            sheet.setColumnWidth(0, 32 * 256);
            Cell firstTitleCell2 = firstTitleRow.createCell(1);
            firstTitleCell2.setCellStyle(headerStyle);
            firstTitleCell2.setCellValue("库区名称");
            sheet.setColumnWidth(1, 32 * 256);
            Cell firstTitleCell3 = firstTitleRow.createCell(2);
            firstTitleCell3.setCellStyle(headerStyle);
            firstTitleCell3.setCellValue("设备类型");
            sheet.setColumnWidth(2, 32 * 256);
            Cell firstTitleCell4 = firstTitleRow.createCell(3);
            firstTitleCell4.setCellStyle(headerStyle);
            firstTitleCell4.setCellValue("设备类别");
            sheet.setColumnWidth(3, 32 * 256);
            Cell firstTitleCell5 = firstTitleRow.createCell(4);
            firstTitleCell5.setCellStyle(headerStyle);
            firstTitleCell5.setCellValue("设备名称");
            sheet.setColumnWidth(4, 32 * 256);
            Cell firstTitleCell6 = firstTitleRow.createCell(5);
            firstTitleCell6.setCellStyle(headerStyle);
            firstTitleCell6.setCellValue("购入数量");
            sheet.setColumnWidth(5, 32 * 256);
            Cell firstTitleCell7 = firstTitleRow.createCell(6);
            firstTitleCell7.setCellStyle(headerStyle);
            firstTitleCell7.setCellValue("金融合计");
            sheet.setColumnWidth(6, 32 * 256);
            Cell firstTitleCell8 = firstTitleRow.createCell(7);
            firstTitleCell8.setCellStyle(headerStyle);
            firstTitleCell8.setCellValue("状态");
            sheet.setColumnWidth(7, 32 * 256);
            Cell firstTitleCell9 = firstTitleRow.createCell(8);
            firstTitleCell9.setCellStyle(headerStyle);
            firstTitleCell9.setCellValue("确认时间");
            sheet.setColumnWidth(8, 32 * 256);

            // 插入数据
            int rowIndex = 3;
            // 创建单元格样式，并设置水平居中
            Font font1 = workbook.createFont();
            font1.setFontName("宋体");
            font1.setFontHeightInPoints((short) 11);
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle.setFont(font1);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);

            JSONArray jsonArray = jsonObject.getJSONArray("data");

            for (Object o : jsonArray) {
                JSONObject jsonObject1 = (JSONObject) o;
                // 获取键
                String key = jsonObject1.keySet().iterator().next();

                // 获取值
                JSONArray jsonArray1 = jsonObject1.getJSONArray(key);

                for (Object o1 : jsonArray1) {
                    // 创建行
                    Row row = sheet.createRow(rowIndex);
                    // 创建单元格
                    Cell cell1 = row.createCell(0);
                    cell1.setCellStyle(cellStyle);
                    cell1.setCellValue(key);
                    sheet.setColumnWidth(0, 32 * 256);

                    JSONObject jsonObject2 = (JSONObject) o1;
                    Cell cell2 = row.createCell(1);
                    cell2.setCellStyle(cellStyle);
                    cell2.setCellValue(jsonObject2.getString("kqmc"));
                    sheet.setColumnWidth(1, 32 * 256);
                    Cell cell3 = row.createCell(2);
                    cell3.setCellStyle(cellStyle);
                    cell3.setCellValue(jsonObject2.getString("sblx"));
                    sheet.setColumnWidth(2, 32 * 256);
                    Cell cell4 = row.createCell(3);
                    cell4.setCellStyle(cellStyle);
                    cell4.setCellValue(jsonObject2.getString("sblb"));
                    sheet.setColumnWidth(3, 32 * 256);
                    Cell cell5 = row.createCell(4);
                    cell5.setCellStyle(cellStyle);
                    cell5.setCellValue(jsonObject2.getString("sbmc"));
                    sheet.setColumnWidth(4, 32 * 256);
                    Cell cell6 = row.createCell(5);
                    cell6.setCellStyle(cellStyle);
                    cell6.setCellValue(jsonObject2.getInteger("grsl") == null ? "" : String.valueOf(jsonObject2.getInteger("grsl")));
                    sheet.setColumnWidth(5, 32 * 256);
                    Cell cell7 = row.createCell(6);
                    cell7.setCellStyle(cellStyle);
                    cell7.setCellValue(jsonObject2.getBigDecimal("jrhj") == null ? "" : String.valueOf(jsonObject2.getBigDecimal("jrhj")));
                    sheet.setColumnWidth(6, 32 * 256);
                    Cell cell8 = row.createCell(7);
                    cell8.setCellStyle(cellStyle);
                    cell8.setCellValue(jsonObject2.getString("zt"));
                    sheet.setColumnWidth(7, 32 * 256);
                    Cell cell9 = row.createCell(8);
                    cell9.setCellStyle(cellStyle);
                    cell9.setCellValue(jsonObject2.getString("qrsj"));
                    sheet.setColumnWidth(8, 32 * 256);
                    rowIndex++;
                }
                // 大于1才合并
                if (jsonArray1.size() > 1) {
                    // 合并单元格
                    CellRangeAddress cellRangeAddress2 = new CellRangeAddress(rowIndex - jsonArray1.size(), rowIndex - 1, 0, 0);
                    sheet.addMergedRegion(cellRangeAddress2);

                    // 设置合并单元格的边框
                    RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress2, sheet);
                    RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress2, sheet);
                    RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress2, sheet);
                    RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress2, sheet);
                }
            }

            // 将工作簿写入输出流
            workbook.write(response.getOutputStream());
            workbook.close();

            return ResponseWrapper.success("导出成功！");
        } catch (Exception e) {
            log.error("仓储设备投资建设明细表管理端市区导出异常", e);
            return ResponseWrapper.error("仓储设备投资建设明细表管理端市区导出异常");
        }
    }

    // 设备器材类别下拉
    @PostMapping("/querySbqclb")
    @ApiOperation(value = "设备器材类别下拉", notes = "设备器材类别下拉")
    public ResponseWrapper querySbqclb(@RequestParam String sblxCode) {
        try {
            if ("粮食设备".equals(sblxCode)) {
                sblxCode = "5476";
            } else if ("仓储器材".equals(sblxCode)) {
                sblxCode = "5475";
            }
            JSONArray jsonArray = ccsbtzjsmxService.querySbqclb(sblxCode);
            return ResponseWrapper.success(jsonArray);
        } catch (Exception e) {
            log.error("设备器材类别下拉异常", e);
            return ResponseWrapper.error("设备器材类别下拉异常");
        }
    }

}
