package com.dxhy.core.thread;


import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigEntity;
import com.dxhy.api.model.excelExport.TDxExcelExportlog;
import com.dxhy.api.model.ncp.ncpJjkcInvoice.TDxRecordInvoiceNcpJjkcVo;
import com.dxhy.api.model.rz.rzCustom.JkszhcxRespVO;
import com.dxhy.common.rabbitmq.RabbitMqSender;
import com.dxhy.common.service.impl.TabulateRedisServiceImpl;
import com.dxhy.common.util.*;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.feign.IFeginWebsocket;
import com.dxhy.core.model.dkCountVo.DkCountCustomsRecordVo;
import com.dxhy.core.model.dkCountVo.DkCountExportInvoiceVo;
import com.dxhy.core.model.dkCountVo.DkCountTDxExportCustoms;
import com.dxhy.core.model.dkCountVo.DkCountZzsDkdjwspzVo;
import com.dxhy.core.service.export.DkCountExportService;
import com.dxhy.core.service.singletable.IExcelExportlogService;
import com.dxhy.core.util.JsonResponseUtils;
import com.dxhy.extend.model.ExtRzjgqdRespVO;
import com.dxhy.jxpt.baseService.config.dao.SysEnterpriseConfigDao;
import com.dxhy.oss.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;

/**
* @Description 成品油勾选页面导出--缴款书
* @Author wangruwei
* @Date 2024/7/17 14:43
**/

@SuppressWarnings("AlibabaMethodTooLong")
@Slf4j

public class DkCountExportThread extends BaseThread {

    /**
     * 默认要导出第几页的数据
     */
    private final int default_curr = 1;
    /**
     * 默认每页导出多少条数据
     */
    private final int default_size = 5000;
    /**
     * 文件名前缀
     */
    private final String fileNamePrefix = "dkCountExport";
    /**
     * 文件名后缀
     */
    private final String fileNameSurfix = ".xlsx";
    /**
     * 文件名中间分隔符
     */
    private final String fileSplit = "_";
    /**
     * excel的标题
     */
    private final String title = "抵扣统计认证明细";

    private WriteSheet writeSheet;
    private WriteTable writeTable;
    private ExcelWriter writer = null;
    /**
     * 导出excel的列名
     */

    private final IExcelExportlogService exportLogService;

    private final List<TDxExcelExportlog> exportLogs;

    private final DkCountExportService dkCountExportService;

    private final FileService fileService;

    private final RabbitMqSender sender;
    private final TabulateRedisServiceImpl tabulateRedisService;

    private final SysEnterpriseConfigDao sysEnterpriseConfigDao;

    public DkCountExportThread(IExcelExportlogService exportLogService, List<TDxExcelExportlog> exportLogs,
                               DkCountExportService dkCountExportService, SysEnterpriseConfigDao sysEnterpriseConfigDao, FileService fileService, RabbitMqSender sender, IFeginWebsocket feginWebsocket,
                               TabulateRedisServiceImpl tabulateRedisService) {

        this.exportLogService = exportLogService;
        this.exportLogs = exportLogs;
        this.dkCountExportService = dkCountExportService;
        this.fileService = fileService;
        this.sender = sender;
        this.feginWebsocket = feginWebsocket;
        this.tabulateRedisService = tabulateRedisService;
        this.sysEnterpriseConfigDao = sysEnterpriseConfigDao;
    }

    private final IFeginWebsocket feginWebsocket;

