package com.zjrs.zwnw.rlzysc.blo.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.rlzysc.blo.CxtjBLO;
import com.zjrs.zwnw.rlzysc.dto.*;
import com.zjrs.zwnw.rlzysc.entity.VRsRlzyGwtjMapper;
import com.zjrs.zwnw.rlzysc.entity.VRsRlzyQztjMapper;
import com.zjrs.zwnw.sxgl.blo.impl.JcsxqdkBLOimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@Service
public class CxtjBLOimpl  extends BLOImpl implements CxtjBLO {

    @Autowired
    VRsRlzyGwtjMapper gwtjMapper;

    @Autowired
    VRsRlzyQztjMapper qztjMapper;


    @Override
    public PageResult dwtjcxList(CxtjQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) qztjMapper.dwtjcxList(page, dto);
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public PageResult grqztjList(CxtjQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) gwtjMapper.grqztjList(page, dto);
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public void dwtjcxExp(CxtjQueryDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "dwtjcx.xlsx");

            String fileName = "dwtjcx.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void grqztjExp(CxtjQueryDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "grqztjcx.xlsx");

            String fileName = "grqztjcx.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } 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 jlyllsList(CxtjQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) qztjMapper.jlyllsList(page, dto);
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public void jlyllsExp(CxtjQueryDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "jlylls.xlsx");

            String fileName = "jlylls.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
