package com.zjrs.zwnw.dbgl.bpo.impl;

import com.zjrs.appcomm.template.excel.ExcelTemplate;
import com.zjrs.appcomm.template.excel.builder.ExportDefinition;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mohrss.leaf.core.framework.domain.bpo.impl.BPOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.dbgl.bpo.CqcxBPO;
import com.zjrs.zwnw.dbgl.dto.DbrwglCqcxListDTO;
import com.zjrs.zwnw.dbgl.dto.DbrwglCqcxQueryDTO;
import com.zjrs.zwnw.dbgl.entity.DbrwMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@Service
public class CqcxBPOimpl extends BPOImpl implements CqcxBPO {

    @Autowired
    private DbrwMapper dbrwMapper;
    @Resource
    private ExcelTemplate excelTemplate;

    @Override
    public void exportCqcx(DbrwglCqcxQueryDTO dto, HttpServletResponse res) {
        dto.setPagesize(10000);
        dto.setPagenum(1);
        Page<DbrwglCqcxListDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<DbrwglCqcxListDTO> pageResult = (Page) dbrwMapper.getCqcxList(page,dto);
        List<DbrwglCqcxListDTO> result = pageResult.getRecords();

        InputStream inputStream = null;
        try {
            inputStream = CqcxBPOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "dbgl" + File.separator + "cqcx.xlsx");

            ExportDefinition<Map, DbrwglCqcxListDTO> exportDefinition = ExportDefinition.builder(Map.class, DbrwglCqcxListDTO.class)
                    .listData(result).listDataRowIndex(1)
                    .listFields(new String[]{
                            "dbbh","rwly", "bae001Name","xmbh","xmmc","ssbmlxName","ssbmName","cqlxName","tbjzrq"
                    })
                    .sheet(inputStream, "Sheet1")
                    .build();
            byte[] b = excelTemplate.exportFile(exportDefinition);

            String fileName = "超期查询导出.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
            OutputStream outputStream = res.getOutputStream();
            outputStream.write(b);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageResult getCqcxList(DbrwglCqcxQueryDTO dto) {
        Page<DbrwglCqcxListDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<DbrwglCqcxListDTO> pageResult = (Page) dbrwMapper.getCqcxList(page,dto);

        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }


}