    @Override
    public void run() {
        for (TDxExcelExportlog exportLog : exportLogs) {
            // 日志表主键
            Long id = exportLog.getId();
            String userid = exportLog.getUserAccount();
            String userName = exportLog.getUserName();
            // 导出条件
            String condition = exportLog.getConditions();
            // 数据源名称
            String dbName = exportLog.getDbName();
            if (StringHelper.isBlank(dbName)) {
                dbName = "";
            }
            String createDate = DateUtils.dateToStrLong(exportLog.getCreateDate());

            JSONObject msg = new JSONObject();

            // 捕获异常，防止某一条日志导出出错，影响其它日志的导出
            // 应该保存的参数
            Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
            Map<String,Object> pramsMap = resetpramsMap(map);
            String gfsh = pramsMap.get("gfsh").toString();
            String gfmc = "";
            SysEnterpriseConfigEntity sysEnterpriseConfigEntity = sysEnterpriseConfigDao.selectOne(new LambdaQueryWrapper<SysEnterpriseConfigEntity>().eq(SysEnterpriseConfigEntity::getNsrsbh, gfsh));
            if(ObjectUtils.isNotEmpty(sysEnterpriseConfigEntity)){
                gfmc = sysEnterpriseConfigEntity.getNsrmc();
            }
            try {
                exportLogService.updateStart(id, dbName);



                //税号
                //税款所属期

                // 导出文件名
                StringBuilder ftpFileName = new StringBuilder();
                ftpFileName.append(userid);
                ftpFileName.append(fileSplit);
                ftpFileName.append(fileNamePrefix);
                ftpFileName.append(fileSplit);
                ftpFileName.append(DateUtils.getStringDateShort());
                ftpFileName.append(fileSplit);
                ftpFileName.append((new Date()).getTime());
                ftpFileName.append(fileNameSurfix);

                // 本地文件存储路径
                StringBuilder excelFile = new StringBuilder();
                excelFile.append(fileService.getLocalPathDefault());
                excelFile.append(ftpFileName.toString());


                ExcelWriterBuilder builder = EasyExcel.write(excelFile.toString());
                builder.registerWriteHandler(new CustomCellWriteHandler());
                writer = builder.build();

                // 递归导出数据到excel
                //发票
                JSONObject sheetTitleFp = getSheetTitleFp();
                this.cicleExport(pramsMap, default_curr, excelFile.toString(), sheetTitleFp,1,"发票",new ArrayList<>(),condition);

                //海关缴款书
                JSONObject sheetTitleHgjks = getSheetTitleHgjks();
                this.cicleHgjks(pramsMap, default_curr, excelFile.toString(), sheetTitleHgjks,2,"海关缴款书",new ArrayList<>(),condition);
                //代扣代缴

                JSONObject sheetTitleDkdj = getSheetTitleDkdj();
                this.cicleDkdj(pramsMap, default_curr, excelFile.toString(), sheetTitleDkdj,3,"代扣代缴",new ArrayList<>(),condition);

                //农产品加计扣除发票
                JSONObject sheetTitleNcpjjFp = getSheetTitleJjkcFp();
                this.cicleJjkcFp(pramsMap, default_curr, excelFile.toString(), sheetTitleNcpjjFp,4,"农产品加计扣除发票",new ArrayList<>(),condition);

                //农产品加计扣除缴款书
                JSONObject sheetTitleJjkcJks = getSheetTitleJjkcJks();
                this.cicleJjkcJks(pramsMap, default_curr, excelFile.toString(), sheetTitleJjkcJks,5,"农产品加计扣除缴款书",new ArrayList<>(),condition);

                //出口转内销发票
                JSONObject sheetTitleCkznxFp = getSheetTitleCkznxFp();
                this.cicleCkznxFp(pramsMap, default_curr, excelFile.toString(), sheetTitleCkznxFp,6,"出口转内销发票",new ArrayList<>(),condition);

                //出口转内销缴款书
                JSONObject sheetTitleCkznxJks = getSheetTitleCkznxJks();
                this.cicleCkznxJks(pramsMap, default_curr, excelFile.toString(), sheetTitleCkznxJks,7,"出口转内销缴款书",new ArrayList<>(),condition);

                //异常发票
                JSONObject sheetTitleycFp = getSheetTitleFp();
                this.cicleYcFp(pramsMap, default_curr, excelFile.toString(), sheetTitleycFp,8,"异常发票",new ArrayList<>(),condition);


                writer.finish();

                // FTP文件存储路径
                String[] toDay = DateUtil.today().split("-");
                String ftpFilePath = fileService.getPathPrefix() + userName + "/" + "excelFile/" + toDay[0] + "/"
                    + toDay[1] + "/" + toDay[2] + "/" + ftpFileName.toString();
                // 文件上传ftp
                fileService.uploadFile(ftpFilePath, excelFile.toString());

                // 设置成功消息内容
                String title = pramsMap.get("skssq")+"认证明细-"+gfmc;
                msg.put("title", super.getSuccTitle(title));
                msg.put("content", super.getSuccContent(createDate));
                msg.put("url", super.getUrl(id));
                msg.put("useraccount", userName);

                // 记录导出完成状态
                exportLogService.updateSucc(id, ftpFilePath, dbName);

            } catch (Exception e) {
                String errmsg = ExceptionUtil.getExceptionDeteil(e, 4000);
                // 设置失败消息内容
                String title = pramsMap.get("skssq")+"认证明细-"+gfmc;
                msg.put("title", super.getFailTitle(title));
                msg.put("content", super.getFailContent(createDate, errmsg));
                msg.put("url", "");
                msg.put("useraccount", userName);

                log.error("", e);
                // 记录导出出错状态
                exportLogService.updateFail(id, errmsg, dbName);
            } finally {
                // 发送导出消息到mq
                String responseResult = JsonResponseUtils.responseResult(msg);
                sender.sendToMsg(responseResult);

                // 申请成功后像websocket发送申请信息
                feginWebsocket.setMessageToWebsocket(userName);
            }
        }
    }

    private Map<String, Object> resetpramsMap(Map<String, Object> map) {
        Map<String,Object> pramsMap = new HashMap<>();

        String company = (String)map.get("company");
        String menuId = (String)map.get("menuId");

        pramsMap.put("company", company);
        pramsMap.put("gfsh", map.get("gfsh"));
        pramsMap.put("skssq", (String)map.get("skssq"));
        //认证类型 -- 抵扣
        pramsMap.put("rzlx", "1");
        pramsMap.put("authStatus", "4");

        pramsMap.put("menuId", menuId);
        return pramsMap;
    }


    /**
     * 递归导出数据到excel文件，防止jvm oom
     *
     * @param pramsMap
     *            查询数据参数集合
     * @param curr
     *            当前第几页
     * @param excelFile
     *            导出的文件
     * @param sheetNum
     * @param sheetName
     * @param condition
     * @throws Exception
     *
     */

    private boolean cicleExport(Map<String, Object> pramsMap, int curr, String excelFile, JSONObject jsonObject, int sheetNum, String sheetName, List<ExtRzjgqdRespVO> beforeDataList, String condition)
        throws Exception {
        Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
        pramsMap = resetpramsMap(map);
        //发票
        JSONObject pageData = dkCountExportService.selectFpByPramsMap(pramsMap, curr, default_size);

        boolean hasNext = pageData.getBooleanValue("hasNext");
        List<ExtRzjgqdRespVO> dataList = (List<ExtRzjgqdRespVO>)pageData.get("datalist");
        if(!CollectionUtils.isEmpty(dataList)){
            beforeDataList.addAll(dataList);
        }
        boolean firstPage = false;
        if (curr == default_curr) {
            firstPage = true;
        }

        // 数据不是最后一页
        if (hasNext) {
            int nextPage = pageData.getIntValue("nextPage");
            // 写入当前页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, false,sheetNum,sheetName);
            // 更新页数
            return cicleExport(pramsMap, nextPage, excelFile, jsonObject, sheetNum, sheetName,beforeDataList, condition);
        } else {
            //给最后一列加合计
            if(!CollectionUtils.isEmpty(beforeDataList)){
                BigDecimal hjje = beforeDataList.stream()
                        .map(ExtRzjgqdRespVO::getJe)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal hjse = beforeDataList.stream()
                        .map(ExtRzjgqdRespVO::getSe)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal dkse = beforeDataList.stream()
                        .map(ExtRzjgqdRespVO::getDkse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                ExtRzjgqdRespVO hj = new ExtRzjgqdRespVO();
                hj.setXh("合计");
                if (ObjectUtils.isNotEmpty(hjje)) {
                    hj.setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(hjje.toString())));
                }
                if (ObjectUtils.isNotEmpty(hjse)) {
                    hj.setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(hjse.toString())));
                }
                if (ObjectUtils.isNotEmpty(dkse)) {
                    hj.setDkse(AmountFormatUtil.fmtMicrometer(MathUtil.round(dkse.toString())));
                }
                dataList.add(hj);
            }
            // 写入最后一页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, true,sheetNum,sheetName);
            return hasNext;
        }


    }


    // 将带千分位的字符串转换为BigDecimal
    private static BigDecimal convertStringToBigDecimal(String amount) {
        try {
            NumberFormat format = NumberFormat.getInstance(Locale.US);
            Number number = format.parse(amount);
            return BigDecimal.valueOf(number.doubleValue());
        } catch (ParseException e) {
            throw new RuntimeException("Error parsing amount: " + amount, e);
        }
    }

    /**
    * @Description 缴款书sheet
    * @param pramsMap
    * @param curr
    * @param excelFile
    * @param jsonObject
    * @param sheetNum
    * @param sheetName
    * @param condition
     * @Return boolean
    * @Author wangruwei
    * @Date 2024/8/22 14:55
    **/
    private boolean cicleHgjks(Map<String, Object> pramsMap, int curr, String excelFile, JSONObject jsonObject, int sheetNum, String sheetName, List<DkCountCustomsRecordVo> beforeDataList, String condition)
            throws Exception {
        Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
        pramsMap = resetpramsMap(map);
        JSONObject pageData = dkCountExportService.selectHgjksByPramsMap(pramsMap, curr, default_size);

        boolean hasNext = pageData.getBooleanValue("hasNext");
        List<DkCountCustomsRecordVo> dataList = (List<DkCountCustomsRecordVo>)pageData.get("datalist");
        if(!CollectionUtils.isEmpty(dataList)){
            beforeDataList.addAll(dataList);
        }

        boolean firstPage = false;
        if (curr == default_curr) {
            firstPage = true;
        }

        // 数据不是最后一页
        if (hasNext) {
            int nextPage = pageData.getIntValue("nextPage");
            // 写入当前页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, false,sheetNum,sheetName);
            // 更新页数
            return cicleHgjks(pramsMap, nextPage, excelFile, jsonObject, sheetNum, sheetName,beforeDataList, condition);
        } else {
            // 写入最后一页数据
            if(!CollectionUtils.isEmpty(beforeDataList)){
                BigDecimal skje = beforeDataList.stream()
                        .map(DkCountCustomsRecordVo::getTaxAmount)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal yxdkse = beforeDataList.stream()
                        .map(DkCountCustomsRecordVo::getYxTaxAmount)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                DkCountCustomsRecordVo hj = new DkCountCustomsRecordVo();
                hj.setXh("合计");
                if (ObjectUtils.isNotEmpty(skje)) {
                    hj.setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(skje.toString())));
                }
                if (ObjectUtils.isNotEmpty(yxdkse)) {
                    hj.setYxTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(yxdkse.toString())));
                }

                dataList.add(hj);
            }
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, true,sheetNum,sheetName);
            return hasNext;
        }
    }

    /**
     * @Description 代扣代缴sheet
     * @param pramsMap
     * @param curr
     * @param excelFile
     * @param jsonObject
     * @param sheetNum
     * @param sheetName
     * @param condition
     * @Return boolean
     * @Author wangruwei
     * @Date 2024/8/22 14:55
     **/
    private boolean cicleDkdj(Map<String, Object> pramsMap, int curr, String excelFile, JSONObject jsonObject, int sheetNum, String sheetName, List<DkCountZzsDkdjwspzVo> beforeDataList, String condition)
            throws Exception {
        Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
        pramsMap = resetpramsMap(map);
        JSONObject pageData = dkCountExportService.selectDkdjByPramsMap(pramsMap, curr, default_size);

        boolean hasNext = pageData.getBooleanValue("hasNext");
        List<DkCountZzsDkdjwspzVo> dataList = (List<DkCountZzsDkdjwspzVo>)pageData.get("datalist");
        if(!CollectionUtils.isEmpty(dataList)){
            beforeDataList.addAll(dataList);
        }
        boolean firstPage = false;
        if (curr == default_curr) {
            firstPage = true;
        }

        // 数据不是最后一页
        if (hasNext) {
            int nextPage = pageData.getIntValue("nextPage");
            // 写入当前页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, false,sheetNum,sheetName);
            // 更新页数
            return cicleDkdj(pramsMap, nextPage, excelFile, jsonObject, sheetNum, sheetName,beforeDataList, condition);
        } else {
            // 写入最后一页数据
            // 写入最后一页数据
            if(!CollectionUtils.isEmpty(beforeDataList)){
                BigDecimal jsje = beforeDataList.stream()
                        .map(DkCountZzsDkdjwspzVo::getJsje)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal sjje = beforeDataList.stream()
                        .map(DkCountZzsDkdjwspzVo::getSjje)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal yxse = beforeDataList.stream()
                        .map(DkCountZzsDkdjwspzVo::getYxse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                DkCountZzsDkdjwspzVo hj = new DkCountZzsDkdjwspzVo();
                hj.setXh("合计");
                if (ObjectUtils.isNotEmpty(jsje)) {
                    hj.setJsje(AmountFormatUtil.fmtMicrometer(MathUtil.round(jsje.toString())));
                }

                if (ObjectUtils.isNotEmpty(sjje)) {
                    hj.setSjje(AmountFormatUtil.fmtMicrometer(MathUtil.round(sjje.toString())));
                }
                if (ObjectUtils.isNotEmpty(yxse)) {
                    hj.setYxse(AmountFormatUtil.fmtMicrometer(MathUtil.round(yxse.toString())));
                }
                dataList.add(hj);
            }
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, true,sheetNum,sheetName);
            return hasNext;
        }
    }

    /**
     * @Description 农产品加计扣除发票sheet
     * @param pramsMap
     * @param curr
     * @param excelFile
     * @param jsonObject
     * @param sheetNum
     * @param sheetName
     * @param condition
     * @Return boolean
     * @Author wangruwei
     * @Date 2024/8/22 14:55
     **/
    private boolean cicleJjkcFp(Map<String, Object> pramsMap, int curr, String excelFile, JSONObject jsonObject, int sheetNum, String sheetName, List<TDxRecordInvoiceNcpJjkcVo> beforeDataList, String condition)
            throws Exception {
        Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
        pramsMap = resetpramsMap(map);
        JSONObject pageData = dkCountExportService.selectJjkcFpByPramsMap(pramsMap, curr, default_size);

        boolean hasNext = pageData.getBooleanValue("hasNext");
        List<TDxRecordInvoiceNcpJjkcVo> dataList = (List<TDxRecordInvoiceNcpJjkcVo>)pageData.get("datalist");
        if(!CollectionUtils.isEmpty(dataList)){
            beforeDataList.addAll(dataList);
        }
        boolean firstPage = false;
        if (curr == default_curr) {
            firstPage = true;
        }

        // 数据不是最后一页
        if (hasNext) {
            int nextPage = pageData.getIntValue("nextPage");
            // 写入当前页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, false,sheetNum,sheetName);
            // 更新页数
            return cicleJjkcFp(pramsMap, nextPage, excelFile, jsonObject, sheetNum, sheetName,beforeDataList, condition);
        } else {
            // 写入最后一页数据
            // 写入最后一页数据
            if(!CollectionUtils.isEmpty(beforeDataList)){
                BigDecimal invoiceAmount = beforeDataList.stream()
                        .map(TDxRecordInvoiceNcpJjkcVo::getInvoiceAmount)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal kjjkcse = beforeDataList.stream()
                        .map(TDxRecordInvoiceNcpJjkcVo::getKjjkcse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal bcjjkcse = beforeDataList.stream()
                        .map(TDxRecordInvoiceNcpJjkcVo::getBcjjkcse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal sykjjkcse = beforeDataList.stream()
                        .map(TDxRecordInvoiceNcpJjkcVo::getSykjjkcse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                TDxRecordInvoiceNcpJjkcVo hj = new TDxRecordInvoiceNcpJjkcVo();
                hj.setXh("合计");
                if (ObjectUtils.isNotEmpty(invoiceAmount)) {
                    hj.setInvoiceAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(invoiceAmount.toString())));
                }
                if (ObjectUtils.isNotEmpty(kjjkcse)) {
                    hj.setKjjkcse(AmountFormatUtil.fmtMicrometer(MathUtil.round(kjjkcse.toString())));
                }
                if (ObjectUtils.isNotEmpty(bcjjkcse)) {
                    hj.setBcjjkcse(AmountFormatUtil.fmtMicrometer(MathUtil.round(bcjjkcse.toString())));
                }
                if (ObjectUtils.isNotEmpty(sykjjkcse)) {
                    hj.setSykjjkcse(AmountFormatUtil.fmtMicrometer(MathUtil.round(sykjjkcse.toString())));
                }
                dataList.add(hj);
            }
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, true,sheetNum,sheetName);
            return hasNext;
        }
    }

    /**
     * @Description 农产品加计扣除缴款书sheet
     * @param pramsMap
     * @param curr
     * @param excelFile
     * @param jsonObject
     * @param sheetNum
     * @param sheetName
     * @param condition
     * @Return boolean
     * @Author wangruwei
     * @Date 2024/8/22 14:55
     **/
    private boolean cicleJjkcJks(Map<String, Object> pramsMap, int curr, String excelFile, JSONObject jsonObject, int sheetNum, String sheetName, List<JkszhcxRespVO> beforeDataList, String condition)
            throws Exception {
        Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
        pramsMap = resetpramsMap(map);
        JSONObject pageData = dkCountExportService.selectJjkcJksByPramsMap(pramsMap, curr, default_size);

        boolean hasNext = pageData.getBooleanValue("hasNext");
        List<JkszhcxRespVO> dataList = (List<JkszhcxRespVO>)pageData.get("datalist");
        if(!CollectionUtils.isEmpty(dataList)){
            beforeDataList.addAll(dataList);
        }
        boolean firstPage = false;
        if (curr == default_curr) {
            firstPage = true;
        }

        // 数据不是最后一页
        if (hasNext) {
            int nextPage = pageData.getIntValue("nextPage");
            // 写入当前页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, false,sheetNum,sheetName);
            // 更新页数
            return cicleJjkcJks(pramsMap, nextPage, excelFile, jsonObject, sheetNum, sheetName,beforeDataList, condition);
        } else {
            // 写入最后一页数据
            if(!CollectionUtils.isEmpty(beforeDataList)){
                BigDecimal taxAmount = beforeDataList.stream()
                        .map(JkszhcxRespVO::getTaxAmount)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal kjjkcse = beforeDataList.stream()
                        .map(JkszhcxRespVO::getKjjkcse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal bcjjkcse = beforeDataList.stream()
                        .map(JkszhcxRespVO::getBcjjkcse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal sykjjkcse = beforeDataList.stream()
                        .map(JkszhcxRespVO::getSykjjkcse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                JkszhcxRespVO hj = new JkszhcxRespVO();
                hj.setXh("合计");
                if (ObjectUtils.isNotEmpty(taxAmount)) {
                    hj.setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(taxAmount.toString())));
                }
                if (ObjectUtils.isNotEmpty(kjjkcse)) {
                    hj.setKjjkcse(AmountFormatUtil.fmtMicrometer(MathUtil.round(kjjkcse.toString())));
                }
                if (ObjectUtils.isNotEmpty(bcjjkcse)) {
                    hj.setBcjjkcse(AmountFormatUtil.fmtMicrometer(MathUtil.round(bcjjkcse.toString())));
                }
                if (ObjectUtils.isNotEmpty(sykjjkcse)) {
                    hj.setSykjjkcse(AmountFormatUtil.fmtMicrometer(MathUtil.round(sykjjkcse.toString())));
                }
                dataList.add(hj);
            }
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, true,sheetNum,sheetName);
            return hasNext;
        }
    }

    /**
     * @Description 出口转内销发票sheet
     * @param pramsMap
     * @param curr
     * @param excelFile
     * @param jsonObject
     * @param sheetNum
     * @param sheetName
     * @param condition
     * @Return boolean
     * @Author wangruwei
     * @Date 2024/8/22 14:55
     **/
    private boolean cicleCkznxFp(Map<String, Object> pramsMap, int curr, String excelFile, JSONObject jsonObject, int sheetNum, String sheetName, List<DkCountExportInvoiceVo> beforeDataList, String condition)
            throws Exception {
        Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
        pramsMap = resetpramsMap(map);
        JSONObject pageData = dkCountExportService.selectCkznxFpByPramsMap(pramsMap, curr, default_size);

        boolean hasNext = pageData.getBooleanValue("hasNext");
        List<DkCountExportInvoiceVo> dataList = (List<DkCountExportInvoiceVo>)pageData.get("datalist");
        if(!CollectionUtils.isEmpty(dataList)){
            beforeDataList.addAll(dataList);
        }
        boolean firstPage = false;
        if (curr == default_curr) {
            firstPage = true;
        }

        // 数据不是最后一页
        if (hasNext) {
            int nextPage = pageData.getIntValue("nextPage");
            // 写入当前页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, false,sheetNum,sheetName);
            // 更新页数
            return cicleCkznxFp(pramsMap, nextPage, excelFile, jsonObject, sheetNum, sheetName,beforeDataList, condition);
        } else {
            // 写入最后一页数据
            if(!CollectionUtils.isEmpty(beforeDataList)){
                BigDecimal exportAmount = beforeDataList.stream()
                        .map(DkCountExportInvoiceVo::getExportAmount)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal exportTaxAmount = beforeDataList.stream()
                        .map(DkCountExportInvoiceVo::getExportTaxAmount)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal exportYxse = beforeDataList.stream()
                        .map(DkCountExportInvoiceVo::getExportYxse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                DkCountExportInvoiceVo hj = new DkCountExportInvoiceVo();
                hj.setXh("合计");
                if (ObjectUtils.isNotEmpty(exportAmount)) {
                    hj.setExportAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(exportAmount.toString())));
                }
                if (ObjectUtils.isNotEmpty(exportTaxAmount)) {
                    hj.setExportTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(exportTaxAmount.toString())));
                }
                if (ObjectUtils.isNotEmpty(exportYxse)) {
                    hj.setExportYxse(AmountFormatUtil.fmtMicrometer(MathUtil.round(exportYxse.toString())));
                }
                dataList.add(hj);
            }
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, true,sheetNum,sheetName);
            return hasNext;
        }
    }

    /**
     * @Description 出口转内销缴款书sheet
     * @param pramsMap
     * @param curr
     * @param excelFile
     * @param jsonObject
     * @param sheetNum
     * @param sheetName
     * @param condition
     * @Return boolean
     * @Author wangruwei
     * @Date 2024/8/22 14:55
     **/
    private boolean cicleCkznxJks(Map<String, Object> pramsMap, int curr, String excelFile, JSONObject jsonObject, int sheetNum, String sheetName, List<DkCountTDxExportCustoms> beforeDataList, String condition)
            throws Exception {
        Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
        pramsMap = resetpramsMap(map);
        JSONObject pageData = dkCountExportService.selectCkznxJksByPramsMap(pramsMap, curr, default_size);

        boolean hasNext = pageData.getBooleanValue("hasNext");
        List<DkCountTDxExportCustoms> dataList = (List<DkCountTDxExportCustoms>)pageData.get("datalist");
        if(!CollectionUtils.isEmpty(dataList)){
            beforeDataList.addAll(dataList);
        }
        boolean firstPage = false;
        if (curr == default_curr) {
            firstPage = true;
        }

        // 数据不是最后一页
        if (hasNext) {
            int nextPage = pageData.getIntValue("nextPage");
            // 写入当前页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, false,sheetNum,sheetName);
            // 更新页数
            return cicleCkznxJks(pramsMap, nextPage, excelFile, jsonObject, sheetNum, sheetName,beforeDataList, condition);
        } else {
            // 写入最后一页数据
            if(!CollectionUtils.isEmpty(beforeDataList)){
                BigDecimal exportTaxAmount = beforeDataList.stream()
                        .map(DkCountTDxExportCustoms::getExportTaxAmount)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal yxExportTaxAmount = beforeDataList.stream()
                        .map(DkCountTDxExportCustoms::getYxExportTaxAmount)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                DkCountTDxExportCustoms hj = new DkCountTDxExportCustoms();
                hj.setXh("合计");
                if (ObjectUtils.isNotEmpty(exportTaxAmount)) {
                    hj.setExportTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(exportTaxAmount.toString())));
                }
                if (ObjectUtils.isNotEmpty(yxExportTaxAmount)) {
                    hj.setYxExportTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(yxExportTaxAmount.toString())));
                }
                dataList.add(hj);
            }
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, true,sheetNum,sheetName);
            return hasNext;
        }
    }

    /**
    * @Description 异常发票
    * @param pramsMap
    * @param curr
    * @param excelFile
    * @param jsonObject
    * @param sheetNum
    * @param sheetName
    * @param condition
     * @Return boolean
    * @Author wangruwei
    * @Date 2024/8/26 14:13
    **/
    private boolean cicleYcFp(Map<String, Object> pramsMap, int curr, String excelFile, JSONObject jsonObject, int sheetNum, String sheetName, List<ExtRzjgqdRespVO> beforeDataList, String condition)
            throws Exception {
        Map<String,Object> map = JSONObject.parseObject(condition,Map.class);
        pramsMap = resetpramsMap(map);
        JSONObject pageData = dkCountExportService.selectYcFpByPramsMap(pramsMap, curr, default_size);

        boolean hasNext = pageData.getBooleanValue("hasNext");
        List<ExtRzjgqdRespVO> dataList = (List<ExtRzjgqdRespVO>)pageData.get("datalist");
        if(!CollectionUtils.isEmpty(dataList)){
            beforeDataList.addAll(dataList);
        }
        boolean firstPage = false;
        if (curr == default_curr) {
            firstPage = true;
        }

        // 数据不是最后一页
        if (hasNext) {
            int nextPage = pageData.getIntValue("nextPage");
            // 写入当前页数据
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, false,sheetNum,sheetName);
            // 更新页数
            return cicleYcFp(pramsMap, nextPage, excelFile, jsonObject, sheetNum, sheetName,beforeDataList, condition);
        } else {
            // 写入最后一页数据
            if(!CollectionUtils.isEmpty(beforeDataList)){
                BigDecimal hjje = beforeDataList.stream()
                        .map(ExtRzjgqdRespVO::getJe)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal hjse = beforeDataList.stream()
                        .map(ExtRzjgqdRespVO::getSe)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal dkse = beforeDataList.stream()
                        .map(ExtRzjgqdRespVO::getDkse)
                        .filter(amount -> StringUtils.isNotEmpty(amount))
                        .map(e->this.convertStringToBigDecimal(e))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                ExtRzjgqdRespVO hj = new ExtRzjgqdRespVO();
                hj.setXh("合计");
                if (ObjectUtils.isNotEmpty(hjje)) {
                    hj.setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(hjje.toString())));
                }
                if (ObjectUtils.isNotEmpty(hjse)) {
                    hj.setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(hjse.toString())));
                }
                if (ObjectUtils.isNotEmpty(dkse)) {
                    hj.setDkse(AmountFormatUtil.fmtMicrometer(MathUtil.round(dkse.toString())));
                }
                dataList.add(hj);
            }
            exportListArrayToExcelBySheet(excelFile, title, jsonObject, dataList, firstPage, true,sheetNum,sheetName);
            return hasNext;
        }
    }

    /**
    * @Description 发票的标题
    * @Author wangruwei
    * @Date 2024/8/22 9:12
    **/
    private JSONObject getSheetTitleFp() {
        LinkedHashMap<String,String> params = new LinkedHashMap();
        params.put("序号", "xh");
        params.put("发票代码", "fpdm");
        params.put("发票号码", "fphm");
        params.put("数电票号码", "eleInvoiceNo");
        params.put("发票类型", "fplx");
        params.put("特定业务", "lqTdyslxDm");
        params.put("购方名称", "gfmc");
        params.put("购方税号", "gfsh");
        params.put("销方名称", "xfmc");
        params.put("销方税号", "xfsh");
        params.put("开票日期", "kprq");
        params.put("金额", "je");
        params.put("税额", "se");
        params.put("抵扣税额", "dkse");
        params.put("税款所属期", "rzhBelongDate");
        params.put("认证类型", "rzlx");
        params.put("认证日期", "rzrq");
        params.put("发票状态", "invoiceStatus");
        params.put("管理状态", "glzt");
        params.put("红字锁定", "lqHzsdbs");
        params.put("成品油异常标识", "lqCpyycbs");
        params.put("机动车异常标识", "lqJdcycbs");

        return getExcelTabulate(params);
    }

    /**
     * @Description 海关缴款书的标题
     * @Author wangruwei
     * @Date 2024/8/22 9:12
     **/
    private JSONObject getSheetTitleHgjks() {
        LinkedHashMap<String,String> params = new LinkedHashMap();
        params.put("序号", "xh");
        params.put("缴款书号码", "customsCode");
        params.put("填发日期", "fillInDateShow");
        params.put("缴款单位名称", "gfName");
        params.put("缴款单位税号", "gfTaxNo");
        params.put("税款金额", "taxAmount");
        params.put("抵扣税款金额", "yxTaxAmount");
        params.put("税款所属期", "rzhBelongDate");
        params.put("认证类型", "rzlx");
        params.put("认证日期", "rzhDateShow");

        return getExcelTabulate(params);
    }

    /**
     * @Description 代扣代缴的标题
     * @Author wangruwei
     * @Date 2024/8/22 9:12
     **/
    private JSONObject getSheetTitleDkdj() {
        LinkedHashMap<String,String> params = new LinkedHashMap();
        params.put("序号", "xh");
        params.put("代扣代缴完税凭证号", "dkdjwspzh");
        params.put("填发日期", "tfrqShow");
        params.put("扣缴义务人名称", "kjywrmc");
        params.put("扣缴义务人税号", "kjywrsbh");
        params.put("被扣缴义务人名称", "bkjnsrmc");
        params.put("被扣缴人纳税人税号", "bkjnsrsbh");
        params.put("计税金额", "jsje");
        params.put("实缴金额", "sjje");
        params.put("有效抵扣税额", "yxse");
        params.put("税款所属期", "rzssq");
        params.put("认证类型", "rzlx");
        params.put("认证日期", "rzsjShow");

        return getExcelTabulate(params);
    }

    /**
     * @Description 农产品加计扣除发票 的标题
     * @Author wangruwei
     * @Date 2024/8/22 9:12
     **/
    private JSONObject getSheetTitleJjkcFp() {
        LinkedHashMap<String,String> params = new LinkedHashMap();
        params.put("序号", "xh");
        params.put("发票代码", "invoiceCode");
        params.put("发票号码", "invoiceNo");
        params.put("数电票号码", "eleInvoiceNo");
        params.put("发票类型", "invoiceType");
        params.put("特定业务", "lqTdyslxDm");
        params.put("购方名称", "gfName");
        params.put("购方税号", "gfTaxNo");
        params.put("销方名称", "xfName");
        params.put("销方税号", "xfTaxNo");
        params.put("开票日期", "invoiceDate");
        params.put("加计属期", "jjssq");
        params.put("申请类型", "sqlx");
        params.put("加计日期", "gxsj");
        params.put("金额", "invoiceAmount");
        params.put("可加计扣除额", "kjjkcse");
        params.put("本次加计扣除额", "bcjjkcse");
        params.put("剩余可加计扣除税额", "sykjjkcse");
        params.put("发票状态", "invoiceStatus");
        params.put("管理状态", "glzt");
        params.put("红字锁定", "lqHzsdbs");
        params.put("成品油异常标识", "lqCpyycbs");
        params.put("机动车异常标识", "lqJdcycbs");

        return getExcelTabulate(params);
    }

    /**
     * @Description 农产品加计扣除缴款书 的标题
     * @Author wangruwei
     * @Date 2024/8/22 9:12
     **/
    private JSONObject getSheetTitleJjkcJks() {
        LinkedHashMap<String,String> params = new LinkedHashMap();
        params.put("序号", "xh");
        params.put("缴款书号码", "customsCode");
        params.put("填发日期", "fillInDate");
        params.put("缴款单位名称", "gfName");
        params.put("缴款单位税号", "gfTaxNo");
        params.put("加计属期", "jjssq");
        params.put("申请类型", "sqlx");
        params.put("加计日期", "gxsj");
        params.put("税款金额", "taxAmount");
        params.put("可加计扣除税额", "kjjkcse");
        params.put("本次加计扣除税额", "bcjjkcse");
        params.put("剩余可加计扣除税额", "sykjjkcse");

        return getExcelTabulate(params);
    }


    /**
     * @Description 出口转内销发票 的标题
     * @Author wangruwei
     * @Date 2024/8/22 9:12
     **/
    private JSONObject getSheetTitleCkznxFp() {
        LinkedHashMap<String,String> params = new LinkedHashMap();
        params.put("序号", "xh");
        params.put("转内销证明编号", "exportNo");
        params.put("发票代码", "invoiceCode");
        params.put("发票号码", "invoiceNo");
        params.put("数电票号码", "eleInvoiceNo");
        params.put("发票类型", "invoiceType");
        params.put("购方名称", "gfName");
        params.put("购方税号", "gfTaxNo");
        params.put("销方名称", "xfName");
        params.put("销方税号", "xfTaxNo");
        params.put("开票日期", "invoiceDateShow");
        params.put("转内销金额", "exportAmount");
        params.put("转内销税额", "exportTaxAmount");
        params.put("转内销抵扣税额", "exportYxse");
        params.put("税款所属期", "rzhBelongDate");
        params.put("认证类型", "rzlx");
        params.put("认证日期", "rzhDateShow");

        return getExcelTabulate(params);
    }

    /**
     * @Description 出口转内销缴款书 的标题
     * @Author wangruwei
     * @Date 2024/8/22 9:12
     **/
    private JSONObject getSheetTitleCkznxJks() {
        LinkedHashMap<String,String> params = new LinkedHashMap();
        params.put("序号", "xh");
        params.put("转内销证明编号", "exportNo");
        params.put("缴款书号码", "customsCode");
        params.put("填发日期", "fillInDateShow");
        params.put("缴款单位名称", "gfName");
        params.put("缴款单位税号", "gfTaxNo");
        params.put("转内销税款金额", "exportTaxAmount");
        params.put("转内销抵扣税额", "yxExportTaxAmount");
        params.put("税款所属期", "rzhBelongDate");
        params.put("认证类型", "rzlx");
        params.put("认证日期", "rzhDateShow");

        return getExcelTabulate(params);
    }

    //获取excel导出的列显示名和列名
    private JSONObject getExcelTabulate(Map<String, String> params) {
        List<String> titleExcleList = new LinkedList();
        List<String> titleExcleProp = new LinkedList();
        params.keySet().forEach(key -> {
            titleExcleList.add(key);
            String value = params.get(key);
            titleExcleProp.add(value);
        });

        JSONObject json = new JSONObject();
        json.put("titleExcleList", titleExcleList);
        json.put("titleExcleProp", titleExcleProp);
        return json;
    }


    /**
     * 按sheet来生成excel。传入数据是和页面查询方式一样
     **/
    public void exportListArrayToExcelBySheet(String excelFile, String title, JSONObject jsonObject, List dataList,
                                                     boolean firstPage, boolean lastPage,int sheetNum,String sheetName) throws Exception {
        // 文件存储路径是否存在
        File filePath = new File(getFilePath(excelFile));
        if (!filePath.exists()) {
            boolean mkdirs = filePath.mkdirs();
            log.info("创建目录：{}", mkdirs);
        }

        File file = new File(excelFile);
        if (firstPage) {
            // 首次导出时，如果文件存在则需要先删除，防止由于之前的任务失败导致的数据重复写入
            if (!file.exists()) {
                boolean delete = file.delete();
                log.info("首次导出删除文件：{}", delete);
            }
        }
        // excel不存在则创建
        if (!file.exists()) {
            boolean newFile = file.createNewFile();
            log.info("创建文件：{}", newFile);
        }
        // 生成excel
        JSONArray jsonArray = jsonObject.getJSONArray("titleExcleList");
        Object[] objects = jsonArray.toArray();
        String[] titleColumns = Arrays.copyOf(objects, objects.length, String[].class);
        JSONArray jsonArray1 = jsonObject.getJSONArray("titleExcleProp");
        Object[] objects1 = jsonArray1.toArray();
        String[] titleProp = Arrays.copyOf(objects1, objects1.length, String[].class);
        List<List<String>> list1 = new ArrayList<>();
        if (dataList != null && dataList.size() > 0) {
            for (Object o : dataList) {
                List<String> data = new ArrayList<>();
                for (String s : titleProp) {
                    try {
                        String value = (String)getValue(o, s);
                        data.add(value);
                    } catch (Exception e) {
                        log.error("反射获取字段出错:{}",e);
                    }
                }
                list1.add(data);
            }
        }
        if (firstPage) {
            List<List<String>> titles = new ArrayList<>();
            for (String titleColumn : titleColumns) {
                titles.add(Collections.singletonList(titleColumn));
            }

            writeSheet = new WriteSheet();
            writeSheet.setSheetNo(sheetNum);
            writeSheet.setSheetName(sheetName);
            writeTable = new WriteTable();
            writeTable.setTableNo(sheetNum);
            writeTable.setHead(titles);


            /*writeSheet = new WriteSheet();
            writeSheet.setSheetNo(sheetNum);
            writeSheet.setSheetName(sheetName);
            writer = EasyExcel.write(excelFile).registerWriteHandler(new CustomCellWriteHandler()).build();
            //writeSheet = EasyExcel.writerSheet(sheetNum).sheetName(sheetName).build();
            writeTable = EasyExcel.writerTable().head(titles).build();*/

            writer.write(list1, writeSheet, writeTable);
        } else {
            writer.write(list1, writeSheet, writeTable);
        }

    }

    public static Object getValue(Object dto, String name) throws Exception {
        String s = name.substring(0, 1).toUpperCase();
        String substring = name.substring(1);
        try {
            Method method = dto.getClass().getMethod("get" + s + substring);
            return method.invoke(dto);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 根据全路径获取存储路径
     *
     * @param fileName
     *            文件名
     *
     *
     */
    private static String getFilePath(String fileName) {
        StringBuilder filePath = new StringBuilder();
        // linux服务器
        if (!fileName.contains(":")) {
            filePath.append("/");
        }
        String[] filePaths = fileName.split("/");

        for (int i = 0; i < filePaths.length - 1; i++) {
            if (!StringHelper.isBlank(filePaths[i])) {
                filePath.append(filePaths[i]);
                filePath.append("/");
            }
        }
        return filePath.toString();
    }

}
